Skip to content

Commit 7a86e76

Browse files
authored
fix: avoid adding an empty compilerOptions object (#50)
Refs: #49
1 parent bba1a4b commit 7a86e76

File tree

11 files changed

+146
-40
lines changed

11 files changed

+146
-40
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"packages",
5050
"npm",
5151
"automatic",
52-
"update"
52+
"update",
53+
"rush"
5354
],
5455
"repository": {
5556
"type": "git",

src/update-ts-references.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const getAllPackageJsons = async (workspaces, cwd) => {
4343
workspaceGlobs.map(
4444
(workspace) =>
4545
new Promise((resolve, reject) => {
46-
glob(`${workspace}/${PACKAGE_JSON}`, {cwd}, (error, files) => {
46+
glob(`${workspace}/${PACKAGE_JSON}`, { cwd }, (error, files) => {
4747
if (error) {
4848
reject(error);
4949
}
@@ -103,7 +103,7 @@ const getPackageNamesAndPackageDir = (packageFilePaths, cwd) =>
103103
packageFilePaths.reduce((map, packageFilePath) => {
104104
const fullPackageFilePath = path.join(cwd, packageFilePath);
105105
const packageJson = require(fullPackageFilePath);
106-
const {name} = packageJson;
106+
const { name } = packageJson;
107107
map.set(name, {
108108
packageDir: path.dirname(fullPackageFilePath),
109109
hasTsEntry: /\.(ts|tsx)$/.test((packageJson.main ? packageJson.main : ''))
@@ -113,7 +113,7 @@ const getPackageNamesAndPackageDir = (packageFilePaths, cwd) =>
113113

114114
const getReferencesFromDependencies = (
115115
configName,
116-
{packageDir},
116+
{ packageDir },
117117
packageName,
118118
packagesMap,
119119
verbose
@@ -142,7 +142,7 @@ const getReferencesFromDependencies = (
142142
return Object.keys(mergedDependencies)
143143
.reduce((referenceArray, dependency) => {
144144
if (packagesMap.has(dependency)) {
145-
const {packageDir: dependencyDir} = packagesMap.get(dependency);
145+
const { packageDir: dependencyDir } = packagesMap.get(dependency);
146146
const relativePath = path.relative(packageDir, dependencyDir);
147147
const detectedConfig = detectTSConfig(dependencyDir, configName)
148148
if (detectedConfig !== null) {
@@ -174,7 +174,7 @@ const updateTsConfig = (
174174
paths,
175175
check,
176176
createPathMappings = false,
177-
{packageDir} = {packageDir: process.cwd()},
177+
{ packageDir } = { packageDir: process.cwd() },
178178
) => {
179179
const tsconfigFilePath = path.join(packageDir, configName);
180180

@@ -183,7 +183,7 @@ const updateTsConfig = (
183183

184184
const currentReferences = config.references || [];
185185

186-
const mergedReferences = references.map(({path}) => ({
186+
const mergedReferences = references.map(({ path }) => ({
187187
path,
188188
...currentReferences.find((currentRef) => currentRef.path === path),
189189
}));
@@ -205,13 +205,13 @@ const updateTsConfig = (
205205
assign(compilerOptions,
206206
paths && Object.keys(paths).length > 0 ? {
207207
paths
208-
} : {paths: undefined})
208+
} : { paths: undefined })
209209

210210
const newTsConfig = assign(config,
211-
{
211+
Object.keys(compilerOptions).length > 0 ? {
212212
compilerOptions,
213213
references: mergedReferences.length ? mergedReferences : undefined,
214-
}
214+
} : { references: mergedReferences.length ? mergedReferences : undefined, }
215215
);
216216
fs.writeFileSync(tsconfigFilePath, stringify(newTsConfig, null, 2) + '\n');
217217
}
@@ -220,30 +220,31 @@ const updateTsConfig = (
220220
return 0;
221221
} catch (error) {
222222
console.error(`could not read ${tsconfigFilePath}`, error);
223-
if(strict)
223+
if (strict)
224224
throw new Error('Expect always a tsconfig.json in the package directory while running in strict mode')
225225
}
226226
};
227227

228228
function getPathsFromReferences(references, tsconfigMap, ignorePathMappings
229-
= []) {
229+
= []) {
230230
return references.reduce((paths, ref) => {
231-
if(ignorePathMappings.includes(ref.name)) return paths
232-
const rootFolder= tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src'
231+
if (ignorePathMappings.includes(ref.name)) return paths
232+
const rootFolder = tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src'
233233
return {
234-
...paths,
235-
[`${ref.name}`]: [`${ref.folder}${rootFolder === '.' ? '' : `/${rootFolder}`}`],
236-
}}, {});
234+
...paths,
235+
[`${ref.name}`]: [`${ref.folder}${rootFolder === '.' ? '' : `/${rootFolder}`}`],
236+
}
237+
}, {});
237238
}
238239

239240
const execute = async ({
240-
cwd, createTsConfig,
241-
verbose,
242-
check,
243-
usecase,
244-
strict,
245-
...configurable
246-
}) => {
241+
cwd, createTsConfig,
242+
verbose,
243+
check,
244+
usecase,
245+
strict,
246+
...configurable
247+
}) => {
247248
let changesCount = 0;
248249
// eslint-disable-next-line no-console
249250
console.log('updating tsconfigs');
@@ -273,7 +274,7 @@ const execute = async ({
273274
withoutRootConfig
274275
} = configurable
275276

276-
let ignorePathMappings = []
277+
let ignorePathMappings = []
277278

278279
if (fs.existsSync(path.join(cwd, usecase))) {
279280
const yamlConfig = yaml.load(
@@ -292,7 +293,7 @@ const execute = async ({
292293
console.log(`createPathMappings ${createPathMappings}`)
293294
console.log('joined workspaces', workspaces);
294295
console.log('ignorePathMappings', ignorePathMappings
295-
);
296+
);
296297
}
297298
}
298299

@@ -382,13 +383,13 @@ const execute = async ({
382383
console.log('rootReferences', rootReferences);
383384
console.log('rootPaths', rootPaths);
384385
}
385-
if(withoutRootConfig === false) {
386+
if (withoutRootConfig === false) {
386387
changesCount += updateTsConfig(
387388
strict,
388389
rootConfigName,
389390
rootReferences,
390391
rootPaths,
391-
check, createPathMappings, {packageDir: cwd},
392+
check, createPathMappings, { packageDir: cwd },
392393
);
393394
}
394395

@@ -398,4 +399,4 @@ const execute = async ({
398399
return changesCount;
399400
};
400401

401-
module.exports = {execute, defaultOptions};
402+
module.exports = { execute, defaultOptions };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "yarn-workspace",
3+
"version": "0.0.1",
4+
"private": true,
5+
"workspaces": [
6+
"workspace-a",
7+
"workspace-b"
8+
],
9+
"devDependencies": {
10+
"typescript": "latest"
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files": [],
3+
"compilerOptions": {
4+
/* Basic Options */
5+
// "allowJs": true,
6+
"composite": true
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.base.json"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "workspace-a",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"workspace-b": "1.0.0"
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"rootDir": "src"
5+
},
6+
"references": [
7+
{
8+
"path": "../some/old/ref"
9+
}
10+
]
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "workspace-b",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"cross-env": "5.0.5"
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"rootDir": "src"
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.base.json"
3+
}

0 commit comments

Comments
 (0)