diff --git a/README.md b/README.md index fd5b6d3..df8eac0 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ npx update-ts-references --help --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 diff --git a/src/index.js b/src/index.js index c3c492e..54c72d0 100755 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,8 @@ const { 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(` @@ -31,6 +32,7 @@ if (help || h) { --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); } @@ -46,7 +48,8 @@ const run = async () => { withoutRootConfig, createTsConfig, createPathMappings, - usecase + usecase, + strict }); if (check && changesCount > 0) { diff --git a/src/update-ts-references.js b/src/update-ts-references.js index 6519711..53168c7 100644 --- a/src/update-ts-references.js +++ b/src/update-ts-references.js @@ -23,7 +23,8 @@ const defaultOptions = { help: false, check: false, createPathMappings: false, - usecase: 'update-ts-references.yaml' + usecase: 'update-ts-references.yaml', + strict: false }; const getAllPackageJsons = async (workspaces, cwd) => { @@ -167,6 +168,7 @@ const ensurePosixPathStyle = (reference) => ({ }); const updateTsConfig = ( + strict, configName, references, paths, @@ -218,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') } }; @@ -233,6 +237,7 @@ const execute = async ({ verbose, check, usecase, + strict, ...configurable }) => { let changesCount = 0; @@ -325,6 +330,7 @@ const execute = async ({ } changesCount += updateTsConfig( + strict, detectedConfig, references, paths, @@ -352,10 +358,11 @@ const execute = async ({ } if(withoutRootConfig === false) { changesCount += updateTsConfig( + strict, rootConfigName, rootReferences, rootPaths, - check, createPathMappings, {packageDir: cwd} + check, createPathMappings, {packageDir: cwd}, ); } diff --git a/test-scenarios/yarn-ws-check-strict/package.json b/test-scenarios/yarn-ws-check-strict/package.json new file mode 100644 index 0000000..f5aeb24 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/package.json @@ -0,0 +1,14 @@ +{ + "name": "yarn-workspace", + "version": "0.0.1", + "private": true, + "workspaces": [ + "workspace-a", + "workspace-b", + "shared/*", + "utils/**" + ], + "devDependencies": { + "typescript": "latest" + } +} diff --git a/test-scenarios/yarn-ws-check-strict/shared/workspace-c/package.json b/test-scenarios/yarn-ws-check-strict/shared/workspace-c/package.json new file mode 100644 index 0000000..a401b15 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/shared/workspace-c/package.json @@ -0,0 +1,10 @@ +{ + "name": "workspace-c", + "version": "1.0.0", + "dependencies": { + "cross-env": "5.0.5" + }, + "peerDependencies": { + "foo-a": "1.0.0" + } +} diff --git a/test-scenarios/yarn-ws-check-strict/shared/workspace-c/tsconfig.json b/test-scenarios/yarn-ws-check-strict/shared/workspace-c/tsconfig.json new file mode 100644 index 0000000..a0a7fc7 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/shared/workspace-c/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "references": [ + { + "path": "../../utils/foos/foo-a" + } + ] +} + \ No newline at end of file diff --git a/test-scenarios/yarn-ws-check-strict/shared/workspace-d/package.json b/test-scenarios/yarn-ws-check-strict/shared/workspace-d/package.json new file mode 100644 index 0000000..e5a993c --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/shared/workspace-d/package.json @@ -0,0 +1,7 @@ +{ + "name": "workspace-d", + "version": "1.0.0", + "dependencies": { + "workspace-c": "1.0.0" + } +} diff --git a/test-scenarios/yarn-ws-check-strict/shared/workspace-d/tsconfig.json b/test-scenarios/yarn-ws-check-strict/shared/workspace-d/tsconfig.json new file mode 100644 index 0000000..101d006 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/shared/workspace-d/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "references": [ + { + "path": "../workspace-c" + } + ] +} + \ No newline at end of file diff --git a/test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/package.json b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/package.json new file mode 100644 index 0000000..699ec9c --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/package.json @@ -0,0 +1,7 @@ +{ + "name": "foo-a", + "version": "1.0.0", + "dependencies": { + "foo-b": "1.0.0" + } +} diff --git a/test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/tsconfig.json b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/tsconfig.json new file mode 100644 index 0000000..ca25224 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "references": [ + { + "path": "../foo-b" + } + ] +} + \ No newline at end of file diff --git a/test-scenarios/yarn-ws-check-strict/utils/foos/foo-b/package.json b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-b/package.json new file mode 100644 index 0000000..2c997b8 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-b/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo-b", + "version": "1.0.0" +} diff --git a/test-scenarios/yarn-ws-check-strict/utils/foos/foo-b/tsconfig.json b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-b/tsconfig.json new file mode 100644 index 0000000..465682e --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/utils/foos/foo-b/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + } +} + \ No newline at end of file diff --git a/test-scenarios/yarn-ws-check-strict/workspace-a/package.json b/test-scenarios/yarn-ws-check-strict/workspace-a/package.json new file mode 100644 index 0000000..f35aa29 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/workspace-a/package.json @@ -0,0 +1,10 @@ +{ + "name": "workspace-a", + "version": "1.0.0", + "dependencies": { + "workspace-b": "1.0.0" + }, + "devDependencies": { + "foo-a": "1.0.0" + } +} diff --git a/test-scenarios/yarn-ws-check-strict/workspace-a/tsconfig.json b/test-scenarios/yarn-ws-check-strict/workspace-a/tsconfig.json new file mode 100644 index 0000000..dbce318 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/workspace-a/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "references": [ + { + "path": "../utils/foos/foo-a" + }, + { + "path": "../workspace-b" + } + ] +} + \ No newline at end of file diff --git a/test-scenarios/yarn-ws-check-strict/workspace-b/package.json b/test-scenarios/yarn-ws-check-strict/workspace-b/package.json new file mode 100644 index 0000000..552cf0e --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/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/yarn-ws-check-strict/workspace-b/tsconfig.json b/test-scenarios/yarn-ws-check-strict/workspace-b/tsconfig.json new file mode 100644 index 0000000..a168534 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/workspace-b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "references": [ + { + "path": "../utils/foos/foo-b" + } + ] +} + \ No newline at end of file diff --git a/test-scenarios/yarn-ws-check-strict/yarn.lock b/test-scenarios/yarn-ws-check-strict/yarn.lock new file mode 100644 index 0000000..da3e0c5 --- /dev/null +++ b/test-scenarios/yarn-ws-check-strict/yarn.lock @@ -0,0 +1,72 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +cross-env@5.0.5: + version "5.0.5" + resolved "https://registry.npmjs.org/cross-env/-/cross-env-5.0.5.tgz#4383d364d9660873dd185b398af3bfef5efffef3" + integrity sha1-Q4PTZNlmCHPdGFs5ivO/717//vM= + dependencies: + cross-spawn "^5.1.0" + is-windows "^1.0.0" + +cross-spawn@^5.1.0: + version "5.1.0" + resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" + integrity sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk= + dependencies: + lru-cache "^4.0.1" + shebang-command "^1.2.0" + which "^1.2.9" + +is-windows@^1.0.0: + version "1.0.2" + resolved "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d" + integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA== + +isexe@^2.0.0: + version "2.0.0" + resolved "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" + integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA= + +lru-cache@^4.0.1: + version "4.1.5" + resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd" + integrity sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g== + dependencies: + pseudomap "^1.0.2" + yallist "^2.1.2" + +pseudomap@^1.0.2: + version "1.0.2" + resolved "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" + integrity sha1-8FKijacOYYkX7wqKw0wa5aaChrM= + +shebang-command@^1.2.0: + version "1.2.0" + resolved "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea" + integrity sha1-RKrGW2lbAzmJaMOfNj/uXer98eo= + dependencies: + shebang-regex "^1.0.0" + +shebang-regex@^1.0.0: + version "1.0.0" + resolved "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" + integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM= + +typescript@latest: + version "4.0.5" + resolved "https://registry.npmjs.org/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389" + integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ== + +which@^1.2.9: + version "1.3.1" + resolved "https://registry.npmjs.org/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" + integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ== + dependencies: + isexe "^2.0.0" + +yallist@^2.1.2: + version "2.1.2" + resolved "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52" + integrity sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI= diff --git a/tests/setup.js b/tests/setup.js index 9c5c0b7..fd750a6 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, usecase, withoutRootConfig) => { +const setup = async (rootFolder, configName, rootConfigName, createTsConfig, createPathMappings, usecase, withoutRootConfig,strict) => { 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` : ''}${usecase ? ` --usecase ${usecase}` : ''}${withoutRootConfig? '--withoutRootConfig' : ''}`, + }${createTsConfig ? ` --createTsConfig` : ''}${createPathMappings ? ` --createPathMappings` : ''}${usecase ? ` --usecase ${usecase}` : ''}${withoutRootConfig? '--withoutRootConfig' : ''}${strict? '--strict' : ''}`, { cwd: rootFolder, } diff --git a/tests/update-ts-references.check.test.js b/tests/update-ts-references.check.test.js index 541e85c..7f0dfd8 100644 --- a/tests/update-ts-references.check.test.js +++ b/tests/update-ts-references.check.test.js @@ -13,6 +13,11 @@ const rootFolderYarnCheckNoChanges = path.join( 'test-run', 'yarn-ws-check-no-changes' ); +const rootFolderYarnCheckStrict = path.join( + process.cwd(), + 'test-run', + 'yarn-ws-check-strict' +); const rootFolderYarnCheckPaths = path.join( process.cwd(), 'test-run', @@ -144,7 +149,7 @@ const tsconfigs = [ test('Detect changes in references with the --check option', async () => { let errorCode = 0; try { - await execSh('npx update-ts-references --check', { + await execSh('npx update-ts-references --check --strict', { stdio: null, cwd: rootFolderYarnCheck, }); @@ -166,7 +171,7 @@ test('Detect changes in references with the --check option', async () => { test('No changes in references detected with the --check option', async () => { let errorCode = 0; try { - await execSh('npx update-ts-references --check', { + await execSh('npx update-ts-references --check --strict', { stdio: null, cwd: rootFolderYarnCheckNoChanges, }); @@ -185,6 +190,19 @@ test('No changes in references detected with the --check option', async () => { }); }); +test('No changes in references detected with the --check option', async () => { + let errorCode = 0; + try { + await execSh('npx update-ts-references --check --strict', { + stdio: null, + cwd: rootFolderYarnCheckStrict, + }); + } catch (e) { + errorCode = e.code; + } + + expect(errorCode).toBe(1); +}); test('Detect changes in paths with the --check option', async () => { let errorCode = 0;