From 5eeea50f70412968d8e16b390da43cab5de02954 Mon Sep 17 00:00:00 2001 From: Reggi Date: Mon, 2 Dec 2024 11:37:55 -0500 Subject: [PATCH] feat: adds type, sorting (#313) BREAKING CHANGE: adds a new `type` prompt and changes the sort order of created packages --- lib/default-input.js | 5 +++++ lib/init-package-json.js | 2 +- package.json | 2 +- test/bins.js | 15 +++------------ test/dependencies.js | 1 + test/fixtures/setup.js | 25 +++++++++++++++++++++++++ test/license.js | 20 +++++++------------- test/name-spaces.js | 38 ++++++++++++-------------------------- test/name-uppercase.js | 19 ++++++------------- test/npm-defaults.js | 1 + test/repository.js | 14 ++------------ 11 files changed, 64 insertions(+), 78 deletions(-) diff --git a/lib/default-input.js b/lib/default-input.js index 0a01bfa..3b38c77 100644 --- a/lib/default-input.js +++ b/lib/default-input.js @@ -261,3 +261,8 @@ exports.license = yes ? license : prompt('license', license, (data) => { const errors = (its.errors || []).concat(its.warnings || []) return invalid(`Sorry, ${errors.join(' and ')}.`) }) + +const type = package.type || getConfig('type') || 'commonjs' +exports.type = yes ? type : prompt('type', type, (data) => { + return data +}) diff --git a/lib/init-package-json.js b/lib/init-package-json.js index 51cbd21..b67ae41 100644 --- a/lib/init-package-json.js +++ b/lib/init-package-json.js @@ -139,7 +139,7 @@ async function init (dir, return } - await pkg.save() + await pkg.save({ sort: true }) return pkg.content } diff --git a/package.json b/package.json index d1de964..b0a746c 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "license": "ISC", "description": "A node module to get your node module started", "dependencies": { - "@npmcli/package-json": "^6.0.0", + "@npmcli/package-json": "^6.1.0", "npm-package-arg": "^12.0.0", "promzard": "^2.0.0", "read": "^4.0.0", 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 be8a1be..f87e410 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) })