Skip to content

Commit

Permalink
feat: export some api for custom command (#20)
Browse files Browse the repository at this point in the history
* feat: export some api for custom command

* fix: typo
  • Loading branch information
czy88840616 authored May 13, 2024
1 parent 66fe001 commit adfcf66
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
48 changes: 36 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
debug,
getRelativeDir,
triggerMessage,
suffixMapping,
} = require('./util');
const path = require('path');
const { replaceTscAliasPaths } = require('tsc-alias');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -246,6 +255,10 @@ function run() {
runAfterTsc();
},
});
let customFn = () => {};
const onExitHandler = async () => {
await customFn();
};

async function onSignal() {
try {
Expand All @@ -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);
}
}
Expand All @@ -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;
19 changes: 19 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

0 comments on commit adfcf66

Please sign in to comment.