Debian Codegraph @

plugins/jieba/node/scripts/prepare-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 const libraryNameByPlatform = {
12 darwin: 'libopencc-jieba.dylib',
13 linux: 'libopencc-jieba.so',
14 win32: 'opencc-jieba.dll',
15 };
16
17 function parsePlatformArch(dirname) {
18 const index = dirname.indexOf('-');
19 if (index === -1) {
20 return null;
21 }
22 return {
23 platform: dirname.slice(0, index),
24 arch: dirname.slice(index + 1),
25 };
26 }
27
28 function copyFile(src, dest) {
29 fs.mkdirSync(path.dirname(dest), { recursive: true });
30 fs.copyFileSync(src, dest);
31 }
32
33 function writeJson(filePath, value) {
34 fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`);
35 }
36
37 function writeReadme(packageDir, packageName, dirname, libraryName) {
38 fs.writeFileSync(path.join(packageDir, 'README.md'), [
39 `# ${packageName}`,
40 '',
41 `Native Jieba segmentation plugin binary for OpenCC on \`${dirname}\`.`,
42 '',
43 'This package is installed automatically as an optional dependency of',
44 '`opencc-jieba`. Most users should install `opencc-jieba` instead of',
45 'installing this package directly.',
46 '',
47 '## Contents',
48 '',
49 '```text',
50 `prebuilds/${dirname}/${libraryName}`,
51 '```',
52 '',
53 '## Usage',
54 '',
55 '```javascript',
56 `const binary = require('${packageName}');`,
57 'console.log(binary.pluginLibrary);',
58 '```',
59 '',
60 '## License',
61 '',
62 `${parentPackage.license}`,
63 '',
64 ].join('\n'));
65 }
66
67 function preparePackage(dirname) {
68 const parsed = parsePlatformArch(dirname);
69 if (!parsed) {
70 console.warn(`Skipping unexpected prebuild directory: ${dirname}`);
71 return null;
72 }
73
74 const { platform, arch } = parsed;
75 const libraryName = libraryNameByPlatform[platform];
76 if (!libraryName) {
77 console.warn(`Skipping unsupported platform: ${dirname}`);
78 return null;
79 }
80
81 const sourceLibrary = path.join(sourcePrebuildsDir, dirname, libraryName);
82 if (!fs.existsSync(sourceLibrary)) {
83 console.warn(`Skipping ${dirname}; missing ${libraryName}`);
84 return null;
85 }
86
87 const packageName = `@opencc/opencc-jieba-${dirname}`;
88 const packageDir = path.join(outputRoot, packageName);
89 const relativeLibraryPath = path.posix.join('prebuilds', dirname, libraryName);
90
91 fs.rmSync(packageDir, { recursive: true, force: true });
92 fs.mkdirSync(packageDir, { recursive: true });
93
94 writeJson(path.join(packageDir, 'package.json'), {
95 name: packageName,
96 version: parentPackage.version,
97 description: `${parentPackage.description} (${dirname} binary)`,
98 license: parentPackage.license,
99 author: parentPackage.author,
100 repository: {
101 type: 'git',
102 url: 'git+https://github.com/BYVoid/OpenCC.git',
103 directory: 'plugins/jieba/node',
104 },
105 main: 'index.js',
106 os: [platform],
107 cpu: [arch],
108 files: [
109 'index.js',
110 'README.md',
111 relativeLibraryPath,
112 ],
113 });
114
115 fs.writeFileSync(path.join(packageDir, 'index.js'), [
116 "const path = require('path');",
117 '',
118 `const pluginLibrary = path.join(__dirname, ${JSON.stringify(relativeLibraryPath)});`,
119 '',
120 'module.exports = {',
121 ' pluginDir: path.dirname(pluginLibrary),',
122 ' pluginLibrary,',
123 '};',
124 '',
125 ].join('\n'));
126
127 writeReadme(packageDir, packageName, dirname, libraryName);
128 copyFile(sourceLibrary, path.join(packageDir, relativeLibraryPath));
129 return packageDir;
130 }
131
132 function main() {
133 if (!fs.existsSync(sourcePrebuildsDir)) {
134 console.error(`Missing prebuilds directory: ${sourcePrebuildsDir}`);
135 process.exit(1);
136 }
137
138 fs.rmSync(outputRoot, { recursive: true, force: true });
139 fs.mkdirSync(outputRoot, { recursive: true });
140
141 const prepared = fs.readdirSync(sourcePrebuildsDir, { withFileTypes: true })
142 .filter(entry => entry.isDirectory())
143 .map(entry => preparePackage(entry.name))
144 .filter(Boolean);
145
146 if (prepared.length === 0) {
147 console.error('No scoped binary packages were prepared.');
148 process.exit(1);
149 }
150
151 console.log('Prepared scoped binary packages:');
152 for (const packageDir of prepared) {
153 console.log(` ${path.relative(packageRoot, packageDir)}`);
154 }
155 }
156
157 if (require.main === module) {
158 main();
159 }