Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Adds a lookup for type files when generating the package.json #92

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,9 @@ When installing locally, npm links bins into `node_modules/.bin`, which
is in the `PATH` environ when npm runs scripts. When
installing globally, they are linked into `{prefix}/bin`, which is
presumably in the `PATH` environment variable.

### `types` and `flow` fields

If you do not have a `types` or `flow` field, then it will check if
corresponding `*.d.ts` or `*.flow.js` files exist for your package
entry file and add them to the `package.json`.
28 changes: 27 additions & 1 deletion read-json.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import("fs")} */
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can totally drop this, but it finalizes the auto-complete in the file

var fs
try {
fs = require('graceful-fs')
Expand Down Expand Up @@ -25,7 +26,8 @@ readJson.extraSet = [
readme,
mans,
bins,
githead
githead,
fillTypes
]

var typoWarned = {}
Expand Down Expand Up @@ -437,6 +439,30 @@ function final (file, data, log, strict, cb) {
})
}

function fillTypes (file, data, cb) {
var index = data.main ? data.main : 'index.js'
function switchExt (file, ext) {
var extless = path.join(path.dirname(file), path.dirname(file), path.basename(file, path.extname(file)))
return './' + extless + '.' + ext
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
}

var dts = switchExt(index, 'd.ts')
var dtsPath = path.join(path.dirname(file), dts)
var hasDTSFields = 'types' in data || 'typings' in data
if (!hasDTSFields && fs.existsSync(dtsPath)) {
data.types = dts
}

var flow = switchExt(index, 'flow.js')
var flowPath = path.join(path.dirname(file), flow)
var hasFlowField = 'flow' in data
if (!hasFlowField && fs.existsSync(flowPath)) {
data.flow = flow
}

cb(null, data)
}

function makePackageId (data) {
var name = cleanString(data.name)
var ver = cleanString(data.version)
Expand Down
57 changes: 57 additions & 0 deletions test/fill-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const t = require('tap')
const read = require('../')
const { resolve } = require('path')

t.test('adds types with a custom main field', t => {
const fixture = resolve(__dirname, 'fixtures/types/custom-dts/two-new-fields.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
main: './custom-path.js',
types: './custom-path.d.ts',
flow: './custom-path.flow.js'
})
t.end()
})
})

t.test('handles the inferred index.js', t => {
const fixture = resolve(__dirname, 'fixtures/types/default-dts/inferred.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
types: './index.d.ts'
})
t.end()
})
})

t.test('handles not overwriting existing fields', t => {
const fixture = resolve(__dirname, 'fixtures/types/with-field/has-types-field.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
types: '@types/express',
flow: './index.flow.js'
})
t.end()
})
})

t.test('does not add types fields if not present', t => {
const fixture = resolve(__dirname, 'fixtures/readmes/package.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.false(data.types, 'types field should not be added')
t.false(data.flow, 'flow field should not be added')
t.end()
})
})
1 change: 1 addition & 0 deletions test/fixtures/types/custom-dts/custom-path.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty because implementation isn't important
1 change: 1 addition & 0 deletions test/fixtures/types/custom-dts/custom-path.flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty because implementation isn't important
5 changes: 5 additions & 0 deletions test/fixtures/types/custom-dts/two-new-fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "two-new-fields",
"main": "./custom-path.js"
}
1 change: 1 addition & 0 deletions test/fixtures/types/default-dts/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty because implementation isn't important
4 changes: 4 additions & 0 deletions test/fixtures/types/default-dts/inferred.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "no-main"
}
5 changes: 5 additions & 0 deletions test/fixtures/types/with-field/has-types-field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "has-types-reference",
"types": "@types/express"
}
1 change: 1 addition & 0 deletions test/fixtures/types/with-field/index.flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty because implementation isn't important