1
1
import path from 'node:path' ;
2
2
import fs from 'node:fs' ;
3
3
import cp from 'node:child_process' ;
4
- import url from 'node:url' ;
5
4
import { writeAndFormat } from './lib/write.js' ;
6
-
7
- const __filename = url . fileURLToPath ( import . meta. url ) ;
8
- const __dirname = path . dirname ( __filename ) ;
5
+ import { fileURLToPath } from 'node:url' ;
9
6
10
7
const logger = console ;
11
8
@@ -21,39 +18,39 @@ void (async ([ruleId, ...args]) => {
21
18
process . exitCode = 1 ;
22
19
return ;
23
20
}
24
- const utilsPath = path . resolve ( __dirname , `../src/utils/index.ts` ) ;
25
- const testUtilsPath = path . resolve ( __dirname , `../tests/utils/utils.ts` ) ;
21
+ const utilsURL = new URL ( `../src/utils/index.ts` , import . meta . url ) ;
22
+ const testUtilsURL = new URL ( `../tests/utils/utils.ts` , import . meta . url ) ;
26
23
27
- const ruleFile = path . resolve ( __dirname , `../src/rules/${ ruleId } .ts` ) ;
28
- const testFile = path . resolve ( __dirname , `../tests/src/rules/${ ruleId } .ts` ) ;
29
- const docFile = path . resolve ( __dirname , `../../../docs/rules/${ ruleId } .md` ) ;
30
- const fixturesRoot = path . resolve ( __dirname , `../tests/fixtures/rules/${ ruleId } /` ) ;
24
+ const ruleFileURL = new URL ( `../src/rules/${ ruleId } .ts` , import . meta . url ) ;
25
+ const testFileURL = new URL ( `../tests/src/rules/${ ruleId } .ts` , import . meta . url ) ;
26
+ const docFileURL = new URL ( `../../../docs/rules/${ ruleId } .md` , import . meta . url ) ;
27
+ const fixturesRootURL = new URL ( `../tests/fixtures/rules/${ ruleId } /` , import . meta . url ) ;
31
28
try {
32
- fs . mkdirSync ( path . dirname ( ruleFile ) , { recursive : true } ) ;
29
+ fs . mkdirSync ( new URL ( './' , ruleFileURL ) , { recursive : true } ) ;
33
30
} catch {
34
31
// ignore
35
32
}
36
33
try {
37
- fs . mkdirSync ( path . dirname ( testFile ) , { recursive : true } ) ;
34
+ fs . mkdirSync ( new URL ( './' , testFileURL ) , { recursive : true } ) ;
38
35
} catch {
39
36
// ignore
40
37
}
41
38
try {
42
- fs . mkdirSync ( path . dirname ( docFile ) , { recursive : true } ) ;
39
+ fs . mkdirSync ( new URL ( './' , docFileURL ) , { recursive : true } ) ;
43
40
} catch {
44
41
// ignore
45
42
}
46
43
try {
47
- fs . mkdirSync ( path . resolve ( fixturesRoot , ' valid') , { recursive : true } ) ;
48
- fs . mkdirSync ( path . resolve ( fixturesRoot , ' invalid') , { recursive : true } ) ;
44
+ fs . mkdirSync ( new URL ( './ valid', fixturesRootURL ) , { recursive : true } ) ;
45
+ fs . mkdirSync ( new URL ( './ invalid', fixturesRootURL ) , { recursive : true } ) ;
49
46
} catch {
50
47
// ignore
51
48
}
52
49
53
50
await writeAndFormat (
54
- ruleFile ,
51
+ ruleFileURL ,
55
52
`import { AST } from 'svelte-eslint-parser';
56
- import { createRule } from '${ getModulePath ( ruleFile , utilsPath ) } ';
53
+ import { createRule } from '${ getModulePath ( ruleFileURL , utilsURL ) } ';
57
54
58
55
export default createRule('${ ruleId } ', {
59
56
meta: {
@@ -74,10 +71,10 @@ export default createRule('${ruleId}', {
74
71
`
75
72
) ;
76
73
await writeAndFormat (
77
- testFile ,
74
+ testFileURL ,
78
75
`import { RuleTester } from '../../utils/eslint-compat.js';
79
- import rule from '${ getModulePath ( testFile , ruleFile ) } ';
80
- import { loadTestCases } from '${ getModulePath ( testFile , testUtilsPath ) } ';
76
+ import rule from '${ getModulePath ( testFileURL , ruleFileURL ) } ';
77
+ import { loadTestCases } from '${ getModulePath ( testFileURL , testUtilsURL ) } ';
81
78
82
79
const tester = new RuleTester({
83
80
languageOptions: {
@@ -90,7 +87,7 @@ tester.run('${ruleId}', rule as any, loadTestCases('${ruleId}'));
90
87
`
91
88
) ;
92
89
await writeAndFormat (
93
- docFile ,
90
+ docFileURL ,
94
91
`# (svelte/${ ruleId } )
95
92
96
93
> description
@@ -139,17 +136,20 @@ This rule reports ???.
139
136
try {
140
137
// Use code -v to know if vscode is installed and do not print anything to the console
141
138
cp . execSync ( 'code -v' , { stdio : 'ignore' } ) ;
142
- cp . execSync ( `code "${ ruleFile } "` ) ;
143
- cp . execSync ( `code "${ testFile } "` ) ;
144
- cp . execSync ( `code "${ docFile } "` ) ;
139
+ cp . execSync ( `code "${ fileURLToPath ( ruleFileURL ) } "` ) ;
140
+ cp . execSync ( `code "${ fileURLToPath ( testFileURL ) } "` ) ;
141
+ cp . execSync ( `code "${ fileURLToPath ( docFileURL ) } "` ) ;
145
142
} catch {
146
143
logger . error ( 'Unable to find code command. Will not open files with VSCode.' ) ;
147
144
}
148
145
} ) ( process . argv . slice ( 2 ) ) ;
149
146
150
147
/** Get module path */
151
- function getModulePath ( from : string , module : string ) : string {
152
- return path . relative ( path . dirname ( from ) , module ) . replace ( / .t s $ / u, '.js' ) ;
148
+ function getModulePath ( from : URL , module : URL ) : string {
149
+ const fromDir = fileURLToPath ( new URL ( './' , from ) ) ;
150
+ const modulePath = fileURLToPath ( module ) ;
151
+
152
+ return path . relative ( fromDir , modulePath ) . replace ( / \\ / g, '/' ) . replace ( / .t s $ / u, '.js' ) ;
153
153
}
154
154
155
155
/** Argument parsing */
0 commit comments