Skip to content
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"packages",
"npm",
"automatic",
"update"
"update",
"rush"
],
"repository": {
"type": "git",
Expand Down
57 changes: 29 additions & 28 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const getAllPackageJsons = async (workspaces, cwd) => {
workspaceGlobs.map(
(workspace) =>
new Promise((resolve, reject) => {
glob(`${workspace}/${PACKAGE_JSON}`, {cwd}, (error, files) => {
glob(`${workspace}/${PACKAGE_JSON}`, { cwd }, (error, files) => {
if (error) {
reject(error);
}
Expand Down Expand Up @@ -103,7 +103,7 @@ const getPackageNamesAndPackageDir = (packageFilePaths, cwd) =>
packageFilePaths.reduce((map, packageFilePath) => {
const fullPackageFilePath = path.join(cwd, packageFilePath);
const packageJson = require(fullPackageFilePath);
const {name} = packageJson;
const { name } = packageJson;
map.set(name, {
packageDir: path.dirname(fullPackageFilePath),
hasTsEntry: /\.(ts|tsx)$/.test((packageJson.main ? packageJson.main : ''))
Expand All @@ -113,7 +113,7 @@ const getPackageNamesAndPackageDir = (packageFilePaths, cwd) =>

const getReferencesFromDependencies = (
configName,
{packageDir},
{ packageDir },
packageName,
packagesMap,
verbose
Expand Down Expand Up @@ -142,7 +142,7 @@ const getReferencesFromDependencies = (
return Object.keys(mergedDependencies)
.reduce((referenceArray, dependency) => {
if (packagesMap.has(dependency)) {
const {packageDir: dependencyDir} = packagesMap.get(dependency);
const { packageDir: dependencyDir } = packagesMap.get(dependency);
const relativePath = path.relative(packageDir, dependencyDir);
const detectedConfig = detectTSConfig(dependencyDir, configName)
if (detectedConfig !== null) {
Expand Down Expand Up @@ -174,7 +174,7 @@ const updateTsConfig = (
paths,
check,
createPathMappings = false,
{packageDir} = {packageDir: process.cwd()},
{ packageDir } = { packageDir: process.cwd() },
) => {
const tsconfigFilePath = path.join(packageDir, configName);

Expand All @@ -183,7 +183,7 @@ const updateTsConfig = (

const currentReferences = config.references || [];

const mergedReferences = references.map(({path}) => ({
const mergedReferences = references.map(({ path }) => ({
path,
...currentReferences.find((currentRef) => currentRef.path === path),
}));
Expand All @@ -205,13 +205,13 @@ const updateTsConfig = (
assign(compilerOptions,
paths && Object.keys(paths).length > 0 ? {
paths
} : {paths: undefined})
} : { paths: undefined })

const newTsConfig = assign(config,
{
Object.keys(compilerOptions).length > 0 ? {
compilerOptions,
references: mergedReferences.length ? mergedReferences : undefined,
}
} : { references: mergedReferences.length ? mergedReferences : undefined, }
);
fs.writeFileSync(tsconfigFilePath, stringify(newTsConfig, null, 2) + '\n');
}
Expand All @@ -220,30 +220,31 @@ const updateTsConfig = (
return 0;
} catch (error) {
console.error(`could not read ${tsconfigFilePath}`, error);
if(strict)
if (strict)
throw new Error('Expect always a tsconfig.json in the package directory while running in strict mode')
}
};

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'
if (ignorePathMappings.includes(ref.name)) return paths
const rootFolder = tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src'
return {
...paths,
[`${ref.name}`]: [`${ref.folder}${rootFolder === '.' ? '' : `/${rootFolder}`}`],
}}, {});
...paths,
[`${ref.name}`]: [`${ref.folder}${rootFolder === '.' ? '' : `/${rootFolder}`}`],
}
}, {});
}

const execute = async ({
cwd, createTsConfig,
verbose,
check,
usecase,
strict,
...configurable
}) => {
cwd, createTsConfig,
verbose,
check,
usecase,
strict,
...configurable
}) => {
let changesCount = 0;
// eslint-disable-next-line no-console
console.log('updating tsconfigs');
Expand Down Expand Up @@ -273,7 +274,7 @@ const execute = async ({
withoutRootConfig
} = configurable

let ignorePathMappings = []
let ignorePathMappings = []

if (fs.existsSync(path.join(cwd, usecase))) {
const yamlConfig = yaml.load(
Expand All @@ -292,7 +293,7 @@ const execute = async ({
console.log(`createPathMappings ${createPathMappings}`)
console.log('joined workspaces', workspaces);
console.log('ignorePathMappings', ignorePathMappings
);
);
}
}

Expand Down Expand Up @@ -382,13 +383,13 @@ const execute = async ({
console.log('rootReferences', rootReferences);
console.log('rootPaths', rootPaths);
}
if(withoutRootConfig === false) {
if (withoutRootConfig === false) {
changesCount += updateTsConfig(
strict,
rootConfigName,
rootReferences,
rootPaths,
check, createPathMappings, {packageDir: cwd},
check, createPathMappings, { packageDir: cwd },
);
}

Expand All @@ -398,4 +399,4 @@ const execute = async ({
return changesCount;
};

module.exports = {execute, defaultOptions};
module.exports = { execute, defaultOptions };
12 changes: 12 additions & 0 deletions test-scenarios/yarn-ws-empty-compilerOptions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "yarn-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-a",
"workspace-b"
],
"devDependencies": {
"typescript": "latest"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
3 changes: 3 additions & 0 deletions test-scenarios/yarn-ws-empty-compilerOptions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.base.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"workspace-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [
{
"path": "../some/old/ref"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.base.json"
}
69 changes: 58 additions & 11 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path');
const fs = require('fs');
const {parse} = require("comment-json")
const {setup} = require('./setup');
const { parse } = require("comment-json")
const { setup } = require('./setup');

const rootFolderYarn = path.join(process.cwd(), 'test-run', 'yarn-ws');
const rootFolderYarnEmptyCompilerOptions = path.join(process.cwd(), 'test-run', 'yarn-ws-empty-compilerOptions')
const rootFolderYarnNohoist = path.join(
process.cwd(),
'test-run',
Expand All @@ -27,7 +28,7 @@ const rootFolderConfigName = path.join(
'yarn-ws-custom-tsconfig-names'
);

const compilerOptions = {outDir: 'dist', rootDir: 'src'};
const compilerOptions = { outDir: 'dist', rootDir: 'src' };


const rootTsConfig = [
Expand Down Expand Up @@ -168,6 +169,52 @@ const tsconfigsIncludingPrepend = [
fooBTsConfig,
];

test('avoid adding an empty compilerOptions', async () => {
const rootTsConfig = [
'.',
{
extends: "./tsconfig.base.json",
references: [
{
path: 'workspace-a',
},
{
path: 'workspace-b',
},
],
},
];
const wsATsConfig = [
'./workspace-a',
{
compilerOptions,
references: [
{
path: '../workspace-b',
},
],
},
];

const wsBTsConfig = [
'./workspace-b',
{
"extends": "./tsconfig.base.json"
},
];
const configs = [rootTsConfig, wsATsConfig, wsBTsConfig]

await setup(rootFolderYarnEmptyCompilerOptions)

configs.forEach((tsconfig) => {
const [configPath, config] = tsconfig;

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

test('Support yarn and npm workspaces', async () => {
await setup(rootFolderYarn);

Expand Down Expand Up @@ -229,7 +276,7 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
composite: true,
paths: { "foo-a": ["utils/foos/foo-a/src"] ,"foo-b": ["utils/foos/foo-b/src"] ,"workspace-a": ["workspace-a/src"],"workspace-b": ["workspace-b"] }
paths: { "foo-a": ["utils/foos/foo-a/src"], "foo-b": ["utils/foos/foo-b/src"], "workspace-a": ["workspace-a/src"], "workspace-b": ["workspace-b"] }
},
files: [],
references: [
Expand All @@ -254,7 +301,7 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
...compilerOptions,
paths: {"foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"]}
paths: { "foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"] }
},
references: [
{
Expand All @@ -270,7 +317,7 @@ test('create paths mappings ', async () => {
const wsBTsConfig = [
'./workspace-b',
{
compilerOptions: {...compilerOptions,rootDir: '.', paths: {"foo-b": ["../utils/foos/foo-b/src"]}},
compilerOptions: { ...compilerOptions, rootDir: '.', paths: { "foo-b": ["../utils/foos/foo-b/src"] } },
references: [
{
path: '../utils/foos/foo-b',
Expand Down Expand Up @@ -323,7 +370,7 @@ test('create paths mappings with ignorePathMappings', async () => {
{
compilerOptions: {
composite: true,
paths: { "foo-a": ["utils/foos/foo-a/src"] ,"foo-b": ["utils/foos/foo-b/src"] ,"workspace-b": ["workspace-b"] }
paths: { "foo-a": ["utils/foos/foo-a/src"], "foo-b": ["utils/foos/foo-b/src"], "workspace-b": ["workspace-b"] }
},
files: [],
references: [
Expand All @@ -348,7 +395,7 @@ test('create paths mappings with ignorePathMappings', async () => {
{
compilerOptions: {
...compilerOptions,
paths: {"foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"]}
paths: { "foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"] }
},
references: [
{
Expand All @@ -364,7 +411,7 @@ test('create paths mappings with ignorePathMappings', async () => {
const wsBTsConfig = [
'./workspace-b',
{
compilerOptions: {...compilerOptions,rootDir: '.', paths: {"foo-b": ["../utils/foos/foo-b/src"]}},
compilerOptions: { ...compilerOptions, rootDir: '.', paths: { "foo-b": ["../utils/foos/foo-b/src"] } },
references: [
{
path: '../utils/foos/foo-b',
Expand Down Expand Up @@ -461,7 +508,7 @@ test('Test create tsconfig', async () => {
});

test('Support option --withoutRootConfig', async () => {
await setup(rootFolderTsRefNoRoot,undefined,undefined,undefined,undefined,undefined,true);
await setup(rootFolderTsRefNoRoot, undefined, undefined, undefined, undefined, undefined, true);

const tsconfigs = [
[
Expand Down Expand Up @@ -492,7 +539,7 @@ test('Support option --withoutRootConfig', async () => {
// should not touch the ignore config
expect(
parse(fs.readFileSync(path.join(rootFolderTsRefNoRoot, 'workspace-ignore', 'tsconfig.json')).toString())
).toEqual({compilerOptions});
).toEqual({ compilerOptions });
});


Expand Down
Loading