-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathcli.ts
183 lines (165 loc) · 4.65 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/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 * as meow from 'meow';
import {init} from './init';
import {clean} from './clean';
import {isYarnUsed} from './util';
import * as execa from 'execa';
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;
yarn?: boolean;
}
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.
lint Checks code for formatting and lint issues.
check Alias for lint. Kept for backward compatibility.
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 actual changes.
--yarn Use yarn instead of npm.
Examples
$ gts init -y
$ gts lint
$ 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'},
dryRun: {type: 'boolean'},
yarn: {type: 'boolean'},
},
});
/**
* Get the current version of node.js being run.
* Exported purely for stubbing purposes.
* @private
*/
export function getNodeVersion() {
return process.version;
}
function usage(msg?: string): void {
if (msg) {
logger.error(msg);
}
cli.showHelp(1);
}
export async function run(verb: string, files: string[]): Promise<boolean> {
// throw if running on an old version of nodejs
const nodeMajorVersion = Number(getNodeVersion().slice(1).split('.')[0]);
console.log(`version: ${nodeMajorVersion}`);
if (nodeMajorVersion < 10) {
throw new Error(
`gts requires node.js 10.x or up. You are currently running
${process.version}, which is not supported. Please upgrade to
a safe, secure version of nodejs!`,
);
}
const 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,
yarn: cli.flags.yarn || isYarnUsed(),
} as Options;
// 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/gts/issues/48
if (verb === 'init') {
return init(options);
}
const flags = Object.assign([], files);
if (flags.length === 0) {
flags.push(
'**/*.ts',
'**/*.js',
'**/*.tsx',
'**/*.jsx',
'--no-error-on-unmatched-pattern',
);
}
switch (verb) {
case 'lint':
case 'check': {
try {
await execa('eslint', flags, {stdio: 'inherit'});
return true;
} catch (e) {
return false;
}
}
case 'fix': {
const fixFlag = options.dryRun ? '--fix-dry-run' : '--fix';
try {
await execa('eslint', [fixFlag, ...flags], {stdio: 'inherit'});
return true;
} catch (e) {
console.error(e);
return false;
}
}
case 'clean':
return clean(options);
default:
usage(`Unknown verb: ${verb}`);
return false;
}
}
if (cli.input.length < 1) {
usage();
}
run(cli.input[0], cli.input.slice(1))
.then(success => {
if (!success) {
// eslint-disable-next-line n/no-process-exit
process.exit(1);
}
})
.catch(e => {
console.error(e);
// eslint-disable-next-line n/no-process-exit
process.exit(1);
});