Debian Codegraph @

npm/prepare-release.mjs

raw
1 #!/usr/bin/env node
2 import * as fs from 'node:fs/promises';
3 import * as path from 'node:path';
4
5 import { execSync } from 'node:child_process';
6
7 // Mapping from to goarch to Node's `process.arch`
8 var ARCH_MAPPING = {
9 '386': 'ia32',
10 'amd64': 'x64',
11 'arm64': 'arm64',
12 };
13
14 // Mapping between goos and Node's `process.platform`
15 var PLATFORM_MAPPING = {
16 'darwin': 'darwin',
17 'linux': 'linux',
18 'windows': 'win32'
19 };
20
21 const NPM = 'npm';
22 const NPM_DIST = path.join(NPM, 'dist');
23 const RELEASES = path.join('dist', 'artifacts.json');
24
25 // read the goreleaser JSON and filter down to just the binaries
26 const json = JSON.parse(await fs.readFile(RELEASES));
27 const binaries = json.filter(r => r.type === 'Binary');
28
29 // clean up any previous runs
30 const output = execSync(`git clean -fdX ${NPM}`);
31 console.log(output.toString())
32
33 // make the dist folder
34 await fs.mkdir(NPM_DIST, { recursive: true })
35
36 // copy each binary into the place the NPM distribution expects it to be
37 await binaries.forEach(async (release) => {
38 const os = PLATFORM_MAPPING[release.goos];
39 const arch = ARCH_MAPPING[release.goarch];
40
41 // use NodeJS constants for the filename, e.g. win32-x64.exe
42 const distfile = `${os}-${arch}${release.extra.Ext}`;
43
44 // copy files even if we don't use them, `package.json` uses a filtered list
45 const target = path.join(NPM_DIST, distfile);
46 await fs.copyFile(release.path, target);
47 console.log('copied ', release.path, 'to', target);
48 });