Switch from yargs to commander

This commit is contained in:
Tony Garnock-Jones 2021-04-01 21:57:49 +02:00
parent 3ad56a5275
commit 545e1bb6de
5 changed files with 66 additions and 133 deletions

View File

@ -30,11 +30,10 @@
"@preserves/core": "^0.10.0",
"@types/glob": "^7.1.3",
"@types/minimatch": "^3.0.3",
"@types/yargs": "^16.0.0",
"chalk": "^4.1.0",
"chokidar": "^3.5.1",
"commander": "^7.2.0",
"glob": "^7.1.6",
"minimatch": "^3.0.4",
"yargs": "^16.2.0"
"minimatch": "^3.0.4"
}
}

View File

@ -31,8 +31,8 @@ export function computeBase(paths: string[]): string {
}
}
export function expandInputGlob(input: string, base0: string | undefined) {
const matches = glob.sync(input);
export function expandInputGlob(input: string[], base0: string | undefined) {
const matches = input.flatMap(i => glob.sync(i));
const base = base0 ?? computeBase(matches);
const failures: Array<Diagnostic> = [];

View File

@ -2,7 +2,7 @@ import { compile } from '../index';
import fs from 'fs';
import path from 'path';
import minimatch from 'minimatch';
import yargs from 'yargs/yargs';
import { Command } from 'commander';
import * as M from '../meta';
import chalk from 'chalk';
import { Position } from '@preserves/core';
@ -10,7 +10,7 @@ import chokidar from 'chokidar';
import { changeExt, Diagnostic, expandInputGlob, formatFailures } from './cli-utils';
export type CommandLineArguments = {
input: string;
inputs: string[];
base: string | undefined;
output: string | undefined;
stdout: boolean;
@ -64,7 +64,7 @@ export function run(options: CommandLineArguments): void {
const watcher = chokidar.watch(r.base, {
ignoreInitial: true,
}).on('all', (_event, filename) => {
if (minimatch(filename, options.input)) {
if (options.inputs.some(i => minimatch(filename, i))) {
watcher.close();
runWatch();
}
@ -83,7 +83,7 @@ export function modulePathTo(file1: string, file2: string): string | null {
export function runOnce(options: CommandLineArguments): CompilationResult {
const { base, failures, inputFiles: inputFiles0 } =
expandInputGlob(options.input, options.base);
expandInputGlob(options.inputs, options.base);
const output = options.output ?? base;
const extensionEnv: M.Environment = options.module.map(arg => {
@ -147,53 +147,34 @@ export function runOnce(options: CommandLineArguments): CompilationResult {
}
export function main(argv: Array<string>) {
const options: CommandLineArguments = yargs(argv)
.command('$0 <input>',
'Compile Preserves schema definitions to TypeScript',
yargs => yargs
.positional('input', {
type: 'string',
description: 'Input filename or glob',
demandOption: true,
})
.option('output', {
type: 'string',
description: 'Output directory for modules (default: next to sources)',
})
.option('stdout', {
type: 'boolean',
description: 'Prints each module to stdout one after the other instead ' +
'of writing them to files in the `--output` directory',
default: false,
})
.option('base', {
type: 'string',
description: 'Base directory for sources (default: common prefix)',
})
.option('core', {
type: 'string',
description: 'Import path for @preserves/core',
default: '@preserves/core',
})
.option('watch', {
type: 'boolean',
descripion: 'Watch base directory for changes',
default: false,
})
.option('traceback', {
type: 'boolean',
description: 'Include stack traces in compiler errors',
default: false,
})
.option('module', {
type: 'string',
array: true,
description: 'Additional Namespace=path imports',
default: [],
}),
argv => argv)
.argv;
options.input = path.normalize(options.input);
Error.stackTraceLimit = Infinity;
run(options);
new Command()
.arguments('[input...]')
.description('Compile Preserves schema definitions to TypeScript', {
input: 'Input filename or glob',
})
.option('--output <directory>', 'Output directory for modules (default: next to sources)')
.option('--stdout', 'Prints each module to stdout one after the other instead ' +
'of writing them to files in the `--output` directory')
.option('--base <directory>', 'Base directory for sources (default: common prefix)')
.option('--core <path>', 'Import path for @preserves/core', '@preserves/core')
.option('--watch', 'Watch base directory for changes')
.option('--traceback', 'Include stack traces in compiler errors')
.option('--module <namespace=path>', 'Additional Namespace=path import',
(nsPath: string, previous: string[]): string[] => [... previous, nsPath],
[])
.action((inputs: string[], rawOptions) => {
const options: CommandLineArguments = {
inputs: inputs.map(i => path.normalize(i)),
base: rawOptions.base,
output: rawOptions.output,
stdout: rawOptions.stdout,
core: rawOptions.core,
watch: rawOptions.watch,
traceback: rawOptions.traceback,
module: rawOptions.module,
};
Error.stackTraceLimit = Infinity;
run(options);
})
.parse(argv, { from: 'user' });
}

View File

@ -1,4 +1,4 @@
import yargs from 'yargs/yargs';
import { Command } from 'commander';
import { canonicalEncode, KeyedDictionary, underlying } from '@preserves/core';
import fs from 'fs';
import path from 'path';
@ -6,13 +6,13 @@ import { fromSchema, fromBundle } from '../meta';
import { expandInputGlob, formatFailures } from './cli-utils';
export type CommandLineArguments = {
input: string;
inputs: string[];
base: string | undefined;
bundle: boolean;
};
export function run(options: CommandLineArguments): void {
const { failures, inputFiles } = expandInputGlob(options.input, options.base);
const { failures, inputFiles } = expandInputGlob(options.inputs, options.base);
if (!options.bundle && inputFiles.length !== 1) {
failures.push({ type: 'error', file: null, detail: {
@ -31,31 +31,27 @@ export function run(options: CommandLineArguments): void {
} else {
fs.writeSync(1, underlying(canonicalEncode(fromSchema(inputFiles[0].schema))));
}
} else {
process.exit(1);
}
}
export function main(argv: Array<string>) {
const options: CommandLineArguments = yargs(argv)
.command('$0 <input>',
'Compile textual Preserves schema definitions to binary format',
yargs => yargs
.positional('input', {
type: 'string',
description: 'Input filename or glob',
demandOption: true,
})
.option('bundle', {
type: 'boolean',
description: 'Determines whether to emit a schema Bundle or a lone Schema',
default: true,
})
.option('base', {
type: 'string',
description: 'Base directory for sources (default: common prefix)',
}),
argv => argv)
.argv;
options.input = path.normalize(options.input);
Error.stackTraceLimit = Infinity;
run(options);
new Command()
.arguments('[input...]')
.description('Compile textual Preserves schema definitions to binary format', {
input: 'Input filename or glob',
})
.option('--no-bundle', 'Emit a single Schema instead of a schema Bundle')
.option('--base <directory>', 'Base directory for sources (default: common prefix)')
.action((inputs: string[], rawOptions) => {
const options: CommandLineArguments = {
inputs: inputs.map(i => path.normalize(i)),
base: rawOptions.base,
bundle: rawOptions.bundle,
};
Error.stackTraceLimit = Infinity;
run(options);
})
.parse(argv, { from: 'user' });
}

View File

@ -622,13 +622,6 @@
dependencies:
"@types/yargs-parser" "*"
"@types/yargs@^16.0.0":
version "16.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-16.0.0.tgz#0e033b23452da5d61b6c44747612cb80ac528751"
integrity sha512-2nN6AGeMwe8+O6nO9ytQfbMQOJy65oi1yK2y/9oReR08DaXSGtMsrLyCM1ooKqfICpCx4oITaR4LkOmdzz41Ww==
dependencies:
"@types/yargs-parser" "*"
abab@^2.0.3, abab@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"
@ -1066,15 +1059,6 @@ cliui@^6.0.0:
strip-ansi "^6.0.0"
wrap-ansi "^6.2.0"
cliui@^7.0.2:
version "7.0.4"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
integrity sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==
dependencies:
string-width "^4.2.0"
strip-ansi "^6.0.0"
wrap-ansi "^7.0.0"
co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
@ -1134,6 +1118,11 @@ commander@^2.20.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
commander@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==
component-emitter@^1.2.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
@ -1616,7 +1605,7 @@ gensync@^1.0.0-beta.2:
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==
get-caller-file@^2.0.1, get-caller-file@^2.0.5:
get-caller-file@^2.0.1:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
@ -4046,15 +4035,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
@ -4095,11 +4075,6 @@ y18n@^4.0.0:
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.1.tgz#8db2b83c31c5d75099bb890b23f3094891e247d4"
integrity sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==
y18n@^5.0.5:
version "5.0.5"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.5.tgz#8769ec08d03b1ea2df2500acef561743bbb9ab18"
integrity sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==
yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
@ -4118,11 +4093,6 @@ yargs-parser@^18.1.2:
camelcase "^5.0.0"
decamelize "^1.2.0"
yargs-parser@^20.2.2:
version "20.2.7"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.7.tgz#61df85c113edfb5a7a4e36eb8aa60ef423cbc90a"
integrity sha512-FiNkvbeHzB/syOjIUxFDCnhSfzAL8R5vs40MgLFBorXACCOAEaWu0gRZl14vG8MR9AOJIZbmkjhusqBYZ3HTHw==
yargs@^15.4.1:
version "15.4.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8"
@ -4140,19 +4110,6 @@ yargs@^15.4.1:
y18n "^4.0.0"
yargs-parser "^18.1.2"
yargs@^16.2.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-16.2.0.tgz#1c82bf0f6b6a66eafce7ef30e376f49a12477f66"
integrity sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==
dependencies:
cliui "^7.0.2"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.0"
y18n "^5.0.5"
yargs-parser "^20.2.2"
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"