-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
90 lines (78 loc) · 3.34 KB
/
index.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
import fs from 'fs';
import path from 'path';
const home = process.env.HOME ? process.env.HOME : process.env.USERPROFILE;
const dataDir = process.env.ALLPROXY_DATA_DIR;
const mkDir = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
};
const initDataDir = () => {
mkDir(dataDir);
mkDir(`${dataDir + path.sep}intercept`);
mkDir(`${dataDir + path.sep}proto`);
mkDir(`${dataDir + path.sep}bin`);
mkDir(`${dataDir + path.sep}sessions`);
mkDir(`${dataDir + path.sep}jsonFields`);
mkDir(`${dataDir + path.sep}scripts`);
mkDir(`${dataDir + path.sep}queries`);
fetchApFileSystem();
};
function fetchApFileSystem() {
const dataDirSep = `${dataDir + path.sep}`;
const apFileSystemPath = 'docs' + path.sep + 'apFileSystem.json';
const docsPluginsPath = 'docs' + path.sep + 'plugins';
const nodeModulesPluginsPath = 'node_modules' + path.sep + 'allproxy' + path.sep + 'client' + path.sep + 'build' + path.sep + 'plugins';
const json = JSON.parse(fs.readFileSync(apFileSystemPath));
// jsonFields
const fields = fs.readdirSync(dataDirSep + 'jsonFields');
if (fields.length === 0) {
console.log('Copying jsonFields');
for (const field of json.jsonFields) {
fs.writeFileSync(dataDirSep + 'jsonFields' + path.sep + field, field);
}
fs.writeFileSync(dataDirSep + 'briefJsonFields.json', json.briefJsonFields);
}
// scripts
if (!fs.existsSync(dataDirSep + 'scripts' + path.sep + 'method')) {
console.log('Setting parse method to ' + json.method);
fs.writeFileSync(dataDirSep + 'scripts' + path.sep + 'method', json.method);
}
if (!fs.existsSync(dataDirSep + 'scripts' + path.sep + 'jsonLogScript')) {
console.log('Copying jsonLogScript');
fs.writeFileSync(dataDirSep + 'scripts' + path.sep + 'jsonLogScript', json.jsonLogScript);
}
// update the plugins in node_modules/allproxy...
for (const pluginName of ["parsejson", "importjson"]) {
console.log('Updating parsejson and importjson plugins');
fs.copyFileSync(
docsPluginsPath + path.sep + pluginName + path.sep + path.sep + 'plugin.js',
nodeModulesPluginsPath + path.sep + pluginName + path.sep + 'plugin.js');
}
// Queries
const queries = fs.readdirSync(dataDirSep + 'queries');
if (queries.length === 0) {
console.log('Copying queries');
for (const dir in json.queries) {
mkDir(dataDirSep + 'queries' + path.sep + dir);
fs.writeFileSync(dataDirSep + 'queries' + path.sep + dir + path.sep + 'query.txt', json.queries[dir].query);
}
}
let jsonQueries = [];
if (fs.existsSync(dataDirSep + 'jsonQueries.json')) {
jsonQueries = JSON.parse(fs.readFileSync(dataDirSep + 'jsonQueries.json'));
}
if (jsonQueries.length === 0) {
console.log('Copying jsonQueries.json');
fs.writeFileSync(dataDirSep + 'jsonQueries.json', json.jsonQueries);
}
let jsonSubQueries = [];
if (fs.existsSync(dataDirSep + 'jsonSubQueries.json')) {
jsonSubQueries = JSON.parse(fs.readFileSync(dataDirSep + 'jsonSubQueries.json'));
}
if (jsonSubQueries.length === 0) {
console.log('Copying jsonSubQueries.json');
fs.writeFileSync(dataDirSep + 'jsonSubQueries.json', json.jsonSubQueries);
}
}
initDataDir();