forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepublish.js
executable file
·65 lines (58 loc) · 2.03 KB
/
prepublish.js
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
65
#!/usr/bin/env node
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { chdir } from 'node:process';
import { fileURLToPath } from 'node:url';
import { readJson, runWithEcho } from './helpers.js';
import publishWasmNodePackage from './publish-wasm-node-package.js';
import { CHANGELOG, MAIN_PACKAGE } from './release-constants.js';
import { getCurrentCommitMessage, getFirstChangelogEntry } from './release-helpers.js';
// We execute everything from the main directory
chdir(fileURLToPath(new URL('..', import.meta.url)));
const version = await getCurrentCommitMessage();
const matched = /^\d+\.\d+\.\d+(-\d+)?$/.exec(version);
if (!matched) {
throw new Error(
`The commit message "${version}" does not satisfy the patterns 0.0.0 or 0.0.0-0.`
);
}
const isPreRelease = !!matched[1];
await verifyChangelog(isPreRelease);
await runWithEcho('npm', ['publish'], { cwd: path.resolve('browser') });
await publishWasmNodePackage();
const { optionalDependencies } = await readJson(MAIN_PACKAGE);
await runWithEcho('npm', ['run', 'prepublish:napi']);
const mainPackage = await readJson(MAIN_PACKAGE);
await writeFile(
MAIN_PACKAGE,
JSON.stringify(
{
...mainPackage,
optionalDependencies: { ...optionalDependencies, ...mainPackage.optionalDependencies }
},
null,
2
)
);
/**
* @param {boolean} isPreRelease
* @return {Promise<void>}
*/
async function verifyChangelog(isPreRelease) {
const changelog = await readFile(CHANGELOG, 'utf8');
const { currentVersion, text } = getFirstChangelogEntry(changelog);
if (currentVersion !== version) {
if (isPreRelease) {
console.log(
`There is no changelog entry for version "${version}", the last entry is for version "${currentVersion}". This is OK for a pre-release.`
);
return;
}
throw new Error(
`There is no changelog entry for version "${version}", the last entry is for version "${currentVersion}".`
);
}
if (text.includes('[replace me]')) {
throw new Error(`The changelog entry must not contain placeholders. The text was:\n${text}`);
}
}