Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use depseek for deps extraction #746

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@webpod/ps": "^0.0.0-beta.2",
"c8": "^9.1.0",
"chalk": "^5.3.0",
"depseek": "^0.4.1",
"dts-bundle-generator": "^9.3.1",
"esbuild": "^0.20.2",
"esbuild-node-externals": "^1.13.0",
Expand Down
1 change: 1 addition & 0 deletions scripts/build-dts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const entry = {
'@types/which',
'zurk',
'@webpod/ps',
'depseek',
], // args['external-inlines'],
},
output: {
Expand Down
45 changes: 16 additions & 29 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// limitations under the License.

import { $ } from './core.js'
import { spinner } from './experimental.js'
import { spinner } from './goods.js'
import { depseek } from './vendor.js'

export async function installDeps(
dependencies: Record<string, string>,
Expand Down Expand Up @@ -88,39 +89,25 @@ const builtins = new Set([
'worker_threads',
'zlib',
])
const importRe = [
/\bimport\s+['"](?<path>[^'"]+)['"]/,
/\bimport\(['"](?<path>[^'"]+)['"]\)/,
/\brequire\(['"](?<path>[^'"]+)['"]\)/,
/\bfrom\s+['"](?<path>[^'"]+)['"]/,
]

const nameRe =
/^(?<name>(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*)\/?.*$/i
const versionRe =
/(\/\/|\/\*)\s*@(?<version>[~^]?(v?[\dx*]+([-.][\d*a-z-]+)*))/i
const versionRe = /^@(?<version>[~^]?(v?[\dx*]+([-.][\d*a-z-]+)*))/i

export function parseDeps(content: Buffer): Record<string, string> {
const deps: Record<string, string> = {}
const lines = content.toString().split('\n')
for (let line of lines) {
const tuple = parseImports(line)
if (tuple) {
deps[tuple.name] = tuple.version
}
}
return deps
}

function parseImports(
line: string
): { name: string; version: string } | undefined {
for (let re of importRe) {
const name = parsePackageName(re.exec(line)?.groups?.path)
const version = parseVersion(line)
if (name) {
return { name, version }
return depseek(content.toString() + '\n', { comments: true }).reduce<
Record<string, string>
>((m, { type, value }, i, list) => {
if (type === 'dep') {
const meta = list[i + 1]
const name = parsePackageName(value)
const version =
(meta?.type === 'comment' && parseVersion(meta?.value.trim())) ||
'latest'
if (name) m[name] = version
}
}
return m
}, {})
}

function parsePackageName(path?: string): string | undefined {
Expand Down
1 change: 1 addition & 0 deletions src/vendor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const YAML: {

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

export { depseekSync as depseek } from 'depseek'
export { type Options as GlobbyOptions } from 'globby'
export { default as chalk, type ChalkInstance } from 'chalk'
export { default as which } from 'which'
Expand Down
2 changes: 2 additions & 0 deletions test/deps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ describe('deps', () => {
import fs from 'fs'
import path from 'path'
import foo from "foo"
// import aaa from 'a'
/* import bbb from 'b' */
import bar from "bar" /* @1.0.0 */
import baz from "baz" // @^2.0
import qux from "@qux/pkg/entry" // @^3.0
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/js-project/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading