-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 1.83 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
const fs = require('fs-extra');
const extractZIP = require('extract-zip');
const path = require('path');
const color = require('ansi-colors');
const request = require('request');
// updateUrl
/**
* PATH
*/
const YAML = require('yaml')
const configGlobal = YAML.parse(fs.readFileSync(path.resolve(process.cwd()+'/config.yaml'), 'utf8'))
const URL_SRC = configGlobal.updateSrc?.url||'https://github.com/vnjson/mcap/archive/refs/heads/main.zip';
const PATH_ZIP = path.join(process.cwd() , "src.zip");
const PATH_UPLOAD_SRC = path.join(process.cwd(), (configGlobal.updateSrc?.dir||'mcap-main') +'/src');
/**
* ACTIONS
*/
async function getZIP (URL_SRC, PATH_ZIP){
return new Promise( (resolve, reject) => {
request(URL_SRC)
.pipe( fs.createWriteStream(PATH_ZIP) )
.on('close', async (err) => {
if(err){
reject(err);
}
else{
resolve();
}
});
});
}
async function init (projectName){
console.log('[ download ] '+ color.yellow(URL_SRC))
try{
// удаляю src.zip
await fs.remove(PATH_ZIP);
// получаю архив проекта с github
await getZIP(URL_SRC, PATH_ZIP);
// распаковываю архив
await extractZIP(PATH_ZIP, { dir: process.cwd() })
// удаляю архив
await fs.remove(PATH_ZIP);
// копирую из каталок ./src в корень проекта
await fs.copy(PATH_UPLOAD_SRC, projectName);
await fs.remove(path.join(process.cwd(), configGlobal.updateSrc?.dir||'mcap-main'));
await fs.remove(path.join(process.cwd(), "public"));
}
catch(err){
throw new Error(err);
}
}
/**
* npm run init project-name
*/
init(process.argv[2]);