diff --git a/lib/default-input.js b/lib/default-input.js index 490e83c..f5de50b 100644 --- a/lib/default-input.js +++ b/lib/default-input.js @@ -260,3 +260,18 @@ exports.license = yes ? license : prompt('license', license, (data) => { const errors = (its.errors || []).concat(its.warnings || []) return invalid(`Sorry, ${errors.join(' and ')}.`) }) + +function validateType (type) { + if (['commonjs', 'module'].includes(type)) { + return type + } + throw new Error(`${type} is not a valid package "type" (must be "commonjs" or "module")`) +} +const type = package.type || getConfig('type') || 'commonjs' +exports.type = yes ? validateType(type) : prompt('type', type, (data) => { + try { + return validateType(data) + } catch (e) { + return invalid(e.message) + } +}) diff --git a/test/bins.js b/test/bins.js index 7d1957e..0698b0a 100644 --- a/test/bins.js +++ b/test/bins.js @@ -10,18 +10,9 @@ t.test('auto bin population', async (t) => { testdir: { bin: { 'run.js': '' }, }, - inputs: [ - 'auto-bin-test', - '', - '', - '', - '', - '', - '', - '', - '', - 'yes', - ], + inputs: { + name: 'auto-bin-test', + }, }) t.same(data.bin, { 'auto-bin-test': 'bin/run.js' }, 'bin auto populated with correct path') diff --git a/test/dependencies.js b/test/dependencies.js index 897b627..55ee86c 100644 --- a/test/dependencies.js +++ b/test/dependencies.js @@ -36,6 +36,7 @@ t.test('read in dependencies and dev deps', async (t) => { t.same(data, { name: 'tap-testdir-dependencies-read-in-dependencies-and-dev-deps', version: '1.0.0', + type: 'commonjs', description: '', author: '', scripts: { test: 'mocha' }, diff --git a/test/fixtures/setup.js b/test/fixtures/setup.js index 945aad4..6b2df9b 100644 --- a/test/fixtures/setup.js +++ b/test/fixtures/setup.js @@ -18,6 +18,8 @@ const setup = async (t, file, { tdir = path.join(tdir, dir) } + inputs = Array.isArray(inputs) ? inputs : validInput(inputs) + const args = [file, CHILD, tdir, inputFile] if (config) { args.push(JSON.stringify(config)) @@ -75,4 +77,27 @@ async function child ({ chdir } = {}) { } } +const standardValue = (value) => { + if (Array.isArray(value) && Array.isArray(value[0])) { + return value + } + return [value] +} + +const validInput = (obj) => { + return [ + ...standardValue(obj.name || ''), + ...standardValue(obj.version || ''), + ...standardValue(obj.description || ''), + ...standardValue(obj.entry || ''), + ...standardValue(obj.test || ''), + ...standardValue(obj.repo || ''), + ...standardValue(obj.keywords || ''), + ...standardValue(obj.author || ''), + ...standardValue(obj.licence || ''), + ...standardValue(obj.type || ''), + ...standardValue(obj.ok || 'yes'), + ] +} + module.exports = { setup, child, isChild, getFixture } diff --git a/test/license.js b/test/license.js index 8a3899a..ab10033 100644 --- a/test/license.js +++ b/test/license.js @@ -7,19 +7,13 @@ if (isChild()) { t.test('license', async (t) => { const { data } = await setup(t, __filename, { - inputs: [ - 'the-name', // package name - '', // version - '', // description - '', // entry point - '', // test - '', // git repo - '', // keywords - '', // author - [/license: \(.*\) $/, 'Apache'], // invalid license - [/license: \(.*\) $/, 'Apache-2.0'], // license - 'yes', // about to write - ], + inputs: { + name: 'the-name', + licence: [ + [/license: \(.*\) $/, 'Apache'], // invalid license + [/license: \(.*\) $/, 'Apache-2.0'], // license + ], + }, }) const wanted = { diff --git a/test/name-spaces.js b/test/name-spaces.js index 72bcd36..3c8b926 100644 --- a/test/name-spaces.js +++ b/test/name-spaces.js @@ -7,19 +7,12 @@ if (isChild()) { t.test('single space', async t => { const { data } = await setup(t, __filename, { - inputs: [ - [/name: \(.*\) $/, 'the name'], // invalid package name - [/name: \(.*\) $/, 'the-name'], // package name - '', // version - '', // description - '', // entry point - '', // test - '', // git repo - '', // keywords - '', // author - '', // license - 'yes', // about to write - ], + inputs: { + name: [ + [/name: \(.*\) $/, 'the name'], // invalid package name + [/name: \(.*\) $/, 'the-name'], // package name + ], + }, }) const wanted = { @@ -36,19 +29,12 @@ t.test('single space', async t => { t.test('multiple spaces', async t => { const { data } = await setup(t, __filename, { - inputs: [ - [/name: \(.*\) $/, 'the name should be this'], // invalid package name - [/name: \(.*\) $/, 'the-name-should-be-this'], // package name - '', // version - '', // description - '', // entry point - '', // test - '', // git repo - '', // keywords - '', // author - '', // license - 'yes', // about to write - ], + inputs: { + name: [ + [/name: \(.*\) $/, 'the name should be this'], // invalid package name + [/name: \(.*\) $/, 'the-name-should-be-this'], // package name + ], + }, }) const wanted = { diff --git a/test/name-uppercase.js b/test/name-uppercase.js index 5c020a1..a8529c4 100644 --- a/test/name-uppercase.js +++ b/test/name-uppercase.js @@ -7,19 +7,12 @@ if (isChild()) { t.test('uppercase', async (t) => { const { data } = await setup(t, __filename, { - inputs: [ - [/name: \(.*\) $/, 'THE-NAME'], - [/name: \(.*\) $/, 'the-name'], - '', - '', - '', - '', - '', - '', - '', - '', - 'yes', - ], + inputs: { + name: [ + [/name: \(.*\) $/, 'THE-NAME'], + [/name: \(.*\) $/, 'the-name'], + ], + }, }) const EXPECT = { diff --git a/test/npm-defaults.js b/test/npm-defaults.js index 27f9acd..96664c5 100644 --- a/test/npm-defaults.js +++ b/test/npm-defaults.js @@ -100,6 +100,7 @@ const EXPECTED = { }, keywords: [], author: 'npmbot (http://npm.im/)', + type: 'commonjs', license: 'WTFPL', } diff --git a/test/repository.js b/test/repository.js index 245fe79..84220f5 100644 --- a/test/repository.js +++ b/test/repository.js @@ -7,18 +7,7 @@ if (isChild()) { t.test('license', async (t) => { const { data } = await setup(t, __filename, { - inputs: [ - 'the-name', // package name - '', // version - '', // description - '', // entry point - '', // test - 'npm/cli', // git repo - '', // keywords - '', // author - '', // license - 'yes', // about to write - ], + inputs: { name: 'the-name', repo: 'npm/cli' }, }) const wanted = { @@ -32,6 +21,7 @@ t.test('license', async (t) => { url: 'git+https://github.com/npm/cli.git', }, main: 'index.js', + type: 'commonjs', } t.has(data, wanted) })