-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·138 lines (115 loc) · 3.78 KB
/
main.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
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
#! /usr/bin/node
const path = require('path');
const fs = require('fs');
const https = require('https');
const child_process = require('child_process');
const name = "AyaPingPing (TypeScript)";
const version = "v4.6.1";
const language = "TypeScript";
const generatorUrl = "https://raw.githubusercontent.com/dalikewara/ayapingping-sh/master/main_v4.sh";
const generatorFile = "main.sh";
const generatorFileTmp = "main_tmp.sh";
const baseStructureDir = "_base_structure";
const pathSeparator = path.sep;
async function main() {
let command = "";
let value = "";
let sourcePrefix = "";
let source = "";
const argsLen = process.argv.length;
if (argsLen >= 3) {
command = process.argv[2];
}
if (argsLen >= 4) {
value = process.argv[3];
}
if (argsLen >= 5) {
sourcePrefix = process.argv[4];
}
if (argsLen >= 6) {
source = process.argv[5];
}
try {
const runtimeDir = await getRuntimeDir();
await checkGenerator(runtimeDir);
const cmd = child_process.spawn(runtimeDir + pathSeparator + generatorFile, [version, language, command, value, sourcePrefix, source], { stdio: 'inherit' });
cmd.on('exit', (code) => {
process.exit(code || 0);
});
cmd.on('error', (error) => {
process.exit(1);
})
} catch (err) {
process.exit(1);
}
}
async function getRuntimeDir() {
return path.dirname(__filename);
}
async function syncGenerator(runtimeDir) {
try {
const response = await new Promise((resolve, reject) => {
https.get(generatorUrl, resolve).on('error', reject);
});
const file = fs.createWriteStream(runtimeDir + pathSeparator + generatorFileTmp);
response.pipe(file);
await new Promise((resolve, reject) => {
file.on('finish', resolve);
file.on('error', reject);
});
if (!isFileValidSH(runtimeDir + pathSeparator + generatorFileTmp)) {
return;
}
const fileData = fs.readFileSync(runtimeDir + pathSeparator + generatorFileTmp);
fs.writeFileSync(runtimeDir + pathSeparator + generatorFile, fileData, { mode: 0o775 });
} catch (err) {}
}
async function checkGenerator(runtimeDir) {
chmod(runtimeDir);
await syncGenerator(runtimeDir);
if (!isFile(runtimeDir + pathSeparator + generatorFile)) {
console.log("no generator found, please connect to the internet and run the command again to synchronize");
return;
}
if (!isFileValidSH(runtimeDir + pathSeparator + generatorFile)) {
console.log("invalid generator file, please connect to the internet and run the command again to synchronize");
}
}
function chmod(runtimeDir) {
try {
fs.chmodSync(runtimeDir + pathSeparator + generatorFile, 0o775);
fs.chmodSync(runtimeDir + pathSeparator + generatorFileTmp, 0o775);
fs.readdirSync(runtimeDir + pathSeparator + baseStructureDir).forEach((file) => {
if (isDir(path.join(runtimeDir, baseStructureDir, file))) {
fs.chmodSync(path.join(runtimeDir, baseStructureDir, file), 0o775);
return
}
fs.chmodSync(path.join(runtimeDir, baseStructureDir, file), 0o664);
});
} catch (err) {}
}
function isFile(path) {
try {
const stats = fs.statSync(path);
return stats.isFile();
} catch (err) {
return false;
}
}
function isDir(path) {
try {
const stats = fs.statSync(path);
return stats.isDirectory();
} catch (err) {
return false;
}
}
function isFileValidSH(path) {
try {
const content = fs.readFileSync(path, 'utf-8');
return content.startsWith("#!/bin/sh");
} catch (err) {
return false;
}
}
main();