@@ -3,34 +3,38 @@ import { createRequire } from "node:module";
3
3
import { dirname , join } from "node:path" ;
4
4
import { fileURLToPath } from "node:url" ;
5
5
6
+ import type { ModuleBody , SourceFile } from "typescript" ;
6
7
// eslint-disable-next-line import/no-extraneous-dependencies
7
8
import ts from "typescript" ;
8
9
9
- const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
10
+ const rootPath = dirname ( fileURLToPath ( import . meta. url ) ) ;
10
11
11
- const e = ( message , fail = true ) => {
12
+ const showMessageAndExit = ( message : string , fail = true ) => {
12
13
// eslint-disable-next-line no-console
13
14
console . log ( message ) ;
14
15
16
+ // eslint-disable-next-line unicorn/no-process-exit
15
17
process . exit ( fail ? 1 : 0 ) ;
16
18
} ;
17
19
18
- function extract ( file ) {
20
+ const extract = ( file : string ) => {
19
21
const program = ts . createProgram ( [ file ] , { } ) ;
20
- const sourceFile = program . getSourceFile ( file ) ;
21
- const globals = [ ] ;
22
+ const sourceFile = program . getSourceFile ( file ) as SourceFile ;
23
+ const globals : string [ ] = [ ] ;
22
24
23
25
ts . forEachChild ( sourceFile , ( node ) => {
24
26
if ( ts . isModuleDeclaration ( node ) ) {
25
- ts . forEachChild ( node . body , ( node ) => {
26
- if ( ts . isVariableStatement ( node ) ) {
27
- ts . forEachChild ( node , ( node ) => {
28
- if ( ts . isVariableDeclarationList ( node ) ) {
29
- for ( const declaration of node . declarations ) {
27
+ ts . forEachChild ( node . body as ModuleBody , ( mNode ) => {
28
+ if ( ts . isVariableStatement ( mNode ) ) {
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ ts . forEachChild ( mNode , ( vNode : any ) => {
31
+ if ( ts . isVariableDeclarationList ( vNode ) ) {
32
+ // eslint-disable-next-line no-restricted-syntax
33
+ for ( const declaration of vNode . declarations ) {
30
34
const name = ts . getNameOfDeclaration ( declaration ) ;
31
35
32
- if ( name ) {
33
- globals . push ( name . escapedText ) ;
36
+ if ( name && "escapedText" in name ) {
37
+ globals . push ( name . escapedText as string ) ;
34
38
}
35
39
}
36
40
}
@@ -41,32 +45,41 @@ function extract(file) {
41
45
} ) ;
42
46
43
47
return globals ;
44
- }
48
+ } ;
45
49
46
50
const require = createRequire ( import . meta. url ) ;
47
51
const packagePath = require . resolve ( "vitest/package.json" ) ;
52
+
48
53
const {
49
54
default : { version : vitestVersion } ,
50
55
} = await import ( packagePath , {
51
56
with : { type : "json" } ,
52
57
} ) ;
53
58
54
59
if ( ! vitestVersion ) {
55
- e ( "Vitest version cannot be read." ) ;
60
+ showMessageAndExit ( "Vitest version cannot be read." ) ;
56
61
}
57
62
58
- writeFileSync ( join ( __dirname , ".." , "VERSION" ) , vitestVersion ) ;
59
-
60
63
const globalsPath = require . resolve ( "vitest/globals.d.ts" ) ;
64
+
61
65
const globalsArray = extract ( globalsPath ) ;
62
- const globals = { } ;
66
+ const globals : Record < string , boolean > = { } ;
67
+
68
+ if ( globalsArray . length === 0 ) {
69
+ showMessageAndExit ( "No globals! Check extractor implementation." ) ;
70
+ }
63
71
64
- if ( globalsArray . length === 0 ) e ( "No globals! Check extractor implementation." ) ;
72
+ globalsArray . forEach ( ( globalName ) => {
73
+ globals [ globalName ] = true ;
74
+ } ) ;
65
75
66
- globalsArray . forEach ( globalName => ( globals [ globalName ] = true ) ) ;
67
- const moduleContent = `export default /** @type {const} */ (${ JSON . stringify ( globals , undefined , 2 ) } );` ;
76
+ const moduleContent = `/**
77
+ * vitest version ${ vitestVersion }
78
+ */
79
+ export default /** @type {const} */ (${ JSON . stringify ( globals , undefined , 4 ) } );
80
+ ` ;
68
81
69
- writeFileSync ( join ( __dirname , ".." , "index.mjs " ) , moduleContent ) ;
82
+ writeFileSync ( join ( rootPath , ".." , "src" , "utils" , "vitest-globals.ts ") , moduleContent ) ;
70
83
71
84
// eslint-disable-next-line no-console
72
85
console . log ( "Finished generation with result:\n" , moduleContent ) ;
0 commit comments