generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
171 lines (140 loc) · 4.72 KB
/
index.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const exec = require('@actions/exec');
const cache = require('@actions/cache');
const { platform } = require('node:process');
const { arch } = require('node:process');
function direnvBinaryURL(version, platform, arch) {
const baseurl = `https://github.com/direnv/direnv/releases/download/v${version}/direnv`
// supported arch: x64, arm64
// supported platform: linux, darwin
const supportedArch = ['x64', 'arm64'];
const supportedPlatform = ['linux', 'darwin'];
if (!supportedArch.includes(arch)) {
throw new Error(`unsupported arch: ${arch}`);
}
if (!supportedPlatform.includes(platform)) {
throw new Error(`unsupported platform: ${platform}`);
}
const archPlatform = `${platform}-${arch}`;
switch (archPlatform) {
case 'linux-x64':
return `${baseurl}.linux-amd64`;
case 'linux-arm64':
return `${baseurl}.linux-arm64`;
case 'darwin-x64':
return `${baseurl}.darwin-amd64`;
case 'darwin-arm64':
return `${baseurl}.darwin-arm64`;
default:
throw new Error(`unsupported platform: ${archPlatform}`);
}
}
// internal functions
async function installTools() {
const direnvVersion = core.getInput('direnvVersion');
core.info(`installing direnv-${direnvVersion} on ${platform}-${arch}`);
// test direnv in cache
const foundToolCache = tc.find('direnv', direnvVersion);
if (foundToolCache) {
core.info('direnv found in tool-cache');
core.addPath(foundToolCache);
} else {
const workspace = process.env['GITHUB_WORKSPACE'];
const key = `hatsunemiku3939-direnv-action-toolcache-${direnvVersion}-${platform}-${arch}`;
const paths = [`${workspace}/.direnv-action`];
const restoreKeys = [key];
// restore from cache
core.info('direnv not found in tool-cache, restoring from cache...');
const cacheKey = await cache.restoreCache(paths.slice(), key, restoreKeys);
if (cacheKey) {
core.info(`direnv restored from cache, key: ${cacheKey}`);
// save tool-cache
core.info(`saving to tool-cache...`);
const cachedPath = await tc.cacheFile(`${workspace}/.direnv-action/direnv`, 'direnv', 'direnv', direnvVersion);
// add to path
core.addPath(cachedPath);
// clear
await exec.exec('rm', [`-rf`, `${workspace}/.direnv-action`]);
} else {
const dlUrl = direnvBinaryURL(direnvVersion, platform, arch);
core.info(`direnv not found in cache, installing ${dlUrl} ...`);
const installPath = await tc.downloadTool(dlUrl);
// set permissions
core.info(`direnv installed ${installPath}, setting permissions...`);
await exec.exec('chmod', ['+x', installPath]);
// rename to direnv
core.info(`renaming executable to direnv...`);
await exec.exec('mkdir', [`${workspace}/.direnv-action`]);
await exec.exec('cp', [installPath, `${workspace}/.direnv-action/direnv`]);
// save to cache
core.info(`saving to cache...`);
await cache.saveCache(paths, key);
// save tool-cache
core.info(`saving to tool-cache...`);
const cachedPath = await tc.cacheFile(installPath, 'direnv', 'direnv', direnvVersion);
// add to path
core.addPath(cachedPath);
// clear
await exec.exec('rm', [`-rf`, `${workspace}/.direnv-action`]);
}
}
}
async function allowEnvrc() {
core.info('allowing envrc...');
await exec.exec(`direnv`, ['allow']);
}
async function exportEnvrc() {
let outputBuffer = '';
const options = {};
options.listeners = {
stdout: (data) => {
outputBuffer += data.toString();
}
};
options.silent = true;
core.info('exporting envrc...');
await exec.exec(`direnv`, ['export', 'json'], options);
return JSON.parse(outputBuffer);
}
async function setMasks(envs) {
const rawMaskList = core.getInput('masks');
const maskList = rawMaskList.split(',').map(function (mask) {
return mask.trim();
});
core.info(`setting masks: ${maskList.join(', ')}`);
maskList.forEach(function (mask) {
const value = envs[mask];
if (value) {
core.setSecret(value);
}
});
}
// action entrypoint
async function main() {
try {
// install direnv
await installTools();
// allow given envrc
await allowEnvrc();
// export envrc to json
const envs = await exportEnvrc();
// set envs
Object.keys(envs).forEach(function (name) {
const value = envs[name];
if (name === 'PATH') {
core.info(`detected PATH in .envrc, appending to PATH...`);
core.addPath(value);
} else {
core.exportVariable(name, value);
}
});
// set masks
await setMasks(envs);
}
catch (error) {
core.setFailed(error.message);
}
}
// run action
main();