Skip to content

Commit 8be25ae

Browse files
committed
build(ccusage): remove launcher bundle step
Rewrite the npm bin launcher as checked ESM JavaScript so the package can ship src/cli.js directly instead of compiling src/cli.ts into dist/cli.js with tsdown. Point package metadata and tests at the source launcher, keep the native binary and publint checks in the build script, and remove the now-unused tsdown build catalog entries from the workspace lockfile.
1 parent ed48f25 commit 8be25ae

10 files changed

Lines changed: 68 additions & 552 deletions

File tree

‎.github/workflows/ci.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ jobs:
141141
- uses: ./.github/actions/setup-nix-cache
142142
# Packing only repackages the native binaries restored from the build
143143
# matrix below (no Rust build happens here), so the full `nix develop` dev
144-
# shell is unnecessary. The prepack scripts run tsdown with node; Nushell
145-
# package checks bring Nu through their Nix shebangs.
144+
# shell is unnecessary. The package checks bring Nu through their Nix
145+
# shebangs.
146146
- run: nix profile install --inputs-from . nixpkgs#pnpm nixpkgs#nodejs
147147
- run: pnpm install --frozen-lockfile
148148
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1

‎apps/ccusage/AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This is the published `ccusage` npm package. The CLI implementation lives in Rus
1111

1212
## Package Notes
1313

14-
- Published bin launcher: `src/cli.ts`
14+
- Published bin launcher: `src/cli.js`
1515
- Rust CLI implementation: `../../rust/crates/ccusage`
1616
- PR benchmark scripts: `scripts/compare-pr-performance.nu` and `scripts/generate-large-fixture.nu`
1717

‎apps/ccusage/justfile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
default:
66
@just --list
77

8-
# Build the package bundle and verify native binary glue.
8+
# Verify native binary glue for the published package.
99
build: build-package
1010

1111
build-package:
12-
pnpm exec tsdown
1312
just ensure-native-binary
13+
pnpm exec publint
1414

1515
prepack:
1616
just build-package

