Skip to content

Commit 5eeea50

Browse files
authoredDec 2, 2024··
feat: adds type, sorting (#313)
BREAKING CHANGE: adds a new `type` prompt and changes the sort order of created packages
1 parent 583a2e4 commit 5eeea50

11 files changed

+64
-78
lines changed
 

‎lib/default-input.js

+5
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,8 @@ exports.license = yes ? license : prompt('license', license, (data) => {
261261
const errors = (its.errors || []).concat(its.warnings || [])
262262
return invalid(`Sorry, ${errors.join(' and ')}.`)
263263
})
264+
265+
const type = package.type || getConfig('type') || 'commonjs'
266+
exports.type = yes ? type : prompt('type', type, (data) => {
267+
return data
268+
})

‎lib/init-package-json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async function init (dir,
139139
return
140140
}
141141

142-
await pkg.save()
142+
await pkg.save({ sort: true })
143143
return pkg.content
144144
}
145145

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"license": "ISC",
2121
"description": "A node module to get your node module started",
2222
"dependencies": {
23-
"@npmcli/package-json": "^6.0.0",
23+
"@npmcli/package-json": "^6.1.0",
2424
"npm-package-arg": "^12.0.0",
2525
"promzard": "^2.0.0",
2626
"read": "^4.0.0",

‎test/bins.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,9 @@ t.test('auto bin population', async (t) => {
1010
testdir: {
1111
bin: { 'run.js': '' },
1212
},
13-
inputs: [
14-
'auto-bin-test',
15-
'',
16-
'',
17-
'',
18-
'',
19-
'',
20-
'',
21-
'',
22-
'',
23-
'yes',
24-
],
13+
inputs: {
14+
name: 'auto-bin-test',
15+
},
2516
})
2617
t.same(data.bin, { 'auto-bin-test': 'bin/run.js' },
2718
'bin auto populated with correct path')

‎test/dependencies.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ t.test('read in dependencies and dev deps', async (t) => {
3636
t.same(data, {
3737
name: 'tap-testdir-dependencies-read-in-dependencies-and-dev-deps',
3838
version: '1.0.0',
39+
type: 'commonjs',
3940
description: '',
4041
author: '',
4142
scripts: { test: 'mocha' },

‎test/fixtures/setup.js

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const setup = async (t, file, {
1818
tdir = path.join(tdir, dir)
1919
}
2020

21+
inputs = Array.isArray(inputs) ? inputs : validInput(inputs)
22+
2123
const args = [file, CHILD, tdir, inputFile]
2224
if (config) {
2325
args.push(JSON.stringify(config))
@@ -75,4 +77,27 @@ async function child ({ chdir } = {}) {
7577
}
7678
}
7779

80+
const standardValue = (value) => {
81+
if (Array.isArray(value) && Array.isArray(value[0])) {
82+
return value
83+
}
84+
return [value]
85+
}
86+
87+
const validInput = (obj) => {
88+
return [
89+
...standardValue(obj.name || ''),
90+
...standardValue(obj.version || ''),
91+
...standardValue(obj.description || ''),
92+
...standardValue(obj.entry || ''),
93+
...standardValue(obj.test || ''),
94+
...standardValue(obj.repo || ''),
95+
...standardValue(obj.keywords || ''),
96+
...standardValue(obj.author || ''),
97+
...standardValue(obj.licence || ''),
98+
...standardValue(obj.type || ''),
99+
...standardValue(obj.ok || 'yes'),
100+
]
101+
}
102+
78103
module.exports = { setup, child, isChild, getFixture }

‎test/license.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ if (isChild()) {
77

88
t.test('license', async (t) => {
99
const { data } = await setup(t, __filename, {
10-
inputs: [
11-
'the-name', // package name
12-
'', // version
13-
'', // description
14-
'', // entry point
15-
'', // test
16-
'', // git repo
17-
'', // keywords
18-
'', // author
19-
[/license: \(.*\) $/, 'Apache'], // invalid license
20-
[/license: \(.*\) $/, 'Apache-2.0'], // license
21-
'yes', // about to write
22-
],
10+
inputs: {
11+
name: 'the-name',
12+
licence: [
13+
[/license: \(.*\) $/, 'Apache'], // invalid license
14+
[/license: \(.*\) $/, 'Apache-2.0'], // license
15+
],
16+
},
2317
})
2418

2519
const wanted = {

‎test/name-spaces.js

+12-26
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@ if (isChild()) {
77

88
t.test('single space', async t => {
99
const { data } = await setup(t, __filename, {
10-
inputs: [
11-
[/name: \(.*\) $/, 'the name'], // invalid package name
12-
[/name: \(.*\) $/, 'the-name'], // package name
13-
'', // version
14-
'', // description
15-
'', // entry point
16-
'', // test
17-
'', // git repo
18-
'', // keywords
19-
'', // author
20-
'', // license
21-
'yes', // about to write
22-
],
10+
inputs: {
11+
name: [
12+
[/name: \(.*\) $/, 'the name'], // invalid package name
13+
[/name: \(.*\) $/, 'the-name'], // package name
14+
],
15+
},
2316
})
2417

2518
const wanted = {
@@ -36,19 +29,12 @@ t.test('single space', async t => {
3629

3730
t.test('multiple spaces', async t => {
3831
const { data } = await setup(t, __filename, {
39-
inputs: [
40-
[/name: \(.*\) $/, 'the name should be this'], // invalid package name
41-
[/name: \(.*\) $/, 'the-name-should-be-this'], // package name
42-
'', // version
43-
'', // description
44-
'', // entry point
45-
'', // test
46-
'', // git repo
47-
'', // keywords
48-
'', // author
49-
'', // license
50-
'yes', // about to write
51-
],
32+
inputs: {
33+
name: [
34+
[/name: \(.*\) $/, 'the name should be this'], // invalid package name
35+
[/name: \(.*\) $/, 'the-name-should-be-this'], // package name
36+
],
37+
},
5238
})
5339

5440
const wanted = {

‎test/name-uppercase.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@ if (isChild()) {
77

88
t.test('uppercase', async (t) => {
99
const { data } = await setup(t, __filename, {
10-
inputs: [
11-
[/name: \(.*\) $/, 'THE-NAME'],
12-
[/name: \(.*\) $/, 'the-name'],
13-
'',
14-
'',
15-
'',
16-
'',
17-
'',
18-
'',
19-
'',
20-
'',
21-
'yes',
22-
],
10+
inputs: {
11+
name: [
12+
[/name: \(.*\) $/, 'THE-NAME'],
13+
[/name: \(.*\) $/, 'the-name'],
14+
],
15+
},
2316
})
2417

2518
const EXPECT = {

‎test/npm-defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const EXPECTED = {
100100
},
101101
keywords: [],
102102
author: 'npmbot <n@p.m> (http://npm.im/)',
103+
type: 'commonjs',
103104
license: 'WTFPL',
104105
}
105106

‎test/repository.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,7 @@ if (isChild()) {
77

88
t.test('license', async (t) => {
99
const { data } = await setup(t, __filename, {
10-
inputs: [
11-
'the-name', // package name
12-
'', // version
13-
'', // description
14-
'', // entry point
15-
'', // test
16-
'npm/cli', // git repo
17-
'', // keywords
18-
'', // author
19-
'', // license
20-
'yes', // about to write
21-
],
10+
inputs: { name: 'the-name', repo: 'npm/cli' },
2211
})
2312

2413
const wanted = {
@@ -32,6 +21,7 @@ t.test('license', async (t) => {
3221
url: 'git+https://github.com/npm/cli.git',
3322
},
3423
main: 'index.js',
24+
type: 'commonjs',
3525
}
3626
t.has(data, wanted)
3727
})

0 commit comments

Comments
 (0)
Please sign in to comment.