Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sigint hooks (#33) #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/build-scripts/bin/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chokidar = require('chokidar');
const detect = require('detect-port');
const path = require('path');
const log = require('../lib/utils/log');
const fse = require('fs-extra');

let child = null;
const rawArgv = parse(process.argv.slice(2));
Expand Down Expand Up @@ -65,9 +66,48 @@ function restartProcess() {
process.exit(code);
}
});

process.on('SIGINT', () => {
const rootDir = process.cwd();
const SIGINThooksFileName = 'SIGINThooks.json';
const hooksPath = path.resolve(rootDir, SIGINThooksFileName);
let hooks = legalSIGINThooks(hooksPath);
if (hooks) {
applySIGINThooks(hooks, hooksPath);
}
process.exit(1);
})
})();
}

const legalSIGINThooks = (hooksPath) => {
let hooks;
if (fse.existsSync(hooksPath)) {
hooks = fse.readJsonSync(hooksPath);
}
return hooks;
}

const applySIGINThooks = (hooks, hooksPath) => {

log.info('handleing SIGINThooks...');
try {
hooks.forEach(item => {
try {
const fn = eval(item);
fn();
} catch(e) {
log.error('SIGINThook item error', e);
}
})
} catch (e) {
log.error('SIGINThooks error', e);
}
fse.removeSync(hooksPath);
log.info('handleing SIGINThooks finished');
return
}

const onUserChange = () => {
console.log('\n');
log.info('build.json has been changed');
Expand Down
8 changes: 7 additions & 1 deletion packages/build-scripts/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export = async function({
log.verbose('OPTIONS', `${command} cliOptions: ${JSON.stringify(args, null, 2)}`);
let serverUrl = '';

const { applyHook, webpack } = context;
const {
applyHook,
webpack,
removeSIGINThooks,
} = context;

let configArr = [];
try {
Expand Down Expand Up @@ -108,6 +112,8 @@ export = async function({
isFirstCompile,
stats,
});

removeSIGINThooks();
});
// require webpack-dev-server after context setup
// context may hijack webpack resolve
Expand Down
31 changes: 31 additions & 0 deletions packages/build-scripts/src/core/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export interface IOnHook {
(eventName: string, callback: IOnHookCallback): void;
}

export interface IOnSIGINThooks {
(callback: (() => void)): void;
}

export interface IPluginConfigWebpack {
(config: WebpackChain): void;
}
Expand Down Expand Up @@ -678,6 +682,33 @@ class Context {
this.configArr = this.configArr.filter((config) => !this.cancelTaskNames.includes(config.name));
return this.configArr;
}

public removeSIGINThooks: () => void = () => {
const SIGINThooksFileName = 'SIGINThooks.json';
const hooksPath = path.resolve(this.rootDir, SIGINThooksFileName);
if (fs.existsSync(hooksPath)) {
fs.removeSync(hooksPath);
}
}

public onSIGINThooks: IOnSIGINThooks = (fn) => {
if (!fn || (typeof fn !== 'function')) {
log.warn('siginthooks arg wrong', 'fn should be function or stringify function');
return;
}

fn = (fn as any).toString();

const SIGINThooksFileName = 'SIGINThooks.json';
const hooksPath = path.resolve(this.rootDir, SIGINThooksFileName);
if (!fs.existsSync(hooksPath)) {
fs.ensureFileSync(hooksPath);
fs.outputFileSync(hooksPath, JSON.stringify([]));
}
const hooks = fs.readJsonSync(hooksPath);
hooks.push(fn);
fs.outputFileSync(hooksPath, JSON.stringify(hooks));
}
}

export default Context;