Skip to content

Commit

Permalink
Build Development
Browse files Browse the repository at this point in the history
- `npm run watch` now watches both of the parts;
- Add `npm run serve` command;
- Remove `TODO: promisify cmd`. It's solves nothing right now;
- build.js: use switch-case-s instead of if-else-s
- build.js: make cmd() flatten the args to enable a better
  self-documenting of the running commands.
  • Loading branch information
rexim committed Nov 22, 2023
1 parent 2fd5632 commit 984b7da
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ This compiles [./serviceworker.ts](./serviceworker.ts) to [./serviceworker.js](.

## Watching

The [./build.js](./build.js) script enables you to [Watch](https://www.typescriptlang.org/docs/handbook/configuring-watch.html#handbook-content) the source code. Since the build is split into two separate parts you have to watch them separately for now:
The [./build.js](./build.js) script enables you to [Watch](https://www.typescriptlang.org/docs/handbook/configuring-watch.html#handbook-content) the source code:

```console
$ npm run watch
```

To watch the `main` part:
```console
Expand Down
65 changes: 41 additions & 24 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { spawn } from 'child_process';

// TODO: promisify cmd
function cmd(program, args) {
console.log('CMD:', program, args);
const p = spawn(program, args);
const p = spawn(program, args.flat()); // NOTE: flattening the args array enables you to group related arguments for better self-documentation of the running command
p.stdout.on('data', (data) => process.stdout.write(data));
p.stderr.on('data', (data) => process.stderr.write(data));
p.on('close', (code) => {
Expand All @@ -30,53 +29,71 @@ const mainTs = [
function tscMain(...extraParams) {
cmd('tsc', [
...commonTscFlags,
'--outDir', 'js',
['--outDir', 'js'],
...extraParams,
...mainTs,
mainTs,
]);
}

function tscServiceWorker(...extraParams) {
cmd('tsc', [
...commonTscFlags,
'--lib', 'webworker',
'--outFile', 'serviceworker.js',
['--lib', 'webworker'],
['--outFile', 'serviceworker.js'],
...extraParams,
'serviceworker.ts'
]);
}

function build(part, ...args) {
if (part === undefined) {
switch (part) {
case undefined:
tscServiceWorker();
tscMain();
} else if (part === 'main') {
break;
case 'main':
tscMain();
} else if (part === 'serviceworker') {
break;
case 'serviceworker':
tscServiceWorker();
} else {
throw new Error(`Unknown build part {part}`);
break;
default:
throw new Error(`Unknown build part ${part}. Available parts: main, serviceworker.`);
}
}

function watch(part, ...args) {
if (part === undefined || part === 'main') {
// TODO: is it possible to watch both parts?
tscMain('-w');
} else if (part === 'serviceworker') {
tscServiceWorker('-w');
} else {
throw new Error(`Unknown watch part {part}`);
switch (part) {
case undefined:
tscMain('-w', '--preserveWatchOutput');
tscServiceWorker('-w', '--preserveWatchOutput');
break;
case 'main':
tscMain('-w', '--preserveWatchOutput');
break;
case 'serviceworker':
tscServiceWorker('-w', '--preserveWatchOutput');
break;
default:
throw new Error(`Unknown watch part ${part}. Available parts: main, serviceworker.`);
}
}

const [nodePath, scriptPath, command, ...args] = process.argv;

if (command === undefined || command === 'build') {
switch (command) {
case undefined:
case 'build':
build(...args);
} else if (command === 'watch') {
break;
case 'watch':
watch(...args);
} else {
// TODO: add `serve` command
throw new Error(`Unknown command {command}`);
break;
case 'serve':
// TODO: maybe replace Python with something from Node itself?
// Python is a pretty unreasonable dependency.
cmd('python3', [['-m', 'http.server'], '6969']);
watch();
break;
default:
throw new Error(`Unknown command ${command}. Available commands: build, watch.`);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"main": "index.js",
"type": "module",
"scripts": {
"build": "node build.js",
"build": "node build.js build",
"watch": "node build.js watch",
"serve": "node build.js serve",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down

0 comments on commit 984b7da

Please sign in to comment.