-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask-cli-init.js
67 lines (54 loc) · 1.46 KB
/
ask-cli-init.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
#!/usr/bin/env node
const https = require('https');
const {execute} = require('./utils/executor');
const fs = require('fs-extra');
const path = require('path');
const program = require('commander');
const DepensUrl = "https://raw.githubusercontent.com/patractlabs/ask-cli/main/depens.json";
program
.option('--latest', 'to use latest version');
program.parse(process.argv);
const options = program.opts();
const useDefault = options.latest === undefined;
function install_depens(depens) {
const keys = Object.keys(depens);
let args = ['install'];
for (let key of keys) {
let dep = `${key}@${depens[key]}`;
args.push(dep);
}
if (args.length > 1) {
execute('npm', args);
}
}
function create_folders() {
const contractsDir = path.join(process.cwd(), 'contracts');
if (fs.existsSync(contractsDir)) {
fs.emptyDirSync(contractsDir);
fs.rmdirSync(contractsDir);
}
fs.mkdirSync(contractsDir);
}
console.log("start to install dependencies for ask! ");
if (useDefault) {
let DefaultDeps = {
"ask-lang": "0.1.0",
"ask-transform": "0.0.20"
};
install_depens(DefaultDeps);
create_folders();
} else {
https.get(DepensUrl, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
// complete response has been received.
resp.on('end', () => {
install_depens(JSON.parse(data));
create_folders();
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}