UNPKG

3.77 kBJavaScriptView Raw
1import fs from 'fs';
2import path from 'path';
3
4import { hela, exec, toFlags } from '@hela/core';
5
6const prog = hela();
7
8export const build = createCommand('build', 'Build output files, using Babel');
9export const bundle = createCommand('bundle', 'Bundle, using Rollup');
10export const docs = createCommand('docs', 'Generate API docs, with Docks');
11export const lint = createCommand('lint', 'Lint files, using ESLint+Prettier', {
12 alias: ['l', 'lnt', 'lnit'],
13});
14export const test = createCommand('test', 'Test files, using Jest', {
15 alias: ['t', 'tst', 'tset'],
16});
17
18export const format = prog
19 .command('format', 'Format running Prettier', { alias: ['fmt'] })
20 .option('-I, --input', 'Input patterns for Prettier, default "**/*".', '**/*')
21 .action((argv) =>
22 exec(`prettier ${argv.input} --write`, { stdio: 'inherit' }),
23 );
24
25export const runAll = prog
26 .command('runAll', 'Run all commands in series', { alias: ['ra', 'runall'] })
27 .option('--bundle', 'Run `bundle` instead of `build`.', false)
28 .option('--docs', 'Run `docs` after all.', false)
29 .option('-a, --all', 'Run on all packages.', true)
30 .option('-e, --exclude', 'Ignore pattern string.')
31 .option(
32 '-m, --testPathPattern',
33 'A regexp pattern string that is matched against all tests paths before executing the test.',
34 )
35 .option(
36 '-t, --testNamePattern,',
37 'Run only tests with a name that matches the regex pattern',
38 )
39 .option('-o, --onlyChanged', 'Run only on changed packages', false)
40 .action(async (argv) =>
41 [lint, test, argv.bundle ? bundle : build, argv.docs ? docs : null]
42 .filter(Boolean)
43 .reduce(
44 // eslint-disable-next-line promise/prefer-await-to-then
45 (acc, cmd) => acc.then(() => cmd(argv)),
46 Promise.resolve(),
47 ),
48 );
49
50function createCommand(name, description) {
51 return prog
52 .command(name, description)
53 .option('-a, --all', 'Run on all packages.', true)
54 .option('-e, --exclude', 'Ignore pattern string.')
55 .option(
56 '-I, --input',
57 'Input patterns for the "lint" command, defaults to src and testing dirs.',
58 )
59 .option(
60 '-m, --testPathPattern',
61 'A regexp pattern string that is matched against all tests paths before executing the test.',
62 )
63 .option(
64 '-t, --testNamePattern,',
65 'Run only tests with a name that matches the regex pattern',
66 )
67 .option('-o, --onlyChanged', 'Run only on changed packages', false)
68 .action(async (argv) => {
69 // switch the env set by default when running Jest. For ensurance.
70 process.env.NODE_ENV = name;
71
72 const opts = { ...argv };
73 opts.o = !opts.all;
74 opts.onlyChanged = !opts.all;
75
76 const ignores = opts.exclude;
77 const inputs = opts.input;
78
79 // remove custom ones, because Jest fails
80 [
81 'a',
82 'm',
83 'e',
84 'o',
85 'I',
86 'input',
87 'exclude',
88 'bundle',
89 'docs',
90 'cwd',
91 ].forEach((key) => {
92 delete opts[key];
93 });
94
95 const flags = toFlags(opts, { allowCamelCase: true });
96
97 const configDir = path.join(__dirname, 'configs', name);
98 const configPath = path.join(configDir, 'config.js');
99
100 // eslint-disable-next-line import/no-dynamic-require, global-require
101 const createConfig = require(configDir);
102
103 const config = createConfig({ ...opts, ignores, input: inputs });
104 const contents = `module.exports=${JSON.stringify(config)}`;
105
106 fs.writeFileSync(configPath, contents);
107
108 // eslint-disable-next-line global-require
109 console.log('Jest', require('jest/package.json').version);
110
111 console.log(`jest -c ${configPath} ${flags}`);
112
113 return exec(`jest -c ${configPath} ${flags}`, { stdio: 'inherit' });
114 });
115}