From 4a7a93db2a19e5c698ed6cc6e6465bdc851a89b7 Mon Sep 17 00:00:00 2001 From: Mirko Kruschke Date: Wed, 27 Mar 2024 17:47:32 +0100 Subject: [PATCH] feat: support different configuration files for different usecases (#39) --- README.md | 4 +- package.json | 2 +- src/index.js | 5 +- src/update-ts-references.js | 8 ++- .../ts-options-usecase-yaml/package.json | 13 +++++ .../tsconfig.root.json | 8 +++ .../update-ts-references.e2e.yaml | 9 +++ .../workspace-a/package.json | 7 +++ .../workspace-a/tsconfig.dev.json | 6 ++ .../workspace-b/package.json | 10 ++++ .../workspace-b/tsconfig.dev.json | 6 ++ .../workspace-ignore/package.json | 10 ++++ .../workspace-ignore/tsconfig.json | 6 ++ tests/setup.js | 4 +- tests/update-ts-references.yaml.test.js | 57 +++++++++++++++++++ 15 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 test-scenarios/ts-options-usecase-yaml/package.json create mode 100644 test-scenarios/ts-options-usecase-yaml/tsconfig.root.json create mode 100644 test-scenarios/ts-options-usecase-yaml/update-ts-references.e2e.yaml create mode 100644 test-scenarios/ts-options-usecase-yaml/workspace-a/package.json create mode 100644 test-scenarios/ts-options-usecase-yaml/workspace-a/tsconfig.dev.json create mode 100644 test-scenarios/ts-options-usecase-yaml/workspace-b/package.json create mode 100644 test-scenarios/ts-options-usecase-yaml/workspace-b/tsconfig.dev.json create mode 100644 test-scenarios/ts-options-usecase-yaml/workspace-ignore/package.json create mode 100644 test-scenarios/ts-options-usecase-yaml/workspace-ignore/tsconfig.json diff --git a/README.md b/README.md index 55c89d4..13a8966 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ npx update-ts-references --help --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 ``` @@ -99,7 +100,8 @@ Additional to that you can configure also the following options: Example configuration see [here](./test-scenarios/ts-options-yaml/update-ts-references.yaml) - +### using multiple configurations for different usecases +Executing update-ts-references with different configurations via the parameter `--usecase`. ## FAQ ### Why is my pnpm workspace alias not working? diff --git a/package.json b/package.json index bcb8872..3e85622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "update-ts-references", - "version": "3.2.1", + "version": "3.3.0", "description": "Updates TypeScript references automatically while using workspaces", "bin": "src/index.js", "scripts": { diff --git a/src/index.js b/src/index.js index 5027afa..05f518b 100755 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ const { h = defaultOptions.help, check = defaultOptions.check, createPathMappings = defaultOptions.createPathMappings, + usecase = defaultOptions.usecase } = minimist(process.argv.slice(2)); if (help || h) { console.log(` @@ -27,6 +28,7 @@ if (help || h) { --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} `); process.exit(0); } @@ -40,7 +42,8 @@ const run = async () => { configName, rootConfigName, createTsConfig, - createPathMappings + createPathMappings, + usecase }); if (check && changesCount > 0) { diff --git a/src/update-ts-references.js b/src/update-ts-references.js index 38d89ae..5936ad0 100644 --- a/src/update-ts-references.js +++ b/src/update-ts-references.js @@ -21,7 +21,8 @@ const defaultOptions = { verbose: false, help: false, check: false, - createPathMappings: false + createPathMappings: false, + usecase: 'update-ts-references.yaml' }; const getAllPackageJsons = async (workspaces, cwd) => { @@ -230,6 +231,7 @@ const execute = async ({ cwd, createTsConfig, verbose, check, + usecase, ...configurable }) => { let changesCount = 0; @@ -260,9 +262,9 @@ const execute = async ({ createPathMappings } = configurable - if (fs.existsSync(path.join(cwd, 'update-ts-references.yaml'))) { + if (fs.existsSync(path.join(cwd, usecase))) { const yamlConfig = yaml.load( - fs.readFileSync(path.join(cwd, 'update-ts-references.yaml')) + fs.readFileSync(path.join(cwd, usecase)) ); configName = yamlConfig.configName ?? configName rootConfigName = yamlConfig.rootConfigName ?? rootConfigName diff --git a/test-scenarios/ts-options-usecase-yaml/package.json b/test-scenarios/ts-options-usecase-yaml/package.json new file mode 100644 index 0000000..73a13fb --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/package.json @@ -0,0 +1,13 @@ +{ + "name": "ts-ref-yaml-workspace", + "version": "0.0.1", + "private": true, + "workspaces": [ + "workspace-b", + "shared/*", + "utils/**/" + ], + "devDependencies": { + "typescript": "latest" + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/tsconfig.root.json b/test-scenarios/ts-options-usecase-yaml/tsconfig.root.json new file mode 100644 index 0000000..23b6225 --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/tsconfig.root.json @@ -0,0 +1,8 @@ +{ + "files": [], + "compilerOptions": { + /* Basic Options */ + // "allowJs": true, + "composite": true + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/update-ts-references.e2e.yaml b/test-scenarios/ts-options-usecase-yaml/update-ts-references.e2e.yaml new file mode 100644 index 0000000..338df0d --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/update-ts-references.e2e.yaml @@ -0,0 +1,9 @@ +configName: 'tsconfig.dev.json' +rootConfigName: 'tsconfig.root.json' +createPathMappings: true +packages: + # all packages in subdirs of packages/ and components/ + - 'workspace-a' + # exclude packages that are inside test directories + - '!**/tests/**' + - '!workspace-ignore' diff --git a/test-scenarios/ts-options-usecase-yaml/workspace-a/package.json b/test-scenarios/ts-options-usecase-yaml/workspace-a/package.json new file mode 100644 index 0000000..6b40e48 --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/workspace-a/package.json @@ -0,0 +1,7 @@ +{ + "name": "workspace-a", + "version": "1.0.0", + "dependencies": { + "workspace-b": "1.0.0" + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/workspace-a/tsconfig.dev.json b/test-scenarios/ts-options-usecase-yaml/workspace-a/tsconfig.dev.json new file mode 100644 index 0000000..1dd8e13 --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/workspace-a/tsconfig.dev.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/workspace-b/package.json b/test-scenarios/ts-options-usecase-yaml/workspace-b/package.json new file mode 100644 index 0000000..552cf0e --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/workspace-b/package.json @@ -0,0 +1,10 @@ +{ + "name": "workspace-b", + "version": "1.0.0", + "dependencies": { + "cross-env": "5.0.5" + }, + "devDependencies": { + "foo-b": "1.0.0" + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/workspace-b/tsconfig.dev.json b/test-scenarios/ts-options-usecase-yaml/workspace-b/tsconfig.dev.json new file mode 100644 index 0000000..1dd8e13 --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/workspace-b/tsconfig.dev.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/workspace-ignore/package.json b/test-scenarios/ts-options-usecase-yaml/workspace-ignore/package.json new file mode 100644 index 0000000..280f2d0 --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/workspace-ignore/package.json @@ -0,0 +1,10 @@ +{ + "name": "ignore", + "version": "1.0.0", + "dependencies": { + "cross-env": "5.0.5" + }, + "devDependencies": { + "foo-b": "1.0.0" + } +} diff --git a/test-scenarios/ts-options-usecase-yaml/workspace-ignore/tsconfig.json b/test-scenarios/ts-options-usecase-yaml/workspace-ignore/tsconfig.json new file mode 100644 index 0000000..1dd8e13 --- /dev/null +++ b/test-scenarios/ts-options-usecase-yaml/workspace-ignore/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + } +} diff --git a/tests/setup.js b/tests/setup.js index 6c9ce0b..55155e9 100644 --- a/tests/setup.js +++ b/tests/setup.js @@ -1,7 +1,7 @@ const execSh = require('exec-sh').promise; const fs = require('fs'); -const setup = async (rootFolder, configName, rootConfigName, createTsConfig, createPathMappings) => { +const setup = async (rootFolder, configName, rootConfigName, createTsConfig, createPathMappings, usecase) => { if (!fs.existsSync(rootFolder)) { throw new Error(`folder is missing -> ${rootFolder}`); } @@ -12,7 +12,7 @@ const setup = async (rootFolder, configName, rootConfigName, createTsConfig, cre configName ? ` --configName ${configName}` : '' }${ rootConfigName ? ` --rootConfigName ${rootConfigName}` : '' - }${createTsConfig ? ` --createTsConfig` : ''}${createPathMappings ? ` --createPathMappings` : ''}`, + }${createTsConfig ? ` --createTsConfig` : ''}${createPathMappings ? ` --createPathMappings` : ''}${usecase ? ` --usecase ${usecase}` : ''}`, { cwd: rootFolder, } diff --git a/tests/update-ts-references.yaml.test.js b/tests/update-ts-references.yaml.test.js index 1fd1e5b..061a704 100644 --- a/tests/update-ts-references.yaml.test.js +++ b/tests/update-ts-references.yaml.test.js @@ -5,6 +5,7 @@ const {parse} = require("comment-json") const rootFolderTsRefYaml = path.join(process.cwd(), 'test-run', 'ts-ref-yaml'); const rootFolderTsOptionsYaml = path.join(process.cwd(), 'test-run', 'ts-options-yaml'); +const rootFolderUsecaseYaml = path.join(process.cwd(), 'test-run', 'ts-options-usecase-yaml'); const compilerOptions = {outDir: 'dist', rootDir: 'src'}; @@ -195,3 +196,59 @@ test('receive options via the config', async () => { parse(fs.readFileSync(path.join(rootFolderTsOptionsYaml, 'workspace-ignore', 'tsconfig.json')).toString()) ).toEqual({compilerOptions}); }); + +test('receive options for a different usecase', async () => { + await setup(rootFolderUsecaseYaml, undefined, undefined, undefined, undefined, 'update-ts-references.e2e.yaml'); + const root = [ + '.', + { + compilerOptions: { + composite: true, + }, + files: [], + references: [ + { + path: 'workspace-a/tsconfig.dev.json', + }, + { + path: 'workspace-b/tsconfig.dev.json', + }, + ], + }, + ]; + + const a = [ + './workspace-a', + { + compilerOptions: { + ...compilerOptions, + paths: {"workspace-b": ["../workspace-b/src"]} + }, + references: [ + { + path: '../workspace-b/tsconfig.dev.json', + }, + ], + }, + ]; + + const b = [ + './workspace-b', + { + compilerOptions, + }, + ]; + + [root,a,b].forEach((tsconfig) => { + const [configPath, config] = tsconfig; + + expect( + parse(fs.readFileSync(path.join(rootFolderUsecaseYaml, configPath, configPath === '.'?'tsconfig.root.json':'tsconfig.dev.json')).toString()) + ).toEqual(config); + }); + + // should not touch the ignore config + expect( + parse(fs.readFileSync(path.join(rootFolderUsecaseYaml, 'workspace-ignore', 'tsconfig.json')).toString()) + ).toEqual({compilerOptions}); +});