@package-pal/cli 0.0.20 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ppal +15 -47
- package/package.json +4 -4
- package/src/lib/install/functions/exec.js +11 -0
- package/src/lib/install/functions/get-path-info.js +56 -56
- package/src/lib/install/functions/get-platform-info.js +49 -49
- package/src/lib/install/functions/launch-fallback.js +53 -0
- package/src/lib/install/functions/link-existing-binary.js +62 -62
- package/src/lib/install/functions/load-missing-binary.js +107 -107
- package/src/lib/install/functions/prepare-binary.js +33 -33
- package/src/lib/install/functions/validate-binary-version.js +14 -3
- package/src/lib/install/install-binary.js +33 -27
package/bin/ppal
CHANGED
|
@@ -1,47 +1,15 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// Fallback, if post-install script did not execute.
|
|
4
|
-
// Adds more overhead due to launching a Node process which launches the CLI process.
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const exec = (binPath) => {
|
|
17
|
-
require('child_process').execFileSync(binPath, process.argv.slice(2), {
|
|
18
|
-
stdio: 'inherit',
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async function main() {
|
|
23
|
-
const {
|
|
24
|
-
platform, targetPackage
|
|
25
|
-
} = getPlatformInfo();
|
|
26
|
-
const { targetBinPath, binExecutableName, outputBinDir } = getPathInfo({ platform, targetPackage });
|
|
27
|
-
const binarySourceDir = join(outputBinDir, 'source');
|
|
28
|
-
|
|
29
|
-
if (targetBinPath) {
|
|
30
|
-
rmSync(binarySourceDir, { force: true, recursive: true });
|
|
31
|
-
return exec(targetBinPath);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const prevDownloadedBinPath = join(binarySourceDir, binExecutableName);
|
|
35
|
-
if (!existsSync(prevDownloadedBinPath)) {
|
|
36
|
-
rmSync(binarySourceDir, { force: true, recursive: true });
|
|
37
|
-
mkdirSync(binarySourceDir, { recursive: true });
|
|
38
|
-
await loadMissingBinary({binExecutableName, targetPackage, outputBinDir: binarySourceDir});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
exec(targetBinPath);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
main().catch((err) => {
|
|
45
|
-
console.error(err);
|
|
46
|
-
process.exit(1);
|
|
47
|
-
});
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// Fallback, for Windows OR if the post-install script did not execute.
|
|
4
|
+
// Adds more overhead due to launching a Node process which launches the CLI process.
|
|
5
|
+
|
|
6
|
+
import { launchFallback } from '../src/lib/install/functions/launch-fallback.js';
|
|
7
|
+
|
|
8
|
+
const main = () => {
|
|
9
|
+
return launchFallback();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
main().catch((err) => {
|
|
13
|
+
console.error(err);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@package-pal/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "CLI tool exposing core PackagePal functionality.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"package",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"tar": "^7.4.3"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
+
"@package-pal/core": "0.0.3",
|
|
20
|
+
"@package-pal/util": "0.0.5",
|
|
19
21
|
"@clack/prompts": "^0.11.0",
|
|
20
|
-
"@package-pal/core": "^0.0.2",
|
|
21
|
-
"@package-pal/util": "^0.0.4",
|
|
22
22
|
"@stricli/core": "^1.2.0",
|
|
23
23
|
"@types/bun": "^1.2.19",
|
|
24
|
-
"typescript": "^5.
|
|
24
|
+
"typescript": "^5.9.2",
|
|
25
25
|
"yoctocolors": "^2.1.1"
|
|
26
26
|
},
|
|
27
27
|
"optionalDependencies": {
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { createRequire } from 'module';
|
|
2
|
-
import {
|
|
3
|
-
dirname, join, resolve,
|
|
4
|
-
} from 'path';
|
|
5
|
-
import { fileURLToPath } from 'url';
|
|
6
|
-
import packageJson from '../../../../package.json' with { type: 'json' };
|
|
7
|
-
|
|
8
|
-
const require = createRequire(import.meta.url);
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @param {{ platform: Bun.Platform, targetPackage: string }} options
|
|
12
|
-
*/
|
|
13
|
-
export const getPathInfo = ({
|
|
14
|
-
platform, targetPackage,
|
|
15
|
-
}) => {
|
|
16
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
17
|
-
const binName = Object.keys(packageJson.bin)[0];
|
|
18
|
-
if (!binName) {
|
|
19
|
-
throw new Error(`Expected '${targetPackage}' bin name.`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const targetVersion = packageJson.version;
|
|
23
|
-
if (!targetVersion) {
|
|
24
|
-
throw new Error(`Expected '${targetPackage}' version.`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const packageRootDir = resolve(
|
|
28
|
-
__dirname, '..', '..', '..', '..',
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
const binExecutableName = platform === 'win32' ? `${binName}.exe` : binName;
|
|
32
|
-
const outputBinDir = join(packageRootDir, 'bin');
|
|
33
|
-
const outputBinBasePath = join(outputBinDir, binName);
|
|
34
|
-
const outputBinPath = join(outputBinDir, binExecutableName);
|
|
35
|
-
|
|
36
|
-
/** @type {string | null} */
|
|
37
|
-
let targetBinPath;
|
|
38
|
-
try {
|
|
39
|
-
targetBinPath = require.resolve(join(
|
|
40
|
-
'@package-pal', targetPackage, 'bin', binExecutableName,
|
|
41
|
-
));
|
|
42
|
-
} catch {
|
|
43
|
-
targetBinPath = null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
packageRootDir,
|
|
48
|
-
outputBinDir,
|
|
49
|
-
binName,
|
|
50
|
-
binExecutableName,
|
|
51
|
-
outputBinBasePath,
|
|
52
|
-
outputBinPath,
|
|
53
|
-
targetBinPath,
|
|
54
|
-
targetVersion,
|
|
55
|
-
};
|
|
56
|
-
};
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import {
|
|
3
|
+
dirname, join, resolve,
|
|
4
|
+
} from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import packageJson from '../../../../package.json' with { type: 'json' };
|
|
7
|
+
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {{ platform: Bun.Platform, targetPackage: string }} options
|
|
12
|
+
*/
|
|
13
|
+
export const getPathInfo = ({
|
|
14
|
+
platform, targetPackage,
|
|
15
|
+
}) => {
|
|
16
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
17
|
+
const binName = Object.keys(packageJson.bin)[0];
|
|
18
|
+
if (!binName) {
|
|
19
|
+
throw new Error(`Expected '${targetPackage}' bin name.`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const targetVersion = packageJson.version;
|
|
23
|
+
if (!targetVersion) {
|
|
24
|
+
throw new Error(`Expected '${targetPackage}' version.`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const packageRootDir = resolve(
|
|
28
|
+
__dirname, '..', '..', '..', '..',
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const binExecutableName = platform === 'win32' ? `${binName}.exe` : binName;
|
|
32
|
+
const outputBinDir = join(packageRootDir, 'bin');
|
|
33
|
+
const outputBinBasePath = join(outputBinDir, binName);
|
|
34
|
+
const outputBinPath = join(outputBinDir, binExecutableName);
|
|
35
|
+
|
|
36
|
+
/** @type {string | null} */
|
|
37
|
+
let targetBinPath;
|
|
38
|
+
try {
|
|
39
|
+
targetBinPath = require.resolve(join(
|
|
40
|
+
'@package-pal', targetPackage, 'bin', binExecutableName,
|
|
41
|
+
));
|
|
42
|
+
} catch {
|
|
43
|
+
targetBinPath = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
packageRootDir,
|
|
48
|
+
outputBinDir,
|
|
49
|
+
binName,
|
|
50
|
+
binExecutableName,
|
|
51
|
+
outputBinBasePath,
|
|
52
|
+
outputBinPath,
|
|
53
|
+
targetBinPath,
|
|
54
|
+
targetVersion,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
arch, platform,
|
|
3
|
-
} from 'os';
|
|
4
|
-
|
|
5
|
-
export const getPlatformInfo = () => {
|
|
6
|
-
const usePlatform = platform();
|
|
7
|
-
const useArch = arch();
|
|
8
|
-
let targetPackage = '';
|
|
9
|
-
|
|
10
|
-
switch (usePlatform) {
|
|
11
|
-
case 'darwin':
|
|
12
|
-
targetPackage = useArch === 'arm64' ? 'cli-darwin-arm64' : 'cli-darwin-x64';
|
|
13
|
-
break;
|
|
14
|
-
|
|
15
|
-
case 'win32':
|
|
16
|
-
targetPackage = 'cli-windows-x64';
|
|
17
|
-
break;
|
|
18
|
-
|
|
19
|
-
case 'linux':
|
|
20
|
-
let isMusl = false;
|
|
21
|
-
try {
|
|
22
|
-
// The report will not have a glibcVersionRuntime property if musl is being used.
|
|
23
|
-
/** @type {{ header?: { glibcVersionRuntime?: unknown } }} */
|
|
24
|
-
const report = process.report.getReport();
|
|
25
|
-
isMusl = !report.header?.glibcVersionRuntime;
|
|
26
|
-
} catch {
|
|
27
|
-
isMusl = true;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
targetPackage
|
|
31
|
-
= useArch === 'arm64'
|
|
32
|
-
? isMusl
|
|
33
|
-
? 'cli-linux-arm64-musl'
|
|
34
|
-
: 'cli-linux-arm64'
|
|
35
|
-
: isMusl
|
|
36
|
-
? 'cli-linux-x64-musl'
|
|
37
|
-
: 'cli-linux-x64';
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (!targetPackage) {
|
|
41
|
-
throw new Error(`Unsupported target: ${usePlatform} ${useArch}.`);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
platform: usePlatform,
|
|
46
|
-
arch: useArch,
|
|
47
|
-
targetPackage,
|
|
48
|
-
};
|
|
49
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
arch, platform,
|
|
3
|
+
} from 'os';
|
|
4
|
+
|
|
5
|
+
export const getPlatformInfo = () => {
|
|
6
|
+
const usePlatform = platform();
|
|
7
|
+
const useArch = arch();
|
|
8
|
+
let targetPackage = '';
|
|
9
|
+
|
|
10
|
+
switch (usePlatform) {
|
|
11
|
+
case 'darwin':
|
|
12
|
+
targetPackage = useArch === 'arm64' ? 'cli-darwin-arm64' : 'cli-darwin-x64';
|
|
13
|
+
break;
|
|
14
|
+
|
|
15
|
+
case 'win32':
|
|
16
|
+
targetPackage = 'cli-windows-x64';
|
|
17
|
+
break;
|
|
18
|
+
|
|
19
|
+
case 'linux':
|
|
20
|
+
let isMusl = false;
|
|
21
|
+
try {
|
|
22
|
+
// The report will not have a glibcVersionRuntime property if musl is being used.
|
|
23
|
+
/** @type {{ header?: { glibcVersionRuntime?: unknown } }} */
|
|
24
|
+
const report = process.report.getReport();
|
|
25
|
+
isMusl = !report.header?.glibcVersionRuntime;
|
|
26
|
+
} catch {
|
|
27
|
+
isMusl = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
targetPackage
|
|
31
|
+
= useArch === 'arm64'
|
|
32
|
+
? isMusl
|
|
33
|
+
? 'cli-linux-arm64-musl'
|
|
34
|
+
: 'cli-linux-arm64'
|
|
35
|
+
: isMusl
|
|
36
|
+
? 'cli-linux-x64-musl'
|
|
37
|
+
: 'cli-linux-x64';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!targetPackage) {
|
|
41
|
+
throw new Error(`Unsupported target: ${usePlatform} ${useArch}.`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
platform: usePlatform,
|
|
46
|
+
arch: useArch,
|
|
47
|
+
targetPackage,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync, mkdirSync, rmSync,
|
|
3
|
+
} from 'fs';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { exec } from './exec.js';
|
|
6
|
+
import { getPathInfo } from './get-path-info.js';
|
|
7
|
+
import { getPlatformInfo } from './get-platform-info.js';
|
|
8
|
+
import { loadMissingBinary } from './load-missing-binary.js';
|
|
9
|
+
import { validateBinaryVersion } from './validate-binary-version.js';
|
|
10
|
+
|
|
11
|
+
export const launchFallback = async () => {
|
|
12
|
+
const {
|
|
13
|
+
platform, targetPackage,
|
|
14
|
+
} = getPlatformInfo();
|
|
15
|
+
const {
|
|
16
|
+
targetBinPath, binExecutableName, outputBinDir, targetVersion,
|
|
17
|
+
} = getPathInfo({
|
|
18
|
+
platform,
|
|
19
|
+
targetPackage,
|
|
20
|
+
});
|
|
21
|
+
const downloadBinarySourceDir = join(
|
|
22
|
+
outputBinDir, 'source', 'bin',
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
if (targetBinPath) {
|
|
26
|
+
rmSync(downloadBinarySourceDir, {
|
|
27
|
+
force: true,
|
|
28
|
+
recursive: true,
|
|
29
|
+
});
|
|
30
|
+
validateBinaryVersion(targetVersion, targetBinPath);
|
|
31
|
+
|
|
32
|
+
return exec(targetBinPath);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const prevDownloadedBinPath = join(downloadBinarySourceDir, binExecutableName);
|
|
36
|
+
if (!existsSync(prevDownloadedBinPath)) {
|
|
37
|
+
rmSync(downloadBinarySourceDir, {
|
|
38
|
+
force: true,
|
|
39
|
+
recursive: true,
|
|
40
|
+
});
|
|
41
|
+
mkdirSync(downloadBinarySourceDir, { recursive: true });
|
|
42
|
+
await loadMissingBinary({
|
|
43
|
+
binExecutableName,
|
|
44
|
+
targetPackage,
|
|
45
|
+
outputBinDir: downloadBinarySourceDir,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const downloadBinPath = join(downloadBinarySourceDir, binExecutableName);
|
|
50
|
+
validateBinaryVersion(targetVersion, downloadBinPath);
|
|
51
|
+
|
|
52
|
+
return exec(downloadBinPath);
|
|
53
|
+
};
|
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import {
|
|
2
|
-
copyFileSync, existsSync, linkSync, symlinkSync, unlinkSync,
|
|
3
|
-
} from 'fs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param {{ platform: Bun.Platform, targetBinPath: string, outputBinPath: string }} options
|
|
7
|
-
*/
|
|
8
|
-
export const linkExistingBinary = ({
|
|
9
|
-
platform, targetBinPath, outputBinPath,
|
|
10
|
-
}) => {
|
|
11
|
-
const isWin = platform === 'win32';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @param {string | undefined} method
|
|
15
|
-
*/
|
|
16
|
-
const verifyLinked = (method) => {
|
|
17
|
-
if (!existsSync(outputBinPath)) {
|
|
18
|
-
throw new Error(`Expected link not found${method ? ` after ${method}` : ''}: '${outputBinPath}'.`);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const errs = [];
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
unlinkSync(outputBinPath);
|
|
26
|
-
} catch (e) {
|
|
27
|
-
errs.push(e);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!isWin) {
|
|
31
|
-
try {
|
|
32
|
-
symlinkSync(targetBinPath, outputBinPath);
|
|
33
|
-
verifyLinked('symlinkSync');
|
|
34
|
-
|
|
35
|
-
return;
|
|
36
|
-
} catch (eSymlinkSync) {
|
|
37
|
-
errs.push(eSymlinkSync);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
linkSync(targetBinPath, outputBinPath);
|
|
43
|
-
verifyLinked('linkSync');
|
|
44
|
-
} catch (eLinkSync) {
|
|
45
|
-
errs.push(eLinkSync);
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
copyFileSync(targetBinPath, outputBinPath);
|
|
49
|
-
verifyLinked('copyFileSync');
|
|
50
|
-
} catch (eCopyFileSync) {
|
|
51
|
-
errs.push(eCopyFileSync);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
verifyLinked(undefined);
|
|
57
|
-
} catch {
|
|
58
|
-
throw new Error('Unable to link CLI binary.', { cause: errs });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
console.info(`Successfully linked binary: '${outputBinPath}' → '${targetBinPath}'.`);
|
|
62
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
copyFileSync, existsSync, linkSync, symlinkSync, unlinkSync,
|
|
3
|
+
} from 'fs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {{ platform: Bun.Platform, targetBinPath: string, outputBinPath: string }} options
|
|
7
|
+
*/
|
|
8
|
+
export const linkExistingBinary = ({
|
|
9
|
+
platform, targetBinPath, outputBinPath,
|
|
10
|
+
}) => {
|
|
11
|
+
const isWin = platform === 'win32';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string | undefined} method
|
|
15
|
+
*/
|
|
16
|
+
const verifyLinked = (method) => {
|
|
17
|
+
if (!existsSync(outputBinPath)) {
|
|
18
|
+
throw new Error(`Expected link not found${method ? ` after ${method}` : ''}: '${outputBinPath}'.`);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const errs = [];
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
unlinkSync(outputBinPath);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
errs.push(e);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!isWin) {
|
|
31
|
+
try {
|
|
32
|
+
symlinkSync(targetBinPath, outputBinPath);
|
|
33
|
+
verifyLinked('symlinkSync');
|
|
34
|
+
|
|
35
|
+
return;
|
|
36
|
+
} catch (eSymlinkSync) {
|
|
37
|
+
errs.push(eSymlinkSync);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
linkSync(targetBinPath, outputBinPath);
|
|
43
|
+
verifyLinked('linkSync');
|
|
44
|
+
} catch (eLinkSync) {
|
|
45
|
+
errs.push(eLinkSync);
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
copyFileSync(targetBinPath, outputBinPath);
|
|
49
|
+
verifyLinked('copyFileSync');
|
|
50
|
+
} catch (eCopyFileSync) {
|
|
51
|
+
errs.push(eCopyFileSync);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
verifyLinked(undefined);
|
|
57
|
+
} catch {
|
|
58
|
+
throw new Error('Unable to link CLI binary.', { cause: errs });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
console.info(`Successfully linked binary: '${outputBinPath}' → '${targetBinPath}'.`);
|
|
62
|
+
};
|
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
import { chmod } from 'fs/promises';
|
|
2
|
-
import { get } from 'https';
|
|
3
|
-
import { join } from 'path';
|
|
4
|
-
import { pipeline } from 'stream/promises';
|
|
5
|
-
import { x } from 'tar';
|
|
6
|
-
import packageJson from '../../../../package.json' with { type: 'json' };
|
|
7
|
-
|
|
8
|
-
const maxAttempts = 5;
|
|
9
|
-
const initialBackoffMs = 500;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param {string} tarballUrl
|
|
13
|
-
* @param {string} binExecutableName
|
|
14
|
-
* @param {string} outputBinDir
|
|
15
|
-
*/
|
|
16
|
-
const tryDownloadAndExtract = (
|
|
17
|
-
tarballUrl, binExecutableName, outputBinDir,
|
|
18
|
-
) => {
|
|
19
|
-
return new Promise((resolve, reject) => {
|
|
20
|
-
get(tarballUrl, (res) => {
|
|
21
|
-
const isRedirect = res.statusCode && (res.statusCode >= 300
|
|
22
|
-
&& res.statusCode < 400);
|
|
23
|
-
const isSuccess = res.statusCode === 200;
|
|
24
|
-
|
|
25
|
-
if (isRedirect) {
|
|
26
|
-
if (!res.headers.location) {
|
|
27
|
-
reject(new Error(`Failed to download binary: Redirect location missing.`));
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
downloadAndExtract(
|
|
31
|
-
res.headers.location, binExecutableName, outputBinDir,
|
|
32
|
-
)
|
|
33
|
-
.then(resolve)
|
|
34
|
-
.catch(reject);
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (!isSuccess) {
|
|
39
|
-
reject(new Error(`Failed to download binary: ${res.statusCode?.toString() ?? 'Unknown'}`));
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const extractStream = x({
|
|
44
|
-
cwd: outputBinDir,
|
|
45
|
-
strip: 1,
|
|
46
|
-
filter: path => path === `package/bin/${binExecutableName}`,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
pipeline(res, extractStream).then(resolve)
|
|
50
|
-
.catch(reject);
|
|
51
|
-
}).on('error', reject);
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @param {string} tarballUrl
|
|
57
|
-
* @param {string} binExecutableName
|
|
58
|
-
* @param {string} outputBinDir
|
|
59
|
-
*/
|
|
60
|
-
const downloadAndExtract = async (
|
|
61
|
-
tarballUrl, binExecutableName, outputBinDir,
|
|
62
|
-
) => {
|
|
63
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
64
|
-
try {
|
|
65
|
-
await tryDownloadAndExtract(
|
|
66
|
-
tarballUrl, binExecutableName, outputBinDir,
|
|
67
|
-
);
|
|
68
|
-
return;
|
|
69
|
-
} catch (error) {
|
|
70
|
-
if (attempt < maxAttempts) {
|
|
71
|
-
const delay = initialBackoffMs * 2 ** (attempt - 1);
|
|
72
|
-
console.warn(`Download failed (attempt ${attempt.toString()}), retrying in ${delay.toString()}ms...`);
|
|
73
|
-
} else {
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
await chmod(join(outputBinDir, binExecutableName), 0o755);
|
|
81
|
-
} catch {
|
|
82
|
-
//
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @param {{ binExecutableName: string, targetPackage: string, outputBinDir: string }} options
|
|
88
|
-
*/
|
|
89
|
-
export const loadMissingBinary = async ({
|
|
90
|
-
binExecutableName, targetPackage, outputBinDir,
|
|
91
|
-
}) => {
|
|
92
|
-
const fullTargetPackageName = `@package-pal/${targetPackage}`;
|
|
93
|
-
/** @type {Record<string, string>} */
|
|
94
|
-
const optionalDeps = packageJson.optionalDependencies;
|
|
95
|
-
const targetPackageVersion = optionalDeps[fullTargetPackageName];
|
|
96
|
-
|
|
97
|
-
if (!targetPackageVersion) {
|
|
98
|
-
throw new Error(`No version found for target package '${fullTargetPackageName}'.`);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const tarballUrl = `https://registry.npmjs.org/${fullTargetPackageName}/-/${targetPackage}-${targetPackageVersion}.tgz`;
|
|
102
|
-
console.info(`Downloading '${fullTargetPackageName}' into '${outputBinDir}' from ${tarballUrl}...`);
|
|
103
|
-
|
|
104
|
-
await downloadAndExtract(
|
|
105
|
-
tarballUrl, binExecutableName, outputBinDir,
|
|
106
|
-
);
|
|
107
|
-
};
|
|
1
|
+
import { chmod } from 'fs/promises';
|
|
2
|
+
import { get } from 'https';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { pipeline } from 'stream/promises';
|
|
5
|
+
import { x } from 'tar';
|
|
6
|
+
import packageJson from '../../../../package.json' with { type: 'json' };
|
|
7
|
+
|
|
8
|
+
const maxAttempts = 5;
|
|
9
|
+
const initialBackoffMs = 500;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} tarballUrl
|
|
13
|
+
* @param {string} binExecutableName
|
|
14
|
+
* @param {string} outputBinDir
|
|
15
|
+
*/
|
|
16
|
+
const tryDownloadAndExtract = (
|
|
17
|
+
tarballUrl, binExecutableName, outputBinDir,
|
|
18
|
+
) => {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
get(tarballUrl, (res) => {
|
|
21
|
+
const isRedirect = res.statusCode && (res.statusCode >= 300
|
|
22
|
+
&& res.statusCode < 400);
|
|
23
|
+
const isSuccess = res.statusCode === 200;
|
|
24
|
+
|
|
25
|
+
if (isRedirect) {
|
|
26
|
+
if (!res.headers.location) {
|
|
27
|
+
reject(new Error(`Failed to download binary: Redirect location missing.`));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
downloadAndExtract(
|
|
31
|
+
res.headers.location, binExecutableName, outputBinDir,
|
|
32
|
+
)
|
|
33
|
+
.then(resolve)
|
|
34
|
+
.catch(reject);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!isSuccess) {
|
|
39
|
+
reject(new Error(`Failed to download binary: ${res.statusCode?.toString() ?? 'Unknown'}`));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const extractStream = x({
|
|
44
|
+
cwd: outputBinDir,
|
|
45
|
+
strip: 1,
|
|
46
|
+
filter: path => path === `package/bin/${binExecutableName}`,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
pipeline(res, extractStream).then(resolve)
|
|
50
|
+
.catch(reject);
|
|
51
|
+
}).on('error', reject);
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {string} tarballUrl
|
|
57
|
+
* @param {string} binExecutableName
|
|
58
|
+
* @param {string} outputBinDir
|
|
59
|
+
*/
|
|
60
|
+
const downloadAndExtract = async (
|
|
61
|
+
tarballUrl, binExecutableName, outputBinDir,
|
|
62
|
+
) => {
|
|
63
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
64
|
+
try {
|
|
65
|
+
await tryDownloadAndExtract(
|
|
66
|
+
tarballUrl, binExecutableName, outputBinDir,
|
|
67
|
+
);
|
|
68
|
+
return;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (attempt < maxAttempts) {
|
|
71
|
+
const delay = initialBackoffMs * 2 ** (attempt - 1);
|
|
72
|
+
console.warn(`Download failed (attempt ${attempt.toString()}), retrying in ${delay.toString()}ms...`);
|
|
73
|
+
} else {
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
await chmod(join(outputBinDir, binExecutableName), 0o755);
|
|
81
|
+
} catch {
|
|
82
|
+
//
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {{ binExecutableName: string, targetPackage: string, outputBinDir: string }} options
|
|
88
|
+
*/
|
|
89
|
+
export const loadMissingBinary = async ({
|
|
90
|
+
binExecutableName, targetPackage, outputBinDir,
|
|
91
|
+
}) => {
|
|
92
|
+
const fullTargetPackageName = `@package-pal/${targetPackage}`;
|
|
93
|
+
/** @type {Record<string, string>} */
|
|
94
|
+
const optionalDeps = packageJson.optionalDependencies;
|
|
95
|
+
const targetPackageVersion = optionalDeps[fullTargetPackageName];
|
|
96
|
+
|
|
97
|
+
if (!targetPackageVersion) {
|
|
98
|
+
throw new Error(`No version found for target package '${fullTargetPackageName}'.`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const tarballUrl = `https://registry.npmjs.org/${fullTargetPackageName}/-/${targetPackage}-${targetPackageVersion}.tgz`;
|
|
102
|
+
console.info(`Downloading '${fullTargetPackageName}' into '${outputBinDir}' from ${tarballUrl}...`);
|
|
103
|
+
|
|
104
|
+
await downloadAndExtract(
|
|
105
|
+
tarballUrl, binExecutableName, outputBinDir,
|
|
106
|
+
);
|
|
107
|
+
};
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
mkdirSync, rmSync,
|
|
3
|
-
} from 'fs';
|
|
4
|
-
import { linkExistingBinary } from './link-existing-binary.js';
|
|
5
|
-
import { loadMissingBinary } from './load-missing-binary.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @param {{ platform: Bun.Platform, binExecutableName: string, targetPackage: string, targetBinPath: string | null, outputBinDir: string, outputBinBasePath: string, outputBinPath: string }} options
|
|
9
|
-
*/
|
|
10
|
-
export const prepareBinary = ({
|
|
11
|
-
platform, binExecutableName, targetPackage, targetBinPath, outputBinDir, outputBinBasePath, outputBinPath,
|
|
12
|
-
}) => {
|
|
13
|
-
rmSync(outputBinBasePath, { force: true });
|
|
14
|
-
rmSync(outputBinPath, { force: true });
|
|
15
|
-
mkdirSync(outputBinDir, { recursive: true });
|
|
16
|
-
|
|
17
|
-
if (targetBinPath) {
|
|
18
|
-
console.info(`Expected CLI binary package is available in '${targetBinPath}'.`);
|
|
19
|
-
linkExistingBinary({
|
|
20
|
-
platform,
|
|
21
|
-
targetBinPath,
|
|
22
|
-
outputBinPath,
|
|
23
|
-
});
|
|
24
|
-
return Promise.resolve();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
console.warn(`Expected CLI binary was not found for '${targetPackage}'.`);
|
|
28
|
-
return loadMissingBinary({
|
|
29
|
-
binExecutableName,
|
|
30
|
-
targetPackage,
|
|
31
|
-
outputBinDir,
|
|
32
|
-
});
|
|
33
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
mkdirSync, rmSync,
|
|
3
|
+
} from 'fs';
|
|
4
|
+
import { linkExistingBinary } from './link-existing-binary.js';
|
|
5
|
+
import { loadMissingBinary } from './load-missing-binary.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {{ platform: Bun.Platform, binExecutableName: string, targetPackage: string, targetBinPath: string | null, outputBinDir: string, outputBinBasePath: string, outputBinPath: string }} options
|
|
9
|
+
*/
|
|
10
|
+
export const prepareBinary = ({
|
|
11
|
+
platform, binExecutableName, targetPackage, targetBinPath, outputBinDir, outputBinBasePath, outputBinPath,
|
|
12
|
+
}) => {
|
|
13
|
+
rmSync(outputBinBasePath, { force: true });
|
|
14
|
+
rmSync(outputBinPath, { force: true });
|
|
15
|
+
mkdirSync(outputBinDir, { recursive: true });
|
|
16
|
+
|
|
17
|
+
if (targetBinPath) {
|
|
18
|
+
console.info(`Expected CLI binary package is available in '${targetBinPath}'.`);
|
|
19
|
+
linkExistingBinary({
|
|
20
|
+
platform,
|
|
21
|
+
targetBinPath,
|
|
22
|
+
outputBinPath,
|
|
23
|
+
});
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
console.warn(`Expected CLI binary was not found for '${targetPackage}'.`);
|
|
28
|
+
return loadMissingBinary({
|
|
29
|
+
binExecutableName,
|
|
30
|
+
targetPackage,
|
|
31
|
+
outputBinDir,
|
|
32
|
+
});
|
|
33
|
+
};
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { exec } from './exec.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} targetVersion
|
|
5
|
+
* @param {string} targetBinPath
|
|
6
|
+
*/
|
|
7
|
+
export const validateBinaryVersion = (targetVersion, targetBinPath) => {
|
|
8
|
+
const stdout = exec(targetBinPath).toString()
|
|
9
|
+
.trim();
|
|
10
|
+
|
|
11
|
+
if (stdout.toLowerCase() !== targetVersion.toLowerCase()) {
|
|
12
|
+
throw new Error(`'${targetBinPath}' binary version mismatch; expected ${targetVersion}, got ${stdout}.`);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -1,27 +1,33 @@
|
|
|
1
|
-
import { getPathInfo } from './functions/get-path-info.js';
|
|
2
|
-
import { getPlatformInfo } from './functions/get-platform-info.js';
|
|
3
|
-
import { prepareBinary } from './functions/prepare-binary.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
import { getPathInfo } from './functions/get-path-info.js';
|
|
2
|
+
import { getPlatformInfo } from './functions/get-platform-info.js';
|
|
3
|
+
import { prepareBinary } from './functions/prepare-binary.js';
|
|
4
|
+
import { validateBinaryVersion } from './functions/validate-binary-version.js';
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const {
|
|
8
|
+
platform, targetPackage,
|
|
9
|
+
} = getPlatformInfo();
|
|
10
|
+
|
|
11
|
+
// Windows can't be optimised to run the binary directly (.exe).
|
|
12
|
+
if (platform !== 'win32') {
|
|
13
|
+
const {
|
|
14
|
+
outputBinDir, binExecutableName, outputBinBasePath, outputBinPath, targetBinPath, targetVersion,
|
|
15
|
+
} = getPathInfo({
|
|
16
|
+
platform,
|
|
17
|
+
targetPackage,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
await prepareBinary({
|
|
21
|
+
platform,
|
|
22
|
+
binExecutableName,
|
|
23
|
+
targetPackage,
|
|
24
|
+
targetBinPath,
|
|
25
|
+
outputBinDir,
|
|
26
|
+
outputBinBasePath,
|
|
27
|
+
outputBinPath,
|
|
28
|
+
});
|
|
29
|
+
validateBinaryVersion(targetVersion, outputBinPath);
|
|
30
|
+
}
|
|
31
|
+
} catch (e) {
|
|
32
|
+
throw new Error('Post install failed to install CLI binary.', { cause: e });
|
|
33
|
+
}
|