11#!/usr/bin/env node
2+ // @ts -check
23import { spawn } from 'node:child_process' ;
34import { chmodSync , realpathSync , statSync } from 'node:fs' ;
45import { createRequire } from 'node:module' ;
@@ -7,37 +8,20 @@ import { fileURLToPath } from 'node:url';
78
89const 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+ */
7769function 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+ */
9890function 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+ */
125120function 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 ) ;
0 commit comments