Debian Codegraph @

scripts/verify-node-main-package.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 packageJson = require(path.join(packageRoot, 'package.json'));
8
9 const requiredFiles = [
10 'binding.gyp',
11 'node/cli.js',
12 'node/opencc.js',
13 'node/opencc.mjs',
14 'node/opencc.d.ts',
15 'node/opencc.d.cts',
16 'node/opencc.d.mts',
17 'prebuilds/assets/s2t.json',
18 'prebuilds/assets/s2twp.json',
19 'prebuilds/assets/CJK_Compatibility_Ideographs.ocd2',
20 'prebuilds/assets/STCharacters.ocd2',
21 'prebuilds/assets/STPhrases.ocd2',
22 'prebuilds/assets/STPhrases_GeneratedFromRegionalPhrases.ocd2',
23 'prebuilds/assets/TWVariantsPhrases.ocd2',
24 ];
25
26 const expectedOptionalPackages = [
27 '@opencc/opencc-darwin-arm64',
28 '@opencc/opencc-darwin-x64',
29 '@opencc/opencc-linux-arm64',
30 '@opencc/opencc-linux-x64',
31 '@opencc/opencc-win32-x64',
32 ];
33
34 function fail(message) {
35 console.error(message);
36 process.exitCode = 1;
37 }
38
39 // The npm tarball must contain every file the node-gyp source build compiles
40 // or includes, so that installs without a prebuilt binary (issue #1409) can
41 // fall back to building from source. Walk the gyp source list and its
42 // #include closure and check each file against the "files" whitelist.
43 function parseGypSourcesAndIncludeDirs() {
44 const gypiPath = path.join(packageRoot, 'node', 'node_opencc.gypi');
45 const gypi = fs.readFileSync(gypiPath, 'utf8');
46 const sources = [];
47 const includeDirs = [];
48 for (const match of gypi.matchAll(/"\.\.(\/[^"]*)?"/g)) {
49 const relativePath = match[1] ? match[1].slice(1) : '.';
50 if (/\.(cpp|cc)$/.test(relativePath)) {
51 sources.push(relativePath);
52 } else if (
53 fs.existsSync(path.join(packageRoot, relativePath)) &&
54 fs.statSync(path.join(packageRoot, relativePath)).isDirectory()
55 ) {
56 includeDirs.push(relativePath);
57 }
58 }
59 return { sources, includeDirs };
60 }
61
62 function collectSourceBuildClosure() {
63 const { sources, includeDirs } = parseGypSourcesAndIncludeDirs();
64 const closure = new Set();
65 const queue = [...sources];
66 while (queue.length > 0) {
67 const relativePath = queue.pop();
68 if (closure.has(relativePath)) {
69 continue;
70 }
71 const filePath = path.join(packageRoot, relativePath);
72 if (!fs.existsSync(filePath)) {
73 fail(`Source build references a file missing from the repository: ${relativePath}`);
74 continue;
75 }
76 closure.add(relativePath);
77 const content = fs.readFileSync(filePath, 'utf8');
78 for (const match of content.matchAll(/^\s*#\s*include\s+"([^"]+)"/gm)) {
79 const include = match[1];
80 const candidates = [
81 path.posix.join(path.posix.dirname(relativePath), include),
82 ...includeDirs.map((dir) => path.posix.normalize(path.posix.join(dir, include))),
83 ];
84 for (const candidate of candidates) {
85 if (fs.existsSync(path.join(packageRoot, candidate))) {
86 queue.push(candidate);
87 break;
88 }
89 }
90 }
91 }
92 return closure;
93 }
94
95 function isPackedByFilesWhitelist(relativePath, filesList) {
96 for (const pattern of filesList) {
97 if (pattern === relativePath) {
98 return true;
99 }
100 if (!pattern.includes('*') && relativePath.startsWith(`${pattern.replace(/\/$/, '')}/`)) {
101 return true;
102 }
103 if (pattern.includes('*')) {
104 const regex = new RegExp(
105 `^${pattern.split('*').map((part) =>
106 part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
107 ).join('[^/]*')}$`
108 );
109 if (regex.test(relativePath)) {
110 return true;
111 }
112 }
113 }
114 return false;
115 }
116
117 function verifySourceBuildFilesPacked() {
118 const filesList = packageJson.files || [];
119 const missing = [...collectSourceBuildClosure()]
120 .filter((relativePath) => !isPackedByFilesWhitelist(relativePath, filesList))
121 .sort();
122 for (const relativePath of missing) {
123 fail(
124 `Source build requires ${relativePath} but it is not covered by ` +
125 'the "files" whitelist in package.json'
126 );
127 }
128 }
129
130 for (const relativePath of requiredFiles) {
131 const filePath = path.join(packageRoot, relativePath);
132 if (!fs.existsSync(filePath)) {
133 fail(`Missing required package file: ${relativePath}`);
134 }
135 }
136
137 for (const packageName of expectedOptionalPackages) {
138 const version = packageJson.optionalDependencies &&
139 packageJson.optionalDependencies[packageName];
140 if (!version) {
141 fail(`Missing optional dependency ${packageName}`);
142 }
143 }
144
145 verifySourceBuildFilesPacked();
146
147 if (process.exitCode) {
148 console.error(
149 'opencc package is not ready. Run npm run prebuild before publishing.'
150 );
151 } else {
152 console.log(`opencc@${packageJson.version} package files are ready.`);
153 }