From 59cab3450415161ede1f21af4b2ba70cc9dd369a Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:08:54 +0530 Subject: [PATCH] chore: support `-x` (exclude) argument update script again, fix logic, cleanup code --- deno.lock | 7 +++-- deps.ts | 2 ++ scripts/update.ts | 78 ++++++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/deno.lock b/deno.lock index 1dfd100..febbbf0 100644 --- a/deno.lock +++ b/deno.lock @@ -36,13 +36,12 @@ "jsr:@std/path@1.0.0-rc.2": "jsr:@std/path@1.0.0-rc.2", "jsr:@std/path@^0.221.0": "jsr:@std/path@0.221.0", "jsr:@std/path@^1.0.0": "jsr:@std/path@1.0.0", - "jsr:@std/path@^1.0.0-rc.4": "jsr:@std/path@1.0.0", "jsr:@std/regexp@^1.0.0": "jsr:@std/regexp@1.0.0", - "jsr:@std/regexp@^1.0.0-rc.1": "jsr:@std/regexp@1.0.0", "jsr:@std/semver@^1.0.0-rc.2": "jsr:@std/semver@1.0.0-rc.2", "jsr:@std/streams@0.221.0": "jsr:@std/streams@0.221.0", "jsr:@std/streams@^1.0.0-rc.3": "jsr:@std/streams@1.0.0-rc.3", "jsr:@std/text@1.0.0-rc.1": "jsr:@std/text@1.0.0-rc.1", + "npm:@types/node": "npm:@types/node@18.16.19", "npm:chokidar@^3.6.0": "npm:chokidar@3.6.0", "npm:zod@^3.23.8": "npm:zod@3.23.8" }, @@ -210,6 +209,10 @@ } }, "npm": { + "@types/node@18.16.19": { + "integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==", + "dependencies": {} + }, "anymatch@3.1.3": { "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", "dependencies": { diff --git a/deps.ts b/deps.ts index 30bbfe3..6cde0c4 100644 --- a/deps.ts +++ b/deps.ts @@ -1,3 +1,5 @@ +import type {} from 'npm:@types/node' + export { abortable, deadline, debounce, delay, retry } from 'jsr:@std/async@^1.0.0-rc.4' export { walk } from 'jsr:@std/fs@^1.0.0-rc.5' export { serveDir, type ServeDirOptions, STATUS_CODE, STATUS_TEXT, type StatusCode } from 'jsr:@std/http@^1.0.0-rc.4' diff --git a/scripts/update.ts b/scripts/update.ts index 839894c..556646c 100644 --- a/scripts/update.ts +++ b/scripts/update.ts @@ -17,6 +17,7 @@ import { is, loadGraph, Mutex, + parseArgs, parseFromJson, resolve, type ResolvedDependency, @@ -60,52 +61,36 @@ const isJsrPackageMeta = is.ObjectOf({ // #endregion +const args = parseArgs(Deno.args, { collect: ['x'] }) +const excludes = (args.x ?? []) as string[] + +// update deps in deno.json + +let importMap = await Deno.readTextFile('deno.json') +const denoJson = JSON.parse(importMap) as { imports: Record | undefined } + +denoJson.imports = Object.fromEntries( + await Promise.all( + Object.entries(denoJson.imports ?? {}).map(async ([key, value]) => [key, await updateSpecifier(value)]), + ), +) + +await Deno.writeTextFile('deno.json', importMap = JSON.stringify(denoJson, null, 2)) +await $`deno fmt deno.json` + // update deps in files const files = (await $`git ls-files -- '*.ts'`.text()) - .split('\n') - .filter(Boolean) - .map((file) => toFileUrl(resolve(file)).href) + .split('\n').filter(Boolean).map((file) => toFileUrl(resolve(file)).href) -const importMap = await Deno.readTextFile('deno.json') const resolvedImportMap = await parseFromJson(toFileUrl(resolve('deno.json')), importMap, { expandImports: true }) const graph = await createGraph(files, { resolve: resolvedImportMap.resolve.bind(resolvedImportMap), - // deno-lint-ignore require-await - load: async (specifier) => { - return files.includes(specifier) ? loadGraph(specifier) : undefined - }, + load: async (specifier) => files.includes(specifier) ? await loadGraph(specifier) : undefined, }) -const modules = graph.modules - .filter(({ specifier }) => files.includes(specifier)) - .map((mod) => ({ - specifier: mod.specifier, - dependencies: (mod.dependencies ?? []).filter((dep) => - // deno-lint-ignore no-explicit-any - supportedProtocols.includes(URL.parse(dep.specifier)?.protocol as any) - ), - })) - .filter((mod) => mod.dependencies.length) - -for (const mod of modules) { - updateDepsInFile(fromFileUrl(mod.specifier), mod.dependencies) -} - -// update deps in deno.json - -const denoJson = JSON.parse(importMap) as { imports: Record | undefined } -const newImports = { ...denoJson.imports } - -for (const [key, value] of Object.entries(denoJson.imports ?? {})) { - newImports[key] = await updateSpecifier(value) -} - -denoJson.imports = newImports - -await Deno.writeTextFile('deno.json', JSON.stringify(denoJson, null, 2)) -await $`deno fmt deno.json` +await Promise.all(graph.modules.map((mod) => updateDepsInFile(mod.specifier, mod.dependencies))) // regenerate lock file @@ -123,7 +108,10 @@ await $`deno cache --reload --lock=deno.lock ${files.join(' ')}` // #region Update logic -async function updateDepsInFile(path: string, deps: DependencyJson[]) { +async function updateDepsInFile(url: string, deps?: DependencyJson[]) { + if (!files.includes(url) || !deps?.length) return + const path = fromFileUrl(url) + const file = await Deno.readFile(path) const text = new TextDecoder().decode(file) const lines = text.split('\n') @@ -138,11 +126,14 @@ async function updateDepsInFile(path: string, deps: DependencyJson[]) { if (start !== end) { console.log(`Span is multiline, skipping update for ${dep.specifier} in ${path}`) - } else { - const newSpecifier = await updateSpecifier(dep.specifier) - const newLine = lines[start]!.slice(0, startChar) + newSpecifier + lines[start]!.slice(endChar) - lines[start] = newLine + return } + + const newSpecifier = await updateSpecifier(dep.specifier) + if (newSpecifier === dep.specifier) return + + const newLine = lines[start]!.slice(0, startChar) + newSpecifier + lines[start]!.slice(endChar) + lines[start] = newLine } for (const dep of deps) { @@ -154,9 +145,12 @@ async function updateDepsInFile(path: string, deps: DependencyJson[]) { } async function updateSpecifier(specifier: string) { + if (!supportedProtocols.some((protocol) => specifier.startsWith(protocol))) return specifier + const parsed = parseDependency(specifier) - let rangeSpecifier: string | undefined = undefined + if (!parsed.version || excludes.includes(parsed.name)) return specifier + let rangeSpecifier: string | undefined = undefined if (parsed.version && /^[~^]/.test(parsed.version)) { rangeSpecifier = parsed.version[0] parsed.version = parsed.version.slice(1)