Skip to content

Commit

Permalink
feat: Add success/failure hooks to watch (#130)
Browse files Browse the repository at this point in the history
* Remove esnext target override from ts rollup config. Make es5 the default target.

* watch hooks

* Finished the hooks

* update readme

* move use strict

* fixed typo in watch help printout in README

* Remove esnext target override from ts rollup config. Make es5 the default target.

* watch hooks

* Finished the hooks

* update readme

*  update yarnlockfile

* Fix yarn.lock
  • Loading branch information
hedgerh authored and jaredpalmer committed Dec 19, 2019
1 parent 2a5e5a5 commit ab54278
Show file tree
Hide file tree
Showing 5 changed files with 1,023 additions and 749 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ Options
--format Specify module format(s) (default cjs,esm)
--tsconfig Specify your custom tsconfig path (default <root-folder>/tsconfig.json)
--verbose Keep outdated console output in watch mode instead of clearing the screen
--onFirstSuccess Run a command on the first successful build
--onSuccess Run a command on a successful build
--onFailure Run a command on a failed build
--noClean Don't clean the dist folder
--transpileOnly Skip type checking
-h, --help Displays this message
Expand All @@ -408,6 +411,9 @@ Examples
$ tsdx watch --format cjs,esm,umd
$ tsdx watch --tsconfig ./tsconfig.foo.json
$ tsdx watch --noClean
$ tsdx watch --onFirstSuccess "echo The first successful build!"
$ tsdx watch --onSuccess "echo Successful build!"
$ tsdx watch --onFailure "The build failed!"
$ tsdx watch --transpileOnly
```
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"build": "tsc -p tsconfig.json",
"lint": "yarn build && yarn lint:post-build",
"lint:post-build": "node dist/index.js lint src test --ignore-pattern 'test/tests/lint'",
"test": "jest --config ./test/jest.config.json"
"test": "jest --config ./test/jest.config.json",
"watch": "chokidar \"./package.json\" \"./src/**/*.ts\" \"node_modules\\@jaredpalmer\\rollup-plugin-preserve-shebang\\dist\\index.js\" -c \"yarn build && echo Success\"",
"start": "tsc -p tsconfig.json --watch"
},
"files": [
"dist",
Expand Down Expand Up @@ -61,6 +63,8 @@
"camelcase": "^5.0.0",
"chalk": "^2.4.2",
"cross-env": "6.0.3",
"chokidar-cli": "^1.2.2",
"cross-spawn": "^6.0.5",
"enquirer": "^2.3.0",
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
Expand Down Expand Up @@ -109,6 +113,7 @@
"@types/node": "^12.0.2",
"@types/ora": "^3.2.0",
"@types/react": "^16.9.11",
"@types/ps-tree": "^1.1.0",
"@types/rollup-plugin-json": "^3.0.2",
"@types/rollup-plugin-sourcemaps": "^0.4.2",
"husky": "^3.0.9",
Expand Down
6 changes: 1 addition & 5 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ export async function createRollupConfig(
sourceMap: true,
declaration: true,
jsx: 'react',
},
},
tsconfigOverride: {
compilerOptions: {
target: 'esnext',
target: 'es5',
},
},
check: opts.transpileOnly === false,
Expand Down
57 changes: 55 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ prog
.example('watch --noClean')
.option('--tsconfig', 'Specify custom tsconfig path')
.example('watch --tsconfig ./tsconfig.foo.json')
.example('build --tsconfig ./tsconfig.foo.json')
.option('--onFirstSuccess', 'Run a command on the first successful build')
.example('watch --onFirstSuccess "echo The first successful build!"')
.option('--onSuccess', 'Run a command on a successful build')
.example('watch --onSuccess "echo Successful build!"')
.option('--onFailure', 'Run a command on a failed build')
.example('watch --onFailure "The build failed!"')
.option('--transpileOnly', 'Skip type checking', false)
.example('build --transpileOnly')
.option('--extractErrors', 'Extract invariant errors to ./errors/codes.json.')
Expand All @@ -346,9 +353,37 @@ prog
await cleanDistFolder();
}
await ensureDistFolder();
opts.name = opts.name || appPackageJson.name;
opts.input = await getInputs(opts.entry, appPackageJson.source);
if (opts.format.includes('cjs')) {
await writeCjsEntryFile(opts.name);
}

type Killer = execa.ExecaChildProcess | null;

let firstTime = true;
let successKiller: Killer = null;
let failureKiller: Killer = null;

function run(command: string) {
if (command) {
const [exec, ...args] = command.split(' ');

return execa(exec, args, {
stdio: 'inherit',
});
}

return null;
}

function killHooks() {
return Promise.all([
successKiller ? successKiller.kill('SIGTERM') : null,
failureKiller ? failureKiller.kill('SIGTERM') : null,
]);
}

const spinner = ora().start();
await watch(
(buildConfigs as RollupWatchOptions[]).map(inputOptions => ({
Expand All @@ -360,6 +395,9 @@ prog
...inputOptions,
}))
).on('event', async event => {
// clear previous onSuccess/onFailure hook processes so they don't pile up
await killHooks();

if (event.code === 'START') {
if (!opts.verbose) {
clearConsole();
Expand All @@ -369,18 +407,28 @@ prog
if (event.code === 'ERROR') {
spinner.fail(chalk.bold.red('Failed to compile'));
logError(event.error);
failureKiller = run(opts.onFailure);
}
if (event.code === 'FATAL') {
spinner.fail(chalk.bold.red('Failed to compile'));
logError(event.error);
failureKiller = run(opts.onFailure);
}
if (event.code === 'END') {
spinner.succeed(chalk.bold.green('Compiled successfully'));
console.log(`
${chalk.dim('Watching for changes')}
`);

try {
await moveTypes();

if (firstTime && opts.onFirstSuccess) {
firstTime = false;
run(opts.onFirstSuccess);
} else {
successKiller = run(opts.onSuccess);
}
} catch (_error) {}
}
});
Expand Down Expand Up @@ -415,8 +463,13 @@ prog
await ensureDistFolder();
const logger = await createProgressEstimator();
if (opts.format.includes('cjs')) {
const promise = writeCjsEntryFile(opts.name).catch(logError);
logger(promise, 'Creating entry file');
try {
await util.promisify(mkdirp)(resolveApp('./dist'));
const promise = writeCjsEntryFile(opts.name).catch(logError);
logger(promise, 'Creating entry file');
} catch (e) {
logError(e);
}
}
try {
const promise = asyncro
Expand Down
Loading

0 comments on commit ab54278

Please sign in to comment.