Debian Codegraph @

scripts/prepare-node-scoped-packages.js

raw symbols SCIP
1 #!/usr/bin/env node
2
3 const fs = require('fs');
4 const path = require('path');
5
6 const packageRoot = path.resolve(__dirname, '..');
7 const sourcePrebuildsDir = path.join(packageRoot, 'prebuilds');
8 const outputRoot = path.join(packageRoot, 'dist', 'scoped-packages');
9 const parentPackage = require(path.join(packageRoot, 'package.json'));
10
11 function parsePlatformArch(dirname) {
12 const index = dirname.indexOf('-');
13 if (index === -1) {
14 return null;
15 }
16 return {
17 platform: dirname.slice(0, index),
18 arch: dirname.slice(index + 1),
19 };
20 }
21
22 function copyFile(src, dest) {
23 fs.mkdirSync(path.dirname(dest), { recursive: true });
24 fs.copyFileSync(src, dest);
25 }
26
27 function writeJson(filePath, value) {
28 fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`);
29 }
30
31 function writeReadme(packageDir, packageName, dirname) {
32 fs.writeFileSync(path.join(packageDir, 'README.md'), [
33 `# ${packageName}`,
34 '',
35 `Native OpenCC Node.js binary for \`${dirname}\`.`,
36 '',
37 'This package is installed automatically as an optional dependency of',
38 '`opencc`. Most users should install `opencc` instead of installing this',
39 'package directly.',
40 '',
41 '## Contents',
42 '',
43 '```text',
44 `prebuilds/${dirname}/opencc.node`,
45 '```',
46 '',
47 '## Usage',
48 '',
49 '```javascript',
50 `const binary = require('${packageName}');`,
51 'console.log(binary.binaryPath);',
52 '```',
53 '',
54 '## License',
55 '',
56 `${parentPackage.license}`,
57 '',
58 ].join('\n'));
59 }
60
61 function preparePackage(dirname) {
62 const parsed = parsePlatformArch(dirname);
63 if (!parsed) {
64 console.warn(`Skipping unexpected prebuild directory: ${dirname}`);
65 return null;
66 }
67
68 const { platform, arch } = parsed;
69 const sourceBinary = path.join(sourcePrebuildsDir, dirname, 'opencc.node');
70 if (!fs.existsSync(sourceBinary)) {
71 console.warn(`Skipping ${dirname}; missing opencc.node`);
72 return null;
73 }
74
75 const packageName = `@opencc/opencc-${dirname}`;
76 const packageDir = path.join(outputRoot, packageName);
77 const relativeBinaryPath = path.posix.join('prebuilds', dirname, 'opencc.node');
78
79 fs.rmSync(packageDir, { recursive: true, force: true });
80 fs.mkdirSync(packageDir, { recursive: true });
81
82 writeJson(path.join(packageDir, 'package.json'), {
83 name: packageName,
84 version: parentPackage.version,
85 description: `${parentPackage.description} (${dirname} binary)`,
86 license: parentPackage.license,
87 author: parentPackage.author,
88 repository: parentPackage.repository,
89 main: 'index.js',
90 os: [platform],
91 cpu: [arch],
92 files: [
93 'index.js',
94 'README.md',
95 relativeBinaryPath,
96 ],
97 });
98
99 fs.writeFileSync(path.join(packageDir, 'index.js'), [
100 "const path = require('path');",
101 '',
102 `const binaryPath = path.join(__dirname, ${JSON.stringify(relativeBinaryPath)});`,
103 '',
104 'module.exports = {',
105 ' binaryPath,',
106 '};',
107 '',
108 ].join('\n'));
109
110 writeReadme(packageDir, packageName, dirname);
111 copyFile(sourceBinary, path.join(packageDir, relativeBinaryPath));
112 return packageDir;
113 }
114
115 function main() {
116 if (!fs.existsSync(sourcePrebuildsDir)) {
117 console.error(`Missing prebuilds directory: ${sourcePrebuildsDir}`);
118 process.exit(1);
119 }
120
121 fs.rmSync(outputRoot, { recursive: true, force: true });
122 fs.mkdirSync(outputRoot, { recursive: true });
123
124 const prepared = fs.readdirSync(sourcePrebuildsDir, { withFileTypes: true })
125 .filter(entry => entry.isDirectory() && entry.name !== 'assets')
126 .map(entry => preparePackage(entry.name))
127 .filter(Boolean);
128
129 if (prepared.length === 0) {
130 console.error('No scoped OpenCC binary packages were prepared.');
131 process.exit(1);
132 }
133
134 console.log('Prepared scoped OpenCC binary packages:');
135 for (const packageDir of prepared) {
136 console.log(` ${path.relative(packageRoot, packageDir)}`);
137 }
138 }
139
140 if (require.main === module) {
141 main();
142 }