Skip to content

Commit 8cb54c6

Browse files
committed
fix(tools): migrate update scripts to cross platform URL's
1 parent 4463dde commit 8cb54c6

File tree

11 files changed

+53
-71
lines changed

11 files changed

+53
-71
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import getReleasePlan from '@changesets/get-release-plan';
2-
import path from 'path';
2+
import { fileURLToPath } from 'url';
33

4-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
4+
const cwdURL = new URL('../../../..', import.meta.url);
55

66
/** Get new version string from changesets */
77
export async function getNewVersion(): Promise<string> {
8-
const releasePlan = await getReleasePlan(path.resolve(__dirname, '../../../..'));
8+
const releasePlan = await getReleasePlan(fileURLToPath(cwdURL));
99

1010
return releasePlan.releases.find(({ name }) => name === 'eslint-plugin-svelte')!.newVersion;
1111
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import path from 'path';
22
import fs from 'fs';
33
import type { RuleModule } from '../../src/types.js';
4+
import util from 'util';
45

5-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
6+
const rulesLibRootURL = new URL('../../src/rules/', import.meta.url);
67

78
/**
89
* Get the all rules
910
* @returns {Array} The all rules
1011
*/
1112
async function readRules() {
12-
const rulesLibRoot = path.resolve(__dirname, '../../src/rules');
1313
const rules: RuleModule[] = [];
1414
for (const name of iterateTsFiles()) {
15-
const module = await import(path.join(rulesLibRoot, name));
15+
const module = await import(new URL(name, rulesLibRootURL).href);
1616
const rule: RuleModule = module && module.default;
1717
if (!rule || typeof rule.create !== 'function') {
1818
continue;
@@ -27,19 +27,18 @@ export const rules = await readRules();
2727

2828
/** Iterate ts files */
2929
function* iterateTsFiles() {
30-
const rulesLibRoot = path.resolve(__dirname, '../../src/rules');
31-
const files = fs.readdirSync(rulesLibRoot);
30+
const files = fs.readdirSync(rulesLibRootURL);
3231

3332
while (files.length) {
3433
const file = files.shift()!;
3534
if (file.endsWith('.ts')) {
3635
yield file;
3736
continue;
3837
}
39-
const filePath = path.join(rulesLibRoot, file);
40-
if (!fs.statSync(filePath).isDirectory()) {
38+
const filePathURL = new URL(file, rulesLibRootURL);
39+
if (!fs.statSync(filePathURL).isDirectory()) {
4140
continue;
4241
}
43-
files.unshift(...fs.readdirSync(filePath).map((n) => path.join(file, n)));
42+
files.unshift(...fs.readdirSync(filePathURL).map((n) => path.join(file, n)));
4443
}
4544
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import fs from 'fs';
22
import { ESLint } from 'eslint';
33
import prettier from 'prettier';
4+
import { fileURLToPath } from 'url';
45

56
/**
67
* Write file and format it with ESLint
78
*/
8-
export function writeAndFormat(fileName: string, content: string): Promise<void> {
9-
fs.writeFileSync(fileName, content, 'utf8');
9+
export async function writeAndFormat(fileURL: URL, content: string): Promise<void> {
10+
fs.writeFileSync(fileURL, content, 'utf8');
11+
12+
const filePath = fileURLToPath(fileURL);
1013
return prettier
11-
.resolveConfig(fileName)
14+
.resolveConfig(fileURL)
1215
.then((prettierrc) => {
1316
if (!prettierrc) {
1417
return content;
1518
}
16-
return prettier.format(content, { filepath: fileName, ...prettierrc });
19+
return prettier.format(content, { filepath: filePath, ...prettierrc });
1720
})
1821
.then((formatted) => {
19-
fs.writeFileSync(fileName, formatted, 'utf8');
22+
fs.writeFileSync(fileURL, formatted, 'utf8');
2023
const eslint = new ESLint({ fix: true });
21-
return eslint.lintText(formatted, { filePath: fileName });
24+
return eslint.lintText(formatted, { filePath });
2225
})
2326
.then(([result]) => {
2427
if (result.output) {
25-
fs.writeFileSync(fileName, result.output, 'utf8');
28+
fs.writeFileSync(fileURL, result.output, 'utf8');
2629
}
2730
});
2831
}

packages/eslint-plugin-svelte/tools/update-docs-rules-index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import path from 'path';
21
import renderRulesTableContent from './render-rules.js';
32
import { writeAndFormat } from './lib/write.js';
43

5-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
6-
74
// -----------------------------------------------------------------------------
8-
const readmeFilePath = path.resolve(__dirname, '../../../docs/rules.md');
5+
const readmeFileURL = new URL('../../../docs/rules.md', import.meta.url);
96
void writeAndFormat(
10-
readmeFilePath,
7+
readmeFileURL,
118
`---
129
sidebarDepth: 0
1310
---

packages/eslint-plugin-svelte/tools/update-docs.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import path from 'path';
21
import fs from 'fs';
32
import { rules } from '../src/utils/rules.js';
43
import type { RuleModule } from '../src/types.js';
54
import { getNewVersion } from './lib/changesets-util.js';
65
import { writeAndFormat } from './lib/write.js';
76

8-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
9-
107
function formatItems(items: string[]) {
118
if (items.length <= 2) {
129
return items.join(' and ');
@@ -21,7 +18,7 @@ function yamlValue(val: unknown) {
2118
return val;
2219
}
2320

24-
const ROOT = path.resolve(__dirname, '../../../docs/rules');
21+
const ROOT_URL = new URL('../../../docs/rules/', import.meta.url);
2522

2623
function pickSince(content: string): string | null | Promise<string> {
2724
const fileIntro = /^---\n((?:.*\n)+)---\n*/.exec(content);
@@ -46,16 +43,16 @@ function pickSince(content: string): string | null | Promise<string> {
4643
class DocFile {
4744
private readonly rule: RuleModule;
4845

49-
private readonly filePath: string;
46+
private readonly fileURL: URL;
5047

5148
private content: string;
5249

5350
private readonly since: string | null | Promise<string>;
5451

5552
public constructor(rule: RuleModule) {
5653
this.rule = rule;
57-
this.filePath = path.join(ROOT, `${rule.meta.docs.ruleName}.md`);
58-
this.content = fs.readFileSync(this.filePath, 'utf8');
54+
this.fileURL = new URL(`./${rule.meta.docs.ruleName}.md`, ROOT_URL);
55+
this.content = fs.readFileSync(this.fileURL, 'utf8');
5956
this.since = pickSince(this.content);
6057
}
6158

@@ -221,7 +218,7 @@ ${
221218
public async write() {
222219
this.content = this.content.replace(/\r?\n/gu, '\n');
223220

224-
await writeAndFormat(this.filePath, this.content);
221+
await writeAndFormat(this.fileURL, this.content);
225222
}
226223
}
227224

packages/eslint-plugin-svelte/tools/update-meta.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { fileURLToPath } from 'url';
21
import { name, version } from '../package.json';
32
import { getNewVersion } from './lib/changesets-util.js';
43
import { writeAndFormat } from './lib/write.js';
54

6-
const fileURL = new URL('../src/meta.ts', import.meta.url);
7-
const META_PATH = fileURLToPath(fileURL);
5+
const META_URL = new URL('../src/meta.ts', import.meta.url);
86

97
void main();
108

119
/** main */
1210
async function main() {
1311
await writeAndFormat(
14-
META_PATH,
12+
META_URL,
1513
`/*
1614
* IMPORTANT!
1715
* This file has been automatically generated,

packages/eslint-plugin-svelte/tools/update-readme.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import path from 'path';
21
import fs from 'fs';
32
import renderRulesTableContent from './render-rules.js';
43
import { writeAndFormat } from './lib/write.js';
54

6-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
5+
const rootURL = new URL('../../../', import.meta.url);
76

87
const insertText = `\n${renderRulesTableContent(
98
(name) => `https://sveltejs.github.io/eslint-plugin-svelte/rules/${name}/`
109
)}\n`;
1110

12-
const readmeFilePath = path.resolve(__dirname, '../../../README.md');
11+
const readmeFileURL = new URL('README.md', rootURL);
1312
const newReadme = fs
14-
.readFileSync(readmeFilePath, 'utf8')
13+
.readFileSync(readmeFileURL, 'utf8')
1514
.replace(
1615
/<!--RULES_TABLE_START-->[\s\S]*<!--RULES_TABLE_END-->/u,
1716
`<!--RULES_TABLE_START-->${insertText.replace(/\$/g, '$$$$')}<!--RULES_TABLE_END-->`
1817
);
19-
void writeAndFormat(readmeFilePath, newReadme);
18+
void writeAndFormat(readmeFileURL, newReadme);
2019

21-
const docsReadmeFilePath = path.resolve(__dirname, '../../../docs/README.md');
20+
const docsReadmeFileURL = new URL('./docs/README.md', rootURL);
2221

2322
void writeAndFormat(
24-
docsReadmeFilePath,
23+
docsReadmeFileURL,
2524
`---
2625
title: "eslint-plugin-svelte"
2726
---
@@ -65,12 +64,12 @@ ${newReadme
6564
.replace(/\n{3,}/gu, '\n\n')}`
6665
);
6766

68-
const docsUserGuideFilePath = path.resolve(__dirname, '../../../docs/user-guide.md');
67+
const docsUserGuideFileURL = new URL('./docs/user-guide.md', rootURL);
6968

70-
const docsUserGuide = fs.readFileSync(docsUserGuideFilePath, 'utf8');
69+
const docsUserGuide = fs.readFileSync(docsUserGuideFileURL, 'utf8');
7170

7271
void writeAndFormat(
73-
docsUserGuideFilePath,
72+
docsUserGuideFileURL,
7473
docsUserGuide
7574
.replace(
7675
/<!--USAGE_GUIDE_START-->[\s\S]*<!--USAGE_GUIDE_END-->/u,

packages/eslint-plugin-svelte/tools/update-rule-types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import fs from 'fs';
2-
import path from 'path';
32
import plugin from '../src/index.js';
43

5-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
6-
74
void main();
85

96
async function main() {
@@ -13,7 +10,7 @@ async function main() {
1310
const ruleTypes = await pluginsToRulesDTS({ svelte: plugin });
1411

1512
void fs.writeFileSync(
16-
path.join(__dirname, '../src/rule-types.ts'),
13+
new URL('../src/rule-types.ts', import.meta.url),
1714
`// IMPORTANT!
1815
// This file has been automatically generated,
1916
// in order to update its content execute "pnpm run update"

packages/eslint-plugin-svelte/tools/update-rules.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import path from 'path';
21
// import eslint from "eslint"
32
import { rules } from './lib/load-rules.js';
43
import { writeAndFormat } from './lib/write.js';
54

6-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
7-
85
/**
96
* Convert text to camelCase
107
*/
@@ -41,7 +38,7 @@ export const rules = [
4138
] as RuleModule[]
4239
`;
4340

44-
const filePath = path.resolve(__dirname, '../src/utils/rules.ts');
41+
const fileURL = new URL('../src/utils/rules.ts', import.meta.url);
4542

4643
// Update file.
47-
void writeAndFormat(filePath, content);
44+
void writeAndFormat(fileURL, content);

packages/eslint-plugin-svelte/tools/update-rulesets.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import path from 'path';
21
import { rules } from './lib/load-rules.js';
32
import { writeAndFormat } from './lib/write.js';
43

5-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
4+
const flatConfigFolderURL = new URL('../src/configs/flat/', import.meta.url);
65

76
// ------------------
87
// Flat Config
@@ -79,10 +78,10 @@ const config: Linter.Config[] = [
7978
export default config
8079
`;
8180

82-
const baseFilePath = path.resolve(__dirname, '../src/configs/flat/base.ts');
81+
const baseFileURL = new URL('base.ts', flatConfigFolderURL);
8382

8483
// Update file.
85-
void writeAndFormat(baseFilePath, baseContent);
84+
void writeAndFormat(baseFileURL, baseContent);
8685

8786
const recommendedContent = `/*
8887
* IMPORTANT!
@@ -110,10 +109,10 @@ const config: Linter.Config[] = [
110109
export default config
111110
`;
112111

113-
const recommendedFilePath = path.resolve(__dirname, '../src/configs/flat/recommended.ts');
112+
const recommendedFileURL = new URL('recommended.ts', flatConfigFolderURL);
114113

115114
// Update file.
116-
void writeAndFormat(recommendedFilePath, recommendedContent);
115+
void writeAndFormat(recommendedFileURL, recommendedContent);
117116

118117
const prettierContent = `/*
119118
* IMPORTANT!
@@ -138,7 +137,7 @@ const config: Linter.Config[] = [
138137
export default config
139138
`;
140139

141-
const prettierFilePath = path.resolve(__dirname, '../src/configs/flat/prettier.ts');
140+
const prettierFileURL = new URL('prettier.ts', flatConfigFolderURL);
142141

143142
// Update file.
144-
void writeAndFormat(prettierFilePath, prettierContent);
143+
void writeAndFormat(prettierFileURL, prettierContent);

0 commit comments

Comments
 (0)