-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
135 lines (114 loc) · 3.82 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
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
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 base = 'https://api.github.com'
var registry = 'https://registry.npmjs.org'
module.exports = createModule
var readmeTemplate = '# <package>\n[](https://nodei.co/npm/<package>/)\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,
processList = [
createDir,
gitInit,
createReadme,
createGitignore
]
if (!options.offline) processList.push(createGitHubrepo, gitRemoteAddOrigin)
processList.push(npmInit, gitAddAndCommit)
if (!options.offline) processList.push(parallel.bind(null, [gitPush, changeDescription]))
if (options.check) processList.unshift(checkName)
series(processList, function (err) {
if (err) console.error('Error: ' + err.message)
else console.log('Done.')
})
// Steps
function checkName (cb) {
console.log('Checking npm for pre-existing module name')
request.head(registry + '/' + name, { headers: headers }, function (err, res) {
if (err) return cb(err)
if (res.statusCode === 200) return cb(new Error('"' + name + '" is already taken on npm.'))
cb(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, function () {
process.chdir(dir)
cb()
})
}
function gitInit (cb) {
console.log('Initialize git..')
exec('git init', function (err, stdo, stde) {
process.stderr.write(stde)
cb(err)
})
}
function gitRemoteAddOrigin (cb) {
console.log('Adding remote origin')
exec('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 npmInit = spawn('npm', ['init'], {cwd: dir, stdio: [process.stdin, 'pipe', 'pipe']})
npmInit.stdout.pipe(process.stdout)
npmInit.stderr.pipe(process.stderr)
npmInit.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 gitAddAndCommit (cb) {
console.log('Adding all and committing all')
var finishGit = [
'git add --all',
'git commit -m "Initial commit"'
]
exec(finishGit.join(' && '), {cwd: dir}, function (err, stdo, stde) {
process.stderr.write(stde)
cb(err)
})
}
function gitPush (cb) {
console.log('Push to GitHub: ' + dir)
exec('git push origin master', {cwd: dir}, function (err, stdo, stde) {
process.stderr.write(stde)
cb(err)
})
}
}