Skip to content

Commit 16ae6bb

Browse files
committed
build(napi/oxlint): simplify build script
1 parent 61c64a6 commit 16ae6bb

File tree

1 file changed

+38
-87
lines changed

1 file changed

+38
-87
lines changed

napi/oxlint/scripts/build.js

Lines changed: 38 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,52 @@
1-
#!/usr/bin/env node
1+
import { execSync } from 'node:child_process';
2+
import { copyFileSync, mkdirSync, readdirSync } from 'node:fs';
3+
import { join } from 'node:path';
24

3-
import { execSync } from 'child_process';
4-
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
5-
import { dirname, join } from 'path';
6-
import { fileURLToPath } from 'url';
7-
8-
const __dirname = dirname(fileURLToPath(import.meta.url));
9-
const rootDir = join(__dirname, '..');
5+
const oxlintDirPath = join(import.meta.dirname, '..'),
6+
distDirPath = join(oxlintDirPath, 'dist'),
7+
parserDirPath = join(oxlintDirPath, '../parser');
108

9+
// Build with tsdown
1110
console.log('Building with tsdown...');
12-
try {
13-
execSync('pnpm tsdown', { stdio: 'inherit', cwd: rootDir });
14-
} catch (error) {
15-
console.error('Build failed:', error);
16-
process.exit(1);
17-
}
18-
19-
// Create directories after tsdown build.
20-
const distDir = join(rootDir, 'dist');
21-
const distGeneratedDir = join(distDir, 'generated');
22-
const parserRawTransferDir = join(distDir, 'parser', 'raw-transfer');
23-
const parserGeneratedDir = join(distDir, 'parser', 'generated', 'lazy');
24-
25-
// Create all necessary directories
26-
if (!existsSync(distGeneratedDir)) {
27-
mkdirSync(distGeneratedDir, { recursive: true });
28-
}
29-
if (!existsSync(parserRawTransferDir)) {
30-
mkdirSync(parserRawTransferDir, { recursive: true });
31-
}
32-
if (!existsSync(parserGeneratedDir)) {
33-
mkdirSync(parserGeneratedDir, { recursive: true });
34-
}
11+
execSync('pnpm tsdown', { stdio: 'inherit', cwd: oxlintDirPath });
3512

36-
console.log('Copying generated files...');
3713
// Copy generated constants file
38-
const constantsPath = join(rootDir, 'src-js/generated/constants.mjs');
39-
if (existsSync(constantsPath)) {
40-
copyFileSync(constantsPath, join(distGeneratedDir, 'constants.mjs'));
41-
console.log('Copied constants.mjs');
42-
} else {
43-
console.error(
44-
'Warning: generated/constants.mjs not found. Make sure to run napi build first.',
45-
);
46-
}
14+
console.log('Copying generated files...');
15+
copyFile(join(oxlintDirPath, 'src-js/generated/constants.mjs'), join(distDirPath, 'generated/constants.mjs'));
16+
17+
// Copy files from `napi/parser` to `napi/oxlint/dist/parser`
18+
console.log('Copying files from parser...');
4719

48-
// Copy parser files
49-
const parserFiles = [
50-
{
51-
src: join(rootDir, '../parser/raw-transfer/lazy-common.mjs'),
52-
dest: join(parserRawTransferDir, 'lazy-common.mjs'),
53-
},
54-
{
55-
src: join(rootDir, '../parser/raw-transfer/node-array.mjs'),
56-
dest: join(parserRawTransferDir, 'node-array.mjs'),
57-
},
58-
{
59-
src: join(rootDir, '../parser/generated/lazy/walk.mjs'),
60-
dest: join(parserGeneratedDir, 'walk.mjs'),
61-
},
62-
{
63-
src: join(rootDir, '../parser/generated/lazy/types.mjs'),
64-
dest: join(parserGeneratedDir, 'types.mjs'),
65-
},
66-
{
67-
src: join(rootDir, '../parser/generated/lazy/constructors.mjs'),
68-
dest: join(parserGeneratedDir, 'constructors.mjs'),
69-
},
20+
const parserFilePaths = [
21+
'raw-transfer/lazy-common.mjs',
22+
'raw-transfer/node-array.mjs',
23+
'generated/lazy/constructors.mjs',
24+
'generated/lazy/types.mjs',
25+
'generated/lazy/walk.mjs',
7026
];
7127

72-
for (const { src, dest } of parserFiles) {
73-
if (existsSync(src)) {
74-
copyFileSync(src, dest);
75-
console.log(`Copied ${src.split('/').pop()}`);
76-
} else {
77-
console.error(`Warning: parser file not found: ${src}`);
78-
}
28+
for (const parserFilePath of parserFilePaths) {
29+
copyFile(join(parserDirPath, parserFilePath), join(distDirPath, 'parser', parserFilePath));
7930
}
8031

81-
// Copy native .node files that might exist in src-js
82-
const nodeFiles = [
83-
'oxlint.darwin-arm64.node',
84-
'oxlint.darwin-x64.node',
85-
'oxlint.linux-x64-gnu.node',
86-
'oxlint.linux-arm64-gnu.node',
87-
'oxlint.linux-x64-musl.node',
88-
'oxlint.linux-arm64-musl.node',
89-
'oxlint.win32-x64-msvc.node',
90-
'oxlint.win32-arm64-msvc.node',
91-
];
32+
// Copy native `.node` files from `src-js`
33+
console.log('Copying `.node` files...');
9234

93-
for (const nodeFile of nodeFiles) {
94-
const srcPath = join(rootDir, 'src-js', nodeFile);
95-
if (existsSync(srcPath)) {
96-
copyFileSync(srcPath, join(distDir, nodeFile));
97-
console.log(`Copied ${nodeFile}`);
98-
}
35+
for (const filename of readdirSync(join(oxlintDirPath, 'src-js'))) {
36+
if (!filename.endsWith('.node')) continue;
37+
copyFile(join(oxlintDirPath, 'src-js', filename), join(distDirPath, filename));
9938
}
10039

10140
console.log('Build complete!');
41+
42+
/**
43+
* Copy a file, creating parent directories if needed.
44+
* @param {string} srcPath - Source file path, absolute
45+
* @param {string} destPath - Destination file path, absolute
46+
* @returns {void}
47+
*/
48+
function copyFile(srcPath, destPath) {
49+
mkdirSync(join(destPath, '..'), { recursive: true });
50+
copyFileSync(srcPath, destPath);
51+
console.log(`- Copied ${srcPath.split('/').pop()}`);
52+
}

0 commit comments

Comments
 (0)