diff --git a/index.js b/index.js index ec16de6..f529c5d 100644 --- a/index.js +++ b/index.js @@ -281,7 +281,7 @@ async function readPackageJson (options, { log } = {}) { author, description: pkg.description, repository: repo, - keywords: pkg.keywords, + keywords: pkg.keywords || [], scripts: pkg.scripts, license: pkg.license }) @@ -298,7 +298,7 @@ async function format (opts, pkg = {}) { pkg.main = opts.main pkg.type = opts.type || 'commonjs' - if (opts.keywords && opts.keywords.length) { + if (opts.keywords) { // TODO: extra parsing going on here due to wesleytodd/opta#1 pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords)]) } diff --git a/test/index.js b/test/index.js index 2eb57cb..c7af12c 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,9 @@ 'use strict' -const path = require('path') -const assert = require('assert') +const path = require('node:path') +const assert = require('node:assert') +const fs = require('node:fs/promises') +const { promisify } = require('node:util') +const execFile = promisify(require('node:child_process').execFile) const { suite, test, before } = require('mocha') const fixtures = require('fs-test-fixtures') const createPkgJson = require('..') @@ -79,7 +82,7 @@ suite('create-package-json', () => { assert.strictEqual(pkg.description, '') assert.strictEqual(pkg.author, 'Test User ') assert.strictEqual(pkg.repository, undefined) - assert.strictEqual(pkg.keywords, undefined) + assert.deepStrictEqual(pkg.keywords, []) assert.strictEqual(pkg.license, 'ISC') assert.strictEqual(pkg.type, 'commonjs') assert.strictEqual(pkg.main, 'index.js') @@ -248,4 +251,35 @@ suite('create-package-json', () => { assert.deepStrictEqual(pkg.man, './man/foo.1') }) }) + + suite('npm init', () => { + test('parity', async () => { + await fix.setup() + try { + await execFile('npm', ['init', '-y'], { + cwd: fix.TMP + }) + } catch (e) { + console.error(e) + throw e + } + const npmInitPkg = JSON.parse(await fs.readFile(path.join(fix.TMP, 'package.json'), 'utf8')) + + await fix.setup() + const pkg = await createPackageJson() + + // Should be the same + assert.strictEqual(pkg.name, npmInitPkg.name) + assert.strictEqual(pkg.version, npmInitPkg.version) + assert.strictEqual(pkg.description, npmInitPkg.description) + assert.strictEqual(pkg.main, npmInitPkg.main) + assert.deepStrictEqual(pkg.scripts, npmInitPkg.scripts) + assert.deepStrictEqual(pkg.keywords, npmInitPkg.keywords) + assert.strictEqual(pkg.license, npmInitPkg.license) + + // Should be different + assert.notStrictEqual(pkg.author, npmInitPkg.author, [pkg.author, npmInitPkg.author].toString()) + assert.notStrictEqual(pkg.type, npmInitPkg.type, [pkg.type, npmInitPkg.type].toString()) + }) + }) })