Comparing version 0.0.2 to 0.1.0
@@ -5,3 +5,3 @@ /** | ||
* ----- | ||
* Last Modified: 2018-02-27 02:09:38 | ||
* Last Modified: 2018-02-28 11:45:34 | ||
*/ | ||
@@ -11,7 +11,13 @@ | ||
const path = require('path') | ||
const http = require('http') | ||
const JSZip = require('jszip') | ||
const chalk = require('chalk') | ||
const ora = require('ora') | ||
const zip = new JSZip() | ||
const AVAILABLE_TYPES = ['gulp', 'gulp2', 'webpack'] | ||
/** | ||
* Generate target folder | ||
* @param {String} dir | ||
*/ | ||
function makeTarget (dir) { | ||
@@ -29,3 +35,10 @@ const targetPath = dir || './' | ||
/** | ||
* Copy template | ||
* @param {String} type | ||
* @param {String} dir | ||
*/ | ||
exports.generate = function (type, dir) { | ||
const targetDir = dir || '.' | ||
if (AVAILABLE_TYPES.indexOf(type) < 0) { | ||
@@ -35,16 +48,13 @@ throw new Error(`No Such Type: ${type}`) | ||
console.log('') | ||
console.log('Start generating', chalk.cyan.underline(type)) | ||
console.log('') | ||
makeTarget(dir) | ||
new JSZip.external.Promise((resolve, reject) => { | ||
fs.readFile(path.resolve(__dirname, `templates/${type}.zip`), (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(data) | ||
} | ||
}) | ||
}) | ||
.then((data) => zip.loadAsync(data)) | ||
downloadFile(type) | ||
.then((data) => { | ||
// Copy files and make folders | ||
Object.keys(data.files).forEach(key => { | ||
const filePath = path.join(dir, key) | ||
const filePath = path.join(targetDir, key) | ||
if (data.files[key].dir) { | ||
@@ -59,5 +69,6 @@ fs.mkdirSync(filePath) | ||
}) | ||
console.log('') | ||
console.log(chalk.green('Success create', chalk.cyan.underline(type), 'on')) | ||
console.log(` ${path.resolve(dir || './')}`) | ||
console.log(` ${path.resolve(targetDir)}`) | ||
console.log('') | ||
@@ -70,1 +81,40 @@ }) | ||
} | ||
/** | ||
* Download zip file from 404 | ||
* @param {String} type | ||
*/ | ||
function downloadFile (type) { | ||
return new Promise((resolve, reject) => { | ||
const spinner = ora(`Downloading ${chalk.underline.cyan(`${type}.zip`)}`).start() | ||
const request = http.get(`http://www.status404.cn/BuildTool/${type}.zip`, (res) => { | ||
if (res.statusCode !== 200) { | ||
spinner.fail(`Fail on downloading ${chalk.underline.cyan(`${type}.zip`)}: ${res.statusCode}`) | ||
reject() | ||
return | ||
} | ||
let data = [] | ||
let dataLen = 0 | ||
res.on('data', (chunk) => { | ||
data.push(chunk) | ||
dataLen += chunk.length | ||
}) | ||
res.on('end', () => { | ||
const buf = Buffer.concat(data) | ||
zip | ||
.loadAsync(buf) | ||
.then((zip) => { | ||
spinner.succeed(`Download Success ${chalk.underline.cyan(`${type}.zip`)}`) | ||
resolve(zip) | ||
}) | ||
}) | ||
}) | ||
request.on('error', (err) => { | ||
spinner.fail(`Fail on downloading ${chalk.underline.cyan(`${type}.zip`)}`) | ||
reject(err) | ||
}) | ||
}) | ||
} |
{ | ||
"name": "zexron-cli", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Easy CLI commands", | ||
@@ -31,4 +31,5 @@ "main": "index.js", | ||
"commander": "^2.14.1", | ||
"jszip": "^3.1.5" | ||
"jszip": "^3.1.5", | ||
"ora": "^2.0.0" | ||
} | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
146
6142
4
6
1
+ Addedora@^2.0.0
+ Addedansi-regex@3.0.1(transitive)
+ Addedcli-cursor@2.1.0(transitive)
+ Addedcli-spinners@1.3.1(transitive)
+ Addedclone@1.0.4(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addedlog-symbols@2.2.0(transitive)
+ Addedmimic-fn@1.2.0(transitive)
+ Addedonetime@2.0.1(transitive)
+ Addedora@2.1.0(transitive)
+ Addedrestore-cursor@2.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstrip-ansi@4.0.0(transitive)
+ Addedwcwidth@1.0.1(transitive)