diff --git a/src/index.ts b/src/index.ts index 7039aa9..3f77af5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,7 @@ export type ResolveTsPathOptions = Omit< * in your transpiled JavaScript code. */ export async function resolveTsPaths( - options: ResolveTsPathOptions = {} + options: ResolveTsPathOptions = {}, ): Promise { const { project = "tsconfig.json", @@ -30,7 +30,7 @@ export async function resolveTsPaths( const programPaths = resolvePaths({ project, src, out }, tsConfig); const aliases = computeAliases( programPaths.basePath, - tsConfig?.options?.paths ?? {} + tsConfig?.options?.paths ?? {}, ); const files = getFilesToProcess(programPaths.outPath, ext); const changes = generateChanges(files, aliases, programPaths); diff --git a/src/main.ts b/src/main.ts index 0b8749a..7def9ff 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,7 +32,7 @@ function main() { const aliases = computeAliases( programPaths.basePath, - tsConfig?.options?.paths ?? {} + tsConfig?.options?.paths ?? {}, ); logger.fancyParams("aliases", aliases); @@ -42,7 +42,7 @@ function main() { const changes = generateChanges(files, aliases, programPaths); logger.fancyParams( "fileChanges", - changes.map(({ file, changes }) => ({ file, changes })) + changes.map(({ file, changes }) => ({ file, changes })), ); if (options.noEmit) { @@ -50,7 +50,7 @@ function main() { bold("resolve-tspaths:"), "discovered", changes.length, - "file(s) for change (none actually changed since --noEmit was given)" + "file(s) for change (none actually changed since --noEmit was given)", ); } else { applyChanges(changes); @@ -58,14 +58,14 @@ function main() { bold("resolve-tspaths:"), "changed", changes.length, - "file(s)" + "file(s)", ); } } catch (error: any) { if (error instanceof StepError) { logger.fancyError( `Error during step '${bold(error.step)}'`, - error.message + error.message, ); } else throw error; } diff --git a/src/steps/computeAliases.ts b/src/steps/computeAliases.ts index 4a21034..24a6d88 100644 --- a/src/steps/computeAliases.ts +++ b/src/steps/computeAliases.ts @@ -8,7 +8,7 @@ import { InvalidAliasError } from "~/utils/errors"; */ export function computeAliases( basePath: string, - paths: { [key: string]: string[] } + paths: { [key: string]: string[] }, ): Alias[] { const regex = /\*$/; @@ -16,7 +16,7 @@ export function computeAliases( alias, prefix: alias.replace(regex, ""), aliasPaths: paths[alias].map((path: string) => - resolve(basePath, path.replace(regex, "")) + resolve(basePath, path.replace(regex, "")), ), })); diff --git a/src/steps/createProgram.ts b/src/steps/createProgram.ts index e641a62..f43f592 100644 --- a/src/steps/createProgram.ts +++ b/src/steps/createProgram.ts @@ -24,7 +24,7 @@ export function createProgram() { .option( "--ext ", "space-delimited list of file extensions to process", - DEFAULT_EXTENSIONS as any + DEFAULT_EXTENSIONS as any, ) .option("--verbose", "output logs", false) .option("--noEmit", "changes will not be emitted", false); diff --git a/src/steps/generateChanges.ts b/src/steps/generateChanges.ts index 203a51e..7717c04 100644 --- a/src/steps/generateChanges.ts +++ b/src/steps/generateChanges.ts @@ -10,7 +10,7 @@ export const IMPORT_EXPORT_REGEX = /((?:require\(|require\.resolve\(|import\()|(?:import|export)\s+(?:[\s\S]*?from\s+)?)['"]([^'"]*)['"]\)?/g; export const ESM_IMPORT_EXPORT_REGEX = - /(?:(?:import\()|(?:import|export)\s+(?:[\s\S]*?from\s+)?)['"]([^'"]*)['"]\)?/g + /(?:(?:import\()|(?:import|export)\s+(?:[\s\S]*?from\s+)?)['"]([^'"]*)['"]\)?/g; export const COMMONJS_IMPORT_EXPORT_REGEX = /(?:(?:require\(|require\.resolve\()\s+)['"]([^'"]*)['"]\)/g; @@ -36,7 +36,7 @@ const MODULE_EXTS = [ export function generateChanges( files: string[], aliases: Alias[], - programPaths: Pick + programPaths: Pick, ): Change[] { const changeList: Change[] = []; @@ -44,7 +44,7 @@ export function generateChanges( const { changed, text, changes } = replaceAliasPathsInFile( file, aliases, - programPaths + programPaths, ); if (!changed) continue; @@ -65,7 +65,7 @@ export function generateChanges( export function replaceAliasPathsInFile( filePath: string, aliases: Alias[], - programPaths: Pick + programPaths: Pick, ): { changed: boolean; text: string; changes: TextChange[] } { if (!existsSync(filePath)) throw new FileNotFoundError(replaceAliasPathsInFile.name, filePath); @@ -87,7 +87,7 @@ export function replaceAliasPathsInFile( filePath, aliases, programPaths, - esmImport + esmImport, ); if (!result.replacement) return original; @@ -103,7 +103,7 @@ export function replaceAliasPathsInFile( result.replacement + original.substring(index + importSpecifier.length) ); - } + }, ); return { changed: originalText !== newText, text: newText, changes }; @@ -123,7 +123,7 @@ export function aliasToRelativePath( outputFile: string, aliases: Alias[], { srcPath, outPath }: Pick, - esModule?: boolean + esModule?: boolean, ): { file: string; original: string; replacement?: string } { const sourceFile = resolve(srcPath, relative(outPath, outputFile)); const sourceFileDirectory = dirname(sourceFile); @@ -133,15 +133,15 @@ export function aliasToRelativePath( importSpecifier.startsWith("./") || importSpecifier.startsWith("../"); const matchingAliases = aliases.filter(({ prefix }) => - importSpecifier.startsWith(prefix) + importSpecifier.startsWith(prefix), ); const absoluteImportPaths = importPathIsRelative ? [resolve(sourceFileDirectory, importSpecifier)] : matchingAliases.flatMap(({ prefix, aliasPaths }) => aliasPaths.map((aliasPath) => - resolve(aliasPath, importSpecifier.replace(prefix, "")) - ) + resolve(aliasPath, importSpecifier.replace(prefix, "")), + ), ); const absoluteImport = absoluteImportPaths.reduce "./" + m + (m) => "./" + m, ); const relativePathJsExtension = prefixedRelativePath.replace( @@ -179,11 +179,11 @@ export function aliasToRelativePath( .replace(/\.ts$/, ".js") .replace(/\.tsx$/, ".jsx") .replace(/\.mts$/, ".mjs") - .replace(/\.cts$/, ".cjs") + .replace(/\.cts$/, ".cjs"), ); const jsxFileExists = isFile( - resolve(outputFileDirectory, relativePathJsExtension) + resolve(outputFileDirectory, relativePathJsExtension), ); const relativePathJsxExtension = jsxFileExists ? relativePathJsExtension @@ -205,10 +205,10 @@ export function aliasToRelativePath( */ function resolveImportPath(importPath: string) { const importPathTs = importPath.replace(/\.[^/.]*js[^/.]*$/, (match) => - match.replace("js", "ts") + match.replace("js", "ts"), ); const importPathWithExtensions = MODULE_EXTS.map( - (ext) => `${importPath}${ext}` + (ext) => `${importPath}${ext}`, ); const possiblePaths = [importPath, importPathTs, ...importPathWithExtensions]; @@ -228,7 +228,7 @@ function resolveImportPath(importPath: string) { : []; const existingIndexPath = possiblePathsAsDirectory.find((path) => - isFile(path) + isFile(path), ); if (existingIndexPath) { diff --git a/src/steps/resolvePaths.ts b/src/steps/resolvePaths.ts index d19f88e..7a92ee7 100644 --- a/src/steps/resolvePaths.ts +++ b/src/steps/resolvePaths.ts @@ -9,7 +9,7 @@ import type { ProgramOptions, ProgramPaths, TSConfig } from "~/types"; */ export function resolvePaths( options: Pick, - tsConfig: TSConfig + tsConfig: TSConfig, ): ProgramPaths { const { baseUrl = "", outDir, paths } = tsConfig.options ?? {}; @@ -17,7 +17,7 @@ export function resolvePaths( if (!out) { throw new StepError( resolvePaths.name, - `Output directory must be specified using either the --out option or in tsconfig` + `Output directory must be specified using either the --out option or in tsconfig`, ); } diff --git a/src/utils/errors.ts b/src/utils/errors.ts index 1683341..1258f8e 100644 --- a/src/utils/errors.ts +++ b/src/utils/errors.ts @@ -1,5 +1,8 @@ export class StepError extends Error { - constructor(public readonly step: string, message: string) { + constructor( + public readonly step: string, + message: string, + ) { super(message); } } @@ -8,7 +11,7 @@ export class FileError extends StepError { constructor( public readonly step: string, public readonly path: string, - message: string + message: string, ) { super(step, `Error processing ${path}: ${message}`); } @@ -21,13 +24,19 @@ export class FileNotFoundError extends FileError { } export class TSConfigPropertyError extends StepError { - constructor(public readonly step: string, public readonly property: string) { + constructor( + public readonly step: string, + public readonly property: string, + ) { super(step, `${property} is not set in tsconfig`); } } export class InvalidAliasError extends StepError { - constructor(public readonly step: string, public readonly alias: string) { + constructor( + public readonly step: string, + public readonly alias: string, + ) { super(step, `The alias ${alias} is not permitted`); } } diff --git a/test/steps/generateChanges.test.ts b/test/steps/generateChanges.test.ts index 6ac57d8..7d33a39 100644 --- a/test/steps/generateChanges.test.ts +++ b/test/steps/generateChanges.test.ts @@ -40,7 +40,7 @@ describe("steps/generateChanges", () => { it("matches import { as } statements", () => { const result = regex.exec( - `import { package as myPackage } from '../package';` + `import { package as myPackage } from '../package';`, ); expect(result).toMatchInlineSnapshot(` [ @@ -80,7 +80,7 @@ describe("steps/generateChanges", () => { "../package", ] `); - }) + }); it("matches export * statements", () => { const result = regex.exec(`export * from 'package';`); @@ -117,7 +117,7 @@ describe("steps/generateChanges", () => { it("matches export { as } statements", () => { const result = regex.exec( - `export { package as myPackage } from '../package';` + `export { package as myPackage } from '../package';`, ); expect(result).toMatchInlineSnapshot(` [ @@ -157,7 +157,7 @@ describe("steps/generateChanges", () => { "../package", ] `); - }) + }); it("matches require statements", () => { const result = regex.exec(`require('package');`); @@ -183,7 +183,7 @@ describe("steps/generateChanges", () => { it("matches const require.resolve statements", () => { const result = regex.exec( - `const package = require.resolve('../package');` + `const package = require.resolve('../package');`, ); expect(result).toMatchInlineSnapshot(` [ @@ -196,7 +196,7 @@ describe("steps/generateChanges", () => { it("matches const {} require statements", () => { const result = regex.exec( - `const { package } = require('~/package/package');` + `const { package } = require('~/package/package');`, ); expect(result).toMatchInlineSnapshot(` [ @@ -239,7 +239,7 @@ describe("steps/generateChanges", () => { "path", "test/fixtures/change/out/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -255,7 +255,7 @@ describe("steps/generateChanges", () => { "~/non-existent", "test/fixtures/change/out/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -271,7 +271,7 @@ describe("steps/generateChanges", () => { "~/root", "test/fixtures/change/out/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -289,7 +289,7 @@ describe("steps/generateChanges", () => { "test/fixtures/change/out/imports.js", aliases, programPaths, - true + true, ); expect(result).toMatchInlineSnapshot(` @@ -307,7 +307,7 @@ describe("steps/generateChanges", () => { "test/fixtures/change/out/imports.js", aliases, programPaths, - true + true, ); expect(result).toMatchInlineSnapshot(` @@ -325,7 +325,7 @@ describe("steps/generateChanges", () => { "test/fixtures/change/out/imports.js", aliases, programPaths, - true + true, ); expect(result).toMatchInlineSnapshot(` @@ -342,7 +342,7 @@ describe("steps/generateChanges", () => { "~/alternate", "test/fixtures/change/out/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -359,7 +359,7 @@ describe("steps/generateChanges", () => { "~/nested/nested-path", "test/fixtures/change/out/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -376,7 +376,7 @@ describe("steps/generateChanges", () => { "~/root", "test/fixtures/change/out/nested/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -393,7 +393,7 @@ describe("steps/generateChanges", () => { "../..", "test/fixtures/change/out/nested/imports.js", [{ alias: "*", prefix: "", aliasPaths: [`${root}/src`] }], - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -409,7 +409,7 @@ describe("steps/generateChanges", () => { "~/alternate", "test/fixtures/change/out/nested/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -426,7 +426,7 @@ describe("steps/generateChanges", () => { "~/data.json", "test/fixtures/change/out/nested/imports.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -443,7 +443,7 @@ describe("steps/generateChanges", () => { "~/directory", "test/fixtures/change/out/directory/file.js", aliases, - programPaths + programPaths, ); expect(result).toMatchInlineSnapshot(` @@ -461,7 +461,7 @@ describe("steps/generateChanges", () => { "test/fixtures/change/out/directory/file.js", aliases, programPaths, - true + true, ); expect(result).toMatchInlineSnapshot(` @@ -479,7 +479,7 @@ describe("steps/generateChanges", () => { "test/fixtures/change/out/directory/file.js", aliases, programPaths, - true + true, ); expect(result).toMatchInlineSnapshot(` @@ -511,7 +511,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/no-change.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(false); expect(results.changes).toHaveLength(0); @@ -521,7 +521,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/imports.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -565,7 +565,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/exports.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -603,7 +603,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/nested/index.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -647,7 +647,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/directory/file.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -680,7 +680,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/import.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toHaveLength(1); @@ -693,10 +693,10 @@ describe("steps/generateChanges", () => { ] `); expect(results.text).not.toContain( - 'const { ./sameName } = require("sameName");' + 'const { ./sameName } = require("sameName");', ); expect(results.text).toContain( - 'const { sameName } = require("./sameName");' + 'const { sameName } = require("./sameName");', ); }); @@ -716,7 +716,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/imports.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.text).toContain(`import { alpha } from "./alpha.js"`); @@ -746,7 +746,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/exports.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.text).toContain(`export { alpha } from "./alpha.js"`); @@ -772,7 +772,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/imports.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.text).toContain(`import { alpha } from "./alpha"`); @@ -798,7 +798,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/exports.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.text).toContain(`export { alpha } from "./alpha"`); @@ -824,14 +824,14 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/index.js`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.text).toContain( - `export { WithJsxPreserve } from "./WithJsxPreserve.jsx"` + `export { WithJsxPreserve } from "./WithJsxPreserve.jsx"`, ); expect(results.text).toContain( - `export { WithJsxReact } from "./WithJsxReact.js"` + `export { WithJsxReact } from "./WithJsxReact.js"`, ); }); }); @@ -841,7 +841,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/no-change.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(false); expect(results.changes).toHaveLength(0); @@ -851,7 +851,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/imports.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -892,7 +892,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/exports.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -926,7 +926,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/nested/index.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -967,7 +967,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/directory/file.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toMatchInlineSnapshot(` @@ -1000,7 +1000,7 @@ describe("steps/generateChanges", () => { const results = replaceAliasPathsInFile( `${root}/out/import.d.ts`, aliases, - programPaths + programPaths, ); expect(results.changed).toBe(true); expect(results.changes).toHaveLength(1); @@ -1013,10 +1013,10 @@ describe("steps/generateChanges", () => { ] `); expect(results.text).not.toContain( - 'export { ./sameName } from "sameName";' + 'export { ./sameName } from "sameName";', ); expect(results.text).toContain( - 'export { sameName } from "./sameName";' + 'export { sameName } from "./sameName";', ); }); }); @@ -1042,7 +1042,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/no-change.js`, `${root}/out/directory.js`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(0); }); @@ -1051,7 +1051,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/imports.js`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1080,7 +1080,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/exports.js`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1105,7 +1105,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/nested/index.js`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1134,7 +1134,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/directory/file.js`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1151,7 +1151,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/alternateSrc/alternate/index.js`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1182,7 +1182,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/no-change.d.ts`, `${root}/out/directory.d.ts`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(0); }); @@ -1191,7 +1191,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/imports.d.ts`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1220,7 +1220,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/exports.d.ts`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1245,7 +1245,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/nested/index.d.ts`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1274,7 +1274,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/directory/file.d.ts`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(` @@ -1291,7 +1291,7 @@ describe("steps/generateChanges", () => { const results = generateChanges( [`${root}/out/alternateSrc/alternate/index.d.ts`], aliases, - programPaths + programPaths, ); expect(results).toHaveLength(1); expect(results[0].changes).toMatchInlineSnapshot(`