Skip to content

Commit

Permalink
feat: adds type, sorting (#313)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: adds a new `type` prompt and changes the sort order of created packages
  • Loading branch information
reggi authored Dec 2, 2024
1 parent 583a2e4 commit 5eeea50
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 78 deletions.
5 changes: 5 additions & 0 deletions lib/default-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
2 changes: 1 addition & 1 deletion lib/init-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async function init (dir,
return
}

await pkg.save()
await pkg.save({ sort: true })
return pkg.content
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 3 additions & 12 deletions test/bins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions test/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 }
20 changes: 7 additions & 13 deletions test/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
38 changes: 12 additions & 26 deletions test/name-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
19 changes: 6 additions & 13 deletions test/name-uppercase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions test/npm-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const EXPECTED = {
},
keywords: [],
author: 'npmbot <[email protected]> (http://npm.im/)',
type: 'commonjs',
license: 'WTFPL',
}

Expand Down
14 changes: 2 additions & 12 deletions test/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
})
Expand Down

0 comments on commit 5eeea50

Please sign in to comment.