Skip to content

Commit

Permalink
feat(yaml): ignore paths for packages (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
MKruschke authored Jan 17, 2025
1 parent eb219bd commit 5177da5
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Additional to that you can configure also the following options:
- rootConfigName (default: tsconfig.json)
- createPathMappings (default: false)
- withoutRootConfig (default: false)
- ignorePathMappings (default: [])

Example configuration see [here](./test-scenarios/ts-options-yaml/update-ts-references.yaml)

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.5.0",
"version": "3.6.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand Down
13 changes: 10 additions & 3 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ const updateTsConfig = (
}
};

function getPathsFromReferences(references, tsconfigMap) {
function getPathsFromReferences(references, tsconfigMap, ignorePathMappings
= []) {
return references.reduce((paths, ref) => {
if(ignorePathMappings.includes(ref.name)) return paths
const rootFolder= tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src'
return {
...paths,
Expand Down Expand Up @@ -271,6 +273,8 @@ const execute = async ({
withoutRootConfig
} = configurable

let ignorePathMappings = []

if (fs.existsSync(path.join(cwd, usecase))) {
const yamlConfig = yaml.load(
fs.readFileSync(path.join(cwd, usecase))
Expand All @@ -280,12 +284,15 @@ const execute = async ({
createPathMappings = yamlConfig.createPathMappings ?? createPathMappings
withoutRootConfig = yamlConfig.withoutRootConfig ?? withoutRootConfig
workspaces = [...(yamlConfig.packages ? yamlConfig.packages : []), ...(workspaces ? workspaces : [])];
ignorePathMappings = yamlConfig.ignorePathMappings ?? []

if (verbose) {
console.log(`configName ${configName}`);
console.log(`rootConfigName ${rootConfigName}`);
console.log(`createPathMappings ${createPathMappings}`)
console.log('joined workspaces', workspaces);
console.log('ignorePathMappings', ignorePathMappings
);
}
}

Expand Down Expand Up @@ -341,7 +348,7 @@ const execute = async ({
verbose
) || []).map(ensurePosixPathStyle);

const paths = getPathsFromReferences(references, tsconfigMap)
const paths = getPathsFromReferences(references, tsconfigMap, ignorePathMappings)

if (verbose) {
console.log(`references of ${packageName}`, references);
Expand Down Expand Up @@ -369,7 +376,7 @@ const execute = async ({
});

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

if (verbose) {
console.log('rootReferences', rootReferences);
Expand Down
2 changes: 2 additions & 0 deletions test-scenarios/ts-options-yaml/update-ts-references.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
configName: 'tsconfig.dev.json'
rootConfigName: 'tsconfig.root.json'
createPathMappings: true
ignorePathMappings:
- name-of-the-package
packages:
# all packages in subdirs of packages/ and components/
- 'workspace-a'
Expand Down
14 changes: 14 additions & 0 deletions test-scenarios/ts-paths-ignore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "ts-ref-yaml-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-a",
"workspace-b",
"shared/*",
"utils/**/"
],
"devDependencies": {
"typescript": "latest"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/ts-paths-ignore/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-paths-ignore/update-ts-references.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
configName: 'tsconfig.json'
rootConfigName: 'tsconfig.json'
createPathMappings: true
ignorePathMappings:
- workspace-a

7 changes: 7 additions & 0 deletions test-scenarios/ts-paths-ignore/utils/foos/foo-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo-a",
"version": "1.0.0",
"dependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-paths-ignore/utils/foos/foo-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
4 changes: 4 additions & 0 deletions test-scenarios/ts-paths-ignore/utils/foos/foo-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo-b",
"version": "1.0.0"
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-paths-ignore/utils/foos/foo-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-paths-ignore/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": {
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-paths-ignore/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0"
}
}
15 changes: 15 additions & 0 deletions test-scenarios/ts-paths-ignore/workspace-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"paths": { "replace-me": ["../utils/old-one/src"]},
},
"references": [
{
"path": "../utils/foos/foo-a"
},
{
"path": "../workspace-b"
}
]
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-paths-ignore/workspace-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-paths-ignore/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "."
}
}
95 changes: 95 additions & 0 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const rootFolderYarnCreate = path.join(
);
const rootFolderPnpm = path.join(process.cwd(), 'test-run', 'pnpm');
const rootFolderTsPaths = path.join(process.cwd(), 'test-run', 'ts-paths');
const rootFolderTsPathsIgnore = path.join(process.cwd(), 'test-run', 'ts-paths-ignore');

const rootFolderLerna = path.join(process.cwd(), 'test-run', 'lerna');
const rootFolderConfigName = path.join(
Expand Down Expand Up @@ -313,6 +314,100 @@ test('create paths mappings ', async () => {

});

test('create paths mappings with ignorePathMappings', async () => {
await setup(rootFolderTsPathsIgnore, undefined, undefined, undefined, true);


const rootTsConfig = [
'.',
{
compilerOptions: {
composite: true,
paths: { "foo-a": ["utils/foos/foo-a/src"] ,"foo-b": ["utils/foos/foo-b/src"] ,"workspace-b": ["workspace-b"] }
},
files: [],
references: [
{
path: 'workspace-a',
},
{
path: 'workspace-b',
},
{
path: 'utils/foos/foo-a',
},
{
path: 'utils/foos/foo-b',
},
],
},
];

const wsATsConfig = [
'./workspace-a',
{
compilerOptions: {
...compilerOptions,
paths: {"foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"]}
},
references: [
{
path: '../utils/foos/foo-a',
},
{
path: '../workspace-b',
},
],
},
];

const wsBTsConfig = [
'./workspace-b',
{
compilerOptions: {...compilerOptions,rootDir: '.', paths: {"foo-b": ["../utils/foos/foo-b/src"]}},
references: [
{
path: '../utils/foos/foo-b',
},
],
},
];

const fooATsConfig = [
'./utils/foos/foo-a',
{
compilerOptions: {
...compilerOptions,
rootDir: "src",
paths: {
"foo-b": ["../foo-b/src"]
},
},
references: [
{
path: '../foo-b',
},
],
},
];

const fooBTsConfig = [
'./utils/foos/foo-b',
{
compilerOptions,
references: undefined,
},
];
[rootTsConfig, wsATsConfig, wsBTsConfig, fooATsConfig, fooBTsConfig].forEach((tsconfig) => {
const [configPath, config] = tsconfig;

expect(
parse(fs.readFileSync(path.join(rootFolderTsPathsIgnore, configPath, 'tsconfig.json')).toString())
).toEqual(config);
});

});

test('Test create tsconfig', async () => {
await setup(rootFolderYarnCreate, undefined, undefined, true);
const r = [
Expand Down

0 comments on commit 5177da5

Please sign in to comment.