Skip to content

Commit

Permalink
feat!: adds type
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed Nov 22, 2024
1 parent eab6037 commit 8bcfcbd
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 76 deletions.
15 changes: 15 additions & 0 deletions lib/default-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
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)
})

0 comments on commit 8bcfcbd

Please sign in to comment.