-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.cjs
More file actions
164 lines (145 loc) · 4.72 KB
/
index.cjs
File metadata and controls
164 lines (145 loc) · 4.72 KB
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
#!/usr/bin/env node
const { execSync } = require("child_process");
const path = require("path");
const fs = require('fs');
const axios = require('axios');
const os = require('os');
let binaryPath = path.join(__dirname, 'bin', 'simplelocalize');
if (os.platform() === 'win32') {
binaryPath += '.exe';
}
const getExpectedCliVersion = () => {
const packageVersion = require('./package.json').version;
const parts = packageVersion.split('.');
return parts[0] + '.' + parts[1] + '.0';
};
const isBinaryInstalled = () => {
try {
const output = execSync(binaryPath + ' --version').toString().trim();
const expectedVersion = getExpectedCliVersion();
return output.includes(expectedVersion);
}
catch (error) {
return false;
}
}
const printBinaryVersion = () => {
try {
console.log(execSync(binaryPath + ' --version').toString());
}
catch (error) {
console.error('Error running SimpleLocalize CLI');
process.exit(1);
}
}
const buildDownloadBinaryUrl = (version) => {
let platform = "";
if (os.platform() === "win32") {
platform = "windows.exe"
}
if (os.platform() === "darwin") {
platform = "mac"
}
if (os.platform() === "linux") {
platform = "linux"
}
let arch = "";
if (os.arch() === "arm64") {
arch = "-arm64"
}
console.log(`Downloading SimpleLocalize CLI ${version} for ${platform}${arch}...`);
return `https://get.simplelocalize.io/binaries/${version}/simplelocalize-cli-${platform}${arch}`
}
async function installBinary() {
const cliVersion = getExpectedCliVersion();
if (fs.existsSync(binaryPath)) {
console.log('Removing existing binary...');
fs.unlinkSync(binaryPath);
}
const downloadUrl = buildDownloadBinaryUrl(cliVersion);
if (!fs.existsSync(path.join(__dirname, 'bin'))) {
fs.mkdirSync(path.join(__dirname, 'bin'));
}
try {
const response = await axios({
url: downloadUrl,
method: 'GET',
responseType: 'stream'
});
const writer = fs.createWriteStream(binaryPath);
response.data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
} catch (error) {
console.error('Error downloading SimpleLocalize CLI binary:', error.message);
process.exit(1);
}
// Make it executable (only on Unix-based systems)
if (os.platform() !== 'win32') {
console.log('Making binary executable...');
fs.chmodSync(binaryPath, 0o755);
}
// Ensure binary exists and is executable before continuing
if (!fs.existsSync(binaryPath)) {
throw new Error('Binary file was not created!');
}
}
const linkToNodeModulesBin = () => {
let nodeModulesBinPath = path.join(__dirname, '..', '..', '.bin', 'simplelocalize');
if (os.platform() === 'win32') {
nodeModulesBinPath += '.exe';
}
// Ensure the .bin directory exists
const binDir = path.dirname(nodeModulesBinPath);
if (!fs.existsSync(binDir)) {
fs.mkdirSync(binDir, { recursive: true });
}
if (fs.existsSync(nodeModulesBinPath)) {
fs.unlinkSync(nodeModulesBinPath);
}
fs.symlinkSync(binaryPath, nodeModulesBinPath);
console.log('Link to node_modules/.bin created.');
}
const init = async () => {
console.log("Checking if SimpleLocalize CLI is installed...");
if (!isBinaryInstalled()) {
console.log("SimpleLocalize CLI not installed. Installing...");
await installBinary();
if (!isBinaryInstalled()) {
console.error('Error installing SimpleLocalize CLI');
process.exit(1);
}
console.log("SimpleLocalize CLI installed successfully!");
} else {
console.log("SimpleLocalize CLI already installed.");
}
printBinaryVersion();
linkToNodeModulesBin();
process.exit(0);
}
// Only run install logic if called with 'install' argument (for postinstall)
if (process.argv[2] === 'install') {
(async () => {
await init();
})();
} else {
// Forward all other CLI commands to the downloaded binary
if (!isBinaryInstalled()) {
console.error('SimpleLocalize CLI binary is not installed. Please run: npm install');
process.exit(1);
}
// Build the command to forward
const args = process.argv.slice(2).map(arg => {
// Quote arguments with spaces for safety
if (/\s/.test(arg)) return `"${arg}"`;
return arg;
}).join(' ');
try {
execSync(`${binaryPath} ${args}`, { stdio: 'inherit' });
process.exit(0);
} catch (error) {
process.exit(error.status || 1);
}
}