Skip to content

Commit 6b2f6a1

Browse files
authored
Merge pull request #80 from SocketDev/lenient-args
make argv parsing more tolerant and align with GH
2 parents 6b68ea1 + cb71763 commit 6b2f6a1

File tree

4 files changed

+62
-95
lines changed

4 files changed

+62
-95
lines changed

lib/utils/path-resolve.js

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { globby } from 'globby'
55
import ignore from 'ignore'
66
// @ts-ignore This package provides no types
77
import { directories } from 'ignore-by-default'
8-
import micromatch from 'micromatch'
98
import { ErrorWithCause } from 'pony-cause'
109

1110
import { InputError } from './errors.js'
@@ -94,85 +93,65 @@ export async function mapGlobResultToFiles (entries, supportedFiles) {
9493
* @throws {InputError}
9594
*/
9695
export async function mapGlobEntryToFiles (entry, supportedFiles) {
97-
/** @type {string|undefined} */
98-
let pkgJSFile
99-
/** @type {string[]} */
100-
let jsLockFiles = []
101-
/** @type {string[]} */
102-
let pyFiles = []
103-
/** @type {string|undefined} */
104-
let pkgGoFile
105-
/** @type {string[]} */
106-
let goExtraFiles = []
107-
10896
const jsSupported = supportedFiles['npm'] || {}
109-
const jsLockFilePatterns = Object.keys(jsSupported)
110-
.filter(key => key !== 'packagejson')
111-
.map(key => /** @type {{ pattern: string }} */ (jsSupported[key]).pattern)
97+
const jsLockFilePatterns = Object.values(jsSupported)
98+
// .filter(key => key !== 'packagejson')
99+
.map(p => `**/${/** @type {{ pattern: string }} */ (p).pattern}`)
112100

113101
const pyFilePatterns = Object.values(supportedFiles['pypi'] || {})
114-
.map(p => /** @type {{ pattern: string }} */ (p).pattern)
102+
.map(p => `**/${/** @type {{ pattern: string }} */ (p).pattern}`)
115103

116104
const goSupported = supportedFiles['go'] || {}
117-
const goSupplementalPatterns = Object.keys(goSupported)
118-
.filter(key => key !== 'gomod')
119-
.map(key => /** @type {{ pattern: string }} */ (goSupported[key]).pattern)
120-
121-
if (entry.endsWith('/')) {
122-
// If the match is a folder and that folder contains a package.json file, then include it
123-
const jsPkg = path.resolve(entry, 'package.json')
124-
if (await fileExists(jsPkg)) pkgJSFile = jsPkg
125-
126-
const goPkg = path.resolve(entry, 'go.mod')
127-
if (await fileExists(goPkg)) pkgGoFile = goPkg
128-
129-
pyFiles = await globby(pyFilePatterns, {
130-
...BASE_GLOBBY_OPTS,
131-
cwd: entry
132-
})
133-
} else {
134-
const entryFile = path.basename(entry)
135-
136-
if (entryFile === 'package.json') {
137-
// If the match is a package.json file, then include it
138-
pkgJSFile = entry
139-
} else if (micromatch.isMatch(entryFile, jsLockFilePatterns)) {
140-
jsLockFiles = [entry]
141-
pkgJSFile = path.resolve(path.dirname(entry), 'package.json')
142-
if (!(await fileExists(pkgJSFile))) return []
143-
} else if (entryFile === 'go.mod') {
144-
pkgGoFile = entry
145-
} else if (micromatch.isMatch(entryFile, goSupplementalPatterns)) {
146-
goExtraFiles = [entry]
147-
pkgGoFile = path.resolve(path.dirname(entry), 'go.mod')
148-
} else if (micromatch.isMatch(entryFile, pyFilePatterns)) {
149-
pyFiles = [entry]
150-
}
151-
}
152-
153-
// If we will include a package.json file but don't already have a corresponding lockfile, then look for one
154-
if (!jsLockFiles.length && pkgJSFile) {
155-
const pkgDir = path.dirname(pkgJSFile)
156-
157-
jsLockFiles = await globby(jsLockFilePatterns, {
158-
...BASE_GLOBBY_OPTS,
159-
cwd: pkgDir
160-
})
161-
}
162-
163-
if (!goExtraFiles.length && pkgGoFile) {
164-
// get go.sum whenever possible
165-
const pkgDir = path.dirname(pkgGoFile)
166-
167-
goExtraFiles = await globby(goSupplementalPatterns, {
168-
...BASE_GLOBBY_OPTS,
169-
cwd: pkgDir
170-
})
171-
}
172-
173-
return [...jsLockFiles, ...pyFiles, ...goExtraFiles]
174-
.concat(pkgJSFile ? [pkgJSFile] : [])
175-
.concat(pkgGoFile ? [pkgGoFile] : [])
105+
const goSupplementalPatterns = Object.values(goSupported)
106+
// .filter(key => key !== 'gomod')
107+
.map(p => `**/${/** @type {{ pattern: string }} */ (p).pattern}`)
108+
109+
const files = await globby([
110+
...jsLockFilePatterns,
111+
...pyFilePatterns,
112+
...goSupplementalPatterns
113+
], {
114+
...BASE_GLOBBY_OPTS,
115+
onlyFiles: true,
116+
cwd: path.resolve((await stat(entry)).isDirectory() ? entry : path.dirname(entry))
117+
})
118+
return files
119+
120+
// if (entry.endsWith('/')) {
121+
// // If the match is a folder and that folder contains a package.json file, then include it
122+
// const jsPkg = path.resolve(entry, 'package.json')
123+
// if (await fileExists(jsPkg)) pkgJSFile = jsPkg
124+
125+
// const goPkg = path.resolve(entry, 'go.mod')
126+
// if (await fileExists(goPkg)) pkgGoFile = goPkg
127+
128+
// pyFiles = await globby(pyFilePatterns, {
129+
// ...BASE_GLOBBY_OPTS,
130+
// cwd: entry
131+
// })
132+
// } else {
133+
// const entryFile = path.basename(entry)
134+
135+
// if (entryFile === 'package.json') {
136+
// // If the match is a package.json file, then include it
137+
// pkgJSFile = entry
138+
// } else if (micromatch.isMatch(entryFile, jsLockFilePatterns)) {
139+
// jsLockFiles = [entry]
140+
// pkgJSFile = path.resolve(path.dirname(entry), 'package.json')
141+
// if (!(await fileExists(pkgJSFile))) return []
142+
// } else if (entryFile === 'go.mod') {
143+
// pkgGoFile = entry
144+
// } else if (micromatch.isMatch(entryFile, goSupplementalPatterns)) {
145+
// goExtraFiles = [entry]
146+
// pkgGoFile = path.resolve(path.dirname(entry), 'go.mod')
147+
// } else if (micromatch.isMatch(entryFile, pyFilePatterns)) {
148+
// pyFiles = [entry]
149+
// }
150+
// }
151+
152+
// return [...jsLockFiles, ...pyFiles, ...goExtraFiles]
153+
// .concat(pkgJSFile ? [pkgJSFile] : [])
154+
// .concat(pkgGoFile ? [pkgGoFile] : [])
176155
}
177156

178157
/**

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"is-interactive": "^2.0.0",
9292
"is-unicode-supported": "^1.3.0",
9393
"meow": "^12.0.1",
94-
"micromatch": "^4.0.5",
9594
"ora": "^6.1.2",
9695
"pony-cause": "^2.1.8",
9796
"prompts": "^2.4.2",

test/path-resolve.test.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,11 @@ describe('Path Resolve', () => {
124124
assert.deepEqual(await sortedMapGlobEntry('/foo.txt', globPatterns), [])
125125
})
126126

127-
it('should throw on errors', async () => {
127+
it('should be lenient on oddities', async () => {
128128
mockFs({
129129
'/package.json': { /* Empty directory */ },
130130
})
131-
await assert.rejects(sortedMapGlobEntry('/', globPatterns), (e) => {
132-
return e instanceof InputError && e.message.includes('Expected \'/package.json\' to be a file')
133-
})
131+
await assert.deepEqual(await sortedMapGlobEntry('/', globPatterns), [])
134132
})
135133
})
136134

