Skip to content

Commit

Permalink
feat: support project without root tsconfig and add new strict mode (#44
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MKruschke authored Nov 20, 2024
1 parent 4a7a93d commit a6dc5a3
Show file tree
Hide file tree
Showing 63 changed files with 2,629 additions and 2,157 deletions.
Binary file added .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ npx update-ts-references --help
Options:
--configName The name of the config files which needs to be updated. Default: tsconfig.json
--rootConfigName The name of the root config file which needs to be updated. Default: tsconfig.json
--withoutRootConfig If you will not have a tsconfig in the root directory or don't want to update it. Default: false
--check Checks if updates would be necessary (without applying them)
--help Show help
--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)
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
--cwd Set working directory. Default: /Users/john-doe/projects/my-project
--verbose Show verbose output. Default: false
--usecase Use a specific usecase configuration. Default: update-ts-references.yaml
--strict Expects always a tsconfig.json in the package directory. Default: false
```

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

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

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "3.3.0",
"version": "3.4.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -65,5 +65,6 @@
"<rootDir>/test-scenarios/",
"<rootDir>/test-run/"
]
}
},
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@ const { execute, defaultOptions } = require('./update-ts-references');
const {
configName = defaultOptions.configName,
rootConfigName = defaultOptions.rootConfigName,
withoutRootConfig= defaultOptions.withoutRootConfig,
createTsConfig = defaultOptions.createTsConfig,
cwd = defaultOptions.cwd,
verbose = defaultOptions.verbose,
help = defaultOptions.help,
h = defaultOptions.help,
check = defaultOptions.check,
createPathMappings = defaultOptions.createPathMappings,
usecase = defaultOptions.usecase
usecase = defaultOptions.usecase,
strict = defaultOptions.strict
} = minimist(process.argv.slice(2));
if (help || h) {
console.log(`
Usage: update-ts-references [options]
Options:
--configName The name of the config files which needs to be updated. Default: ${defaultOptions.configName}
--rootConfigName The name of the root config file which needs to be updated. Default: ${defaultOptions.configName}
--withoutRootConfig If you will not have a tsconfig in the root directory or don't want to update it. Default: ${defaultOptions.withoutRootConfig}
--check Checks if updates would be necessary (without applying them)
--help Show help
--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)
--createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src"
--cwd Set working directory. Default: ${defaultOptions.cwd}
--verbose Show verbose output. Default: ${defaultOptions.verbose}
--usecase The use case for the script. Default: ${defaultOptions.usecase}
--strict Expects always a tsconfig.json in the package directory. Default: ${defaultOptions.strict}
`);
process.exit(0);
}
Expand All @@ -41,9 +45,11 @@ const run = async () => {
check,
configName,
rootConfigName,
withoutRootConfig,
createTsConfig,
createPathMappings,
usecase
usecase,
strict
});

if (check && changesCount > 0) {
Expand Down
28 changes: 20 additions & 8 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ const TSCONFIG_JSON = 'tsconfig.json'
const defaultOptions = {
configName: TSCONFIG_JSON,
rootConfigName: TSCONFIG_JSON,
withoutRootConfig: false,
createTsConfig: false,
cwd: process.cwd(),
verbose: false,
help: false,
check: false,
createPathMappings: false,
usecase: 'update-ts-references.yaml'
usecase: 'update-ts-references.yaml',
strict: false
};

const getAllPackageJsons = async (workspaces, cwd) => {
Expand Down Expand Up @@ -166,6 +168,7 @@ const ensurePosixPathStyle = (reference) => ({
});

const updateTsConfig = (
strict,
configName,
references,
paths,
Expand Down Expand Up @@ -217,6 +220,8 @@ const updateTsConfig = (
return 0;
} catch (error) {
console.error(`could not read ${tsconfigFilePath}`, error);
if(strict)
throw new Error('Expect always a tsconfig.json in the package directory while running in strict mode')
}
};

Expand All @@ -232,6 +237,7 @@ const execute = async ({
verbose,
check,
usecase,
strict,
...configurable
}) => {
let changesCount = 0;
Expand Down Expand Up @@ -259,7 +265,8 @@ const execute = async ({
let {
configName,
rootConfigName,
createPathMappings
createPathMappings,
withoutRootConfig
} = configurable

if (fs.existsSync(path.join(cwd, usecase))) {
Expand All @@ -269,6 +276,7 @@ const execute = async ({
configName = yamlConfig.configName ?? configName
rootConfigName = yamlConfig.rootConfigName ?? rootConfigName
createPathMappings = yamlConfig.createPathMappings ?? createPathMappings
withoutRootConfig = yamlConfig.withoutRootConfig ?? withoutRootConfig
workspaces = [...(yamlConfig.packages ? yamlConfig.packages : []), ...(workspaces ? workspaces : [])];

if (verbose) {
Expand Down Expand Up @@ -322,6 +330,7 @@ const execute = async ({
}

changesCount += updateTsConfig(
strict,
detectedConfig,
references,
paths,
Expand All @@ -347,12 +356,15 @@ const execute = async ({
console.log('rootReferences', rootReferences);
console.log('rootPaths', rootPaths);
}
changesCount += updateTsConfig(
rootConfigName,
rootReferences,
rootPaths,
check, createPathMappings, {packageDir: cwd}
);
if(withoutRootConfig === false) {
changesCount += updateTsConfig(
strict,
rootConfigName,
rootReferences,
rootPaths,
check, createPathMappings, {packageDir: cwd},
);
}

if (verbose) {
console.log(`counted changes ${changesCount}`);
Expand Down
13 changes: 13 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ts-ref-yaml-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-b",
"shared/*",
"utils/**/"
],
"devDependencies": {
"typescript": "latest"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/shared/workspace-c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-c",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"peerDependencies": {
"foo-a": "1.0.0"
}
}
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,7 @@
{
"name": "workspace-d",
"version": "1.0.0",
"dependencies": {
"workspace-c": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
7 changes: 7 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/update-ts-references.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
withoutRootConfig: true
packages:
# all packages in subdirs of packages/ and components/
- 'workspace-a'
# exclude packages that are inside test directories
- '!**/tests/**'
- '!workspace-ignore'
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"
}
}
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,4 @@
{
"name": "foo-b",
"version": "1.0.0"
}
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,4 @@
{
"name": "test-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/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"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/workspace-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/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-ref-noroot-yaml/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-noroot-yaml/workspace-ignore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ignore",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
13 changes: 13 additions & 0 deletions test-scenarios/ts-ref-noroot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ts-ref-yaml-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-b",
"shared/*",
"utils/**/"
],
"devDependencies": {
"typescript": "latest"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-noroot/shared/workspace-c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-c",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"peerDependencies": {
"foo-a": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-noroot/shared/workspace-c/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
7 changes: 7 additions & 0 deletions test-scenarios/ts-ref-noroot/shared/workspace-d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-d",
"version": "1.0.0",
"dependencies": {
"workspace-c": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-noroot/shared/workspace-d/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/ts-ref-noroot/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-ref-noroot/update-ts-references.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
packages:
# all packages in subdirs of packages/ and components/
- 'workspace-a'
# exclude packages that are inside test directories
- '!**/tests/**'
- '!workspace-ignore'
Loading

0 comments on commit a6dc5a3

Please sign in to comment.