Skip to content

Commit 962f8e8

Browse files
committed
chore: expand test coverage
- add coverage for most implemented non-`npm install` behavior - add a wallabyjs config file - add [`read-package-json`](https://npm.im/read-package-json) for some normalization - rename fixtures, reorganize tests somewhat - add a helper function to run `createPackageJson` in silent mode w/ `barePrompt` - change some assertions to use `deepStrictEqual` - add TODO list to `README.md` - better handling of `scripts`, `keywords`, and `repository` fields - update build matrix
1 parent 18ad440 commit 962f8e8

11 files changed

Lines changed: 252 additions & 34 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
node-version: [8.x, 10.x, 12.x]
12+
node-version: [10.x, 12.x, 14.x, 15.x]
1313
steps:
1414
- uses: actions/checkout@v1
1515
- name: Use Node.js ${{ matrix.node-version }}

.wallaby.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
module.exports = () => {
4+
return {
5+
files: ['package.json', 'index.js', 'lib/**/*.js', 'bin/*', 'test/fixtures/**/*', 'test/fixtures/**/.*' ],
6+
tests: ['test/*.js'],
7+
env: {
8+
type: 'node',
9+
runner: 'node'
10+
},
11+
testFramework: 'mocha'
12+
};
13+
};

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,26 @@ const createPackageJson = require('create-package-json')
8989
console.log(pkg) // The json after writing and installing
9090
})()
9191
```
92+
93+
## Implementation TODO
94+
95+
> See [`package.json` docs](https://docs.npmjs.com/files/package.json)
96+
97+
- [ ] `exports`
98+
- [ ] `bin`
99+
- [ ] `funding`
100+
- [ ] `files`
101+
- [ ] `browser`
102+
- [ ] `directories`
103+
- [ ] `config`
104+
- [ ] `dependencies`
105+
- [ ] `devDependencies`
106+
- [ ] `bundledDependencies`
107+
- [ ] `optionalDependencies`
108+
- [ ] `engines`
109+
- [ ] `os`
110+
- [ ] `cpu`
111+
- [ ] `publishConfig`
112+
- [ ] `homepage`
113+
- [ ] `bugs`
114+
- [ ] `contributors`

index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const path = require('path')
33
const fs = require('fs-extra')
44
const opta = require('opta')
55
const parseList = require('safe-parse-list')
6+
const { promisify } = require('util')
7+
const readPkg = promisify(require('read-package-json'))
68
const { Loggerr } = require('loggerr')
79
const packageName = require('./lib/package-name')
810
const git = require('./lib/git')
@@ -226,7 +228,7 @@ async function readPackageJson (options, { log } = {}) {
226228
const opts = options.values()
227229
let pkg = {}
228230
try {
229-
pkg = await fs.readJSON(path.resolve(opts.cwd, 'package.json'))
231+
pkg = await readPkg(path.resolve(opts.cwd, 'package.json'))
230232
log.debug('Read existing package.json', pkg)
231233
} catch (e) {
232234
// @TODO log this?
@@ -240,9 +242,10 @@ async function readPackageJson (options, { log } = {}) {
240242
type: pkg.type,
241243
author: pkg.author,
242244
description: pkg.description,
243-
repository: pkg.repository && pkg.repository.url,
245+
repository: pkg.repository,
244246
keywords: pkg.keywords,
245-
scripts: Object.assign({}, pkg.scripts, opts.scripts)
247+
scripts: pkg.scripts,
248+
license: pkg.license
246249
})
247250

248251
return pkg
@@ -258,21 +261,27 @@ async function format (opts, pkg = {}) {
258261
pkg.type = opts.type || 'commonjs'
259262

260263
if (opts.keywords && opts.keywords.length) {
261-
pkg.keywords = opts.keywords
264+
// TODO: extra parsing going on here due to wesleytodd/opta#1
265+
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords)])
262266
}
263267

264268
// Scripts
265269
if (Object.keys(opts.scripts).length) {
266-
pkg.scripts = opts.scripts
270+
pkg.scripts = { ...(pkg.scripts || {}), ...opts.scripts }
267271
}
268272

273+
// TODO: to test the empty string, we need to stub git.author()
269274
pkg.author = opts.author || ''
270275
pkg.license = opts.license
271276

272277
if (opts.repository) {
273-
pkg.repository = {
274-
type: 'git',
275-
url: opts.repository
278+
if (typeof opts.repository === 'string') {
279+
pkg.repository = {
280+
type: 'git',
281+
url: opts.repository
282+
}
283+
} else {
284+
pkg.repository = opts.repository
276285
}
277286
}
278287

@@ -306,6 +315,7 @@ async function format (opts, pkg = {}) {
306315
}
307316

308317
module.exports.write = write
318+
// TODO: look at https://npm.im/json-file-plus for writing
309319
async function write (pkgPath, opts, pkg, { log } = {}) {
310320
// Write package json
311321
log.info(`Writing package.json\n${pkgPath}`)
@@ -334,3 +344,7 @@ async function write (pkgPath, opts, pkg, { log } = {}) {
334344
// Read full package back to return
335345
return fs.readJSON(pkgPath)
336346
}
347+
348+
function uniquify (arr = []) {
349+
return [...new Set(arr)]
350+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"loggerr": "^3.0.0",
3838
"npm-package-arg": "^8.0.1",
3939
"opta": "^0.0.6",
40+
"read-package-json": "^3.0.0",
4041
"safe-parse-list": "^0.1.1",
4142
"validate-npm-package-name": "^3.0.0"
4243
},
File renamed without changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@test/existing",
3+
"version": "0.0.0",
4+
"description": "A test package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "exit 0"
8+
},
9+
"author": "Test <tester@example.com>",
10+
"license": "ISC",
11+
"keywords": ["foo", "bar"]
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@test/existing",
3+
"version": "0.0.0",
4+
"description": "A test package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "exit 0"
8+
},
9+
"author": "Test <tester@example.com>",
10+
"license": "ISC"
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@test/existing",
3+
"version": "0.0.0",
4+
"description": "A test package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "exit 0"
8+
},
9+
"author": "Test <tester@example.com>",
10+
"license": "ISC",
11+
"repository": "https://git-landfill.com/garbage"
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@test/existing",
3+
"version": "0.0.0",
4+
"description": "A test package",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "exit 0"
8+
},
9+
"author": "Test <tester@example.com>",
10+
"license": "ISC",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://git-landfill.com/garbage"
14+
}
15+
}

0 commit comments

Comments
 (0)