-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·77 lines (68 loc) · 2.63 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
#!/usr/bin/env node
/**
* Created by Keith Morris on 11/10/14.
*/
const fs = require('fs'),
hyperquest = require('hyperquest'),
promptUser = require('./lib/prompt-user'),
qs = require('querystring'),
unzip = require('adm-zip'),
yargs = require('yargs');
const url = "https://underscores.me/";
const argv = yargs
.usage("Run underscores without command line arguments and you will be prompted for all options. " +
"Otherwise, run underscores with arguments to specify one or more options at the command prompt.")
.example('$0', "you will be prompted for everything")
.example('$0 -n "My Great Theme"', 'You will be prompted for everything but the name of the theme.')
.alias('name', 'n')
.describe('name', 'The name of your theme.')
.alias('description', 'd')
.describe('description', 'The theme\'s description.')
.alias('slug', 'g')
.describe('slug', 'The theme\'s Wordpress slug (this also defines resultant directory name).')
.alias('author', 'a')
.describe('author', 'Your theme\'s author.')
.alias('url', ['u', 'author_uri'])
.describe('url', 'Your theme\'s author\'s url')
.alias('sass', 's')
.describe('sass', 'Download the "Sassified" version of the theme.')
.alias('keep', 'k')
.describe('keep', 'Keep the downloaded zip file after extraction')
.alias('help', 'h')
.describe('help', 'Show this help message')
.argv;
if (argv.help) {
console.log(yargs.help());
process.exit();
}
promptUser(argv, function (options) {
const data = qs.stringify(options.params),
reqOpt = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
let filename,
filepath;
console.log('Generating and downloading theme.');
const req = hyperquest.post(url, reqOpt, function (err, res) {
filename = qs.parse(res.headers['content-disposition'].split('; ')[1].replace(/"/g, '')).filename;
filepath = './' + filename;
const zipStream = fs.createWriteStream(filepath);
res.pipe(zipStream);
zipStream.on('finish', function () {
console.log('Finish downloading theme ' + filename);
const files = unzip(filepath);
files.extractAllTo('./', true);
console.log('Finished unzipping theme.');
if (!options.keepZip) {
fs.unlink(filepath, function (err) {
if (err) throw err;
console.log(filename + ' was deleted');
});
}
});
});
req.end(data, 'utf8');
});