File tree 2 files changed +34
-4
lines changed
2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -286,10 +286,13 @@ export function getVersion(): string {
286
286
return createRequire ( import . meta. url ) ( '../package.json' ) . version
287
287
}
288
288
289
- function isMain ( ) {
290
- if ( import . meta. url . startsWith ( 'file:' ) ) {
291
- const modulePath = url . fileURLToPath ( import . meta. url ) . replace ( / \. \w + $ / , '' )
292
- const mainPath = fs . realpathSync ( process . argv [ 1 ] ) . replace ( / \. \w + $ / , '' )
289
+ export function isMain (
290
+ metaurl = import . meta. url ,
291
+ scriptpath = process . argv [ 1 ]
292
+ ) {
293
+ if ( metaurl . startsWith ( 'file:' ) ) {
294
+ const modulePath = url . fileURLToPath ( metaurl ) . replace ( / \. \w + $ / , '' )
295
+ const mainPath = fs . realpathSync ( scriptpath ) . replace ( / \. \w + $ / , '' )
293
296
return mainPath === modulePath
294
297
}
295
298
Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ import assert from 'node:assert'
16
16
import { test , describe , beforeEach } from 'node:test'
17
17
import { fileURLToPath } from 'node:url'
18
18
import '../build/globals.js'
19
+ import { isMain } from '../build/cli.js'
20
+
21
+ const __filename = fileURLToPath ( import . meta. url )
19
22
20
23
describe ( 'cli' , ( ) => {
21
24
// Helps detect unresolved ProcessPromise.
@@ -224,4 +227,28 @@ describe('cli', () => {
224
227
let p = await $ `node build/cli.js test/fixtures/exit-code.mjs` . nothrow ( )
225
228
assert . equal ( p . exitCode , 42 )
226
229
} )
230
+
231
+ describe ( 'internals' , ( ) => {
232
+ test ( 'isMain() checks process entry point' , ( ) => {
233
+ assert . equal ( isMain ( import . meta. url , __filename ) , true )
234
+
235
+ assert . equal (
236
+ isMain ( import . meta. url . replace ( '.js' , '.cjs' ) , __filename ) ,
237
+ true
238
+ )
239
+
240
+ try {
241
+ assert . equal (
242
+ isMain (
243
+ 'file:///root/zx/test/cli.test.js' ,
244
+ '/root/zx/test/all.test.js'
245
+ ) ,
246
+ true
247
+ )
248
+ assert . throw ( )
249
+ } catch ( e ) {
250
+ assert . equal ( e . code , 'ENOENT' )
251
+ }
252
+ } )
253
+ } )
227
254
} )
You can’t perform that action at this time.
0 commit comments