-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathverify-package.mjs
More file actions
64 lines (57 loc) · 2.2 KB
/
Copy pathverify-package.mjs
File metadata and controls
64 lines (57 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { access, readFile } from 'node:fs/promises';
import { execFileSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
const root = resolve(import.meta.dirname, '..');
const output = resolve(root, 'dist/plotly');
const requiredFiles = [
'README.md',
'LICENSE',
'CHANGELOG.md',
'angular-plotly.png',
'package.json',
];
await Promise.all(requiredFiles.map(file => access(resolve(output, file))));
const rootPackage = JSON.parse(await readFile(resolve(root, 'package.json'), 'utf8'));
const libraryPackage = JSON.parse(await readFile(resolve(output, 'package.json'), 'utf8'));
const declarationsPath = libraryPackage.typings ?? 'index.d.ts';
const bundlePath = libraryPackage.module;
requiredFiles.push(declarationsPath, bundlePath);
await Promise.all([declarationsPath, bundlePath].map(file => access(resolve(output, file))));
if (rootPackage.version !== libraryPackage.version) {
throw new Error(`Package versions differ: root=${rootPackage.version}, library=${libraryPackage.version}`);
}
const major = Number(rootPackage.version.split('.')[0]);
for (const dependency of ['@angular/common', '@angular/core']) {
const expected = `>=${major}.0.0 <${major + 1}.0.0`;
if (libraryPackage.peerDependencies?.[dependency] !== expected) {
throw new Error(`${dependency} must use peer range ${expected}`);
}
}
const declarations = await readFile(resolve(output, declarationsPath), 'utf8');
for (const symbol of [
'PlotlyComponent',
'PlotlyModule',
'PlotlyService',
'PlotlyViaCDNModule',
'PlotlyViaWindowModule',
]) {
if (!declarations.includes(symbol)) {
throw new Error(`Public declaration is missing ${symbol}`);
}
}
const packResult = JSON.parse(execFileSync('npm', ['pack', '--dry-run', '--json', output], {
cwd: root,
encoding: 'utf8',
env: {
...process.env,
npm_config_cache: join(tmpdir(), 'angular-plotly-npm-cache'),
},
}));
const packedFiles = new Set(packResult[0].files.map(file => file.path));
for (const file of requiredFiles) {
if (!packedFiles.has(file)) {
throw new Error(`npm package is missing ${file}`);
}
}
console.log(`Verified angular-plotly.js ${rootPackage.version} (${packedFiles.size} packaged files).`);