-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabc-cli
executable file
·107 lines (87 loc) · 3.12 KB
/
abc-cli
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
#!/usr/bin/env node
/*
This is a helper forwarding commands to packages/abc-cli/build/main.js
*/
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
const NodeVersionMin = 18;
const projectRoot = path.resolve(__dirname);
const cliRoot = path.resolve(projectRoot, 'packages/abc-cli');
const cliMain = path.resolve(cliRoot, 'build', 'main.js');
const rootNodeModules = path.resolve(cliRoot, 'node_modules');
const cliNodeModules = path.resolve(cliRoot, 'node_modules');
const lastBuildDateFile = path.resolve(cliRoot, '.last-build-date.json');
if (require.main === module) {
try {
main(process.argv);
process.exit(0);
} catch (err) {
if (process.env.DEBUG) {
console.error(err);
} else {
console.error(err.message);
}
process.exit(1);
}
}
function main(args) {
checkNodeVersion();
checkPrerequisites();
if (!isCliUpToDate()) {
updateCli();
}
abcCliCommand(args);
}
function checkNodeVersion() {
const output = execSync('node -v', { cwd: cliRoot, stdio: 'pipe' });
const versionMatch = output && output.toString('utf-8').match(/^v([0-9]+)/);
const version = versionMatch && versionMatch.length && parseInt(versionMatch[1]);
if (!version || version < NodeVersionMin) {
throw new Error(`Please use at least Node.js ${NodeVersionMin}`);
}
}
function checkPrerequisites() {
const tools = [{ name: 'pnpm', doc: 'https://pnpm.io/fr/installation' }];
for (const tool of tools) {
try {
execSync('command -v ' + tool.name);
} catch (err) {
throw new Error('You must install "' + tool.name + '", see: ' + tool.doc);
}
}
}
function isCliUpToDate() {
if (isContinuousIntegration()) {
return false;
}
const nodeModulesExists = fs.existsSync(cliNodeModules) && fs.existsSync(rootNodeModules);
const cliMainExists = fs.existsSync(cliMain);
const lastBuildDateExists = fs.existsSync(lastBuildDateFile);
if (!nodeModulesExists || !cliMainExists || !lastBuildDateExists) {
return false;
}
const lastModification = new Date(execSync(`git log -1 --pretty="format:%ci" ${cliRoot}`, { cwd: cliRoot }).toString());
const lastBuildDate = new Date(require(lastBuildDateFile).date);
return lastModification <= lastBuildDate;
}
function updateCli() {
console.log('\n🔨 Abc CLI must be initialized, it may take a few minutes ... 🔧\n');
// When we are in CI, we must configure PNPM before the first install
if (isContinuousIntegration()) {
console.log('CI environment detected, applying configuration...\n');
const storePath = path.resolve(projectRoot, '.pnpm-store');
execSync('pnpm config set store-dir ' + storePath);
}
execSync('pnpm install -f', { cwd: projectRoot, stdio: 'inherit' });
execSync('pnpm run clean-build', { cwd: cliRoot, stdio: 'inherit' });
fs.writeFileSync(lastBuildDateFile, JSON.stringify({ date: new Date() }));
console.log('\n');
}
function isContinuousIntegration() {
return process.env.CI === 'true';
}
function abcCliCommand(args) {
const argsStr = args.slice(2).join(' ');
execSync(`node ${cliMain} ${argsStr}`, { cwd: process.cwd(), stdio: 'inherit' });
}