-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (141 loc) · 4.52 KB
/
index.js
File metadata and controls
166 lines (141 loc) · 4.52 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var fs = require('fs')
var path = require('path')
var request = require('request')
var exec = require('child_process').exec
var spawn = require('child_process').spawn
var series = require('run-series')
var parallel = require('run-parallel')
var stringify = require('json-stable-stringify')
var base = 'https://api.github.com'
var registry = 'https://registry.npmjs.org'
module.exports = createModule
var readmeTemplate = '# <package>\n[](https://nodei.co/npm/<package>/)\n'
var TEST_TEMPLATE = 'var test = require(\'tape\')\n\n' +
'test(\'dummy test\', function (t) {\n t.end()\n})'
function createModule(name, token, options, cb) {
var headers = {"user-agent": "npm create-module"}
var dir = path.join(process.cwd(), name)
headers['Authorization'] = 'token ' + token
var input = {
name: name
}
var repo
var processList = [
createGitHubrepo,
createDir,
gitInit,
createReadme,
createGitignore,
npmInit,
parallel.bind(null, [editPackage, addTest]),
parallel.bind(null, [gitPush, changeDescription, npmInstall])
]
if (options.check !== undefined) {
console.log('Checking npm for pre-existing module name')
processList.unshift(checkName)
}
series(processList, function (err) {
if(err) console.error('Error: ' + err.message)
else console.log('Done.')
})
function checkName(fn) {
request.head(registry + '/' + name, { headers: headers }, function (err, res) {
if (err) return fn(err)
if (res.statusCode === 200) return fn(new Error('"' + name + '" is already taken on npm.'))
fn(null)
})
}
function createGitHubrepo(cb) {
console.log('Creating GitHub repo..')
request.post(base + '/user/repos', {json: input, headers: headers}, function (err, res, repository) {
if(err) return cb(err)
repo = repository
console.log('Created repo', repo.full_name)
cb(null, repo)
})
}
function createDir(cb) {
console.log('Creating directory ' + dir)
fs.mkdir(dir, cb)
}
function gitInit(cb) {
console.log('Initialize git..')
exec('git init && git remote add origin ' + repo.clone_url, {cwd: dir}, function (err, stdo, stde) {
process.stderr.write(stde)
cb(err)
})
}
function createReadme(cb) {
console.log('Create readme.md...')
fs.writeFile(path.join(dir, 'readme.md'), readmeTemplate.replace(/<package>/g, name), cb)
}
function createGitignore(cb) {
console.log('Create .gitignore...')
fs.writeFile(path.join(dir, '.gitignore'), 'node_modules\n', cb)
}
function npmInit(cb) {
var init = spawn('npm', ['init'], {cwd: dir, stdio: [process.stdin, 'pipe', 'pipe']})
init.stdout.pipe(process.stdout)
init.stderr.pipe(process.stderr)
init.on('close', function (code) {
var err
if (code > 0) err = new Error('Failed npm init')
cb(err)
})
}
function changeDescription (cb) {
input.description = require(path.join(dir, 'package.json')).description
var repoUrl = [base, 'repos', repo.full_name].join('/')
request.patch(repoUrl, { json: input, headers: headers }, cb)
}
function editPackage (cb) {
var pkgPath = path.join(dir, 'package.json')
var pkg = require(pkgPath)
if (options.check) pkg.private = true
pkg.scripts.style = 'standard'
pkg['pre-commit'] = [
'style'
]
if (!pkg.scripts.test || pkg.scripts.test.indexOf('Error: no test specified') !== -1) {
pkg.scripts.test = 'node test'
}
pkg.devDependencies = {
'pre-commit': 'latest',
'standard': 'latest',
'tape': 'latest'
}
fs.writeFile(pkgPath, stringify(pkg, { space: ' ' }), cb)
}
function addTest (cb) {
fs.mkdir(path.join(dir, 'test'), function (err) {
if (err) cb(err)
fs.writeFile(
path.join(dir, 'test', 'index.js'),
TEST_TEMPLATE,
cb
)
})
}
function npmInstall(cb) {
var install = spawn('npm', ['install'], {cwd: dir, stdio: [process.stdin, 'pipe', 'pipe']})
install.stdout.pipe(process.stdout)
install.stderr.pipe(process.stderr)
install.on('close', function (code) {
var err
if (code > 0) err = new Error('Failed npm init')
cb(err)
})
}
function gitPush(cb) {
console.log('Commit and push to GitHub')
var finishGit = [
'git add --all',
'git commit -m "Initial commit"',
'git push origin master'
]
exec(finishGit.join(' && '), {cwd: dir}, function (err, stdo, stde) {
process.stderr.write(stde)
cb(err)
})
}
}