‎apps/ccusage/package.json‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@
1515
},
1616
"funding": "https://github.com/sponsors/ryoppippi",
1717
"bin": {
18-
"ccusage": "./src/cli.ts"
18+
"ccusage": "./src/cli.js"
1919
},
2020
"files": [
2121
"config-schema.json",
22-
"dist/**/*.js"
22+
"src/cli.js"
2323
],
2424
"type": "module",
25-
"publishConfig": {
26-
"bin": {
27-
"ccusage": "./dist/cli.js"
28-
}
29-
},
3025
"scripts": {
31-
"build": "tsdown && scripts/ensure-native-binary.nu",
26+
"build": "scripts/ensure-native-binary.nu && publint",
3227
"prepack": "pnpm run build && clean-pkg-json"
3328
},
3429
"devDependencies": {
3530
"@types/node": "catalog:types",
3631
"clean-pkg-json": "catalog:release",
37-
"publint": "catalog:lint",
38-
"tsdown": "catalog:build"
32+
"publint": "catalog:lint"
3933
},
4034
"optionalDependencies": {
4135
"@ccusage/ccusage-darwin-arm64": "workspace:*",
Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
// @ts-check
23
import { spawn } from 'node:child_process';
34
import { chmodSync, realpathSync, statSync } from 'node:fs';
45
import { createRequire } from 'node:module';
@@ -7,37 +8,20 @@ import { fileURLToPath } from 'node:url';
78

89
const require = createRequire(import.meta.url);
910

10-
type CliRuntime =
11-
| {
12-
args: string[];
13-
command: string;
14-
}
15-
| {
16-
errorMessage: string;
17-
};
18-
19-
type FileMode = {
20-
mode: number;
21-
};
22-
23-
type NativeSpawnResult = {
24-
error?: Error;
25-
signal?: NodeJS.Signals | null;
26-
status: number | null;
27-
};
28-
29-
type NativeSpawner = (command: string, args: string[]) => Promise<NativeSpawnResult>;
30-
31-
type MainModuleOptions = {
32-
argvEntry?: string;
33-
moduleUrl: string;
34-
realpathPath?: (path: string) => string;
35-
};
36-
37-
function getNativePackageName(
38-
platform: string = process.platform,
39-
arch: string = process.arch,
40-
): string | undefined {
11+
/**
12+
* @typedef {{ args: string[]; command: string } | { errorMessage: string }} CliRuntime
13+
* @typedef {{ mode: number }} FileMode
14+
* @typedef {{ error?: Error; signal?: NodeJS.Signals | null; status: number | null }} NativeSpawnResult
15+
* @typedef {(command: string, args: string[]) => Promise<NativeSpawnResult>} NativeSpawner
16+
* @typedef {{ argvEntry?: string; moduleUrl: string; realpathPath?: (path: string) => string }} MainModuleOptions
17+
*/
18+
19+
/**
20+
* @param {string} [platform]
21+
* @param {string} [arch]
22+
* @returns {string | undefined}
23+
*/
24+
function getNativePackageName(platform = process.platform, arch = process.arch) {
4125
if (platform === 'darwin') {
4226
if (arch === 'arm64') {
4327
return '@ccusage/ccusage-darwin-arm64';
@@ -70,19 +54,23 @@ function getNativePackageName(
7054
return undefined;
7155
}
7256

73-
function getNativeBinarySubpath(platform: string = process.platform): string {
57+
/**
58+
* @param {string} [platform]
59+
* @returns {string}
60+
*/
61+
function getNativeBinarySubpath(platform = process.platform) {
7462
return platform === 'win32' ? 'bin/ccusage.exe' : 'bin/ccusage';
7563
}
7664

65+
/**
66+
* @param {{ arch?: string; platform?: string; resolvePath?: (id: string) => string }} [options]
67+
* @returns {string | undefined}
68+
*/
7769
function resolveNativeBinary({
7870
arch = process.arch,
7971
platform = process.platform,
8072
resolvePath = (id) => require.resolve(id),
81-
}: {
82-
arch?: string;
83-
platform?: string;
84-
resolvePath?: (id: string) => string;
85-
} = {}): string | undefined {
73+
} = {}) {
8674
const packageName = getNativePackageName(platform, arch);
8775
if (packageName == null) {
8876
return undefined;
@@ -95,17 +83,16 @@ function resolveNativeBinary({
9583
}
9684
}
9785

86+
/**
87+
* @param {{ arch?: string; argv: string[]; nativeBinaryPath?: string | null; platform?: string }} options
88+
* @returns {CliRuntime}
89+
*/
9890
function resolveCliRuntime({
9991
argv,
10092
arch = process.arch,
10193
nativeBinaryPath = resolveNativeBinary(),
10294
platform = process.platform,
103-
}: {
104-
arch?: string;
105-
argv: string[];
106-
nativeBinaryPath?: string | null;
107-
platform?: string;
108-
}): CliRuntime {
95+
}) {
10996
if (nativeBinaryPath != null) {
11097
return {
11198
args: argv,
@@ -118,21 +105,24 @@ function resolveCliRuntime({
118105
};
119106
}
120107

121-
function errorMessage(error: unknown): string {
108+
/**
109+
* @param {unknown} error
110+
* @returns {string}
111+
*/
112+
function errorMessage(error) {
122113
return error instanceof Error ? error.message : String(error);
123114
}
124115

116+
/**
117+
* @param {{ binaryPath: string; chmodPath?: (path: string, mode: number) => void; platform?: string; statPath?: (path: string) => FileMode }} options
118+
* @returns {string | undefined}
119+
*/
125120
function ensureNativeBinaryExecutable({
126121
binaryPath,
127122
chmodPath = chmodSync,
128123
platform = process.platform,
129124
statPath = statSync,
130-
}: {
131-
binaryPath: string;
132-
chmodPath?: (path: string, mode: number) => void;
133-
platform?: string;
134-
statPath?: (path: string) => FileMode;
135-
}): string | undefined {
125+
}) {
136126
if (platform === 'win32') {
137127
return undefined;
138128
}
@@ -149,11 +139,11 @@ function ensureNativeBinaryExecutable({
149139
}
150140
}
151141

152-
function isMainModule({
153-
argvEntry = process.argv[1],
154-
moduleUrl,
155-
realpathPath = realpathSync,
156-
}: MainModuleOptions): boolean {
142+
/**
143+
* @param {MainModuleOptions} options
144+
* @returns {boolean}
145+
*/
146+
function isMainModule({ argvEntry = process.argv[1], moduleUrl, realpathPath = realpathSync }) {
157147
if (argvEntry == null) {
158148
return false;
159149
}
@@ -166,7 +156,10 @@ function isMainModule({
166156
}
167157
}
168158

169-
function createNativeSpawner(): NativeSpawner {
159+
/**
160+
* @returns {NativeSpawner}
161+
*/
162+
function createNativeSpawner() {
170163
return async (command, args) =>
171164
new Promise((resolve) => {
172165
const child = spawn(command, args, { stdio: 'inherit' });
@@ -186,10 +179,12 @@ function createNativeSpawner(): NativeSpawner {
186179
});
187180
}
188181

189-
async function runCli(
190-
argv: string[],
191-
spawnNative: NativeSpawner = createNativeSpawner(),
192-
): Promise<number> {
182+
/**
183+
* @param {string[]} argv
184+
* @param {NativeSpawner} [spawnNative]
185+
* @returns {Promise<number>}
186+
*/
187+
async function runCli(argv, spawnNative = createNativeSpawner()) {
193188
const runtime = resolveCliRuntime({ argv });
194189
if ('errorMessage' in runtime) {
195190
process.stderr.write(runtime.errorMessage);

‎apps/ccusage/src/cli.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
isMainModule,
66
resolveCliRuntime,
77
resolveNativeBinary,
8-
} from './cli.ts';
8+
} from './cli.js';
99

1010
void describe(resolveCliRuntime.name, () => {
1111
void it('resolves the native package binary for the current supported platform', () => {
@@ -113,10 +113,10 @@ void describe(resolveCliRuntime.name, () => {
113113
void it('treats package bin symlinks as the main module entry point', () => {
114114
const actual = isMainModule({
115115
argvEntry: '/project/node_modules/.bin/ccusage',
116-
moduleUrl: 'file:///project/node_modules/ccusage/dist/cli.js',
116+
moduleUrl: 'file:///project/node_modules/ccusage/src/cli.js',
117117
realpathPath: (path) =>
118118
path === '/project/node_modules/.bin/ccusage'
119-
? '/project/node_modules/ccusage/dist/cli.js'
119+
? '/project/node_modules/ccusage/src/cli.js'
120120
: path,
121121
});
122122

‎apps/ccusage/tsdown.config.ts‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎justfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Whole-repo jobs that the Nix flake owns (formatting, checks, schema) stay here.
66
#
77
# pnpm policy: repo-global tools provided by the Nix dev shell (cargo, oxlint, nix)
8-
# are called directly; package-scoped node binaries (vitepress, tsdown)
8+
# are called directly; package-scoped node binaries (vitepress)
99
# go through pnpm; `build` is delegated with `pnpm run` because npm prepack
1010
# invokes that script by name.
1111
#

0 commit comments

Comments
 (0)