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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
// initialize a package.json file
module.exports = init
var prompt = require("./utils/prompt")
, path = require("path")
, readJson = require("./utils/read-json")
, fs = require("./utils/graceful-fs")
, promiseChain = require("./utils/promise-chain")
, exec = require("./utils/exec")
, semver = require("semver")
, log = require("./utils/log")
, npm = require("../npm")
, output = require("./utils/output")
init.usage = "npm init [folder]"
function init (args, cb) {
var folder = args[0] || "."
, ll = npm.config.get("loglevel")
npm.config.set("loglevel", "paused")
if (folder.charAt(0) !== "/") folder = path.join(process.cwd(), folder)
readJson(path.join(folder, "package.json"), function (er, data) {
if (er) data = {}
data.author = data.author ||
{ name: npm.config.get("init.author.name")
, email: npm.config.get("init.author.email")
, url: npm.config.get("init.author.url") }
init_(data, folder, function (er) {
npm.config.set("loglevel", ll)
if (!er) log(path.join(folder, "package.json"), "written")
cb(er)
})
})
}
function init_ (data, folder, cb) {
var dl
output.write(
["This utility will walk you through creating a package.json file."
,"It only covers the most common items, and tries to guess sane defaults."
,"See `npm help json` for definitive documentation on these fields"
,"and exactly what they do."
,"Press ^C at any time to quit."
,""
].join("\n"))
promiseChain(cb)
( prompt
, ["Package name: ", defaultName(folder, data)]
, function (n) { data.name = n }
)
( prompt
, ["Description: ", data.description]
, function (d) { data.description = d }
)
( defaultVersion, [folder, data], function (v) { data.version = v } )
(function (cb) {
prompt("Package version: ", data.version, function (er, v) {
if (er) return cb(er)
data.version = v
cb()
})
}, [])
( prompt
, ["Project homepage: ", data.homepage || data.url || "none"]
, function (u) {
if (u === "none") return
data.homepage = u
delete data.url
}
)
( defaultRepo, [folder, data], function (r) { data.repository = r } )
(function (cb) {
prompt("Project git repository: "
, data.repository && data.repository.url || "none"
, function (er, r) {
if (er) return cb(er)
if (r !== "none") {
data.repository = (data.repository || {}).url = r
}
cb()
})
}, [])
( prompt
, ["Author name: ", data.author && data.author.name]
, function (n) {
if (!n) return
(data.author = data.author || {}).name = n
}
)
( prompt
, ["Author email: ", data.author && data.author.email || "none"]
, function (n) {
if (n === "none") return
(data.author = data.author || {}).email = n
}
)
( prompt
, ["Author url: ", data.author && data.author.url || "none"]
, function (n) {
if (n === "none") return
(data.author = data.author || {}).url = n
}
)
( prompt
, ["Main module/entry point: ", data.main || "none"]
, function (m) {
if (m === "none") {
delete data.main
return
}
data.main = m
}
)
( defaultLib, [data, folder], function (d) { dl = d } )
( function (cb) {
prompt("Which folder do your modules live in: ", dl, cb)
}
, []
, function (l) {
if (l === "none") {
if (data.directories && data.directories.lib) {
delete data.directories.lib
}
return
}
(data.directories = data.directories || {}).lib = l
}
)
( prompt
, ["Test command: ", data.scripts && data.scripts.test || "none"]
, function (t) {
if (t === "none") return
(data.scripts = data.scripts || {}).test = t
}
)
( prompt
, [ "What versions of node does it run on? "
, data.engines && data.engines.node || "*"
]
, function (nodever) {
(data.engines = data.engines || {}).node = nodever
}
)
// todo: friendly dependency resolution stuffs.
(cleanupPaths, [data, folder])
(function (cb) {
try { data = readJson.processJson(data) }
catch (er) { return cb(er) }
Object.keys(data)
.filter(function (k) { return k.match(/^_/) })
.forEach(function (k) { delete data[k] })
readJson.unParsePeople(data)
var str = JSON.stringify(data, null, 2)
, msg = "About to write to "
+ path.join(folder, "package.json")
+ "\n\n"
+ str
+ "\n\n"
output.write(msg, cb)
})
(function (cb) {
prompt("\nIs this ok? ", "yes", function (er, ok) {
if (er) return cb(er)
if (ok.toLowerCase().charAt(0) !== "y") {
return cb(new Error("cancelled"))
}
return cb()
})
})
(function (cb) {
fs.writeFile( path.join(folder, "package.json")
, JSON.stringify(data, null, 2)
, cb
)
})
()
}
function defaultLib (data, folder, cb) {
// existing lib setting?
if (data.directories && data.directories.lib) {
return cb(null, data.directories.lib)
}
// two options at this point.
// if main is ./lib/name.js, look first for ./lib/name/
// failing that, use ./lib
// if main is ./foo.js, then look for ./lib
var l
if (data.main) {
l = path.join(folder, path.dirname(data.main))
} else {
l = folder
}
// now try to walk down any ./lib or ./name folders we can find.
// it just has to be a reasonable guess
defaultLibWalker(l, data.name, cb)
}
function defaultLibWalker (folder, name, cb) {
fs.readdir(folder, function (er, files) {
// oops?
return (er) ? cb(null, path.dirname(folder)) // oops?
: (files.indexOf(name) !== -1) ?
defaultLibWalker(path.join(folder, name), name, cb)
: (files.indexOf("lib") !== -1) ?
defaultLibWalker(path.join(folder, "lib"), name, cb)
: cb(null, folder)
})
}
// sync - no io
function defaultName (folder, data) {
if (data.name) return data.name
return path.basename(folder).replace(/^node[_-]?|[-\.]?js$/g, "")
}
function defaultVersion (folder, data, cb) {
if (data.version) return cb(null, data.version)
exec("git", ["describe", "--tags"], process.env, false, folder,
function (er, code, out) {
out = (out || "").trim()
if (semver.valid(out)) return cb(null, out)
out = npm.config.get("init.version")
if (semver.valid(out)) return cb(null, out)
return cb(null, "0.0.0")
})
}
function defaultRepo (folder, data, cb) {
if (data.repository) return cb(null, data.repository)
process.chdir(folder)
exec("git", ["remote", "-v"], process.env, false, function (er, code, out) {
out = (out || "")
.trim()
.split("\n").filter(function (line) {
return line.search(/^origin/) !== -1
})[0]
if (!out) return cb(null, {})
repo = {
type: "git"
, url: out.split(/\s/)[1].replace("git@github.com:", "git://github.com/")
}
return cb(null, repo)
})
}
function cleanupPaths (data, folder, cb) {
if (data.main) {
data.main = cleanupPath(data.main, folder)
}
var dirs = data.directories
if (dirs) {
Object.keys(dirs).forEach(function (dir) {
dirs[dir] = cleanupPath(dirs[dir], folder)
})
}
cb()
}
function cleanupPath (m, folder) {
if (m.indexOf(folder) === 0) m = path.join(".", m.substr(folder.length))
return m
}
|