Skip to content

Commit

Permalink
feat: introduce option --strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko Kruschke committed Nov 20, 2024
1 parent 952d923 commit 62119f0
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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);
}
Expand All @@ -46,7 +48,8 @@ const run = async () => {
withoutRootConfig,
createTsConfig,
createPathMappings,
usecase
usecase,
strict
});

if (check && changesCount > 0) {
Expand Down
11 changes: 9 additions & 2 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -167,6 +168,7 @@ const ensurePosixPathStyle = (reference) => ({
});

const updateTsConfig = (
strict,
configName,
references,
paths,
Expand Down Expand Up @@ -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')
}
};

Expand All @@ -233,6 +237,7 @@ const execute = async ({
verbose,
check,
usecase,
strict,
...configurable
}) => {
let changesCount = 0;
Expand Down Expand Up @@ -325,6 +330,7 @@ const execute = async ({
}

changesCount += updateTsConfig(
strict,
detectedConfig,
references,
paths,
Expand Down Expand Up @@ -352,10 +358,11 @@ const execute = async ({
}
if(withoutRootConfig === false) {
changesCount += updateTsConfig(
strict,
rootConfigName,
rootReferences,
rootPaths,
check, createPathMappings, {packageDir: cwd}
check, createPathMappings, {packageDir: cwd},
);
}

Expand Down
14 changes: 14 additions & 0 deletions test-scenarios/yarn-ws-check-strict/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "yarn-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-a",
"workspace-b",
"shared/*",
"utils/**"
],
"devDependencies": {
"typescript": "latest"
}
}
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,12 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [
{
"path": "../../utils/foos/foo-a"
}
]
}

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,12 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [
{
"path": "../workspace-c"
}
]
}

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"
}
}
12 changes: 12 additions & 0 deletions test-scenarios/yarn-ws-check-strict/utils/foos/foo-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [
{
"path": "../foo-b"
}
]
}

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,7 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}

10 changes: 10 additions & 0 deletions test-scenarios/yarn-ws-check-strict/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"
}
}
15 changes: 15 additions & 0 deletions test-scenarios/yarn-ws-check-strict/workspace-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [
{
"path": "../utils/foos/foo-a"
},
{
"path": "../workspace-b"
}
]
}

10 changes: 10 additions & 0 deletions test-scenarios/yarn-ws-check-strict/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"
}
}
12 changes: 12 additions & 0 deletions test-scenarios/yarn-ws-check-strict/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"references": [
{
"path": "../utils/foos/foo-b"
}
]
}

72 changes: 72 additions & 0 deletions test-scenarios/yarn-ws-check-strict/yarn.lock
Original file line number Diff line number Diff line change
@@ -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=
4 changes: 2 additions & 2 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
Expand All @@ -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,
}
Expand Down
22 changes: 20 additions & 2 deletions tests/update-ts-references.check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -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;
Expand Down

0 comments on commit 62119f0

Please sign in to comment.