Skip to content

Commit 00eec09

Browse files
committed
refactor: switch scripts to esm
1 parent e14e300 commit 00eec09

File tree

9 files changed

+312
-297
lines changed

9 files changed

+312
-297
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
run: pnpm build
4545

4646
- name: Run release script
47-
run: npx tsx ./scripts/release.ts
47+
run: npx tsx ./scripts/release.mts
4848
env:
4949
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
5050
VSCE_TOKEN: ${{ secrets.VSCE_TOKEN }}

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"description": "Integrates phpfmt into VS Code",
66
"main": "./dist/extension",
77
"scripts": {
8-
"build": "nr clean && tsc && tsup && tsx scripts/copy.ts",
9-
"build:docs": "tsx scripts/docs.ts",
8+
"build": "nr clean && tsc && tsup && tsx scripts/copy.mts",
9+
"build:docs": "tsx scripts/docs.mts",
1010
"prebuild": "nr build:docs",
1111
"watch": "tsup --watch",
1212
"clean": "rimraf out",
@@ -670,6 +670,8 @@
670670
"@vscode/vsce": "^2.31.1",
671671
"adm-zip": "^0.5.10",
672672
"consola": "^3.2.3",
673+
"debug": "^4.3.4",
674+
"dirname-filename-esm": "^1.1.2",
673675
"eslint": "^8.57.0",
674676
"eslint-config-love": "^71.0.0",
675677
"eslint-config-prettier": "^9.1.0",

pnpm-lock.yaml

+16-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/copy.mts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from 'node:path';
2+
import fs from 'node:fs/promises';
3+
import phpfmt from 'phpfmt';
4+
import { dirname } from 'dirname-filename-esm';
5+
6+
const __dirname = dirname(import.meta);
7+
8+
try {
9+
const pkgPath = path.join(__dirname, '..');
10+
const distPath = path.join(pkgPath, 'dist');
11+
const destPath = path.join(distPath, phpfmt.v2.pharName);
12+
const pharContent = await fs.readFile(phpfmt.v2.pharPath);
13+
await fs.writeFile(destPath, pharContent);
14+
} catch (err) {
15+
console.error(err);
16+
process.exit(1);
17+
}

scripts/copy.ts

-16
This file was deleted.

scripts/docs.mts

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import path from 'node:path';
2+
import os from 'node:os';
3+
import fs from 'node:fs/promises';
4+
import phpfmt from 'phpfmt';
5+
import { dirname } from 'dirname-filename-esm';
6+
7+
const __dirname = dirname(import.meta);
8+
9+
const pkgJsonPath = path.join(__dirname, '../package.json');
10+
const readmePath: string = path.join(__dirname, '../README.md');
11+
12+
try {
13+
const pkg = JSON.parse(String(await fs.readFile(pkgJsonPath)));
14+
const configuration = pkg.contributes.configuration;
15+
16+
let config: string =
17+
'| Key | Type | Description | Default |' +
18+
os.EOL +
19+
'| -------- | ----------- | ----------- | ----------- |' +
20+
os.EOL;
21+
22+
for (const configKey of Object.keys(configuration.properties)) {
23+
const configValue = configuration.properties[configKey];
24+
config += `| ${configKey} | `;
25+
26+
if (typeof configValue.type === 'string') {
27+
config += `\`${configValue.type}\``;
28+
} else if (Array.isArray(configValue.type)) {
29+
config += `\`${configValue.type.join(' \\| ')}\``;
30+
}
31+
config += ` | ${configValue.description}`;
32+
33+
if (typeof configValue.default === 'string') {
34+
config += ` | "${configValue.default}"`;
35+
} else if (typeof configValue.default === 'number') {
36+
config += ` | ${configValue.default}`;
37+
} else if (
38+
Array.isArray(configValue.default) ||
39+
typeof configValue.default === 'boolean'
40+
) {
41+
config += ` | ${JSON.stringify(configValue.default)}`;
42+
} else {
43+
throw new Error('uncovered type');
44+
}
45+
46+
config += ' | ' + os.EOL;
47+
}
48+
49+
let readmeContent = String(await fs.readFile(readmePath));
50+
readmeContent = readmeContent.replace(
51+
/<!-- Configuration START -->([\s\S]*)<!-- Configuration END -->/,
52+
() => {
53+
return (
54+
'<!-- Configuration START -->' +
55+
os.EOL +
56+
config +
57+
os.EOL +
58+
'<!-- Configuration END -->'
59+
);
60+
}
61+
);
62+
63+
const { Transformation } = await import('../src/Transformation');
64+
const transformation = new Transformation('php', phpfmt.v2);
65+
66+
const transformations = await transformation.getTransformations();
67+
68+
readmeContent = readmeContent.replace(
69+
/<!-- Transformations START -->([\s\S]*)<!-- Transformations END -->/,
70+
() => {
71+
return (
72+
'<!-- Transformations START -->' +
73+
os.EOL +
74+
'| Key | Description |' +
75+
os.EOL +
76+
'| -------- | ----------- |' +
77+
os.EOL +
78+
transformations
79+
.map(item => {
80+
let row = `| ${item.key} | `;
81+
row += item.description;
82+
row += ' |';
83+
return row;
84+
})
85+
.join(os.EOL) +
86+
os.EOL +
87+
'<!-- Transformations END -->'
88+
);
89+
}
90+
);
91+
const passes = transformation.getPasses();
92+
93+
const enums = passes.map(pass => {
94+
const p = transformations.find(t => t.key === pass);
95+
return {
96+
enum: pass,
97+
description: p?.description ?? 'Core pass'
98+
};
99+
});
100+
101+
pkg.contributes.configuration.properties['phpfmt.passes'].items.enum =
102+
enums.map(o => o.enum);
103+
pkg.contributes.configuration.properties[
104+
'phpfmt.passes'
105+
].items.enumDescriptions = enums.map(o => o.description);
106+
pkg.contributes.configuration.properties['phpfmt.exclude'].items.enum =
107+
enums.map(o => o.enum);
108+
pkg.contributes.configuration.properties[
109+
'phpfmt.exclude'
110+
].items.enumDescriptions = enums.map(o => o.description);
111+
112+
await fs.writeFile(readmePath, readmeContent);
113+
await fs.writeFile(pkgJsonPath, JSON.stringify(pkg, null, 2) + os.EOL);
114+
} catch (err) {
115+
console.error(err);
116+
process.exit(1);
117+
}

scripts/docs.ts

-116
This file was deleted.

0 commit comments

Comments
 (0)