@@ -157,7 +155,7 @@ describe('Path Resolve', () => {
157155
mockFs({
158156
'/package-lock.json': '{}',
159157
})
160-
assert.deepEqual(await sortedMapGlobEntry('/', globPatterns), [])
158+
assert.deepEqual(await sortedMapGlobEntry('/', globPatterns), ['/package-lock.json'])
161159
})
162160

163161
it('should support alternative lock files', async () => {
@@ -191,19 +189,11 @@ describe('Path Resolve', () => {
191189
assert.strict.deepEqual(await sortedMapGlobEntry('/package.json', globPatterns), ['/package.json'])
192190
})
193191

194-
it('should not validate the input file', async () => {
192+
it('should validate the input file', async () => {
195193
mockFs({})
196-
assert.deepEqual(await sortedMapGlobEntry('/package.json', globPatterns), ['/package.json'])
197-
})
198-
199-
it('should not validate the input file, but still add a complementary lock file', async () => {
200-
mockFs({
201-
'/package-lock.json': '{}',
194+
return assert.rejects(sortedMapGlobEntry('/package.json', globPatterns), (err) => {
195+
return err instanceof Error && err.message.includes('ENOENT')
202196
})
203-
assert.deepEqual(await sortedMapGlobEntry('/package.json', globPatterns), [
204-
'/package-lock.json',
205-
'/package.json'
206-
])
207197
})
208198

209199
it('should support alternative lock files', async () => {

0 commit comments

Comments
 (0)