-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod-rewrite-module-specifiers-to-full-paths): implement proj…
…ect (#2)
- Loading branch information
1 parent
6c11351
commit f3ea9fe
Showing
42 changed files
with
2,635 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/codemod-rewrite-module-specifiers-to-full-paths/.eslintrc.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const baseEslintConfig = require('@pkerschbaum/config-eslint/eslint-ecma.cjs'); | ||
|
||
module.exports = { | ||
...baseEslintConfig, | ||
parserOptions: { | ||
...baseEslintConfig.parserOptions, | ||
tsconfigRootDir: __dirname, | ||
}, | ||
ignorePatterns: [ | ||
...(baseEslintConfig.ignorePatterns || []), | ||
'**/codemod-inputs/**', | ||
'**/codemod-outputs/**', | ||
], | ||
rules: { | ||
...baseEslintConfig.rules, | ||
'no-console': 'off', | ||
}, | ||
}; |
64 changes: 64 additions & 0 deletions
64
packages/codemod-rewrite-module-specifiers-to-full-paths/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"name": "@pkerschbaum/codemod-rewrite-module-specifiers-to-full-paths", | ||
"version": "0.0.1", | ||
"homepage": "https://github.com/pkerschbaum/packages/tree/main/packages/codemod-rewrite-module-specifiers-to-full-paths", | ||
"bugs": { | ||
"url": "https://github.com/pkerschbaum/packages/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/pkerschbaum/packages", | ||
"directory": "packages/codemod-rewrite-module-specifiers-to-full-paths" | ||
}, | ||
"license": "MIT", | ||
"author": "Patrick Kerschbaum <[email protected]>", | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./*": "./dist/*.js" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"./dist/*" | ||
] | ||
} | ||
}, | ||
"bin": "./dist/bin/codemod.js", | ||
"files": [ | ||
"dist/**", | ||
"!dist/**/*.d.ts.map" | ||
], | ||
"scripts": { | ||
"build": "pnpm run internal:compile", | ||
"dev": "pnpm run build --watch --preserveWatchOutput", | ||
"internal:compile": "tsc -p ./tsconfig.build.json", | ||
"lint": "pnpm run lint:file .", | ||
"lint:file": "eslint --max-warnings 0", | ||
"lint:fix": "pnpm run lint --fix", | ||
"nuke": "pnpm run nuke:artifacts && del-cli node_modules", | ||
"nuke:artifacts": "del-cli dist \"*.tsbuildinfo\"", | ||
"test": "vitest run --config=\"./test/vitest.config.mts\"" | ||
}, | ||
"dependencies": { | ||
"@commander-js/extra-typings": "^12.1.0", | ||
"@pkerschbaum/commons-ecma": "workspace:*", | ||
"@pkerschbaum/commons-node": "workspace:*", | ||
"@pkerschbaum/runtime-extensions-node": "workspace:*", | ||
"commander": "^12.1.0", | ||
"jscodeshift": "^17.0.0", | ||
"p-limit": "^6.1.0", | ||
"tiny-invariant": "^1.3.3", | ||
"typescript": "^5.5.4" | ||
}, | ||
"devDependencies": { | ||
"@types/jscodeshift": "^0.11.11", | ||
"@types/node": "^20", | ||
"@vitest/coverage-v8": "^2.0.5", | ||
"ts-expose-internals": "^5.5.4", | ||
"vite-tsconfig-paths": "^5.0.1", | ||
"vitest": "^2.0.5" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/codemod-rewrite-module-specifiers-to-full-paths/src/bin/codemod.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import '@pkerschbaum/runtime-extensions-node'; | ||
|
||
import * as commander from '@commander-js/extra-typings'; | ||
import fs from 'node:fs'; | ||
|
||
import { loadTypeScriptProgram } from '#pkg/load-typescript-program'; | ||
import { rewriteModuleSpecifiersOfTypeScriptProject } from '#pkg/transform/index'; | ||
|
||
const commanderProgram = new commander.Command() | ||
.addOption( | ||
new commander.Option( | ||
'--project <path-to-tsconfig-json>', | ||
'Path to the TypeScript configuration file (e.g. "./tsconfig.json").', | ||
).makeOptionMandatory(), | ||
) | ||
.addOption( | ||
new commander.Option( | ||
'--basepath <path>', | ||
'A root directory to resolve relative path entries in the TypeScript config file to (e.g. option "outDir"). If omitted, the directory of the TypeScript configuration file passed with "--project" is used.', | ||
), | ||
); | ||
commanderProgram.parse(); | ||
const options = commanderProgram.opts(); | ||
|
||
async function run() { | ||
const { default: pLimit } = await import('p-limit'); | ||
|
||
const typeScriptProgram = await loadTypeScriptProgram(options); | ||
|
||
const limit = pLimit(10); | ||
const operations = typeScriptProgram.fileNames.map((absolutePathSourceFile) => | ||
limit(async () => { | ||
const text = await fs.promises.readFile(absolutePathSourceFile, 'utf8'); | ||
const newText = rewriteModuleSpecifiersOfTypeScriptProject( | ||
typeScriptProgram, | ||
absolutePathSourceFile, | ||
text, | ||
); | ||
await fs.promises.writeFile(absolutePathSourceFile, newText); | ||
}), | ||
); | ||
await Promise.all(operations); | ||
} | ||
|
||
void run(); |
63 changes: 63 additions & 0 deletions
63
packages/codemod-rewrite-module-specifiers-to-full-paths/src/load-typescript-program.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import path from 'node:path'; | ||
import invariant from 'tiny-invariant'; | ||
import ts from 'typescript'; | ||
|
||
import { fsUtils } from '@pkerschbaum/commons-node/utils/fs'; | ||
|
||
export type TypeScriptProgram = { | ||
compilerOptions: ts.CompilerOptions; | ||
paths: | ||
| { | ||
absoluteBasePath: string; | ||
patterns: ReadonlyArray<string | ts.Pattern>; | ||
} | ||
| undefined; | ||
fileNames: string[]; | ||
}; | ||
|
||
export async function loadTypeScriptProgram(opts: { | ||
project: string; | ||
basepath?: string | undefined; | ||
}): Promise<TypeScriptProgram> { | ||
const projectAbsolutePath = path.resolve(opts.project); | ||
invariant( | ||
await fsUtils.existsPath(projectAbsolutePath), | ||
`expected to find project, but did not! projectAbsolutePath=${projectAbsolutePath}`, | ||
); | ||
|
||
const basepath = opts.basepath ?? path.dirname(projectAbsolutePath); | ||
|
||
const configFile = ts.readConfigFile(projectAbsolutePath, ts.sys.readFile.bind(ts.sys)); | ||
invariant( | ||
configFile.config, | ||
`expected to find config, but couldn't! projectAbsolutePath=${projectAbsolutePath}`, | ||
); | ||
const { options: compilerOptions, fileNames } = ts.parseJsonConfigFileContent( | ||
configFile.config, | ||
ts.sys, | ||
basepath, | ||
); | ||
|
||
const paths = computePathsContext(compilerOptions); | ||
|
||
return { | ||
compilerOptions, | ||
paths, | ||
fileNames, | ||
}; | ||
} | ||
|
||
function computePathsContext(compilerOptions: ts.CompilerOptions) { | ||
let pathsPatterns = compilerOptions.configFile?.configFileSpecs?.pathPatterns; | ||
let absoluteBasePath = undefined; | ||
if (!compilerOptions.paths) { | ||
return undefined; | ||
} | ||
|
||
pathsPatterns = ts.tryParsePatterns(compilerOptions.paths); | ||
|
||
invariant(compilerOptions.pathsBasePath); | ||
absoluteBasePath = compilerOptions.baseUrl ?? compilerOptions.pathsBasePath; | ||
|
||
return { absoluteBasePath, patterns: pathsPatterns }; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/codemod-rewrite-module-specifiers-to-full-paths/src/transform/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '#pkg/transform/rewrite-module-specifiers-of-typescript-project'; |
Oops, something went wrong.