/
cli.ts
129 lines (114 loc) · 3.53 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'path';
import meow from 'meow';
import updateNotifier from 'update-notifier';
import {init} from './init';
import {clean} from './clean';
const packageJson = require('../../package.json');
export interface Logger {
log: (...args: Array<{}>) => void;
error: (...args: Array<{}>) => void;
dir: (obj: {}, options?: {}) => void;
}
export interface Options {
dryRun: boolean;
gtsRootDir: string;
targetRootDir: string;
yes: boolean;
no: boolean;
logger: Logger;
}
export type VerbFilesFunction =
(options: Options, files: string[], fix?: boolean) => Promise<boolean>;
const logger: Logger = console;
const cli = meow({
help: `
Usage
$ gts <verb> [<file>...] [options]
Verb can be:
init Adds default npm scripts to your package.json.
check Checks code for formatting and lint issues.
fix Fixes formatting and linting issues (if possible).
clean Removes all files generated by the build.
Options
--help Prints this help message.
-y, --yes Assume a yes answer for every prompt.
-n, --no Assume a no answer for every prompt.
--dry-run Don't make any acutal changes.
Examples
$ gts init -y
$ gts check
$ gts fix
$ gts fix src/file1.ts src/file2.ts
$ gts clean`,
flags: {
help: {type: 'boolean'},
yes: {type: 'boolean', alias: 'y'},
no: {type: 'boolean', alias: 'n'},
'dry-run': {type: 'boolean'}
}
});
function usage(msg?: string): void {
if (msg) {
logger.error(msg);
}
cli.showHelp(1);
}
async function run(verb: string, files: string[]): Promise<boolean> {
const options: Options = {
dryRun: cli.flags.dryRun || false,
// Paths are relative to the transpiled output files.
gtsRootDir: path.resolve(__dirname, '../..'),
targetRootDir: process.cwd(),
yes: cli.flags.yes || cli.flags.y || false,
no: cli.flags.no || cli.flags.n || false,
logger
};
// Linting/formatting depend on typescript. We don't want to load the
// typescript module during init, since it might not exist.
// See: https://github.com/google/ts-style/issues/48
if (verb === 'init') {
return await init(options);
}
const lint: VerbFilesFunction = require('./lint').lint;
const format: VerbFilesFunction = require('./format').format;
switch (verb) {
case 'check':
const passLint = await lint(options, files);
const passFormat = await format(options, files);
return passLint && passFormat;
case 'fix':
return (
await lint(options, files, true) &&
await format(options, files, true));
case 'clean':
return await clean(options);
default:
usage(`Unknown verb: ${verb}`);
return false;
}
}
updateNotifier({pkg: packageJson}).notify();
if (cli.input.length < 1) {
usage();
}
run(cli.input[0], cli.input.slice(1)).then(success => {
if (!success) {
process.exit(1);
}
});