Skip to content

Commit

Permalink
refactor: extract deps
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 6, 2024
1 parent 630e9b9 commit 680b921
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 790 deletions.
688 changes: 0 additions & 688 deletions deno.lock

This file was deleted.

10 changes: 5 additions & 5 deletions release-npm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { version } from "./src/version.ts"
import { description } from "./src/constant.ts"
import * as esbuild from "https://deno.land/x/esbuild@v0.19.11/mod.js"
import { build, emptyDir } from "https://deno.land/x/dnt@0.39.0/mod.ts"
import * as esbuild from "https://deno.land/x/esbuild@v0.20.1/mod.js"
import { build, emptyDir } from "https://deno.land/x/dnt@0.40.0/mod.ts"
import { execa } from "https://deno.land/x/easy_std@v0.7.0/src/process.ts"
import {
dirname,
fromFileUrl,
resolve,
} from "https://deno.land/std@0.212.0/path/mod.ts"
} from "https://deno.land/std@0.218.2/path/mod.ts"

const npm = resolve(dirname(fromFileUrl(import.meta.url)), "npm")

Expand Down Expand Up @@ -80,7 +80,7 @@ async function sanitiseDeps() {
if (packageJson["devDependencies"]) {
packageJson["devDependencies"] = Object.assign(
packageJson["devDependencies"],
dependencies
dependencies,
)
}
delete packageJson[key]
Expand All @@ -90,7 +90,7 @@ async function sanitiseDeps() {

await Deno.writeTextFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2)
JSON.stringify(packageJson, null, 2),
)

