Skip to content

Commit 62ff92c

Browse files
committed
feat: exclude file from parameters if it's excluded in configuration file
1 parent 5cbb479 commit 62ff92c

File tree

4 files changed

+108
-11
lines changed

4 files changed

+108
-11
lines changed

cli.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env node
22

3-
const { spawnSync } = require('child_process')
43
const fs = require('fs')
4+
const { spawnSync } = require('child_process')
55
const { dirname, join } = require('path')
66

7+
const { minimatch } = require('minimatch')
8+
79
const randomChars = () => {
810
return Math.random().toString(36).slice(2)
911
}
@@ -29,24 +31,32 @@ const argsProjectValue =
2931
const argsLeaveIncludesUntouched =
3032
args.findIndex(arg => ['-i'].includes(arg)) !== -1
3133

32-
const files = args.filter(file => /\.(ts|tsx)$/.test(file))
34+
// Load existing config
35+
const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json')
36+
const tsconfigContent = fs.readFileSync(tsconfigPath).toString()
37+
// Use 'eval' to read the JSON as regular JavaScript syntax so that comments are allowed
38+
let tsconfig = {}
39+
eval(`tsconfig = ${tsconfigContent}`)
40+
41+
const isExcluded = file =>
42+
(tsconfig?.exclude ?? []).some(pattern => minimatch(file, pattern))
43+
44+
const filesInArgs = args.filter(file => /\.(ts|tsx)$/.test(file))
45+
46+
const files = filesInArgs.filter(file => !isExcluded(file))
47+
3348
if (files.length === 0) {
3449
process.exit(0)
3550
}
3651

37-
const remainingArgsToForward = args.slice().filter(arg => !files.includes(arg))
52+
const remainingArgsToForward = args
53+
.slice()
54+
.filter(arg => !filesInArgs.includes(arg))
3855

3956
if (argsProjectIndex !== -1) {
4057
remainingArgsToForward.splice(argsProjectIndex, 2)
4158
}
4259

43-
// Load existing config
44-
const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json')
45-
const tsconfigContent = fs.readFileSync(tsconfigPath).toString()
46-
// Use 'eval' to read the JSON as regular JavaScript syntax so that comments are allowed
47-
let tsconfig = {}
48-
eval(`tsconfig = ${tsconfigContent}`)
49-
5060
// Write a temp config file
5161
const tmpTsconfigPath = resolveFromRoot(`tsconfig.${randomChars()}.json`)
5262
const tmpTsconfig = {

package-lock.json

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
},
2323
"devDependencies": {
2424
"typescript": "4.1.3"
25+
},
26+
"dependencies": {
27+
"minimatch": "^9.0.3"
2528
}
2629
}

yarn.lock

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22
# yarn lockfile v1
33

44

5+
balanced-match@^1.0.0:
6+
version "1.0.2"
7+
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
8+
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
9+
10+
brace-expansion@^2.0.1:
11+
version "2.0.1"
12+
resolved "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz"
13+
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
14+
dependencies:
15+
balanced-match "^1.0.0"
16+
17+
minimatch@^9.0.3:
18+
version "9.0.3"
19+
resolved "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz"
20+
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
21+
dependencies:
22+
brace-expansion "^2.0.1"
23+
524
typescript@4.1.3:
625
version "4.1.3"
7-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
26+
resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz"
827
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==

0 commit comments

Comments
 (0)