Skip to content

Commit

Permalink
fix(pnpm): use kitPnpmPath
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlindquist committed Oct 11, 2024
1 parent 0857348 commit f7140cd
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 241 deletions.
150 changes: 81 additions & 69 deletions src/api/global.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import {
assignPropsTo,
home,
isBin,
isDir,
isFile,
kitPath,
kenvPath,
wait,
getLogFromScriptPath,
createPathResolver
assignPropsTo,
home,
isBin,
isDir,
isFile,
kitPath,
kenvPath,
wait,
getLogFromScriptPath,
createPathResolver,
} from "../core/utils.js"

import { getScripts } from "../core/db.js"
import type { PromptConfig } from "../types/core"
import {
format,
formatDistanceToNow
format,
formatDistanceToNow,
} from "@johnlindquist/kit-internal/date-fns"
import { kitPnpmPath } from "../core/resolvers.js"

global.actionFlag = ""
global.getScripts = getScripts
Expand All @@ -33,51 +34,54 @@ await import("./packages/tmpPromise.js")
await import("./packages/git.js")

global.env = async (envKey, promptConfig) => {
if (!envKey) throw new Error(`Environment Key Required`)

let secret =
typeof (promptConfig as PromptConfig)?.secret === "boolean"
? (promptConfig as PromptConfig).secret
: envKey.includes("KEY") ||
envKey.includes("SECRET") ||
envKey.includes("TOKEN")
? true
: false
if ((promptConfig as any)?.reset !== true) {
let envVal = global.env[envKey] || process.env[envKey]
if (envVal) return envVal
}

let input =
typeof promptConfig === "function"
? await promptConfig()
: typeof promptConfig === "string"
? await global.mini({
enter: "Write to .env",
shortcuts: [],
placeholder: promptConfig,
secret,
keyword: ""
})
: await global.mini({
enter: "Write to .env",
shortcuts: [],
placeholder: `Set ${envKey}:`,
...promptConfig,
secret,
keyword: ""
})

if (input?.startsWith("~")) input = input.replace(/^~/, home())
await global.cli("set-env-var", envKey, input)
global.env[envKey] = process.env[envKey] = input
return input
if (!envKey) throw new Error(`Environment Key Required`)

let secret =
typeof (promptConfig as PromptConfig)?.secret ===
"boolean"
? (promptConfig as PromptConfig).secret
: envKey.includes("KEY") ||
envKey.includes("SECRET") ||
envKey.includes("TOKEN")
? true
: false
if ((promptConfig as any)?.reset !== true) {
let envVal = global.env[envKey] || process.env[envKey]
if (envVal) return envVal
}

let input =
typeof promptConfig === "function"
? await promptConfig()
: typeof promptConfig === "string"
? await global.mini({
enter: "Write to .env",
shortcuts: [],
placeholder: promptConfig,
secret,
keyword: "",
})
: await global.mini({
enter: "Write to .env",
shortcuts: [],
placeholder: `Set ${envKey}:`,
...promptConfig,
secret,
keyword: "",
})

if (input?.startsWith("~"))
input = input.replace(/^~/, home())
await global.cli("set-env-var", envKey, input)
global.env[envKey] = process.env[envKey] = input
return input
}

assignPropsTo(process.env, global.env)

global.wait = wait
global.kitPath = kitPath
global.kitPnpmPath = kitPnpmPath
global.kenvPath = kenvPath
global.isBin = isBin
global.isDir = isDir
Expand All @@ -88,41 +92,49 @@ global.home = home
global.memoryMap = new Map()

global.getLog = () => {
let log = getLogFromScriptPath(global.kitScript)
return log
let log = getLogFromScriptPath(global.kitScript)
return log
}

let intervals

// A proxy around setInterval that keeps track of all intervals
global.setInterval = new Proxy(setInterval, {
apply: (target, thisArg, args: Parameters<typeof setInterval>) => {
let id = target(...args)
intervals = intervals || new Set()
intervals.add(id)
return id
}
apply: (
target,
thisArg,
args: Parameters<typeof setInterval>
) => {
let id = target(...args)
intervals = intervals || new Set()
intervals.add(id)
return id
},
})

let timeouts

global.setTimeout = new Proxy(setTimeout, {
apply: (target, thisArg, args: Parameters<typeof setTimeout>) => {
let id = target(...args)
timeouts = timeouts || new Set()
timeouts.add(id)
return id
}
apply: (
target,
thisArg,
args: Parameters<typeof setTimeout>
) => {
let id = target(...args)
timeouts = timeouts || new Set()
timeouts.add(id)
return id
},
})

