Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace custom tsConfig resolver with xtends #175

Merged
merged 3 commits into from
Oct 31, 2024
Merged
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
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"publish:rc": "npm publish --no-git-tag-version --tag rc"
},
"dependencies": {
"@topoconfig/extends": "^0.16.2",
"depseek": "^0.4.1",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
Expand All @@ -53,8 +54,8 @@
"devDependencies": {
"@qiwi/npm-run-all": "^4.1.7",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.13",
"@types/node": "^22.7.4",
"@types/jest": "^29.5.14",
"@types/node": "^22.8.5",
"@types/semver": "^7.5.8",
"cpy-cli": "^5.0.0",
"cross-env": "^7.0.3",
Expand All @@ -64,20 +65,20 @@
"esbuild-plugin-extract-helpers": "^0.0.6",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-qiwi": "^2.1.3",
"eslint-config-qiwi": "^2.1.7",
"glob-runner": "^2.1.1",
"jest": "^29.7.0",
"minimist": "^1.2.8",
"mkdirp": "^3.0.1",
"prettier": "^3.3.3",
"prettier-config-qiwi": "^2.1.2",
"prettier-config-qiwi": "^3.0.0",
"rimraf": "^5.0.8",
"semver": "^7.6.3",
"tempy": "^3.1.0",
"ts-jest": "^29.2.5",
"typedoc": "^0.26.8",
"typescript": "^5.6.2",
"zx": "^8.1.8"
"typedoc": "^0.26.10",
"typescript": "^5.6.3",
"zx": "^8.1.9"
},
"repository": {
"type": "git",
Expand Down
10 changes: 5 additions & 5 deletions src/main/ts/finder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, join, resolve } from 'node:path'
import path from 'node:path'
import {asArray, extToGlob, glob, readJson, resolveTsConfig} from './util'
import {DEFAULT_FIX_OPTIONS} from "./options";

Expand All @@ -18,7 +18,7 @@ export const getTsconfigTargets = (
cwd: string,
): string[] =>
asArray(tsconfig).reduce<string[]>((targets, file) => {
const tsconfigJson = resolveTsConfig(resolve(cwd, file))
const tsconfigJson = resolveTsConfig(path.resolve(cwd, file))
const outDir = tsconfigJson?.compilerOptions?.outDir
const module = tsconfigJson?.compilerOptions?.module.toLowerCase?.()

Expand Down Expand Up @@ -103,7 +103,7 @@ const getExternalPackages = async (cwd: string): Promise<TPackageEntry[]> =>
})
.then(async (files: string[]) => Promise.all(files.map(async file => {
const manifest = await readJson(file)
const root = dirname(file)
const root = path.dirname(file)
const exports = getExportsEntries(manifest.exports)

return {
Expand All @@ -121,7 +121,7 @@ const getPackageEntryPoints = async ({name, exports, root, main}: TPackageEntry)
(await Promise.all(exports.map(([key, values]) =>
Promise.all(values.map(async(value) =>
(await glob(value, {cwd: root, onlyFiles: true, absolute: false}))
.map(file => join(file)
.map(file => path.join(file)
.replace(
resolvePrefix('.', value),
resolvePrefix(name, key)))
Expand All @@ -143,5 +143,5 @@ const resolvePrefix = (prefix: string, pattern?: string): string => {
}
}

return join(prefix, _pattern)
return path.join(prefix, _pattern)
}
10 changes: 5 additions & 5 deletions src/main/ts/fixes/fix-module-ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, resolve } from 'node:path'
import path from 'node:path'
import { patchRefs } from 'depseek'
import { TFixer } from '../interface'
import { unixify } from '../util'
Expand All @@ -23,8 +23,8 @@ export const resolveDependency = (
cwd: string,
jsExt: string[] = DEFAULT_FIX_OPTIONS.jsExt,
): string => {
const dir = dirname(parent)
const nmdir = resolve(cwd, 'node_modules')
const dir = path.dirname(parent)
const nmdir = path.resolve(cwd, 'node_modules')
const bases = /^\..+\.[^./\\]+$/.test(nested)
? [nested, nested.replace(/\.[^./\\]+$/, '')]
: [nested]
Expand All @@ -34,8 +34,8 @@ export const resolveDependency = (
}, [])

return (
variants.find((f) => files.includes(unixify(resolve(dir, f)))) ||
variants.find((f) => files.includes(unixify(resolve(nmdir, f)))) ||
variants.find((f) => files.includes(unixify(path.resolve(dir, f)))) ||
variants.find((f) => files.includes(unixify(path.resolve(nmdir, f)))) ||
nested
)
}
6 changes: 3 additions & 3 deletions src/main/ts/fixes/fix-sourcemap-ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {basename} from 'node:path'
import path from 'node:path'
import {TFixer} from '../interface'

export const fixSourceMapRef: TFixer = (ctx) => {
Expand All @@ -7,8 +7,8 @@ export const fixSourceMapRef: TFixer = (ctx) => {
originName === filename
? contents
: contents.replace(
`//# sourceMappingURL=${basename(originName)}.map`,
`//# sourceMappingURL=${basename(filename)}.map`
`//# sourceMappingURL=${path.basename(originName)}.map`,
`//# sourceMappingURL=${path.basename(filename)}.map`
)

return {...ctx, contents: _contents}
Expand Down
21 changes: 4 additions & 17 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { dirname, resolve } from 'node:path'

import fse from 'fs-extra'
import json5 from 'json5'
import { populateSync } from '@topoconfig/extends'

import { TSConfig } from './interface'

Expand All @@ -24,21 +23,9 @@ export const remove = fse.unlinkSync

export const unixify = (path: string): string => path.replace(/\\/g, '/')

export const resolveTsConfig = (file: string): TSConfig => {
const data = readJson<TSConfig>(file)

if (data.extends) {
const parent = resolveTsConfig(resolve(dirname(file), data.extends))

return {
...parent,
...data,
compilerOptions: { ...parent.compilerOptions, ...data.compilerOptions },
}
}

return data
}
export const resolveTsConfig = (file: string): TSConfig => populateSync(file, {
compilerOptions: 'merge'
})

export const omitUndefinedKeys = <T extends Record<string, any>>(obj: T): T =>
Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== undefined)) as T
Expand Down
36 changes: 18 additions & 18 deletions src/test/ts/fix.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as cp from 'node:child_process'
import { dirname, resolve } from 'node:path'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import fse from 'fs-extra'
Expand All @@ -19,9 +19,9 @@
import { normalizeOptions} from '../../main/ts/options'
import { read, readJson, glob } from '../../main/ts/util'

const __dirname = dirname(fileURLToPath(import.meta.url))
const fakeProject = resolve(__dirname, '../fixtures/ts-project')
const nestjsSwaggerProject = resolve(__dirname, '../fixtures/nestjs-swagger-project')
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const fakeProject = path.resolve(__dirname, '../fixtures/ts-project')
const nestjsSwaggerProject = path.resolve(__dirname, '../fixtures/nestjs-swagger-project')

describe('normalizeOptions()', () => {
it('merges DEFAULT_FIX_OPTIONS with specified opts input', () => {
Expand Down Expand Up @@ -51,7 +51,7 @@
describe('fix()', () => {
it('patches ts sources as required by opts', async () => {
const temp = temporaryDirectory()
const cwd = resolve(temp)
const cwd = path.resolve(temp)

fse.copySync(fakeProject, cwd)

Expand All @@ -64,15 +64,15 @@
filenameVar: false,
})

expect(read(resolve(temp, 'src/main/ts/index.ts'))).toMatchSnapshot()
expect(read(resolve(temp, 'src/main/ts/index-ref.ts'))).toMatchSnapshot()
expect(read(resolve(temp, 'src/main/ts/index-ref-2/index.ts'))).toMatchSnapshot()
expect(read(path.resolve(temp, 'src/main/ts/index.ts'))).toMatchSnapshot()
expect(read(path.resolve(temp, 'src/main/ts/index-ref.ts'))).toMatchSnapshot()
expect(read(path.resolve(temp, 'src/main/ts/index-ref-2/index.ts'))).toMatchSnapshot()
})

it('patches target (tsc-compiled) files as required by opts', async () => {
const temp = temporaryDirectory()
const cwd = resolve(temp)
const out = resolve(temp)
const cwd = path.resolve(temp)
const out = path.resolve(temp)

fse.copySync(fakeProject, cwd)

Expand All @@ -86,7 +86,7 @@
sourceMap: true
})

const res = resolve(temp, 'target/es6/index.mjs')
const res = path.resolve(temp, 'target/es6/index.mjs')
const contents = read(res)

// NodeJS v16.14.0+ requires {assert: {type: 'json'}} for json imports
Expand All @@ -96,13 +96,13 @@
fse.writeFileSync(res, contents.replace(` assert { type: 'json' };`, ''))
}

expect(read(resolve(temp, 'target/es6/index.d.ts'))).toMatchSnapshot()
expect(read(resolve(temp, 'target/es6/only-types.mjs'))).toMatchSnapshot()
expect(read(resolve(temp, 'target/es6/index.mjs'))).toMatchSnapshot()
expect(readJson(resolve(temp, 'target/es6/index.mjs.map')).file).toBe('index.mjs')
expect(read(path.resolve(temp, 'target/es6/index.d.ts'))).toMatchSnapshot()
expect(read(path.resolve(temp, 'target/es6/only-types.mjs'))).toMatchSnapshot()
expect(read(path.resolve(temp, 'target/es6/index.mjs'))).toMatchSnapshot()
expect(readJson(path.resolve(temp, 'target/es6/index.mjs.map')).file).toBe('index.mjs')
expect(
cp
.execSync(`node --experimental-top-level-await --experimental-json-modules ${res}`, {

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium test

This shell command depends on an uncontrolled
absolute path
.
cwd: temp,
env: {},
timeout: 5000,
Expand All @@ -128,7 +128,7 @@
absolute: true,
},
)
const file = resolve(fakeProject, 'target/es6/index.js')
const file = path.resolve(fakeProject, 'target/es6/index.js')
const content = read(file)

it('fixRelativeModuleReferences() appends file ext to module refs except for the ones that declare "exports" in pkg.json', () => {
Expand Down Expand Up @@ -182,7 +182,7 @@
})

it('fixContents() replaces `.` with `./index.cjs`', () => {
const file = resolve(fakeProject, 'target/es6/index-ref.js')
const file = path.resolve(fakeProject, 'target/es6/index-ref.js')
const content = read(file)
const _files = files.map(f => f.replace(/\.js$/, '.cjs'))
expect(
Expand All @@ -202,7 +202,7 @@
})

it('fixContents() patches `require` args', () => {
const file = resolve(nestjsSwaggerProject, 'event.dto.js')
const file = path.resolve(nestjsSwaggerProject, 'event.dto.js')
const content = read(file)
const files = glob.sync(
[
Expand Down
Loading