Skip to content

Commit

Permalink
refactor: switch scripts to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Oct 6, 2024
1 parent e14e300 commit 00eec09
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 297 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: pnpm build

- name: Run release script
run: npx tsx ./scripts/release.ts
run: npx tsx ./scripts/release.mts
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
VSCE_TOKEN: ${{ secrets.VSCE_TOKEN }}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "Integrates phpfmt into VS Code",
"main": "./dist/extension",
"scripts": {
"build": "nr clean && tsc && tsup && tsx scripts/copy.ts",
"build:docs": "tsx scripts/docs.ts",
"build": "nr clean && tsc && tsup && tsx scripts/copy.mts",
"build:docs": "tsx scripts/docs.mts",
"prebuild": "nr build:docs",
"watch": "tsup --watch",
"clean": "rimraf out",
Expand Down Expand Up @@ -670,6 +670,8 @@
"@vscode/vsce": "^2.31.1",
"adm-zip": "^0.5.10",
"consola": "^3.2.3",
"debug": "^4.3.4",
"dirname-filename-esm": "^1.1.2",
"eslint": "^8.57.0",
"eslint-config-love": "^71.0.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
21 changes: 16 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions scripts/copy.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import phpfmt from 'phpfmt';
import { dirname } from 'dirname-filename-esm';

const __dirname = dirname(import.meta);

try {
const pkgPath = path.join(__dirname, '..');
const distPath = path.join(pkgPath, 'dist');
const destPath = path.join(distPath, phpfmt.v2.pharName);
const pharContent = await fs.readFile(phpfmt.v2.pharPath);
await fs.writeFile(destPath, pharContent);
} catch (err) {
console.error(err);
process.exit(1);
}
16 changes: 0 additions & 16 deletions scripts/copy.ts

This file was deleted.

117 changes: 117 additions & 0 deletions scripts/docs.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs/promises';
import phpfmt from 'phpfmt';
import { dirname } from 'dirname-filename-esm';

const __dirname = dirname(import.meta);

const pkgJsonPath = path.join(__dirname, '../package.json');
const readmePath: string = path.join(__dirname, '../README.md');

try {
const pkg = JSON.parse(String(await fs.readFile(pkgJsonPath)));
const configuration = pkg.contributes.configuration;

let config: string =
'| Key | Type | Description | Default |' +
os.EOL +
'| -------- | ----------- | ----------- | ----------- |' +
os.EOL;

for (const configKey of Object.keys(configuration.properties)) {
const configValue = configuration.properties[configKey];
config += `| ${configKey} | `;

if (typeof configValue.type === 'string') {
config += `\`${configValue.type}\``;
} else if (Array.isArray(configValue.type)) {
config += `\`${configValue.type.join(' \\| ')}\``;
}
config += ` | ${configValue.description}`;

if (typeof configValue.default === 'string') {
config += ` | "${configValue.default}"`;
} else if (typeof configValue.default === 'number') {
config += ` | ${configValue.default}`;
} else if (
Array.isArray(configValue.default) ||
typeof configValue.default === 'boolean'
) {
config += ` | ${JSON.stringify(configValue.default)}`;
} else {
throw new Error('uncovered type');
}

config += ' | ' + os.EOL;
}

let readmeContent = String(await fs.readFile(readmePath));
readmeContent = readmeContent.replace(
/<!-- Configuration START -->([\s\S]*)<!-- Configuration END -->/,
() => {
return (
'<!-- Configuration START -->' +
os.EOL +
config +
os.EOL +
'<!-- Configuration END -->'
);
}
);

const { Transformation } = await import('../src/Transformation');
const transformation = new Transformation('php', phpfmt.v2);

const transformations = await transformation.getTransformations();

readmeContent = readmeContent.replace(
/<!-- Transformations START -->([\s\S]*)<!-- Transformations END -->/,
() => {
return (
'<!-- Transformations START -->' +
os.EOL +
'| Key | Description |' +
os.EOL +
'| -------- | ----------- |' +
os.EOL +
transformations
.map(item => {
let row = `| ${item.key} | `;
row += item.description;
row += ' |';
return row;
})
.join(os.EOL) +
os.EOL +
'<!-- Transformations END -->'
);
}
);
const passes = transformation.getPasses();

const enums = passes.map(pass => {
const p = transformations.find(t => t.key === pass);
return {
enum: pass,
description: p?.description ?? 'Core pass'
};
});

pkg.contributes.configuration.properties['phpfmt.passes'].items.enum =
enums.map(o => o.enum);
pkg.contributes.configuration.properties[
'phpfmt.passes'
].items.enumDescriptions = enums.map(o => o.description);
pkg.contributes.configuration.properties['phpfmt.exclude'].items.enum =
enums.map(o => o.enum);
pkg.contributes.configuration.properties[
'phpfmt.exclude'
].items.enumDescriptions = enums.map(o => o.description);

await fs.writeFile(readmePath, readmeContent);
await fs.writeFile(pkgJsonPath, JSON.stringify(pkg, null, 2) + os.EOL);
} catch (err) {
console.error(err);
process.exit(1);
}
116 changes: 0 additions & 116 deletions scripts/docs.ts

This file was deleted.

Loading

0 comments on commit 00eec09

Please sign in to comment.