Skip to content

Commit 2f39b24

Browse files
authored
refactor(tool): unify the cli entrypoint (#275)
I'm preparing to add more tools to `@voidzero-dev/vite-plus-tools`. Before adding more commands and tools, I want to unify the CLI entry point first.
1 parent defe1aa commit 2f39b24

14 files changed

Lines changed: 108 additions & 55 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- uses: oxc-project/setup-node@fdbf0dfd334c4e6d56ceeb77d91c76339c2a0885 # v1.0.4
4949

5050
- name: Set binding version
51-
run: pnpm exec replace-file-content packages/cli/binding/Cargo.toml 'version = "0.0.0"' 'version = "0.0.0-${{ github.sha }}"'
51+
run: pnpm exec tool replace-file-content packages/cli/binding/Cargo.toml 'version = "0.0.0"' 'version = "0.0.0-${{ github.sha }}"'
5252

5353
- name: Build
5454
if: ${{ matrix.settings.target == 'x86_64-unknown-linux-gnu' }}

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"scripts": {
2929
"build": "oxnode ./build.ts",
30-
"snap-test": "snap-test"
30+
"snap-test": "tool snap-test"
3131
},
3232
"files": [
3333
"bin",

packages/global/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"scripts": {
2121
"build": "rolldown -c rolldown.config.ts",
22-
"snap-test": "snap-test"
22+
"snap-test": "tool snap-test"
2323
},
2424
"dependencies": {
2525
"@voidzero-dev/vite-plus": "workspace:*",

packages/tools/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
"private": true,
44
"type": "module",
55
"bin": {
6-
"json-edit": "./src/json-edit.ts",
7-
"snap-test": "./src/snap-test.ts",
8-
"replace-file-content": "./src/replace-file-content.ts"
6+
"tool": "./src/bin.js",
7+
"json-edit": "./src/json-edit.ts"
98
},
109
"devDependencies": {
1110
"minimatch": "catalog:"
1211
},
1312
"scripts": {
14-
"snap-test": "snap-test"
13+
"snap-test": "tool snap-test"
1514
}
1615
}

packages/tools/snap-tests/replace-file-content/snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ crossterm = { workspace = true }
1414
napi = { workspace = true }
1515
napi-derive = { workspace = true }
1616

17-
> replace-file-content foo/example.toml 'version = "0.0.0"' 'version = "1.0.0"' && cat foo/example.toml # should edit toml file
17+
> tool replace-file-content foo/example.toml 'version = "0.0.0"' 'version = "1.0.0"' && cat foo/example.toml # should edit toml file
1818
[package]
1919
name = "foo"
2020
version = "1.0.0"

packages/tools/snap-tests/replace-file-content/steps.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
},
44
"commands": [
55
"cat foo/example.toml # should show original toml file",
6-
"replace-file-content foo/example.toml 'version = \"0.0.0\"' 'version = \"1.0.0\"' && cat foo/example.toml # should edit toml file"
6+
"tool replace-file-content foo/example.toml 'version = \"0.0.0\"' 'version = \"1.0.0\"' && cat foo/example.toml # should edit toml file"
77
]
88
}

packages/tools/src/__tests__/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { tmpdir } from 'node:os';
22

33
import { describe, expect, test } from '@voidzero-dev/vite-plus/test';
44

5-
import { isPassThroughEnv, replaceUnstableOutput } from '../utils.ts';
5+
import { isPassThroughEnv, replaceUnstableOutput } from '../utils';
66

77
describe('replaceUnstableOutput()', () => {
88
test('replace unstable semver version', () => {

packages/tools/src/bin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import '@oxc-node/core/register';
2+
3+
// defer the import to avoid the register hook is not being called
4+
await import('./index.ts');

packages/tools/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { replaceFileContent } from './replace-file-content';
2+
import { snapTest } from './snap-test';
3+
4+
const subcommand = process.argv[2];
5+
6+
switch (subcommand) {
7+
case 'snap-test':
8+
await snapTest();
9+
break;
10+
case 'replace-file-content':
11+
replaceFileContent();
12+
break;
13+
default:
14+
console.error(`Unknown subcommand: ${subcommand}`);
15+
console.error('Available subcommands: snap-test, replace-file-content');
16+
process.exit(1);
17+
}

packages/tools/src/json-edit.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#!/usr/bin/env node
22

33
import { readFileSync, writeFileSync } from 'node:fs';
4+
import { parseArgs } from 'node:util';
45

5-
const filename = process.argv[2];
6-
const script = process.argv[3];
6+
const { positionals } = parseArgs({
7+
allowPositionals: true,
8+
});
9+
10+
const filename = positionals[0];
11+
const script = positionals[1];
712

813
if (!filename || !script) {
914
console.error('Usage: json-edit <filename> <script>');

0 commit comments

Comments
 (0)