/
init.ts
334 lines (294 loc) · 8.62 KB
/
init.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* 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 cp from 'child_process';
import * as fs from 'fs';
import * as inquirer from 'inquirer';
import * as path from 'path';
import {ncp} from 'ncp';
import * as util from 'util';
import {
getPkgManagerCommand,
readFilep as read,
readJsonp as readJson,
writeFileAtomicp as write,
Bag,
DefaultPackage,
} from './util';
import {Options} from './cli';
import {PackageJson} from '@npm/types';
import chalk = require('chalk');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');
const ncpp = util.promisify(ncp);
const DEFAULT_PACKAGE_JSON: PackageJson = {
name: '',
version: '0.0.0',
description: '',
main: 'build/src/index.js',
types: 'build/src/index.d.ts',
files: ['build/src'],
license: 'Apache-2.0',
keywords: [],
scripts: {test: 'echo "Error: no test specified" && exit 1'},
};
async function query(
message: string,
question: string,
defaultVal: boolean,
options: Options
): Promise<boolean> {
if (options.yes) {
return true;
} else if (options.no) {
return false;
}
if (message) {
options.logger.log(message);
}
const answers: inquirer.Answers = await inquirer.prompt({
type: 'confirm',
name: 'query',
message: question,
default: defaultVal,
});
return answers.query;
}
export async function addScripts(
packageJson: PackageJson,
options: Options
): Promise<boolean> {
let edits = false;
const pkgManager = getPkgManagerCommand(options.yarn);
const scripts: Bag<string> = {
check: 'gts check',
clean: 'gts clean',
compile: 'tsc',
fix: 'gts fix',
prepare: `${pkgManager} run compile`,
pretest: `${pkgManager} run compile`,
posttest: `${pkgManager} run check`,
};
if (!packageJson.scripts) {
packageJson.scripts = {};
}
for (const script of Object.keys(scripts)) {
let install = true;
const existing = packageJson.scripts[script];
const target = scripts[script];
if (existing !== target) {
if (existing) {
const message =
`package.json already has a script for ${chalk.bold(script)}:\n` +
`-${chalk.red(existing)}\n+${chalk.green(target)}`;
install = await query(message, 'Replace', false, options);
}
if (install) {
// eslint-disable-next-line require-atomic-updates
packageJson.scripts[script] = scripts[script];
edits = true;
}
}
}
return edits;
}
export async function addDependencies(
packageJson: PackageJson,
options: Options
): Promise<boolean> {
let edits = false;
const deps: DefaultPackage = {
gts: `^${pkg.version}`,
typescript: pkg.devDependencies.typescript,
'@types/node': pkg.devDependencies['@types/node'],
};
if (!packageJson.devDependencies) {
packageJson.devDependencies = {};
}
for (const dep of Object.keys(deps)) {
let install = true;
const existing = packageJson.devDependencies[dep];
const target = deps[dep];
if (existing !== target) {
if (existing) {
const message =
`Already have devDependency for ${chalk.bold(dep)}:\n` +
`-${chalk.red(existing)}\n+${chalk.green(target)}`;
install = await query(message, 'Overwrite', false, options);
}
if (install) {
// eslint-disable-next-line require-atomic-updates
packageJson.devDependencies[dep] = deps[dep];
edits = true;
}
}
}
return edits;
}
function formatJson(object: {}) {
const json = JSON.stringify(object, null, ' ');
return `${json}\n`;
}
async function writePackageJson(
packageJson: PackageJson,
options: Options
): Promise<void> {
options.logger.log('Writing package.json...');
if (!options.dryRun) {
await write('./package.json', formatJson(packageJson));
}
const preview = {
scripts: packageJson.scripts,
devDependencies: packageJson.devDependencies,
};
options.logger.dir(preview);
}
export const ESLINT_CONFIG = {
extends: './node_modules/gts/',
};
export const ESLINT_IGNORE = 'build/\n';
async function generateConfigFile(
options: Options,
filename: string,
contents: string
) {
let existing;
try {
existing = await read(filename, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
/* not found, create it. */
} else {
throw new Error(`Unknown error reading ${filename}: ${err.message}`);
}
}
let writeFile = true;
if (existing && existing === contents) {
options.logger.log(`No edits needed in ${filename}`);
return;
} else if (existing) {
writeFile = await query(
`${chalk.bold(filename)} already exists`,
'Overwrite',
false,
options
);
}
if (writeFile) {
options.logger.log(`Writing ${filename}...`);
if (!options.dryRun) {
await write(filename, contents);
}
options.logger.log(contents);
}
}
async function generateESLintConfig(options: Options): Promise<void> {
return generateConfigFile(
options,
'./.eslintrc.json',
formatJson(ESLINT_CONFIG)
);
}
function generateESLintIgnore(options: Options): Promise<void> {
return generateConfigFile(options, './.eslintignore', ESLINT_IGNORE);
}
async function generateTsConfig(options: Options): Promise<void> {
const config = formatJson({
extends: './node_modules/gts/tsconfig-google.json',
compilerOptions: {rootDir: '.', outDir: 'build'},
include: ['src/**/*.ts', 'test/**/*.ts'],
});
return generateConfigFile(options, './tsconfig.json', config);
}
async function generatePrettierConfig(options: Options): Promise<void> {
const style = `module.exports = {
...require('gts/.prettierrc.json')
}`;
return generateConfigFile(options, './.prettierrc.js', style);
}
export async function installDefaultTemplate(
options: Options
): Promise<boolean> {
const cwd = process.cwd();
const sourceDirName = path.join(__dirname, '../template');
const targetDirName = path.join(cwd, 'src');
try {
fs.mkdirSync(targetDirName);
} catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
// Else, continue and populate files into the existing directory.
}
// Only install the template if no ts files exist in target directory.
const files = fs.readdirSync(targetDirName);
const tsFiles = files.filter(file => file.toLowerCase().endsWith('.ts'));
if (tsFiles.length !== 0) {
options.logger.log(
'Target src directory already has ts files. ' +
'Template files not installed.'
);
return false;
}
await ncpp(sourceDirName, targetDirName);
options.logger.log('Default template installed.');
return true;
}
export async function init(options: Options): Promise<boolean> {
let generatedPackageJson = false;
let packageJson;
try {
packageJson = await readJson('./package.json');
} catch (err) {
if (err.code !== 'ENOENT') {
throw new Error(`Unable to open package.json file: ${err.message}`);
}
const generate = await query(
`${chalk.bold('package.json')} does not exist.`,
'Generate',
true,
options
);
if (!generate) {
options.logger.log('Please run from a directory with your package.json.');
return false;
}
packageJson = DEFAULT_PACKAGE_JSON;
generatedPackageJson = true;
}
const addedDeps = await addDependencies(packageJson, options);
const addedScripts = await addScripts(packageJson, options);
if (generatedPackageJson || addedDeps || addedScripts) {
await writePackageJson(packageJson, options);
} else {
options.logger.log('No edits needed in package.json.');
}
await generateTsConfig(options);
await generateESLintConfig(options);
await generateESLintIgnore(options);
await generatePrettierConfig(options);
await installDefaultTemplate(options);
// Run `npm install` after initial setup so `npm run check` works right away.
if (!options.dryRun) {
// --ignore-scripts so that compilation doesn't happen because there's no
// source files yet.
cp.spawnSync(
getPkgManagerCommand(options.yarn),
['install', '--ignore-scripts'],
{stdio: 'inherit'}
);
}
return true;
}