-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { execSync } from "node:child_process" | ||
|
||
export function execCmd(cmd: string) { | ||
try { | ||
return execSync(cmd, { encoding: "utf-8" }).trim() | ||
} catch { | ||
return "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { readFile, constants } from "node:fs/promises" | ||
import { accessSync } from "node:fs" | ||
import { join } from "node:path" | ||
import { spawn } from "node:child_process" | ||
import { isInteractive, verboseLog } from "./command" | ||
import { PkgJson } from "@devicescript/compiler" | ||
import { consoleBooleanAsk } from "./prompt" | ||
|
||
function isYarnRepo(): boolean { | ||
try { | ||
accessSync(join(process.cwd(), "yarn.lock"), constants.R_OK) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
function getPackageInstallerCommand( | ||
packageName?: string | ||
): string[] { | ||
if (isYarnRepo()) { | ||
if (!packageName) return ["yarn", "install"] | ||
|
||
return ["yarn", "add", packageName] | ||
} | ||
|
||
if (!packageName) return ["npm", "install"] | ||
|
||
return ["npm", "install", "--save", "--no-workspaces", packageName] | ||
} | ||
|
||
async function isPackageInstalledLocally(pkgName: string): Promise<boolean> { | ||
const pkgJsonString = await readFile(join(process.cwd(), "package.json"), { | ||
encoding: "utf-8", | ||
}) | ||
const pkgJson = JSON.parse(pkgJsonString) as PkgJson | ||
|
||
return Object.keys(pkgJson.dependencies).includes(pkgName) | ||
} | ||
|
||
async function spawnAsyncInstaller(packageName: string) { | ||
let resolve: () => void | ||
let reject: () => void | ||
|
||
const [rootCmd, ...cmdArgs] = getPackageInstallerCommand(packageName) | ||
|
||
const installProcess = spawn(rootCmd, cmdArgs, { | ||
cwd: process.cwd(), | ||
env: process.env, | ||
}) | ||
|
||
installProcess.stderr.on("data", data => { | ||
verboseLog(`package installer process: ${data}`) | ||
}) | ||
|
||
installProcess.on("close", code => { | ||
verboseLog(`install process exit with code ${code}`) | ||
|
||
if (code === 0) resolve() | ||
else reject() | ||
}) | ||
|
||
return new Promise((res, rej) => { | ||
// @ts-ignore | ||
resolve = res | ||
reject = rej | ||
}) | ||
} | ||
|
||
export async function askForPackageInstallation( | ||
pkgName: string, | ||
installByDefault = true | ||
) { | ||
if (!isInteractive) return | ||
|
||
if (await isPackageInstalledLocally(pkgName)) return | ||
|
||
const shouldInstallPackage = await consoleBooleanAsk( | ||
`Install package "${pkgName}"`, | ||
installByDefault | ||
) | ||
|
||
if (shouldInstallPackage) { | ||
try { | ||
console.log(`Installing package "${pkgName}" ...`) | ||
await spawnAsyncInstaller(pkgName) | ||
console.log(`Package "${pkgName}" installed!`) | ||
} catch (e) { | ||
const installCmd = getPackageInstallerCommand(pkgName).join(" ") | ||
console.log( | ||
`Automatic package installation failed :( You can try to install it manually by running "${installCmd}"` | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters