-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.ts
executable file
·490 lines (460 loc) · 14.1 KB
/
index.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env node
import sade from 'sade';
import glob from 'tiny-glob/sync';
import {
rollup,
watch,
RollupOptions,
OutputOptions,
RollupWatchOptions,
WatcherOptions,
} from 'rollup';
import asyncro from 'asyncro';
import chalk from 'chalk';
import util from 'util';
import * as fs from 'fs-extra';
import jest from 'jest';
import { CLIEngine } from 'eslint';
import logError from './logError';
import path from 'path';
import mkdirp from 'mkdirp';
import execa from 'execa';
import ora from 'ora';
import { paths } from './constants';
import * as Messages from './messages';
import { createRollupConfig } from './createRollupConfig';
import { createJestConfig } from './createJestConfig';
import { createEslintConfig } from './createEslintConfig';
import { resolveApp, safePackageName, clearConsole } from './utils';
import { concatAllArray } from 'jpjs';
import getInstallCmd from './getInstallCmd';
import getInstallArgs from './getInstallArgs';
import { Input, Select } from 'enquirer';
const pkg = require('../package.json');
const createLogger = require('progress-estimator');
// All configuration keys are optional, but it's recommended to specify a storage location.
// Learn more about configuration options below.
const logger = createLogger({
storagePath: path.join(__dirname, '.progress-estimator'),
});
const prog = sade('tsdx');
let appPackageJson: {
name: string;
source?: string;
jest?: any;
eslint?: any;
};
try {
appPackageJson = fs.readJSONSync(resolveApp('package.json'));
} catch (e) {}
export const isDir = (name: string) =>
fs
.stat(name)
.then(stats => stats.isDirectory())
.catch(() => false);
export const isFile = (name: string) =>
fs
.stat(name)
.then(stats => stats.isFile())
.catch(() => false);
async function jsOrTs(filename: string) {
const extension = (await isFile(resolveApp(filename + '.ts')))
? '.ts'
: (await isFile(resolveApp(filename + '.tsx')))
? '.tsx'
: '.js';
return resolveApp(`${filename}${extension}`);
}
async function getInputs(entries: string[], source?: string) {
let inputs: any[] = [];
let stub: any[] = [];
stub
.concat(
entries && entries.length
? entries
: (source && resolveApp(source)) ||
((await isDir(resolveApp('src'))) && (await jsOrTs('src/index')))
)
.map(file => glob(file))
.forEach(input => inputs.push(input));
return concatAllArray(inputs);
}
function createBuildConfigs(
opts: any
): Array<RollupOptions & { output: OutputOptions }> {
return concatAllArray(
opts.input.map((input: string) => [
opts.format.includes('cjs') &&
createRollupConfig('cjs', { env: 'development', ...opts, input }),
opts.format.includes('cjs') &&
createRollupConfig('cjs', { env: 'production', ...opts, input }),
opts.format.includes('esm') &&
createRollupConfig('esm', { ...opts, input }),
opts.format.includes('umd') &&
createRollupConfig('umd', { env: 'development', ...opts, input }),
opts.format.includes('umd') &&
createRollupConfig('umd', { env: 'production', ...opts, input }),
])
).filter(Boolean);
}
async function moveTypes() {
try {
// Move the typescript types to the base of the ./dist folder
await fs.copy(paths.appDist + '/src', paths.appDist, {
overwrite: true,
});
await fs.remove(paths.appDist + '/src');
} catch (e) {}
}
prog
.version(pkg.version)
.command('create <pkg>')
.describe('Create a new package with TSDX')
.example('create mypackage')
.option('--template', 'Specify a template. Allowed choices: [basic, react]')
.example('create --template react mypackage')
.action(async (pkg: string, opts: any) => {
console.log(
chalk.blue(`
::::::::::: :::::::: ::::::::: ::: :::
:+: :+: :+: :+: :+: :+: :+:
+:+ +:+ +:+ +:+ +:+ +:+
+#+ +#++:++#++ +#+ +:+ +#++:+
+#+ +#+ +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+# #+# #+#
### ######## ######### ### ###
`)
);
const bootSpinner = ora(`Creating ${chalk.bold.green(pkg)}...`);
let template;
// Helper fn to prompt the user for a different
// folder name if one already exists
async function getProjectPath(projectPath: string): Promise<string> {
if (fs.existsSync(projectPath)) {
bootSpinner.fail(`Failed to create ${chalk.bold.red(pkg)}`);
const prompt = new Input({
message: `A folder named ${chalk.bold.red(
pkg
)} already exists! ${chalk.bold('Choose a different name')}`,
initial: pkg + '-1',
result: (v: string) => v.trim(),
});
pkg = await prompt.run();
projectPath = fs.realpathSync(process.cwd()) + '/' + pkg;
bootSpinner.start(`Creating ${chalk.bold.green(pkg)}...`);
return getProjectPath(projectPath); // recursion!
} else {
return projectPath;
}
}
try {
// get the project path
let projectPath = await getProjectPath(
fs.realpathSync(process.cwd()) + '/' + pkg
);
const prompt = new Select({
message: 'Choose a template',
choices: ['basic', 'react'],
});
if (opts.template) {
template = opts.template.trim();
if (!prompt.choices.includes(template)) {
bootSpinner.fail(`Invalid template ${chalk.bold.red(template)}`);
template = await prompt.run();
}
} else {
template = await prompt.run();
}
bootSpinner.start();
// copy the template
await fs.copy(
path.resolve(__dirname, `../templates/${template}`),
projectPath,
{
overwrite: true,
}
);
// fix gitignore
await fs.move(
path.resolve(projectPath, './gitignore'),
path.resolve(projectPath, './.gitignore')
);
// Install deps
process.chdir(projectPath);
const safeName = safePackageName(pkg);
const pkgJson = {
name: safeName,
version: '0.1.0',
main: 'dist/index.js',
module: `dist/${safeName}.esm.js`,
typings: 'dist/index.d.ts',
files: ['dist'],
scripts: {
start: 'tsdx watch',
build: 'tsdx build',
test: template === 'react' ? 'tsdx test --env=jsdom' : 'tsdx test',
},
peerDependencies: template === 'react' ? { react: '>=16' } : {},
husky: {
hooks: {
'pre-commit': 'pretty-quick --staged',
},
},
prettier: {
printWidth: 80,
semi: true,
singleQuote: true,
trailingComma: 'es5',
},
};
await fs.outputJSON(path.resolve(projectPath, 'package.json'), pkgJson);
bootSpinner.succeed(`Created ${chalk.bold.green(pkg)}`);
Messages.start(pkg);
} catch (error) {
bootSpinner.fail(`Failed to create ${chalk.bold.red(pkg)}`);
logError(error);
process.exit(1);
}
let deps = [
'@types/jest',
'husky',
'pretty-quick',
'prettier',
'tsdx',
'tslib',
'typescript',
].sort();
if (template === 'react') {
deps = [
...deps,
'@types/react',
'@types/react-dom',
'react',
'react-dom',
].sort();
}
const installSpinner = ora(Messages.installing(deps)).start();
try {
const cmd = getInstallCmd();
await execa(cmd, getInstallArgs(cmd, deps));
installSpinner.succeed('Installed dependencies');
console.log(Messages.start(pkg));
} catch (error) {
installSpinner.fail('Failed to install dependencies');
logError(error);
process.exit(1);
}
});
prog
.command('watch')
.describe('Rebuilds on any change')
.option('--entry, -i', 'Entry module(s)')
.example('watch --entry src/foo.tsx')
.option('--target', 'Specify your target environment', 'web')
.example('watch --target node')
.option('--name', 'Specify name exposed in UMD builds')
.example('watch --name Foo')
.option('--format', 'Specify module format(s)', 'cjs,esm')
.example('watch --format cjs,esm')
.option(
'--verbose',
'Keep outdated console output in watch mode instead of clearing the screen'
)
.example('watch --verbose')
.option('--tsconfig', 'Specify custom tsconfig path')
.example('watch --tsconfig ./tsconfig.foo.json')
.option(
'--extractErrors',
'Extract errors to codes.json and provide a url for decoding.'
)
.example(
'build --extractErrors=https://reactjs.org/docs/error-decoder.html?invariant='
)
.action(async (dirtyOpts: any) => {
const opts = await normalizeOpts(dirtyOpts);
const buildConfigs = createBuildConfigs(opts);
await ensureDistFolder();
if (opts.format.includes('cjs')) {
await writeCjsEntryFile(opts.name);
}
const spinner = ora().start();
await watch(
(buildConfigs as RollupWatchOptions[]).map(inputOptions => ({
watch: {
silent: true,
include: ['src/**'],
exclude: ['node_modules/**'],
} as WatcherOptions,
...inputOptions,
}))
).on('event', async event => {
if (event.code === 'START') {
if (!opts.verbose) {
clearConsole();
}
spinner.start(chalk.bold.cyan('Compiling modules...'));
}
if (event.code === 'ERROR') {
spinner.fail(chalk.bold.red('Failed to compile'));
logError(event.error);
}
if (event.code === 'FATAL') {
spinner.fail(chalk.bold.red('Failed to compile'));
logError(event.error);
}
if (event.code === 'END') {
spinner.succeed(chalk.bold.green('Compiled successfully'));
console.log(`
${chalk.dim('Watching for changes')}
`);
try {
await moveTypes();
} catch (_error) {}
}
});
});
prog
.command('build')
.describe('Build your project once and exit')
.option('--entry, -i', 'Entry module(s)')
.example('build --entry src/foo.tsx')
.option('--target', 'Specify your target environment', 'web')
.example('build --target node')
.option('--name', 'Specify name exposed in UMD builds')
.example('build --name Foo')
.option('--format', 'Specify module format(s)', 'cjs,esm')
.example('build --format cjs,esm')
.option('--tsconfig', 'Specify custom tsconfig path')
.example('build --tsconfig ./tsconfig.foo.json')
.option(
'--extractErrors',
'Extract errors to codes.json and provide a url for decoding.'
)
.example(
'build --extractErrors=https://reactjs.org/docs/error-decoder.html?invariant='
)
.action(async (dirtyOpts: any) => {
const opts = await normalizeOpts(dirtyOpts);
const buildConfigs = createBuildConfigs(opts);
await ensureDistFolder();
if (opts.format.includes('cjs')) {
const promise = writeCjsEntryFile(opts.name).catch(logError);
logger(promise, 'Creating entry file');
}
try {
const promise = asyncro
.map(
buildConfigs,
async (inputOptions: RollupOptions & { output: OutputOptions }) => {
let bundle = await rollup(inputOptions);
await bundle.write(inputOptions.output);
await moveTypes();
}
)
.catch((e: any) => {
throw e;
});
logger(promise, 'Building modules');
await promise;
} catch (error) {
logError(error);
process.exit(1);
}
});
async function normalizeOpts(opts: any) {
return {
...opts,
name: opts.name || appPackageJson.name,
input: await getInputs(opts.entry, appPackageJson.source),
format: opts.format.split(',').map((format: string) => {
if (format === 'es') {
return 'esm';
}
return format;
}),
};
}
function ensureDistFolder() {
return util.promisify(mkdirp)(resolveApp('dist'));
}
function writeCjsEntryFile(name: string) {
const baseLine = `module.exports = require('./${safePackageName(name)}`;
const contents = `
'use strict'
if (process.env.NODE_ENV === 'production') {
${baseLine}.cjs.production.min.js')
} else {
${baseLine}.cjs.development.js')
}
`;
return fs.writeFile(resolveApp(`./dist/index.js`), contents);
}
prog
.command('test')
.describe(
'Run jest test runner in watch mode. Passes through all flags directly to Jest'
)
.action(async () => {
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
const argv = process.argv.slice(2);
argv.push(
'--config',
JSON.stringify({
...createJestConfig(
relativePath => path.resolve(__dirname, '..', relativePath),
paths.appRoot
),
...appPackageJson.jest,
})
);
const [, ...argsToPassToJestCli] = argv;
jest.run(argsToPassToJestCli);
});
prog
.command('lint')
.describe('Run eslint with Prettier')
.example('lint src test')
.option('--fix', 'Fixes fixable errors and warnings')
.example('lint src test --fix')
.option('--ignore-pattern', 'Ignore a pattern')
.example('lint src test --ignore-pattern test/foobar.ts')
.option('--write-file', 'Write the config file locally')
.example('lint src test --write-file')
.action(
async (opts: {
fix: boolean;
'ignore-pattern': string;
'write-file': boolean;
_: string[];
}) => {
const cli = new CLIEngine({
baseConfig: {
...createEslintConfig({
rootDir: paths.appRoot,
writeFile: opts['write-file'],
}),
...appPackageJson.eslint,
},
extensions: ['.ts', '.tsx'],
fix: opts.fix,
ignorePattern: opts['ignore-pattern'],
});
const report = cli.executeOnFiles(opts['_']);
if (opts.fix) {
CLIEngine.outputFixes(report);
}
console.log(cli.getFormatter()(report.results));
if (report.errorCount) {
process.exit(1);
}
}
);
prog.parse(process.argv);