This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
105 lines (81 loc) · 2.78 KB
/
index.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from 'node:fs';
import path from 'node:path';
import execa from 'execa';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import {
getMoonDir,
getPluginsDir,
getProtoDir,
getToolchainCacheKey,
getToolsDir,
} from './helpers';
const WINDOWS = process.platform === 'win32';
async function installBin(bin: string, versionInput: string) {
core.info(`Installing \`${bin}\` globally`);
const version = core.getInput(versionInput) || 'latest';
const binFile = WINDOWS ? `${bin}.exe` : bin;
const binDir = bin === 'moon' ? path.join(getMoonDir(), 'bin') : path.join(getProtoDir(), 'bin');
const binPath = path.join(binDir, binFile);
if (version !== 'latest' && fs.existsSync(binPath)) {
core.addPath(binDir);
core.info('Binary already exists, skipping installation');
return;
}
const scriptName = WINDOWS ? `${bin}.ps1` : `${bin}.sh`;
const scriptPath = path.join(getProtoDir(), 'temp', scriptName);
// If the installer already exists, delete it and ensure were using the latest
if (fs.existsSync(scriptPath)) {
fs.unlinkSync(scriptPath);
}
const script = await tc.downloadTool(`https://moonrepo.dev/install/${scriptName}`, scriptPath);
const args = version === 'latest' ? [] : [version];
core.info(`Downloaded installation script to ${script}`);
// eslint-disable-next-line no-magic-numbers
await fs.promises.chmod(script, 0o755);
await execa(script, args);
// Make it available without exe extension
if (WINDOWS) {
await fs.promises.copyFile(binPath, path.join(binDir, bin));
}
core.info(`Installed binary to ${binPath}`);
core.addPath(binDir);
core.info(`Added directory ${binDir} to PATH`);
}
async function restoreCache() {
if (!cache.isFeatureAvailable()) {
return;
}
core.info('Attempting to restore cached toolchain');
const primaryKey = await getToolchainCacheKey();
const cacheKey = await cache.restoreCache(
[getPluginsDir(), getToolsDir()],
primaryKey,
[`moon-toolchain-${process.platform}`, 'moon-toolchain'],
{},
false,
);
if (cacheKey) {
core.saveState('cacheHitKey', cacheKey);
core.info(`Toolchain cache restored using key ${primaryKey}`);
} else {
core.info(`Toolchain cache does not exist using key ${primaryKey}`);
}
core.setOutput('cache-key', cacheKey ?? primaryKey);
core.setOutput('cache-hit', !!cacheKey);
}
async function run() {
core.warning(
'This action is deprecated and will be removed in a future release. Please use https://github.com/moonrepo/setup-toolchain instead.',
);
try {
await restoreCache();
await installBin('proto', 'proto-version');
await installBin('moon', 'version');
} catch (error: unknown) {
core.setFailed((error as Error).message);
}
}
// eslint-disable-next-line unicorn/prefer-top-level-await
void run();