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 all 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`.
29 changes: 28 additions & 1 deletion read-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var fs = require('fs')

var path = require('path')

var glob = require('glob')
Expand All @@ -21,6 +20,7 @@ readJson.extraSet = [
mans,
bins,
githead,
fillTypes,
]

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

function fillTypes (file, data, cb) {
var index = data.main ? data.main : 'index.js'
if (data.exports && typeof data.exports === 'string') index = data.exports
if (data.exports && data.exports['.']) index = data.exports['.']

function switchExt (file, ext) {
var extless = path.join(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
105 changes: 105 additions & 0 deletions test/fill-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 subpaths and starting with ./', t => {
const fixture = resolve(__dirname, 'fixtures/types/subpaths/subpath.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
main: './a/b/c.js',
types: './a/b/c.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()
})
})

// https://nodejs.org/api/esm.html#esm_writing_dual_packages_while_avoiding_or_minimizing_hazards

t.test('handles esm modules', t => {
const fixture = resolve(__dirname, 'fixtures/types/esmodule-exports/exports.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
types: './a/b/c.d.ts'
})

t.false(data.flow, 'flow field should not be added')
t.end()
})
})

// https://nodejs.org/api/esm.html#esm_exports_sugar

t.test('handles esm modules with sugared exports', t => {
const fixture = resolve(__dirname, 'fixtures/types/esmodule-exports-sugar/sugar.json')
read(fixture, (er, data) => {
if (er) {
throw er
}
t.match(data, {
flow: './a/b.flow.js'
})

t.false(data.types, 'types 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"
}
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/types/esmodule-exports-sugar/sugar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exports": "./a/b.js"
}
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions test/fixtures/types/esmodule-exports/exports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"exports": {
".": "./a/b/c.js",
"./a": "./a.mjs"
}
}
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions test/fixtures/types/subpaths/subpath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "subpaths",
"main": "./a/b/c.js"
}
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