-
Notifications
You must be signed in to change notification settings - Fork 1
/
jst-nuke-init
executable file
·117 lines (103 loc) · 3.12 KB
/
jst-nuke-init
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
var program = require('commander')
program
.option('-c, --create-repo', 'Creates the repo on GitHub')
.option('-y, --yarn', 'Install dependencies using yarn instead of npm')
.parse(process.argv)
var {execSync} = require('child_process')
var inquirer = require('inquirer')
var clone = require('gitclone')
var replaceInFile = require('replace-in-file')
var cd = process.chdir
var config = require('./read-config')
var questions = [
{
name: 'humanName',
message: 'Name of JSTransformer (capitalized properly)'
},
{
name: 'url',
message: 'URL to find more about the transformer'
}
]
if (program.createRepo) {
var GitHubAPI = require('github')
var github = new GitHubAPI()
}
var replace = function (replace, withWhat) {
replaceInFile.sync({
files: ['package.json', 'README.md', 'index.js'],
replace: replace,
with: withWhat
})
}
var errHandler = function (err) {
if (err) {
if (!err.stack) {
err = new Error(err)
}
console.error(err.stack)
}
}
var run = function (command, output=true) {
result = execSync(command).toString()
if (output) {
console.log(result)
}
}
var githubLogin = function(answers) {
return new Promise((reject, resolve) => {
if (!config.token)
throw new Error(`please go to
https://github.com/settings/tokens
and add a token. Put the token in config.ini`)
try {
github.authenticate({
type: "token",
token: config.token
})
}
catch (e) { return reject(e) }
console.log('authenticated with GitHub')
resolve()
})
}
var getInfo = function () {
return inquirer.prompt(questions).then(function (answers) {
answers.lowerName = answers.humanName.toLowerCase().replace(' ', '-')
answers.transformer = `jstransformer-${answers.lowerName}`
return answers
})
}
getInfo().then(answers => {
console.log(answers)
clone('jstransformers/boilerplate', {dest: answers.transformer}, function () {
cd(answers.transformer)
if (program.createRepo) {
githubLogin(answers)
.catch(errHandler)
.then(() => {
console.log('Creating repo on GitHub')
return github.repos.createForOrg({
org: 'jstransformers',
name: answers.transformer,
description: `${answers.humanName} support for JSTransformers`,
homepage: `https://npmjs.com/${answers.transformer}`,
has_wiki: false
}).then(() => {
console.log('Finished making repo on GitHub')
console.log(`Go to https://travis-ci.org/profile/jstransformers and click sync, and then
https://travis-ci.org/jstransformers/${answers.transformer} and click enable`)
})
.catch(errHandler)
})
}
run(`git remote set-url origin https://github.com/jstransformers/${answers.transformer}`)
run(program.yarn ? 'yarn' : 'npm install')
// removes lines until "# jstransformer-foo"
replace(/[\w\W]+?(?=# jstransformer-foo)/, '')
replace(/http:\/\/example.com/g, answers.url)
replace(/foo/g, answers.lowerName)
replace(/Foo/g, answers.humanName)
})
})