-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
43 lines (35 loc) · 1.19 KB
/
install.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
import { rm } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import yqBin from './index.js';
function getProjectRoot() {
// `projectRoot` on postinstall could be INIT_CWD introduced in npm >= 5.4
// see: https://github.com/npm/npm/issues/16990
const initCwd = process.env.INIT_CWD;
if (initCwd) {
return initCwd;
}
// Fallback of getting INIT_CWD
const cwd = process.cwd();
const paths = cwd.split(path.sep);
// If `process.cwd` ends with 'node_modules/*' then get the dependent root directory,
// otherwise return the `cwd` (ordinary it will be the postinstall process of yq-bin itself).
if (paths.length > 1 && paths.at(-2) === 'node_modules') {
return path.resolve('../../', cwd);
}
return cwd;
}
async function main() {
const projectRoot = getProjectRoot();
const vendorDir = path.join(projectRoot, './vendor');
await rm(vendorDir, { force: true, recursive: true });
const bin = await yqBin(projectRoot);
bin.run(['-V']).then(() => {
console.log('yq binary successfully installed!');
})
.catch(error => {
console.error('yq binary installation failed!');
throw new Error(error);
});
}
main();