1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const command_1 = require("@oclif/command");
|
4 | const _ = require("lodash");
|
5 | const qq = require("qqjs");
|
6 | const Tarballs = require("../../tarballs");
|
7 | function debArch(arch) {
|
8 | if (arch === 'x64')
|
9 | return 'amd64';
|
10 | if (arch === 'x86')
|
11 | return 'i386';
|
12 | if (arch === 'arm')
|
13 | return 'armel';
|
14 | throw new Error(`invalid arch: ${arch}`);
|
15 | }
|
16 | const debVersion = (buildConfig) => `${buildConfig.version.split('-')[0]}-1`;
|
17 | class PackDeb extends command_1.Command {
|
18 | async run() {
|
19 | if (process.platform !== 'linux')
|
20 | throw new Error('must be run from linux');
|
21 | const { flags } = this.parse(PackDeb);
|
22 | const buildConfig = await Tarballs.buildConfig(flags.root);
|
23 | const { config } = buildConfig;
|
24 | await Tarballs.build(buildConfig, { platform: 'linux', pack: false });
|
25 | const dist = buildConfig.dist('deb');
|
26 | await qq.emptyDir(dist);
|
27 | const build = async (arch) => {
|
28 | const target = { platform: 'linux', arch };
|
29 | const versionedDebBase = `${config.bin}_${debVersion(buildConfig)}_${debArch(arch)}`;
|
30 | const workspace = qq.join(buildConfig.tmp, 'apt', `${versionedDebBase}.apt`);
|
31 | await qq.rm(workspace);
|
32 | await qq.mkdirp([workspace, 'DEBIAN']);
|
33 | await qq.mkdirp([workspace, 'usr/bin']);
|
34 | await qq.mkdirp([workspace, 'usr/lib']);
|
35 | await qq.mv(buildConfig.workspace(target), [workspace, 'usr/lib', config.dirname]);
|
36 | await qq.write([workspace, 'usr/lib', config.dirname, 'bin', config.bin], scripts.bin(config));
|
37 | await qq.write([workspace, 'DEBIAN/control'], scripts.control(buildConfig, debArch(arch)));
|
38 | await qq.chmod([workspace, 'usr/lib', config.dirname, 'bin', config.bin], 0o755);
|
39 | await qq.x(`ln -s "../lib/${config.dirname}/bin/${config.bin}" "${workspace}/usr/bin/${config.bin}"`);
|
40 | await qq.x(`chown -R root "${workspace}"`);
|
41 | await qq.x(`chgrp -R root "${workspace}"`);
|
42 | await qq.x(`dpkg --build "${workspace}" "${qq.join(dist, `${versionedDebBase}.deb`)}"`);
|
43 | };
|
44 | const arches = _.uniq(buildConfig.targets
|
45 | .filter(t => t.platform === 'linux')
|
46 | .map(t => t.arch));
|
47 | for (let a of arches)
|
48 | await build(a);
|
49 | await qq.x('apt-ftparchive packages . > Packages', { cwd: dist });
|
50 | await qq.x('gzip -c Packages > Packages.gz', { cwd: dist });
|
51 | await qq.x('bzip2 -k Packages', { cwd: dist });
|
52 | await qq.x('xz -k Packages', { cwd: dist });
|
53 | const ftparchive = qq.join(buildConfig.tmp, 'apt', 'apt-ftparchive.conf');
|
54 | await qq.write(ftparchive, scripts.ftparchive(config));
|
55 | await qq.x(`apt-ftparchive -c "${ftparchive}" release . > Release`, { cwd: dist });
|
56 | const gpgKey = config.scopedEnvVar('DEB_KEY');
|
57 | if (gpgKey) {
|
58 | await qq.x(`gpg --digest-algo SHA512 --clearsign -u ${gpgKey} -o InRelease Release`, { cwd: dist });
|
59 | await qq.x(`gpg --digest-algo SHA512 -abs -u ${gpgKey} -o Release.gpg Release`, { cwd: dist });
|
60 | }
|
61 | }
|
62 | }
|
63 | PackDeb.description = 'pack CLI into debian package';
|
64 | PackDeb.flags = {
|
65 | root: command_1.flags.string({ char: 'r', description: 'path to oclif CLI root', default: '.', required: true }),
|
66 | };
|
67 | exports.default = PackDeb;
|
68 | const scripts = {
|
69 | bin: (config) => `#!/usr/bin/env bash
|
70 | set -e
|
71 | echoerr() { echo "$@" 1>&2; }
|
72 | get_script_dir () {
|
73 | SOURCE="\${BASH_SOURCE[0]}"
|
74 | # While \$SOURCE is a symlink, resolve it
|
75 | while [ -h "\$SOURCE" ]; do
|
76 | DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )"
|
77 | SOURCE="\$( readlink "\$SOURCE" )"
|
78 | # If \$SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory
|
79 | [[ \$SOURCE != /* ]] && SOURCE="\$DIR/\$SOURCE"
|
80 | done
|
81 | DIR="\$( cd -P "\$( dirname "\$SOURCE" )" && pwd )"
|
82 | echo "\$DIR"
|
83 | }
|
84 | DIR=\$(get_script_dir)
|
85 | export ${config.scopedEnvVarKey('UPDATE_INSTRUCTIONS')}="update with \\"sudo apt update && sudo apt install ${config.bin}\\""
|
86 | \$DIR/node \$DIR/run "\$@"
|
87 | `,
|
88 | control: (config, arch) => `Package: ${config.config.bin}
|
89 | Version: ${debVersion(config)}
|
90 | Section: main
|
91 | Priority: standard
|
92 | Architecture: ${arch}
|
93 | Maintainer: ${config.config.scopedEnvVar('AUTHOR') || config.config.pjson.author}
|
94 | Description: ${config.config.pjson.description}
|
95 | `,
|
96 | ftparchive: (config) => `
|
97 | APT::FTPArchive::Release {
|
98 | Origin "${config.scopedEnvVar('AUTHOR') || config.pjson.author}";
|
99 | Suite "stable";
|
100 | `
|
101 | };
|