Skip to content

Commit fb260f1

Browse files
committed
refactor to expose more underlying methods
1 parent 152dc95 commit fb260f1

2 files changed

Lines changed: 43 additions & 23 deletions

File tree

index.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,73 @@ const arrayOrUndefined = require('./lib/array-or-undefined')
2323
// publishConfig
2424

2525
module.exports = async function createPackageJson (input = {}) {
26-
// Removed undefined values from input
26+
// We need this for the defaults
27+
const cwd = process.cwd()
28+
29+
// Removed undefined values from input and default some options
2730
const options = Object.keys(input).reduce((o, key) => {
2831
if (typeof input[key] !== 'undefined') {
2932
o[key] = input[key]
3033
}
3134
return o
32-
}, {})
33-
34-
// Option defaults
35-
let opts = Object.assign({
35+
}, {
3636
spacer: 2,
37-
directory: process.cwd(),
37+
directory: cwd,
38+
pkgPath: path.join(input.directory || cwd, 'package.json'),
3839
ignoreExisting: false,
3940
noPrompt: false,
4041
silent: false
41-
}, options)
42+
})
4243

4344
// Read existing package.json
44-
opts.pkgPath = path.join(opts.directory, 'package.json')
45+
const pkg = await readPackageJson(options)
46+
47+
// Build up the package field options and merge it all together
48+
const opts = await buildPackageOptions(options, pkg)
49+
50+
// Format the json and write it out
51+
return write(await prompt(opts, options), format(opts, pkg))
52+
}
53+
54+
module.exports.readPackageJson = readPackageJson
55+
async function readPackageJson (opts = {}) {
4556
let pkg = {}
46-
if (opts.ignoreExisting === true) {
57+
if (opts.ignoreExisting !== true) {
4758
try {
4859
pkg = await fs.readJSON(opts.pkgPath)
4960
} catch (e) {
5061
// ignore if missing or unreadable
5162
}
5263
}
64+
return pkg
65+
}
66+
67+
module.exports.buildPackageOptions = buildPackageOptions
68+
async function buildPackageOptions (options = {}, pkg = {}) {
69+
const opts = {}
5370

5471
// Construct all the options from the input
5572
// Set things from opts, package.json or defaults
56-
opts.version = opts.version || pkg.version || '1.0.0'
57-
opts.description = opts.description || pkg.description || ''
58-
opts.author = opts.author || pkg.author || await getAuthor()
59-
opts.repository = opts.repository || (pkg.repository && pkg.repository.url) || await readGitRemote(opts.directory)
60-
opts.keywords = arrayOrUndefined(opts.keywords) || pkg.keywords || []
61-
opts.license = opts.license || pkg.license || 'ISC'
62-
opts.main = opts.main || pkg.main || 'index.js'
63-
opts.private = defined(opts.private, pkg.private)
64-
opts.dependencies = mapDepToVersionString(arrayOrUndefined(opts.dependencies) || pkg.dependencies || [])
65-
opts.devDependencies = mapDepToVersionString(arrayOrUndefined(opts.devDependencies) || pkg.devDependencies || [])
73+
opts.version = options.version || pkg.version || '1.0.0'
74+
opts.description = options.description || pkg.description || ''
75+
opts.author = options.author || pkg.author || await getAuthor()
76+
opts.repository = options.repository || (pkg.repository && pkg.repository.url) || await readGitRemote(options.directory)
77+
opts.keywords = arrayOrUndefined(options.keywords) || pkg.keywords || []
78+
opts.license = options.license || pkg.license || 'ISC'
79+
opts.main = options.main || pkg.main || 'index.js'
80+
opts.private = defined(options.private, pkg.private)
81+
opts.dependencies = mapDepToVersionString(arrayOrUndefined(options.dependencies) || pkg.dependencies || [])
82+
opts.devDependencies = mapDepToVersionString(arrayOrUndefined(options.devDependencies) || pkg.devDependencies || [])
6683

6784
// Merge together scripts from opts and package.json
68-
opts.scripts = Object.assign({}, opts.scripts || {}, pkg.scripts || {})
85+
opts.scripts = Object.assign({}, options.scripts || {}, pkg.scripts || {})
6986

7087
// Get name and scope, if not from options from cwd
71-
const {name, scope} = getScopeAndName(opts.scope, opts.name || pkg.name, opts.directory)
88+
const {name, scope} = getScopeAndName(options.scope, options.name || pkg.name, options.directory)
7289
opts.name = name
7390
opts.scope = scope
7491

75-
// Merge it back together and write it out
76-
return write((opts.noPrompt === true) ? opts : await prompt(opts, options), format(opts, pkg))
92+
return Object.assign({}, options, opts)
7793
}
7894

7995
module.exports.format = format

lib/prompts.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const arrayFromList = require('./array-from-list')
66
const arrayOrUndefined = require('./array-or-undefined')
77

88
module.exports = async function prompt (opts = {}, options = {}) {
9+
if (opts.noPrompt === true) {
10+
return opts
11+
}
12+
913
const answers = await inquirer.prompt([{
1014
name: 'name',
1115
message: 'Package name:',

0 commit comments

Comments
 (0)