-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_init.cjs
executable file
·83 lines (67 loc) · 2.17 KB
/
build_init.cjs
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
#!/usr/bin/env node
/*
* Copyright (C) 2024 Savoir-faire Linux Inc.
* SPDX-License-Identifier: Apache-2.0
*/
const fs = require('fs-extra');
const path = require('path');
const { execSync } = require('child_process');
const NODE_MODULES_STAMP = 'package-lock.json';
const COCKPIT_REPO_FILE = 'pkg/lib';
// Function to execute shell command synchronously
function runCommand(command) {
console.log(`Running command: ${command}`);
execSync(command, { stdio: 'inherit' });
}
// Function to copy cockpit/lib in the project
function copyCockpitLib() {
const COCKPIT_REPO_PATH = path.join(__dirname, 'node_modules', 'cockpit-repo', COCKPIT_REPO_FILE);
const DESTINATION_PATH = path.join(__dirname, COCKPIT_REPO_FILE);
fs.copySync(COCKPIT_REPO_PATH, DESTINATION_PATH);
}
function dependenciesChanged() {
const nodeModulesStampPath = path.resolve(__dirname, NODE_MODULES_STAMP);
// Checks if package-lock.json is older than package.json
if (!fs.existsSync(nodeModulesStampPath)) return true;
const packageLockStats = fs.statSync(nodeModulesStampPath);
const packageStats = fs.statSync('package.json');
return packageStats.mtimeMs > packageLockStats.mtimeMs;
}
function buildDist() {
const distPath = path.join(__dirname, 'dist');
fs.ensureDirSync(distPath);
execSync('node build.js', { stdio: 'inherit' });
}
function clean() {
const directoriesToRemove = ['dist', 'node_modules', 'pkg'];
const fileToRemove = 'package-lock.json';
directoriesToRemove.forEach(dir => {
const dirPath = path.resolve(__dirname, dir);
try {
fs.removeSync(dirPath);
console.log(`folder ${dir} removed with succes`);
} catch (err) {
console.error(`Failed to remove ${dir}:`, err);
}
const filePath = path.resolve(__dirname, fileToRemove);
try {
fs.removeSync(filePath);
console.log(`File ${fileToRemove} removed with succes`);
} catch (err) {
console.error(`Failed to remove ${fileToRemove}:`, err);
}
});
}
function main() {
if (dependenciesChanged()) {
runCommand('npm install');
}
copyCockpitLib();
buildDist();
}
const args = process.argv.slice(2);
if (args.includes('--clean')) {
clean();
} else {
main();
}