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

feat: append paths for non ts enabled packages on the root tsconfig #37

Merged
merged 2 commits into from
Nov 28, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npx update-ts-references --help
--check Checks if updates would be necessary (without applying them)
--help Show help
--createTsConfig Create default TS configs for packages where the main entry in the package.json have a ts|tsx extension (Note: respects the --configName parameter)
-createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
--cwd Set working directory. Default: /Users/john-doe/projects/my-project
--verbose Show verbose output. Default: false

Expand Down Expand Up @@ -68,7 +68,7 @@ The output for the created file looks like the following
```

## using --createPathMappings
will create path mappings under `compilerOptions` for a better IDE support. It respects the `rootDir` if no `rootDir` available it falls back to `src`
will create path mappings under `compilerOptions` for a better IDE support. It assumes the source files are under `src`.

```json
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "3.1.0",
"version": "3.2.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand Down
46 changes: 33 additions & 13 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,17 @@ const getReferencesFromDependencies = (
const ensurePosixPathStyle = (reference) => ({
...reference,
path: reference.path.split(path.sep).join(path.posix.sep),
folder: reference.folder.split(path.sep).join(path.posix.sep),
folder: reference.folder?.split(path.sep).join(path.posix.sep),
});

const updateTsConfig = (
configName,
win32OrPosixReferences,
references,
paths,
check,
createPathMappings = false,
{packageDir} = {packageDir: process.cwd()},
) => {
const references = (win32OrPosixReferences || []).map(ensurePosixPathStyle);
const tsconfigFilePath = path.join(packageDir, configName);

try {
Expand All @@ -195,12 +195,9 @@ const updateTsConfig = (
if (check === false) {

const compilerOptions = config?.compilerOptions ?? {};
if (createPathMappings)
if (createPathMappings && paths && Object.keys(paths).length > 0)
assign(compilerOptions, {
paths: references.reduce((paths, ref) => ({
...paths,
[`${ref.name}`]: [`${ref.folder}/${config.compilerOptions?.rootDir ?? 'src'}`]
}), {})
paths
})

const newTsConfig = assign(config,
Expand All @@ -219,6 +216,13 @@ const updateTsConfig = (
}
};

function getPathsFromReferences(references) {
return references.reduce((paths, ref) => ({
...paths,
[`${ref.name}`]: [`${ref.folder}/src`]
}), {});
}

const execute = async ({
cwd, createTsConfig,
verbose,
Expand Down Expand Up @@ -286,8 +290,8 @@ const execute = async ({
console.log('packagesMap', packagesMap);
}

const rootReferences = [];

let rootReferences = [];
let rootPaths = [];
packagesMap.forEach((packageEntry, packageName) => {
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig, cwd)

Expand All @@ -297,36 +301,52 @@ const execute = async ({
path: path.join(path.relative(cwd, packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''),
folder: path.relative(cwd, packageEntry.packageDir),
});
const references = getReferencesFromDependencies(
const references = (getReferencesFromDependencies(
configName,
packageEntry,
packageName,
packagesMap,
verbose
);
) || []).map(ensurePosixPathStyle);

const paths = getPathsFromReferences(references)

if (verbose) {
console.log(`references of ${packageName}`, references);
console.log(`paths of ${packageName}`, paths);
}

changesCount += updateTsConfig(
detectedConfig,
references,
paths,
check,
createPathMappings,
packageEntry
);
} else {
// eslint-disable-next-line no-console
console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`);
rootPaths.push({
name: packageName,
path: path.relative(cwd, packageEntry.packageDir),
folder: path.relative(cwd, packageEntry.packageDir),
});
}
});

rootReferences = (rootReferences || []).map(ensurePosixPathStyle);
rootPaths = getPathsFromReferences((rootPaths || []).map(ensurePosixPathStyle))

if (verbose) {
console.log('rootReferences', rootReferences);
console.log('rootPaths', rootPaths);
}
changesCount += updateTsConfig(
rootConfigName,
rootReferences,
check, false, {packageDir: cwd}
rootPaths,
check, createPathMappings, {packageDir: cwd}
);

if (verbose) {
Expand Down
2 changes: 1 addition & 1 deletion test-scenarios/ts-paths/utils/foos/foo-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src2"
"rootDir": "src"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-paths/utils/foos/foo-c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo-c",
"version": "1.0.0",
"dependencies": {
}
}
5 changes: 3 additions & 2 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
composite: true,
paths: { "foo-c": ["utils/foos/foo-c/src"] }
},
files: [],
references: [
Expand Down Expand Up @@ -291,9 +292,9 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
...compilerOptions,
rootDir: "src2",
rootDir: "src",
paths: {
"foo-b": ["../foo-b/src2"]
"foo-b": ["../foo-b/src"]
},
},
references: [
Expand Down
Loading