diff --git a/README.md b/README.md index 96184c7..2427a6b 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,40 @@ $ npx mwtsc --watch --project tsconfig.production.json --run ./bootstrap.js * 1、support `--run` option, run file after compile success * 2、support copy non-ts file to dist directory when build source code * 3、support ts alias path by tsc-alias + +## API + +You can use `mwtsc` API in your code to extend your own logic. + +```ts +// custom.js +const { run, chokidar } = require('mwtsc'); +const { restart, exit, onExit, onRestart } = run(); + +const watcher = chokidar.watch('node_modules/**/*.ts', { + ignoreInitial: true, + ignored: ['**/*.d.ts'], +}); + +watcher.on('all', (event, path) => { + console.log(event, path); + restart([ + '/home/admin/workspace/midwayjs/midway/packages/mock/src/app.ts', + ]); +}); + +onExit(async () => { + await watcher.close(); +}); + +``` + +Rewrite dev command in `package.json` . + +```json +{ + "scripts": { + "dev": "./custom.js --watch --run ./bootstrap.js" + } +} +``` diff --git a/lib/index.js b/lib/index.js index 9fdecf7..91048ef 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,6 +11,7 @@ const { debug, getRelativeDir, triggerMessage, + suffixMapping, } = require('./util'); const path = require('path'); const { replaceTscAliasPaths } = require('tsc-alias'); @@ -136,19 +137,27 @@ function run() { } function cleanOutDirAndRestart(p) { - const distPath = path.join(cwd, outDir, p); - if (!fs.existsSync(distPath)) return; - const stat = fs.statSync(distPath); - // is file - if (stat.isFile()) { - // remove file - fs.unlinkSync(distPath); - } else { - // is directory - deleteFolderRecursive(distPath); + // p = 'a.ts' + let changedCnt = 0; + for (const pa of suffixMapping(p)) { + const distPath = path.join(cwd, outDir, pa); + if (!fs.existsSync(distPath)) continue; + const stat = fs.statSync(distPath); + changedCnt++; + // is file + if (stat.isFile()) { + // remove file + fs.unlinkSync(distPath); + } else { + // is directory + deleteFolderRecursive(distPath); + } + } + + if (changedCnt) { + runAfterTsc(); + restart(); } - runAfterTsc(); - restart(); } let fileDeleteWatcher; @@ -246,6 +255,10 @@ function run() { runAfterTsc(); }, }); + let customFn = () => {}; + const onExitHandler = async () => { + await customFn(); + }; async function onSignal() { try { @@ -257,9 +270,11 @@ function run() { if (fileDeleteWatcher) { await fileDeleteWatcher.close(); } + await onExitHandler(); process.exit(0); } catch (err) { console.error(err); + await onExitHandler(); process.exit(1); } } @@ -269,6 +284,15 @@ function run() { process.once('SIGQUIT', onSignal); // kill(15) default process.once('SIGTERM', onSignal); + + return { + restart, + exit: onSignal, + onExit: fn => { + customFn = fn; + }, + }; } exports.run = run; +exports.chokidar = chokidar; diff --git a/lib/util.js b/lib/util.js index 2e461d0..2b8b56a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -288,3 +288,22 @@ exports.triggerMessage = function (message) { process.emit('message', message); } }; + +const suffixMap = { + '.ts': ['.js', '.d.ts'], + '.tsx': ['.jsx', '.d.ts'], + '.js': ['.js', '.d.ts'], + '.jsx': ['.jsx', '.d.ts'], + '.d.ts': ['.d.ts'], + '.d.tsx': ['.d.ts'], + '.mts': ['.js', '.d.ts'], + '.mtsx': ['.jsx', '.d.ts'], + '.cts': ['.js', '.d.ts'], + '.ctsx': ['.jsx', '.d.ts'], +}; + +exports.suffixMapping = function (p) { + if (!p) return []; + const suffix = path.extname(p); + return suffixMap[suffix] || [suffix]; +};