Skip to content

Commit 1a03a62

Browse files
authoredMar 27, 2024··
refactor: use depseek for deps extraction (#746)
* refactor: use depseek for deps parsing * style: linting * test: extend deps capture test
1 parent cafb90d commit 1a03a62

File tree

7 files changed

+32
-32
lines changed

7 files changed

+32
-32
lines changed
 

‎package-lock.json

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"@webpod/ps": "^0.0.0-beta.2",
6868
"c8": "^9.1.0",
6969
"chalk": "^5.3.0",
70+
"depseek": "^0.4.1",
7071
"dts-bundle-generator": "^9.3.1",
7172
"esbuild": "^0.20.2",
7273
"esbuild-node-externals": "^1.13.0",

‎scripts/build-dts.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const entry = {
3737
'@types/which',
3838
'zurk',
3939
'@webpod/ps',
40+
'depseek',
4041
], // args['external-inlines'],
4142
},
4243
output: {

‎src/deps.ts

+16-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
// limitations under the License.
1414

1515
import { $ } from './core.js'
16-
import { spinner } from './experimental.js'
16+
import { spinner } from './goods.js'
17+
import { depseek } from './vendor.js'
1718

1819
export async function installDeps(
1920
dependencies: Record<string, string>,
@@ -88,39 +89,25 @@ const builtins = new Set([
8889
'worker_threads',
8990
'zlib',
9091
])
91-
const importRe = [
92-
/\bimport\s+['"](?<path>[^'"]+)['"]/,
93-
/\bimport\(['"](?<path>[^'"]+)['"]\)/,
94-
/\brequire\(['"](?<path>[^'"]+)['"]\)/,
95-
/\bfrom\s+['"](?<path>[^'"]+)['"]/,
96-
]
92+
9793
const nameRe =
9894
/^(?<name>(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*)\/?.*$/i
99-
const versionRe =
100-
/(\/\/|\/\*)\s*@(?<version>[~^]?(v?[\dx*]+([-.][\d*a-z-]+)*))/i
95+
const versionRe = /^@(?<version>[~^]?(v?[\dx*]+([-.][\d*a-z-]+)*))/i
10196

10297
export function parseDeps(content: Buffer): Record<string, string> {
103-
const deps: Record<string, string> = {}
104-
const lines = content.toString().split('\n')
105-
for (let line of lines) {
106-
const tuple = parseImports(line)
107-
if (tuple) {
108-
deps[tuple.name] = tuple.version
109-
}
110-
}
111-
return deps
112-
}
113-
114-
function parseImports(
115-
line: string
116-
): { name: string; version: string } | undefined {
117-
for (let re of importRe) {
118-
const name = parsePackageName(re.exec(line)?.groups?.path)
119-
const version = parseVersion(line)
120-
if (name) {
121-
return { name, version }
98+
return depseek(content.toString() + '\n', { comments: true }).reduce<
99+
Record<string, string>
100+
>((m, { type, value }, i, list) => {
101+
if (type === 'dep') {
102+
const meta = list[i + 1]
103+
const name = parsePackageName(value)
104+
const version =
105+
(meta?.type === 'comment' && parseVersion(meta?.value.trim())) ||
106+
'latest'
107+
if (name) m[name] = version
122108
}
123-
}
109+
return m
110+
}, {})
124111
}
125112

126113
function parsePackageName(path?: string): string | undefined {

‎src/vendor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const YAML: {
5050

5151
export const fs: typeof import('fs-extra') = _fs
5252

53+
export { depseekSync as depseek } from 'depseek'
5354
export { type Options as GlobbyOptions } from 'globby'
5455
export { default as chalk, type ChalkInstance } from 'chalk'
5556
export { default as which } from 'which'

‎test/deps.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ describe('deps', () => {
9595
import fs from 'fs'
9696
import path from 'path'
9797
import foo from "foo"
98+
// import aaa from 'a'
99+
/* import bbb from 'b' */
98100
import bar from "bar" /* @1.0.0 */
99101
import baz from "baz" // @^2.0
100102
import qux from "@qux/pkg/entry" // @^3.0

‎test/fixtures/js-project/package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.