mirror of
https://github.com/honojs/hono.git
synced 2024-11-21 10:08:58 +01:00
chore(build): validate if exporting is correct in package.json
and jsr.json
(#3638)
* feat(build): for both exports to be the same * some fix * fix exclude of coverage * update * stylish error message and add comment * revert auto lint * chore: format
This commit is contained in:
parent
48d2adcf73
commit
a6ccfa29ca
@ -7,14 +7,15 @@
|
||||
|
||||
/// <reference types="bun-types/bun" />
|
||||
|
||||
import fs, { write } from 'fs'
|
||||
import path from 'path'
|
||||
import arg from 'arg'
|
||||
import { $, stdout } from 'bun'
|
||||
import { build } from 'esbuild'
|
||||
import type { Plugin, PluginBuild, BuildOptions } from 'esbuild'
|
||||
import * as glob from 'glob'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { removePrivateFields } from './remove-private-fields'
|
||||
import { $, stdout } from 'bun'
|
||||
import { validateExports } from './validate-exports'
|
||||
|
||||
const args = arg({
|
||||
'--watch': Boolean,
|
||||
@ -22,6 +23,14 @@ const args = arg({
|
||||
|
||||
const isWatch = args['--watch'] || false
|
||||
|
||||
const readJsonExports = (path: string) => JSON.parse(fs.readFileSync(path, 'utf-8')).exports
|
||||
|
||||
const [packageJsonExports, jsrJsonExports] = ['./package.json', './jsr.json'].map(readJsonExports)
|
||||
|
||||
// Validate exports of package.json and jsr.json
|
||||
validateExports(packageJsonExports, jsrJsonExports, 'jsr.json')
|
||||
validateExports(jsrJsonExports, packageJsonExports, 'package.json')
|
||||
|
||||
const entryPoints = glob.sync('./src/**/*.ts', {
|
||||
ignore: ['./src/**/*.test.ts', './src/mod.ts', './src/middleware.ts', './src/deno/**/*.ts'],
|
||||
})
|
||||
|
31
build/validate-exports.test.ts
Normal file
31
build/validate-exports.test.ts
Normal file
@ -0,0 +1,31 @@
|
||||
/// <reference types="vitest/globals" />
|
||||
|
||||
import { validateExports } from './validate-exports'
|
||||
|
||||
const mockExports1 = {
|
||||
'./a': './a.ts',
|
||||
'./b': './b.ts',
|
||||
'./c/a': './c.ts',
|
||||
'./d/*': './d/*.ts',
|
||||
}
|
||||
|
||||
const mockExports2 = {
|
||||
'./a': './a.ts',
|
||||
'./b': './b.ts',
|
||||
'./c/a': './c.ts',
|
||||
'./d/a': './d/a.ts',
|
||||
}
|
||||
|
||||
const mockExports3 = {
|
||||
'./a': './a.ts',
|
||||
'./c/a': './c.ts',
|
||||
'./d/*': './d/*.ts',
|
||||
}
|
||||
|
||||
describe('validateExports', () => {
|
||||
it('Works', async () => {
|
||||
expect(() => validateExports(mockExports1, mockExports1, 'package.json')).not.toThrowError()
|
||||
expect(() => validateExports(mockExports1, mockExports2, 'jsr.json')).not.toThrowError()
|
||||
expect(() => validateExports(mockExports1, mockExports3, 'package.json')).toThrowError()
|
||||
})
|
||||
})
|
37
build/validate-exports.ts
Normal file
37
build/validate-exports.ts
Normal file
@ -0,0 +1,37 @@
|
||||
export const validateExports = (
|
||||
source: Record<string, unknown>,
|
||||
target: Record<string, unknown>,
|
||||
fileName: string
|
||||
) => {
|
||||
const isEntryInTarget = (entry: string): boolean => {
|
||||
if (entry in target) {
|
||||
return true
|
||||
}
|
||||
|
||||
// e.g., "./utils/*" -> "./utils"
|
||||
const wildcardPrefix = entry.replace(/\/\*$/, '')
|
||||
if (entry.endsWith('/*')) {
|
||||
return Object.keys(target).some(
|
||||
(targetEntry) =>
|
||||
targetEntry.startsWith(wildcardPrefix + '/') && targetEntry !== wildcardPrefix
|
||||
)
|
||||
}
|
||||
|
||||
const separatedEntry = entry.split('/')
|
||||
while (separatedEntry.length > 0) {
|
||||
const pattern = `${separatedEntry.join('/')}/*`
|
||||
if (pattern in target) {
|
||||
return true
|
||||
}
|
||||
separatedEntry.pop()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Object.keys(source).forEach((sourceEntry) => {
|
||||
if (!isEntryInTarget(sourceEntry)) {
|
||||
throw new Error(`Missing "${sourceEntry}" in '${fileName}'`)
|
||||
}
|
||||
})
|
||||
}
|
1
jsr.json
1
jsr.json
@ -80,6 +80,7 @@
|
||||
"./testing": "./src/helper/testing/index.ts",
|
||||
"./dev": "./src/helper/dev/index.ts",
|
||||
"./ws": "./src/helper/websocket/index.ts",
|
||||
"./conninfo": "./src/helper/conninfo/index.ts",
|
||||
"./utils/body": "./src/utils/body.ts",
|
||||
"./utils/buffer": "./src/utils/buffer.ts",
|
||||
"./utils/color": "./src/utils/color.ts",
|
||||
|
@ -39,6 +39,11 @@
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/cjs/index.js"
|
||||
},
|
||||
"./request": {
|
||||
"types": "./dist/types/request.d.ts",
|
||||
"import": "./dist/request.js",
|
||||
"require": "./dist/cjs/request.js"
|
||||
},
|
||||
"./types": {
|
||||
"types": "./dist/types/types.d.ts",
|
||||
"import": "./dist/types.js",
|
||||
@ -387,6 +392,9 @@
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"request": [
|
||||
"./dist/types/request"
|
||||
],
|
||||
"types": [
|
||||
"./dist/types/types"
|
||||
],
|
||||
|
@ -8,7 +8,7 @@ export default defineConfig({
|
||||
},
|
||||
test: {
|
||||
globals: true,
|
||||
include: ['**/src/**/(*.)+(spec|test).+(ts|tsx|js)', '**/scripts/**/(*.)+(spec|test).+(ts|tsx|js)'],
|
||||
include: ['**/src/**/(*.)+(spec|test).+(ts|tsx|js)', '**/scripts/**/(*.)+(spec|test).+(ts|tsx|js)', '**/build/**/(*.)+(spec|test).+(ts|tsx|js)'],
|
||||
exclude: [...configDefaults.exclude, '**/sandbox/**', '**/*.case.test.+(ts|tsx|js)'],
|
||||
setupFiles: ['./.vitest.config/setup-vitest.ts'],
|
||||
coverage: {
|
||||
@ -20,7 +20,7 @@ export default defineConfig({
|
||||
...(configDefaults.coverage.exclude ?? []),
|
||||
'benchmarks',
|
||||
'runtime-tests',
|
||||
'build.ts',
|
||||
'build/build.ts',
|
||||
'src/test-utils',
|
||||
'perf-measures',
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user