|
| 1 | +'use strict' |
| 2 | +// vim: set ft=javascript ts=2 sw=2: |
| 3 | +const path = require('path') |
| 4 | +const fs = require('fs-extra') |
| 5 | +const shell = require('shelljs') |
| 6 | +const defined = require('defined') |
| 7 | +const prompt = require('./lib/prompts') |
| 8 | +const arrayOrUndefined = require('./lib/array-or-undefined') |
| 9 | + |
| 10 | +// @TODO https://docs.npmjs.com/files/package.json |
| 11 | +// man |
| 12 | +// bin |
| 13 | +// files |
| 14 | +// browser |
| 15 | +// directories |
| 16 | +// config |
| 17 | +// peerDependencies |
| 18 | +// bundledDependencies |
| 19 | +// optionalDependencies |
| 20 | +// engines |
| 21 | +// engineStrict |
| 22 | +// cpu |
| 23 | +// publishConfig |
| 24 | + |
| 25 | +module.exports = async function createPackageJson (options = {}) { |
| 26 | + // Construct all the options from the input |
| 27 | + const opts = await buildOptions(options) |
| 28 | + |
| 29 | + // Merge it back together and write it out |
| 30 | + return write((opts.noPrompt === true) ? opts : await prompt(opts, options), format(opts, pkg)) |
| 31 | +} |
| 32 | + |
| 33 | +module.exports.buildOptions = buildOptions |
| 34 | +async function buildOptions (input = {}) { |
| 35 | + // Removed undefined values from input |
| 36 | + const options = Object.keys(input).reduce((o, key) => { |
| 37 | + if (typeof input[key] !== 'undefined') { |
| 38 | + o[key] = input[key] |
| 39 | + } |
| 40 | + return o |
| 41 | + }, {}) |
| 42 | + |
| 43 | + // Option defaults |
| 44 | + const opts = Object.assign({ |
| 45 | + spacer: 2, |
| 46 | + directory: process.cwd(), |
| 47 | + ignoreExisting: false, |
| 48 | + noPrompt: false, |
| 49 | + silent: false |
| 50 | + }, options) |
| 51 | + |
| 52 | + // Read existing package.json |
| 53 | + opts.pkgPath = path.join(opts.directory, 'package.json') |
| 54 | + let pkg = {} |
| 55 | + if (opts.ignoreExisting === true) { |
| 56 | + try { |
| 57 | + pkg = await fs.readJSON(opts.pkgPath) |
| 58 | + } catch (e) { |
| 59 | + // ignore if missing or unreadable |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Set things from opts, package.json or defaults |
| 64 | + opts.version = opts.version || pkg.version || '1.0.0' |
| 65 | + opts.description = opts.description || pkg.description || '' |
| 66 | + opts.author = opts.author || pkg.author || await getAuthor() |
| 67 | + opts.repository = opts.repository || (pkg.repository && pkg.repository.url) || await readGitRemote(opts.directory) |
| 68 | + opts.keywords = arrayOrUndefined(opts.keywords) || pkg.keywords || [] |
| 69 | + opts.license = opts.license || pkg.license || 'ISC' |
| 70 | + opts.main = opts.main || pkg.main || 'index.js' |
| 71 | + opts.private = defined(opts.private, pkg.private) |
| 72 | + opts.dependencies = mapDepToVersionString(arrayOrUndefined(opts.dependencies) || pkg.dependencies || []) |
| 73 | + opts.devDependencies = mapDepToVersionString(arrayOrUndefined(opts.devDependencies) || pkg.devDependencies || []) |
| 74 | + |
| 75 | + // Merge together scripts from opts and package.json |
| 76 | + opts.scripts = Object.assign({}, opts.scripts || {}, pkg.scripts || {}) |
| 77 | + |
| 78 | + // Get name and scope, if not from options from cwd |
| 79 | + const {name, scope} = getScopeAndName(opts.scope, opts.name || pkg.name, opts.directory) |
| 80 | + opts.name = name |
| 81 | + opts.scope = scope |
| 82 | + |
| 83 | + return opts |
| 84 | +} |
| 85 | + |
| 86 | +module.exports.format = format |
| 87 | +function format (opts, pkg = {}) { |
| 88 | + pkg.name = opts.scope ? `${opts.scope}/${opts.name}` : opts.name |
| 89 | + pkg.version = opts.version |
| 90 | + pkg.description = opts.description |
| 91 | + pkg.author = opts.author |
| 92 | + pkg.keywords = opts.keywords |
| 93 | + pkg.license = opts.license |
| 94 | + pkg.main = opts.main |
| 95 | + if (opts.repository) { |
| 96 | + pkg.repository = { |
| 97 | + type: 'git', |
| 98 | + url: opts.repository |
| 99 | + } |
| 100 | + } |
| 101 | + pkg.scripts = opts.scripts |
| 102 | + if (opts.private === true) { |
| 103 | + pkg.private = true |
| 104 | + } |
| 105 | + return pkg |
| 106 | +} |
| 107 | + |
| 108 | +module.exports.write = write |
| 109 | +async function write (opts, pkg) { |
| 110 | + // Write package json |
| 111 | + await fs.outputJSON(opts.pkgPath, pkg, { |
| 112 | + spaces: opts.spacer || 2 |
| 113 | + }) |
| 114 | + |
| 115 | + // Run installs |
| 116 | + if (opts.dependencies && opts.dependencies.length) { |
| 117 | + await shell.exec(`npm i --save ${opts.dependencies.join(' ')}`, { |
| 118 | + cwd: opts.directory, |
| 119 | + silent: opts.silent |
| 120 | + }) |
| 121 | + } |
| 122 | + if (opts.devDependencies && opts.devDependencies.length) { |
| 123 | + await shell.exec(`npm i --save-dev ${opts.devDependencies.join(' ')}`, { |
| 124 | + cwd: opts.directory, |
| 125 | + silent: opts.silent |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + // Read full package back to return |
| 130 | + return fs.readJSON(opts.pkgPath) |
| 131 | +} |
| 132 | + |
| 133 | +// |
| 134 | +// Helper Functions |
| 135 | +// |
| 136 | + |
| 137 | +function readGitRemote (dir) { |
| 138 | + // Taken from npm: https://github.com/npm/init-package-json/blob/latest/default-input.js#L188-L208 |
| 139 | + return new Promise((resolve) => { |
| 140 | + fs.readFile(path.join(dir, '.git', 'config'), 'utf8', function (err, conf) { |
| 141 | + if (err || !conf) { |
| 142 | + return resolve() |
| 143 | + } |
| 144 | + conf = conf.split(/\r?\n/) |
| 145 | + const i = conf.indexOf('[remote "origin"]') |
| 146 | + let u |
| 147 | + if (i !== -1) { |
| 148 | + // Check if one of the next two lines is the remote url |
| 149 | + u = conf[i + 1] |
| 150 | + if (!u.match(/^\s*url =/)) { |
| 151 | + u = conf[i + 2] |
| 152 | + } |
| 153 | + if (!u.match(/^\s*url =/)) { |
| 154 | + u = null |
| 155 | + } else { |
| 156 | + u = u.replace(/^\s*url = /, '') |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Replace github url |
| 161 | + if (u && u.match(/^git@github.com:/)) { |
| 162 | + u = u.replace(/^git@github.com:/, 'https://github.com/') |
| 163 | + } |
| 164 | + |
| 165 | + resolve(u) |
| 166 | + }) |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +function getAuthor () { |
| 171 | + const name = shell.exec('git config --get user.name', { silent: true }).stdout.trim() |
| 172 | + const email = shell.exec('git config --get user.email', { silent: true }).stdout.trim() |
| 173 | + if (!name) { |
| 174 | + return |
| 175 | + } |
| 176 | + return `${name} <${email}>` |
| 177 | +} |
| 178 | + |
| 179 | +function getScopeAndName (scope, name, cwd) { |
| 180 | + // If no package name, get cwd base name |
| 181 | + if (!name) { |
| 182 | + name = path.basename(cwd) |
| 183 | + } |
| 184 | + |
| 185 | + // If no scope, see if name has a scope in it |
| 186 | + if (!scope && name && name.startsWith('@')) { |
| 187 | + [scope, name] = name.split('/') |
| 188 | + } |
| 189 | + |
| 190 | + // If still no scope, see if one directory up starts with an @ |
| 191 | + const dirname = path.basename(path.dirname(cwd)) |
| 192 | + if (dirname.startsWith('@')) { |
| 193 | + scope = dirname |
| 194 | + } |
| 195 | + |
| 196 | + return { name, scope } |
| 197 | +} |
| 198 | + |
| 199 | +function mapDepToVersionString (deps) { |
| 200 | + return Array.isArray(deps) ? deps : Object.keys(deps).map((name) => `${name}@${deps[name]}`) |
| 201 | +} |
0 commit comments