Skip to content

Commit a6dc5a3

Browse files
authored
feat: support project without root tsconfig and add new strict mode (#44)
1 parent 4a7a93d commit a6dc5a3

63 files changed

Lines changed: 2629 additions & 2157 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.yarn/install-state.gz

537 KB
Binary file not shown.

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ npx update-ts-references --help
1717
Options:
1818
--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
20+
--withoutRootConfig If you will not have a tsconfig in the root directory or don't want to update it. Default: false
2021
--check Checks if updates would be necessary (without applying them)
2122
--help Show help
2223
--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)
2324
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
2425
--cwd Set working directory. Default: /Users/john-doe/projects/my-project
2526
--verbose Show verbose output. Default: false
2627
--usecase Use a specific usecase configuration. Default: update-ts-references.yaml
27-
28+
--strict Expects always a tsconfig.json in the package directory. Default: false
2829
```
2930

3031
or you add it as dev dependency and include it in the `postinstall` script in the package.json
@@ -97,6 +98,7 @@ Additional to that you can configure also the following options:
9798
- configName (default: tsconfig.json)
9899
- rootConfigName (default: tsconfig.json)
99100
- createPathMappings (default: false)
101+
- withoutRootConfig (default: false)
100102

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "update-ts-references",
3-
"version": "3.3.0",
3+
"version": "3.4.0",
44
"description": "Updates TypeScript references automatically while using workspaces",
55
"bin": "src/index.js",
66
"scripts": {
@@ -65,5 +65,6 @@
6565
"<rootDir>/test-scenarios/",
6666
"<rootDir>/test-run/"
6767
]
68-
}
68+
},
69+
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
6970
}

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,32 @@ const { execute, defaultOptions } = require('./update-ts-references');
77
const {
88
configName = defaultOptions.configName,
99
rootConfigName = defaultOptions.rootConfigName,
10+
withoutRootConfig= defaultOptions.withoutRootConfig,
1011
createTsConfig = defaultOptions.createTsConfig,
1112
cwd = defaultOptions.cwd,
1213
verbose = defaultOptions.verbose,
1314
help = defaultOptions.help,
1415
h = defaultOptions.help,
1516
check = defaultOptions.check,
1617
createPathMappings = defaultOptions.createPathMappings,
17-
usecase = defaultOptions.usecase
18+
usecase = defaultOptions.usecase,
19+
strict = defaultOptions.strict
1820
} = minimist(process.argv.slice(2));
1921
if (help || h) {
2022
console.log(`
2123
Usage: update-ts-references [options]
2224
Options:
2325
--configName The name of the config files which needs to be updated. Default: ${defaultOptions.configName}
2426
--rootConfigName The name of the root config file which needs to be updated. Default: ${defaultOptions.configName}
27+
--withoutRootConfig If you will not have a tsconfig in the root directory or don't want to update it. Default: ${defaultOptions.withoutRootConfig}
2528
--check Checks if updates would be necessary (without applying them)
2629
--help Show help
2730
--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)
2831
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
2932
--cwd Set working directory. Default: ${defaultOptions.cwd}
3033
--verbose Show verbose output. Default: ${defaultOptions.verbose}
3134
--usecase The use case for the script. Default: ${defaultOptions.usecase}
35+
--strict Expects always a tsconfig.json in the package directory. Default: ${defaultOptions.strict}
3236
`);
3337
process.exit(0);
3438
}
@@ -41,9 +45,11 @@ const run = async () => {
4145
check,
4246
configName,
4347
rootConfigName,
48+
withoutRootConfig,
4449
createTsConfig,
4550
createPathMappings,
46-
usecase
51+
usecase,
52+
strict
4753
});
4854

4955
if (check && changesCount > 0) {

src/update-ts-references.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const TSCONFIG_JSON = 'tsconfig.json'
1616
const defaultOptions = {
1717
configName: TSCONFIG_JSON,
1818
rootConfigName: TSCONFIG_JSON,
19+
withoutRootConfig: false,
1920
createTsConfig: false,
2021
cwd: process.cwd(),
2122
verbose: false,
2223
help: false,
2324
check: false,
2425
createPathMappings: false,
25-
usecase: 'update-ts-references.yaml'
26+
usecase: 'update-ts-references.yaml',
27+
strict: false
2628
};
2729

2830
const getAllPackageJsons = async (workspaces, cwd) => {
@@ -166,6 +168,7 @@ const ensurePosixPathStyle = (reference) => ({
166168
});
167169

168170
const updateTsConfig = (
171+
strict,
169172
configName,
170173
references,
171174
paths,
@@ -217,6 +220,8 @@ const updateTsConfig = (
217220
return 0;
218221
} catch (error) {
219222
console.error(`could not read ${tsconfigFilePath}`, error);
223+
if(strict)
224+
throw new Error('Expect always a tsconfig.json in the package directory while running in strict mode')
220225
}
221226
};
222227

@@ -232,6 +237,7 @@ const execute = async ({
232237
verbose,
233238
check,
234239
usecase,
240+
strict,
235241
...configurable
236242
}) => {
237243
let changesCount = 0;
@@ -259,7 +265,8 @@ const execute = async ({
259265
let {
260266
configName,
261267
rootConfigName,
262-
createPathMappings
268+
createPathMappings,
269+
withoutRootConfig
263270
} = configurable
264271

265272
if (fs.existsSync(path.join(cwd, usecase))) {
@@ -269,6 +276,7 @@ const execute = async ({
269276
configName = yamlConfig.configName ?? configName
270277
rootConfigName = yamlConfig.rootConfigName ?? rootConfigName
271278
createPathMappings = yamlConfig.createPathMappings ?? createPathMappings
279+
withoutRootConfig = yamlConfig.withoutRootConfig ?? withoutRootConfig
272280
workspaces = [...(yamlConfig.packages ? yamlConfig.packages : []), ...(workspaces ? workspaces : [])];
273281

274282
if (verbose) {
@@ -322,6 +330,7 @@ const execute = async ({
322330
}
323331

324332
changesCount += updateTsConfig(
333+
strict,
325334
detectedConfig,
326335
references,
327336
paths,
@@ -347,12 +356,15 @@ const execute = async ({
347356
console.log('rootReferences', rootReferences);
348357
console.log('rootPaths', rootPaths);
349358
}
350-
changesCount += updateTsConfig(
351-
rootConfigName,
352-
rootReferences,
353-
rootPaths,
354-
check, createPathMappings, {packageDir: cwd}
355-
);
359+
if(withoutRootConfig === false) {
360+
changesCount += updateTsConfig(
361+
strict,
362+
rootConfigName,
363+
rootReferences,
364+
rootPaths,
365+
check, createPathMappings, {packageDir: cwd},
366+
);
367+
}
356368

357369
if (verbose) {
358370
console.log(`counted changes ${changesCount}`);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "ts-ref-yaml-workspace",
3+
"version": "0.0.1",
4+
"private": true,
5+
"workspaces": [
6+
"workspace-b",
7+
"shared/*",
8+
"utils/**/"
9+
],
10+
"devDependencies": {
11+
"typescript": "latest"
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "workspace-c",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"cross-env": "5.0.5"
6+
},
7+
"peerDependencies": {
8+
"foo-a": "1.0.0"
9+
}
10+
}
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "workspace-d",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"workspace-c": "1.0.0"
6+
}
7+
}

0 commit comments

Comments
 (0)