1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Errors = require("@oclif/errors");
|
4 | const findYarnWorkspaceRoot = require("find-yarn-workspace-root");
|
5 | const path = require("path");
|
6 | const qq = require("qqjs");
|
7 | const log_1 = require("../log");
|
8 | const bin_1 = require("./bin");
|
9 | const node_1 = require("./node");
|
10 | const pack = async (from, to) => {
|
11 | const prevCwd = qq.cwd();
|
12 | qq.cd(path.dirname(from));
|
13 | await qq.mkdirp(path.dirname(to));
|
14 | log_1.log(`packing tarball from ${qq.prettifyPaths(from)} to ${qq.prettifyPaths(to)}`);
|
15 | await (to.endsWith('gz') ?
|
16 | qq.x('tar', ['czf', to, path.basename(from)]) :
|
17 | qq.x(`tar c ${path.basename(from)} | xz > ${to}`));
|
18 | qq.cd(prevCwd);
|
19 | };
|
20 | async function build(c, options = {}) {
|
21 | const { xz, config } = c;
|
22 | const prevCwd = qq.cwd();
|
23 | const packCLI = async () => {
|
24 | const stdout = await qq.x.stdout('npm', ['pack', '--unsafe-perm'], { cwd: c.root });
|
25 | return path.join(c.root, stdout.split('\n').pop());
|
26 | };
|
27 | const extractCLI = async (tarball) => {
|
28 | await qq.emptyDir(c.workspace());
|
29 | await qq.mv(tarball, c.workspace());
|
30 | tarball = path.basename(tarball);
|
31 | tarball = qq.join([c.workspace(), tarball]);
|
32 | qq.cd(c.workspace());
|
33 | await qq.x(`tar -xzf ${tarball}`);
|
34 |
|
35 | for (const f of await qq.ls('package', { fullpath: true }))
|
36 | await qq.mv(f, '.');
|
37 | await qq.rm('package', tarball, 'bin/run.cmd');
|
38 | };
|
39 | const updatePJSON = async () => {
|
40 | qq.cd(c.workspace());
|
41 | const pjson = await qq.readJSON('package.json');
|
42 | pjson.version = c.version;
|
43 | pjson.oclif.update = pjson.oclif.update || {};
|
44 | pjson.oclif.update.s3 = pjson.oclif.update.s3 || {};
|
45 | pjson.oclif.update.s3.bucket = c.s3Config.bucket;
|
46 | await qq.writeJSON('package.json', pjson);
|
47 | };
|
48 | const addDependencies = async () => {
|
49 | qq.cd(c.workspace());
|
50 | const yarnRoot = findYarnWorkspaceRoot(c.root) || c.root;
|
51 | const yarn = await qq.exists([yarnRoot, 'yarn.lock']);
|
52 | if (yarn) {
|
53 | await qq.cp([yarnRoot, 'yarn.lock'], '.');
|
54 | await qq.x('yarn --no-progress --production --non-interactive');
|
55 | }
|
56 | else {
|
57 | let lockpath = qq.join(c.root, 'package-lock.json');
|
58 | if (!await qq.exists(lockpath)) {
|
59 | lockpath = qq.join(c.root, 'npm-shrinkwrap.json');
|
60 | }
|
61 | await qq.cp(lockpath, '.');
|
62 | await qq.x('npm install --production');
|
63 | }
|
64 | };
|
65 | const buildTarget = async (target) => {
|
66 | const workspace = c.workspace(target);
|
67 | const key = config.s3Key('versioned', '.tar.gz', target);
|
68 | const base = path.basename(key);
|
69 | log_1.log(`building target ${base}`);
|
70 | await qq.rm(workspace);
|
71 | await qq.cp(c.workspace(), workspace);
|
72 | await node_1.fetchNodeBinary({
|
73 | nodeVersion: c.nodeVersion,
|
74 | output: path.join(workspace, 'bin', 'node'),
|
75 | platform: target.platform,
|
76 | arch: target.arch,
|
77 | tmp: qq.join(config.root, 'tmp'),
|
78 | });
|
79 | if (options.pack === false)
|
80 | return;
|
81 | await pack(workspace, c.dist(key));
|
82 | if (xz)
|
83 | await pack(workspace, c.dist(config.s3Key('versioned', '.tar.xz', target)));
|
84 | if (!c.updateConfig.s3.host)
|
85 | return;
|
86 | const rollout = (typeof c.updateConfig.autoupdate === 'object' && c.updateConfig.autoupdate.rollout);
|
87 | const manifest = {
|
88 | rollout: rollout === false ? undefined : rollout,
|
89 | version: c.version,
|
90 | channel: c.channel,
|
91 | baseDir: config.s3Key('baseDir', target),
|
92 | gz: config.s3Url(config.s3Key('versioned', '.tar.gz', target)),
|
93 | xz: xz ? config.s3Url(config.s3Key('versioned', '.tar.xz', target)) : undefined,
|
94 | sha256gz: await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.gz', target))),
|
95 | sha256xz: xz ? await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.xz', target))) : undefined,
|
96 | node: {
|
97 | compatible: config.pjson.engines.node,
|
98 | recommended: c.nodeVersion,
|
99 | },
|
100 | };
|
101 | await qq.writeJSON(c.dist(config.s3Key('manifest', target)), manifest);
|
102 | };
|
103 | const buildBaseTarball = async () => {
|
104 | if (options.pack === false)
|
105 | return;
|
106 | await pack(c.workspace(), c.dist(config.s3Key('versioned', '.tar.gz')));
|
107 | if (xz)
|
108 | await pack(c.workspace(), c.dist(config.s3Key('versioned', '.tar.xz')));
|
109 | if (!c.updateConfig.s3.host) {
|
110 | Errors.warn('No S3 bucket or host configured. CLI will not be able to update.');
|
111 | return;
|
112 | }
|
113 | const manifest = {
|
114 | version: c.version,
|
115 | baseDir: config.s3Key('baseDir'),
|
116 | channel: config.channel,
|
117 | gz: config.s3Url(config.s3Key('versioned', '.tar.gz')),
|
118 | xz: config.s3Url(config.s3Key('versioned', '.tar.xz')),
|
119 | sha256gz: await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.gz'))),
|
120 | sha256xz: xz ? await qq.hash('sha256', c.dist(config.s3Key('versioned', '.tar.xz'))) : undefined,
|
121 | rollout: (typeof c.updateConfig.autoupdate === 'object' && c.updateConfig.autoupdate.rollout),
|
122 | node: {
|
123 | compatible: config.pjson.engines.node,
|
124 | recommended: c.nodeVersion,
|
125 | },
|
126 | };
|
127 | await qq.writeJSON(c.dist(config.s3Key('manifest')), manifest);
|
128 | };
|
129 | log_1.log(`gathering workspace for ${config.bin} to ${c.workspace()}`);
|
130 | await extractCLI(await packCLI());
|
131 | await updatePJSON();
|
132 | await addDependencies();
|
133 | await bin_1.writeBinScripts({ config, baseWorkspace: c.workspace(), nodeVersion: c.nodeVersion });
|
134 | await buildBaseTarball();
|
135 | for (const target of c.targets) {
|
136 | if (!options.platform || options.platform === target.platform) {
|
137 |
|
138 | await buildTarget(target);
|
139 | }
|
140 | }
|
141 | qq.cd(prevCwd);
|
142 | }
|
143 | exports.build = build;
|