Skip to content
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

Merged
merged 21 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions build-tests/ts-command-line-test/.vscode/launch.json
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" ]
}
]
}
20 changes: 20 additions & 0 deletions build-tests/ts-command-line-test/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fsx = require('fs-extra');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not FileSystem?

Copy link
Collaborator Author

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.

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())}`);
16 changes: 16 additions & 0 deletions build-tests/ts-command-line-test/package.json
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"
}
}
12 changes: 12 additions & 0 deletions build-tests/ts-command-line-test/src/BusinessLogic.ts
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}) `);
}
}
38 changes: 38 additions & 0 deletions build-tests/ts-command-line-test/src/PushAction.ts
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'
});
}
}
36 changes: 36 additions & 0 deletions build-tests/ts-command-line-test/src/RunAction.ts
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.'
});
}
}
34 changes: 34 additions & 0 deletions build-tests/ts-command-line-test/src/WidgetCommandLine.ts
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();
}
}
7 changes: 7 additions & 0 deletions build-tests/ts-command-line-test/src/start.ts
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();
27 changes: 27 additions & 0 deletions build-tests/ts-command-line-test/tsconfig.json
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, but tsd.d.ts isn't needed; removed.

]
}
2 changes: 2 additions & 0 deletions build-tests/ts-command-line-test/widget.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
node .\lib\start.js %*
2 changes: 2 additions & 0 deletions build-tests/ts-command-line-test/widget.sh
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]"
}
6 changes: 5 additions & 1 deletion common/config/rush/nonbrowser-approved-packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/approved-packages.schema.json",
"packages": [
{
"name": "@microsoft/ts-command-line",
"allowedCategories": [ "tests" ]
},
{
"name": "@jest/core",
"allowedCategories": [ "libraries" ]
Expand Down Expand Up @@ -176,7 +180,7 @@
},
{
"name": "@rushstack/ts-command-line",
"allowedCategories": [ "libraries" ]
"allowedCategories": [ "libraries", "tests" ]
},
{
"name": "@rushstack/typings-generator",
Expand Down
Loading