global.clearAllIntervals = () => {
intervals?.forEach((id) => clearInterval(id))
intervals = new Set()
intervals?.forEach(id => clearInterval(id))
intervals = new Set()
}

global.clearAllTimeouts = () => {
timeouts?.forEach((id) => clearTimeout(id))
timeouts = new Set()
timeouts?.forEach(id => clearTimeout(id))
timeouts = new Set()
}

global.formatDate = format
Expand Down
126 changes: 61 additions & 65 deletions src/cli/install.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,73 @@
import {
formatDistanceToNow,
parseISO
formatDistanceToNow,
parseISO,
} from "@johnlindquist/kit-internal/date-fns"
import { KIT_FIRST_PATH } from "../core/utils.js"
import { createPackageManagerCommand } from "./lib/install.js"

let install = async (packageNames:string[]) => {
let cwd = kenvPath()
let install = async (packageNames: string[]) => {
let cwd = kenvPath()

// if (process.env.SCRIPTS_DIR) {
// cwd = kenvPath(process.env.SCRIPTS_DIR)
// }
// if (process.env.SCRIPTS_DIR) {
// cwd = kenvPath(process.env.SCRIPTS_DIR)
// }
let command = await createPackageManagerCommand(
"i",
packageNames
)

let isYarn = await isFile(kenvPath("yarn.lock"))
let [tool, toolArgs] = (
isYarn
? `yarn${global.isWin ? `.cmd` : ``} add`
: `pnpm${global.isWin ? `.cmd` : ``} i`
).split(" ")

let toolPath = global.isWin ? (isYarn ? "yarn" : "pnpm") : tool

let packages = packageNames.join(" ")
let command = `${toolPath} ${toolArgs} -D ${packages}`.trim()

return await term({
name: "pnpm install",
command,
env: {
...global.env,
PATH: KIT_FIRST_PATH,
DISABLE_AUTO_UPDATE: "true" // Disable auto-update for zsh
},
cwd
})
return await term({
name: "pnpm install",
command,
env: {
...global.env,
PATH: KIT_FIRST_PATH,
DISABLE_AUTO_UPDATE: "true", // Disable auto-update for zsh
},
cwd,
})
}

let packages = await arg(
{
enter: "Install",
placeholder: "Which npm package/s would you like to install?"
},
async (input) => {
if (!input || input?.length < 3)
return [
{
info: true,
miss: true,
name: `Search for npm packages`
}
]
type pkgs = {
objects: {
package: {
name: string
description: string
date: string
}
}[]
}
let response = await get<pkgs>(
`http://registry.npmjs.com/-/v1/search?text=${input}&size=20`
)
let packages = response.data.objects
return packages.map((o) => {
return {
name: o.package.name,
value: o.package.name,
description: `${o.package.description} - ${formatDistanceToNow(
parseISO(o.package.date)
)} ago`
}
})
}
{
enter: "Install",
placeholder:
"Which npm package/s would you like to install?",
},
async input => {
if (!input || input?.length < 3)
return [
{
info: true,
miss: true,
name: `Search for npm packages`,
},
]
type pkgs = {
objects: {
package: {
name: string
description: string
date: string
}
}[]
}
let response = await get<pkgs>(
`http://registry.npmjs.com/-/v1/search?text=${input}&size=20`
)
let packages = response.data.objects
return packages.map(o => {
return {
name: o.package.name,
value: o.package.name,
description: `${
o.package.description
} - ${formatDistanceToNow(
parseISO(o.package.date)
)} ago`,
}
})
}
)

let installNames = [...packages.split(" ")]
Expand Down
37 changes: 37 additions & 0 deletions src/cli/lib/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { kitPnpmPath } from "../../core/resolvers.js"

export async function createPackageManagerCommand(
command: "i" | "un",
packageNames: string[]
) {
let isYarn = await isFile(kenvPath("yarn.lock"))

let packageManager = isYarn ? "yarn" : "pnpm"
let toolCommand = command as
| "add"
| "i"
| "remove"
| "uninstall"

if (isYarn) {
if (command == "i") toolCommand = "add"
}

// Add .cmd extension for Windows
if (global.isWin) {
packageManager += ".cmd"
}

if (!isYarn) {
packageManager = kitPnpmPath(packageManager)
}

// Combine package manager and command
let installCommand = `${packageManager} ${command}`

let packages = packageNames.join(" ")
let fullCommand =
`${installCommand} -D ${packages}`.trim()

return fullCommand
}
Loading

0 comments on commit f7140cd

Please sign in to comment.