Skip to content

Commit 11e95d3

Browse files
authored
feat!: adding basic support for jsconfig.json files (#52)
1 parent ece8630 commit 11e95d3

File tree

19 files changed

+166
-24
lines changed

19 files changed

+166
-24
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npx update-ts-references --help
1515
Usage: update-ts-references [options]
1616
1717
Options:
18-
--configName The name of the config files which needs to be updated. Default: tsconfig.json
18+
--configName The name of the config files which needs to be updated. Default: tsconfig.json
1919
--rootConfigName The name of the root config file which needs to be updated. Default: tsconfig.json
2020
--withoutRootConfig If you will not have a tsconfig in the root directory or don't want to update it. Default: false
2121
--check Checks if updates would be necessary (without applying them)
@@ -70,7 +70,7 @@ The output for the created file looks like the following
7070
```
7171

7272
## using --createPathMappings
73-
will create path mappings under `compilerOptions` for a better IDE support. It assumes the source files are under `src`.
73+
will create path mappings under `compilerOptions` for a better IDE support. The generated mappings respect the `rootDir` defined in the referenced `tsconfig.json` or `jsconfig.json` and only fall back to `src` when no `rootDir` is provided.
7474

7575
```json
7676
{
@@ -106,6 +106,14 @@ Example configuration see [here](./test-scenarios/ts-options-yaml/update-ts-refe
106106
### using multiple configurations for different usecases
107107
Executing update-ts-references with different configurations via the parameter `--usecase`.
108108

109+
## about jsconfig.json support
110+
When a dependency does not ship a `tsconfig.json` but provides a `jsconfig.json`, the reference automatically targets that file and the path mappings pick up its `rootDir` as well, if `--createPathMappings` is enabled. `jsconfig.json` files participate in package-level `references`, but they stay out of the root `tsconfig.json`.
111+
112+
### in combination with the --configName argument
113+
Currently the name of the config file derives from the the configName which is `tsconfig.json` and would look for `jsconfig.json`.
114+
115+
Example: ` --configName tsconfig.test.json` would look for `jsconfig.test.json`
116+
109117
## FAQ
110118
### Why is my pnpm workspace alias not working?
111119

src/update-ts-references.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const assert = require('assert').strict;
1212

1313
const PACKAGE_JSON = 'package.json';
1414
const TSCONFIG_JSON = 'tsconfig.json'
15+
const JSCONFIG_JSON = 'jsconfig.json'
1516

1617
const defaultOptions = {
1718
configName: TSCONFIG_JSON,
@@ -72,6 +73,15 @@ const getAllPackageJsons = async (workspaces, cwd) => {
7273
);
7374
};
7475

76+
const detectJSConfig = (directory, configName) => {
77+
const jsConfigName = configName.replace(/^ts/, 'js')
78+
let detectedConfig = fs.existsSync(path.join(directory, jsConfigName)) ? jsConfigName : null
79+
if (jsConfigName !== JSCONFIG_JSON && detectedConfig === null) {
80+
detectedConfig = fs.existsSync(path.join(directory, JSCONFIG_JSON)) ? JSCONFIG_JSON : null
81+
}
82+
return detectedConfig
83+
}
84+
7585
const detectTSConfig = (directory, configName, createConfig, cwd) => {
7686
let detectedConfig = fs.existsSync(path.join(directory, configName)) ? configName : null
7787
if (configName !== TSCONFIG_JSON && detectedConfig === null) {
@@ -154,6 +164,18 @@ const getReferencesFromDependencies = (
154164
folder: relativePath,
155165
},
156166
];
167+
} else {
168+
const detectedJsConfig = detectJSConfig(dependencyDir, configName)
169+
if (detectedJsConfig !== null) {
170+
return [
171+
...referenceArray,
172+
{
173+
name: dependency,
174+
path: path.join(relativePath, detectedJsConfig),
175+
folder: relativePath,
176+
},
177+
];
178+
}
157179
}
158180
}
159181
return referenceArray;
@@ -225,11 +247,12 @@ const updateTsConfig = (
225247
}
226248
};
227249

228-
function getPathsFromReferences(references, tsconfigMap, ignorePathMappings
250+
function getPathsFromReferences(references, tsconfigMap, jsconfigMap, ignorePathMappings
229251
= []) {
230252
return references.reduce((paths, ref) => {
231253
if (ignorePathMappings.includes(ref.name)) return paths
232-
const rootFolder = tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src'
254+
const config = tsconfigMap[ref.name] ?? jsconfigMap[ref.name]
255+
const rootFolder = config?.compilerOptions?.rootDir ?? 'src'
233256
return {
234257
...paths,
235258
[`${ref.name}`]: [`${ref.folder}${rootFolder === '.' ? '' : `/${rootFolder}`}`],
@@ -316,6 +339,7 @@ const execute = async ({
316339
let rootReferences = [];
317340
let rootPaths = [];
318341
let tsconfigMap = {}
342+
let jsconfigMap = {}
319343
packagesMap.forEach((packageEntry, packageName) => {
320344
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig, cwd)
321345

@@ -329,6 +353,25 @@ const execute = async ({
329353
compilerOptions
330354
}
331355
}
356+
} else {
357+
const detectedJsConfig = detectJSConfig(packageEntry.packageDir, configName)
358+
if (detectedJsConfig) {
359+
360+
let compilerOptions
361+
try {
362+
compilerOptions = parse(fs.readFileSync(path.join(packageEntry.packageDir, detectedJsConfig)).toString()).compilerOptions
363+
} catch {
364+
//ignore
365+
}
366+
367+
jsconfigMap = {
368+
...jsconfigMap,
369+
[packageName]: {
370+
detectedConfig,
371+
compilerOptions
372+
}
373+
}
374+
}
332375
}
333376
});
334377

@@ -349,7 +392,7 @@ const execute = async ({
349392
verbose
350393
) || []).map(ensurePosixPathStyle);
351394

352-
const paths = getPathsFromReferences(references, tsconfigMap, ignorePathMappings)
395+
const paths = getPathsFromReferences(references, tsconfigMap, jsconfigMap, ignorePathMappings)
353396

354397
if (verbose) {
355398
console.log(`references of ${packageName}`, references);
@@ -366,8 +409,11 @@ const execute = async ({
366409
packageEntry
367410
);
368411
} else {
369-
// eslint-disable-next-line no-console
370-
console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`);
412+
const detectedJsConfig = jsconfigMap[packageName]?.detectedConfig
413+
if (!detectedJsConfig) {
414+
// eslint-disable-next-line no-console
415+
console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`);
416+
}
371417
rootPaths.push({
372418
name: packageName,
373419
path: path.relative(cwd, packageEntry.packageDir),
@@ -377,7 +423,7 @@ const execute = async ({
377423
});
378424

379425
rootReferences = (rootReferences || []).map(ensurePosixPathStyle);
380-
rootPaths = getPathsFromReferences((rootReferences || []).map(ensurePosixPathStyle), tsconfigMap, ignorePathMappings)
426+
rootPaths = getPathsFromReferences((rootReferences || []).map(ensurePosixPathStyle), tsconfigMap, {}, ignorePathMappings)
381427

382428
if (verbose) {
383429
console.log('rootReferences', rootReferences);
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "js-only",
3+
"version": "1.0.0",
4+
"dependencies": {}
5+
}

test-scenarios/ts-paths/workspace-a/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"name": "workspace-a",
33
"version": "1.0.0",
44
"dependencies": {
5-
"workspace-b": "1.0.0"
5+
"workspace-b": "1.0.0",
6+
"js-only": "1.0.0",
7+
"js-only2": "1.0.0"
68
},
79
"devDependencies": {
8-
"foo-a": "1.0.0"
10+
"foo-a": "1.0.0"
911
}
1012
}

test-scenarios/yarn-ws-check-paths/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"workspaces": [
66
"workspace-a",
77
"workspace-b",
8+
"workspace-c",
89
"shared/*",
910
"utils/**"
1011
],
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "js-only",
3+
"version": "1.0.0",
4+
"dependencies": {}
5+
}

test-scenarios/yarn-ws-check-paths/workspace-a/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"workspace-b": "1.0.0"
66
},
77
"devDependencies": {
8-
"foo-a": "1.0.0"
8+
"foo-a": "1.0.0"
99
}
1010
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "workspace-c",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"js-only": "1.0.0"
6+
}
7+
}

0 commit comments

Comments
 (0)