await execa(["npm", "install"], {
Expand Down
39 changes: 39 additions & 0 deletions src/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { cacheDirs } from "./constant.ts"
import {
brightGreen,
emptyDir,
exists,
gray,
isAbsolute,
resolve,
slash,
} from "./deps.ts"
import { usePm } from "./pm.ts"

export function logClean(dir: string) {
console.log(
`${brightGreen("√ clean")} ${gray(slash(dir))}`,
)
}

export async function cleanDirs(dirs = cacheDirs, root = Deno.cwd()) {
const newDirs = dirs.filter(Boolean).map((dir) => {
if (isAbsolute(dir!)) {
return dir
}
return resolve(root, dir!)
}) as string[]
await Promise.all(newDirs.map(async (dir) => {
if (await exists(dir)) {
await emptyDir(dir)
logClean(dir)
}
}))
}

export async function cleanWorkspaces(dirs: string[] = cacheDirs) {
const pm = usePm()
await Promise.all(
pm.workspaces.map((workspace) => cleanDirs(dirs, workspace)),
)
}
24 changes: 12 additions & 12 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { cacheDirs, description } from "./constant.ts"
import { cleanWorkspaces } from "./clean.ts"
import {
brightGreen,
brightYellow,
Command,
dim,
ensureFile,
EnumType,
gray,
paramCase,
red,
resolve,
yellow,
} from "https://deno.land/std@0.209.0/fmt/colors.ts"
import { ensureFile } from "https://deno.land/std@0.209.0/fs/ensure_file.ts"
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/command.ts"
import { EnumType } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/types/enum.ts"

import { resolve } from "https://deno.land/std@0.209.0/path/resolve.ts"
import paramCase from "https://deno.land/x/case@2.2.0/paramCase.ts"
import { cacheDirs, description } from "./constant.ts"
import { cleanWorkspaces, existsFile } from "./fs.ts"
} from "./deps.ts"
import { existsFile } from "./find.ts"
import { getLockFromPm, nodePms, type PmType, usePm } from "./pm.ts"
import { execa } from "./process.ts"
import { version } from "./version.ts"
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function action() {
}
if (options.reinstall) {
// clean cache and node_modules
await cleanWorkspaces(pm.workspaces, [...cacheDirs, "node_modules"])
await cleanWorkspaces([...cacheDirs, "node_modules"])
// reinstall
await pm.install()
}
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function action() {
}

// clean cache and node_modules
await cleanWorkspaces(pm.workspaces, [...cacheDirs, "node_modules"])
await cleanWorkspaces([...cacheDirs, "node_modules"])

// reinstall
await pm.install()
Expand Down Expand Up @@ -162,7 +162,7 @@ export async function action() {
).arguments("<...deps:string>").action((_, ...deps) => pm.uninstall(deps))

const clean = new Command().alias("clean").description(`clean cache`).action(
() => cleanWorkspaces(pm.workspaces, cacheDirs),
() => cleanWorkspaces(),
)

await commander
Expand Down
6 changes: 3 additions & 3 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const description =
"js runtime project management tool | js runtime 项目管理命令工具"

export const cacheDirs = [
"dist",
".nuxt",
Expand All @@ -9,6 +12,3 @@ export const cacheDirs = [
".cache",
"docs/.vitepress/cache",
]

export const description =
"js runtime project management tool | js runtime 项目管理命令工具"
35 changes: 35 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export {
brightGreen,
brightYellow,
dim,
gray,
red,
yellow,
} from "https://deno.land/std@0.218.2/fmt/colors.ts"
export { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/command.ts"
export { EnumType } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/types/enum.ts"
export { default as paramCase } from "https://deno.land/x/case@2.2.0/paramCase.ts"

export {
emptyDir,
ensureFile,
exists,
expandGlob,
} from "https://deno.land/std@0.218.2/fs/mod.ts"
export {
createUpBases,
slash,
} from "https://deno.land/x/easy_std@v0.7.0/src/path.ts"

export { parse } from "https://deno.land/std@0.218.2/yaml/parse.ts"
export { createContext } from "npm:unctx@2.3.1"

export { runtime } from "npm:std-env@3.7.0"
export { execa as denoExeca } from "https://deno.land/x/easy_std@v0.7.0/src/process.ts"
export {
isAbsolute,
isGlob,
join,
relative,
resolve,
} from "https://deno.land/std@0.218.2/path/mod.ts"
31 changes: 31 additions & 0 deletions src/find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createUpBases, exists, join, resolve, slash } from "./deps.ts"

export function existsFile(path: string) {
return exists(path, {
isFile: true,
isDirectory: false,
})
}

export async function findUp(files: string[], root = Deno.cwd()) {
const upPaths = createUpBases(root)
for (const upPath of upPaths) {
for (const file of files) {
const path = join(upPath, file)
if (await exists(path)) {
return slash(path)
}
}
}
return null
}

export async function find(files: string[], root = Deno.cwd()) {
for (const file of files) {
const resolvedFile = resolve(root, file)
if (await existsFile(file)) {
return resolvedFile
}
}
return null
}
61 changes: 0 additions & 61 deletions src/fs.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/log.ts

This file was deleted.

15 changes: 4 additions & 11 deletions src/pm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { expandGlob } from "https://deno.land/std@0.212.0/fs/expand_glob.ts"
import { isAbsolute } from "https://deno.land/std@0.212.0/path/is_absolute.ts"
import { isGlob } from "https://deno.land/std@0.212.0/path/is_glob.ts"
import { resolve } from "https://deno.land/std@0.212.0/path/resolve.ts"
import { relative } from "https://deno.land/std@0.212.0/path/relative.ts"
import { parse } from "https://deno.land/std@0.212.0/yaml/parse.ts"
import { slash } from "https://deno.land/x/easy_std@v0.7.0/src/path.ts"
import { createContext } from "npm:unctx"
import { createContext, relative, resolve, slash } from "./deps.ts"
import { execa } from "./process.ts"
import { find, findUp } from "./fs.ts"
import { find, findUp } from "./find.ts"

export type PmType = "npm" | "yarn" | "pnpm" | "deno"

Expand Down Expand Up @@ -69,11 +62,11 @@ export async function initPm(root = Deno.cwd()) {
}
const isYarn = this.type === "yarn"
if (isYarn && deps.length === 0) {
await execa([ctx.type, ...options])
await execa([this.type, ...options])
return
}
await execa(
[ctx.type, isYarn ? "add" : "install", ...deps, ...options],
[this.type, isYarn ? "add" : "install", ...deps, ...options],
)
return
},
Expand Down
3 changes: 1 addition & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { runtime } from "npm:std-env@3.7.0"
import { execa as denoExeca } from "https://deno.land/x/easy_std@v0.7.0/src/process.ts"
import { denoExeca, runtime } from "./deps.ts"

export async function execa(cmd: string[], cwd?: string) {
if (runtime === "deno") {
Expand Down

0 comments on commit 680b921

Please sign in to comment.