-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (81 loc) · 2.38 KB
/
app.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
#!/usr/bin/env node
import { exec, execSync } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
import { readFileSync } from 'fs';
import readline from 'readline';
function checkPearInstallation() {
try {
execSync('pear --version', { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
function promptToInstallPear() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('[info] "pear" is not installed. Would you like to install it now? (y/n): ', (answer) => {
rl.close();
if (answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes') {
installPear();
} else {
console.error(`
[error] "pear" is required to run this application. Please install it manually.
Visit the official documentation for installation instructions:
https://docs.pearproject.org/
`);
process.exit(1);
}
});
}
function installPear() {
console.log('[info] Installing "pear"...');
exec('npm install -g pear', (err, stdout, stderr) => {
if (err) {
console.error(`[error] Failed to install "pear": ${err.message}`);
console.error(`
Please try installing it manually using the following command:
npm install -g pear
Visit the official documentation for more details:
https://docs.pearproject.org/
`);
process.exit(1);
}
if (stderr) {
console.error(`[stderr] ${stderr}`);
}
console.log(stdout);
console.log('[info] "pear" has been successfully installed. Proceeding...');
startApp();
});
}
function startApp() {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJsonPath = resolve(__dirname, 'package.json');
const packageData = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const pearkey = packageData.bin.pearkey;
if (!pearkey) {
console.error('Error: pearkey is not defined in package.json');
process.exit(1);
}
exec(`pear run pear://${pearkey}`, (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(stdout);
});
}
if (!checkPearInstallation()) {
promptToInstallPear();
} else {
startApp();
}