-
Notifications
You must be signed in to change notification settings - Fork 607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ts-command-line] Add defineCommandLineRemainder() feature #1869
Changes from 15 commits
bc81507
b6d4827
67ebf53
7c69af0
40c97c5
bee3458
43019fd
cf1b4bb
95e46d3
fd256db
a2c9e3d
d25e672
5b68633
94c1c73
7b013d5
30e9ea2
9a4eaca
94b7b16
f3ad884
bcc3be1
8571c7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Widget CLI", | ||
"program": "${workspaceFolder}/lib/start.js", | ||
"args": [ "run", "1", "2", "3" ] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const fsx = require('fs-extra'); | ||
const child_process = require('child_process'); | ||
const path = require('path'); | ||
const process = require('process'); | ||
|
||
function executeCommand(command) { | ||
console.log('---> ' + command); | ||
child_process.execSync(command, { stdio: 'inherit' }); | ||
} | ||
|
||
// Clean the old build outputs | ||
console.log(`==> Starting build.js for ${path.basename(process.cwd())}`); | ||
fsx.emptyDirSync('dist'); | ||
fsx.emptyDirSync('lib'); | ||
fsx.emptyDirSync('temp'); | ||
|
||
// Run the TypeScript compiler | ||
executeCommand('node node_modules/typescript/lib/tsc'); | ||
|
||
console.log(`==> Finished build.js for ${path.basename(process.cwd())}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "ts-command-line-test", | ||
"description": "Building this project is a regression test for ts-command-line", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"build": "node build.js", | ||
"start": "node ./lib/start.js" | ||
}, | ||
"devDependencies": { | ||
"@rushstack/ts-command-line": "4.3.14", | ||
"@types/node": "10.17.13", | ||
"fs-extra": "~7.0.1", | ||
"typescript": "~3.7.2" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
export class BusinessLogic { | ||
public static async doTheWork(force: boolean): Promise<void> { | ||
console.log(`Business logic did the work. (force=${force}) `); | ||
} | ||
|
||
public static configureLogger(verbose: boolean): void { | ||
console.log(`Business logic configured the logger. (verbose=${verbose}) `); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
import { CommandLineFlagParameter, CommandLineAction, CommandLineChoiceParameter } from '@rushstack/ts-command-line'; | ||
import { BusinessLogic } from './BusinessLogic'; | ||
|
||
export class PushAction extends CommandLineAction { | ||
private _force: CommandLineFlagParameter; | ||
private _protocol: CommandLineChoiceParameter; | ||
|
||
public constructor() { | ||
super({ | ||
actionName: 'push', | ||
summary: 'Pushes a widget to the service', | ||
documentation: 'Your long description goes here.' | ||
}); | ||
} | ||
|
||
protected onExecute(): Promise<void> { // abstract | ||
return BusinessLogic.doTheWork(this._force.value); | ||
} | ||
|
||
protected onDefineParameters(): void { // abstract | ||
this._force = this.defineFlagParameter({ | ||
parameterLongName: '--force', | ||
parameterShortName: '-f', | ||
description: 'Push and overwrite any existing state' | ||
}); | ||
|
||
this._protocol = this.defineChoiceParameter({ | ||
parameterLongName: '--protocol', | ||
description: 'Specify the protocol to use', | ||
alternatives: ['ftp', 'webdav', 'scp'], | ||
environmentVariable: 'WIDGET_PROTOCOL', | ||
defaultValue: 'scp' | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line'; | ||
|
||
|
||
export class RunAction extends CommandLineAction { | ||
private _title: CommandLineStringParameter; | ||
|
||
public constructor() { | ||
super({ | ||
actionName: 'run', | ||
summary: 'Runs the rest of the command line as a shell command', | ||
documentation: 'Your long description goes here.' | ||
}); | ||
} | ||
|
||
protected onExecute(): Promise<void> { // abstract | ||
console.log(`Title: ${this._title.value || '(none)'}`); | ||
console.log('Remainder: ' + JSON.stringify(this.remainder!.values)); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
protected onDefineParameters(): void { // abstract | ||
this._title = this.defineStringParameter({ | ||
parameterLongName: '--title', | ||
argumentName: 'TITLE', | ||
description: 'An optional title to show' | ||
}); | ||
|
||
this.defineCommandLineRemainder({ | ||
description: 'The remaining arguments will be blah.' | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
import { CommandLineParser, CommandLineFlagParameter } from '@rushstack/ts-command-line'; | ||
import { PushAction } from './PushAction'; | ||
import { RunAction } from './RunAction'; | ||
import { BusinessLogic } from './BusinessLogic'; | ||
|
||
export class WidgetCommandLine extends CommandLineParser { | ||
private _verbose: CommandLineFlagParameter; | ||
|
||
public constructor() { | ||
super({ | ||
toolFilename: 'widget', | ||
toolDescription: 'The widget tool is really great.' | ||
}); | ||
|
||
this.addAction(new PushAction()); | ||
this.addAction(new RunAction()); | ||
} | ||
|
||
protected onDefineParameters(): void { // abstract | ||
this._verbose = this.defineFlagParameter({ | ||
parameterLongName: '--verbose', | ||
parameterShortName: '-v', | ||
description: 'Show extra logging detail' | ||
}); | ||
} | ||
|
||
protected onExecute(): Promise<void> { // override | ||
BusinessLogic.configureLogger(this._verbose.value); | ||
return super.onExecute(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
import { WidgetCommandLine } from './WidgetCommandLine'; | ||
|
||
const commandLine: WidgetCommandLine = new WidgetCommandLine(); | ||
commandLine.execute(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "commonjs", | ||
"declaration": true, | ||
"sourceMap": true, | ||
"experimentalDecorators": true, | ||
"strictNullChecks": true, | ||
"types": [ | ||
"node" | ||
], | ||
"lib": [ | ||
"es5", | ||
"scripthost", | ||
"es2015.collection", | ||
"es2015.promise", | ||
"es2015.iterable", | ||
"dom" | ||
], | ||
"outDir": "lib" | ||
}, | ||
"include": [ | ||
"src/**/*.ts", | ||
"typings/tsd.d.ts" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This project doesn't use RSC because it's intended as a code sample. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, but |
||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@echo off | ||
node .\lib\start.js %* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
exec node ./lib/start.js "$@" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@rushstack/ts-command-line", | ||
"comment": "Add a new feature defineCommandLineRemainder() which allows additional unvalidated CLI arguments, e.g. to pass along to another tool", | ||
"type": "minor" | ||
} | ||
], | ||
"packageName": "@rushstack/ts-command-line", | ||
"email": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@rushstack/ts-command-line", | ||
"comment": "Fix a bug with environmentVariable mapping for CommandLineFlagParameter", | ||
"type": "patch" | ||
} | ||
], | ||
"packageName": "@rushstack/ts-command-line", | ||
"email": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@rushstack/ts-command-line", | ||
"comment": "Add the ability for an environment variable to specify multiple values for CommandLineStringListParameter, encoded as a JSON array", | ||
"type": "minor" | ||
} | ||
], | ||
"packageName": "@rushstack/ts-command-line", | ||
"email": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@rushstack/ts-command-line", | ||
"comment": "Use API Extractor to trim internal APIs from the .d.ts rollup", | ||
"type": "patch" | ||
} | ||
], | ||
"packageName": "@rushstack/ts-command-line", | ||
"email": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@rushstack/ts-command-line", | ||
"comment": "Fix some bugs that prevented a CommandLineParser from being defined without any actions", | ||
"type": "minor" | ||
} | ||
], | ||
"packageName": "@rushstack/ts-command-line", | ||
"email": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@rushstack/ts-command-line", | ||
"comment": "Improve the README.md and API documentation", | ||
"type": "patch" | ||
} | ||
], | ||
"packageName": "@rushstack/ts-command-line", | ||
"email": "[email protected]" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
FileSystem
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This library predates
node-core-library
. If we ever do an operation that would genuinely benefit, I'll add it as a dependency.