Skip to content

Commit dceb67b

Browse files
committed
feat: move defaults into opta so they are sharable
1 parent 9ee5081 commit dceb67b

File tree

3 files changed

+60
-39
lines changed

3 files changed

+60
-39
lines changed

index.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path')
33
const fs = require('fs-extra')
44
const opta = require('opta')
55
const parseList = require('safe-parse-list')
6-
const scopeAndName = require('./lib/scope-and-name')
6+
const packageName = require('./lib/package-name')
77
const git = require('./lib/git')
88
const npm = require('./lib/npm')
99

@@ -16,7 +16,8 @@ function initOpts () {
1616
prompt: false,
1717
flag: {
1818
alias: 'd',
19-
defaultDescription: 'process.cwd()'
19+
defaultDescription: 'process.cwd()',
20+
default: () => process.cwd()
2021
}
2122
},
2223

@@ -33,16 +34,23 @@ function initOpts () {
3334
type: 'string',
3435
prompt: {
3536
message: 'Package name:',
36-
validate: npm.validatePackageName
37+
validate: npm.validatePackageName,
38+
default: (promptInput, allInput) => {
39+
return packageName(allInput.name, allInput.cwd)
40+
}
3741
}
3842
},
3943
version: {
4044
type: 'string',
4145
flag: {
42-
key: 'package-version'
46+
key: 'package-version',
47+
alias: 'V'
4348
},
4449
prompt: {
45-
message: 'Version:'
50+
message: 'Initial version:',
51+
default: (promptInput, allInput) => {
52+
return allInput.version || '1.0.0'
53+
}
4654
}
4755
},
4856
description: {
@@ -54,13 +62,19 @@ function initOpts () {
5462
author: {
5563
type: 'string',
5664
prompt: {
57-
message: 'Author:'
65+
message: 'Author:',
66+
default: (promptInput, allInput) => {
67+
return allInput.author || git.author({ cwd: allInput.cwd })
68+
}
5869
}
5970
},
6071
repository: {
6172
type: 'string',
6273
prompt: {
63-
message: 'Repository:'
74+
message: 'Repository:',
75+
default: (promptInput, allInput) => {
76+
return allInput.repository || git.remote({ cwd: allInput.cwd })
77+
}
6478
}
6579
},
6680
keywords: {
@@ -84,14 +98,19 @@ function initOpts () {
8498
prompt: {
8599
message: 'Module Type:',
86100
type: 'list',
87-
choices: ['commonjs', 'module']
101+
choices: ['commonjs', 'module'],
102+
default: (promptInput, allInput) => {
103+
return allInput.type || 'commonjs'
104+
}
88105
}
89106
},
90107
main: {
91108
type: 'string',
92-
default: 'index.js',
93109
prompt: {
94-
message: 'Main:'
110+
message: 'Main:',
111+
default: (promptInput, allInput) => {
112+
return allInput.main || 'index.js'
113+
}
95114
}
96115
},
97116
private: {
@@ -157,29 +176,14 @@ async function main (input, _opts = {}) {
157176
let opts = options.values()
158177

159178
// Read current state and set defaults
160-
const pkgPath = path.resolve(opts.cwd, 'package.json')
161-
const pkg = opts.ignoreExisting ? {} : await readPackageJson(pkgPath)
162-
163-
// Set defaults
164-
options.defaults({
165-
version: pkg.version || '1.0.0',
166-
name: scopeAndName(input.name || pkg.name, opts.cwd),
167-
type: pkg.type || 'commonjs',
168-
author: pkg.author || await git.author({ cwd: opts.cwd }),
169-
description: pkg.description,
170-
repository: async () => {
171-
// @TODO Do more here, read from git, etc
172-
return (pkg.repository && pkg.repository.url) || git.remote({ cwd: opts.cwd })
173-
},
174-
keywords: parseList(pkg.keywords)
175-
})
179+
const pkg = opts.ignoreExisting ? {} : await readPackageJson(options)
176180

177181
await options.prompt({
178182
promptor: _opts.promptor
179183
})()
180184

181185
opts = options.values()
182-
return write(pkgPath, opts, await format(opts, pkg))
186+
return write(path.resolve(opts.cwd, 'package.json'), opts, await format(opts, pkg))
183187
}
184188

185189
module.exports.options = initOpts().options
@@ -190,14 +194,27 @@ module.exports.cli = function () {
190194
}
191195

192196
module.exports.readPackageJson = readPackageJson
193-
async function readPackageJson (pkgPath, opts = {}) {
197+
async function readPackageJson (options) {
198+
const opts = options.values()
194199
let pkg = {}
195200
try {
196-
pkg = await fs.readJSON(pkgPath)
201+
pkg = await fs.readJSON(path.resolve(opts.cwd, 'package.json'))
197202
} catch (e) {
198203
// @TODO log this?
199204
// ignore if missing or unreadable
200205
}
206+
207+
// Set defaults from the package.json
208+
options.defaults({
209+
version: pkg.version,
210+
name: pkg.name,
211+
type: pkg.type,
212+
author: pkg.author,
213+
description: pkg.description,
214+
repository: pkg.repository && pkg.repository.url,
215+
keywords: pkg.keywords
216+
})
217+
201218
return pkg
202219
}
203220

File renamed without changes.

test/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ const fixtures = require('fs-test-fixtures')
77
const createPackageJson = require('../')
88

99
const barePrompt = {
10-
promptor: () => () => { return {} }
10+
promptor: () => async (prompts) => {
11+
// Set defaults from prompts
12+
const out = await Promise.all(prompts.map(async (p) => {
13+
if (!p.when) {
14+
return []
15+
}
16+
let ret = typeof p.default === 'function' ? p.default({}) : p.default
17+
if (ret && typeof ret.then === 'function') {
18+
ret = await ret
19+
}
20+
return [p.name, ret]
21+
}))
22+
return Object.fromEntries(out)
23+
}
1124
}
1225

1326
suite.only('create-git', () => {
@@ -63,15 +76,6 @@ suite.only('create-git', () => {
6376
assert.strictEqual(pkg.main, 'index.js')
6477
})
6578

66-
test('load scope and name from dirs', async () => {
67-
await fix.setup('scope')
68-
const pkg = await createPackageJson({
69-
cwd: path.join(fix.TMP, '@test', 'scoped')
70-
}, barePrompt)
71-
72-
assert.strictEqual(pkg.name, '@test/scoped')
73-
})
74-
7579
test('load from existing package.json', async () => {
7680
await fix.setup('existing')
7781
const pkg = await createPackageJson({

0 commit comments

Comments
 (0)