Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI auto-updates on every run (#441)
- Loading branch information
Showing
5 changed files
with
265 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": patch | ||
--- | ||
|
||
CLI autoupdates on run |
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,103 @@ | ||
import { exec } from "child_process"; | ||
|
||
type PackageManager = "npm" | "yarn" | "pnpm"; | ||
|
||
interface Installation { | ||
packageManager: PackageManager; | ||
isGlobal: boolean; | ||
} | ||
|
||
export async function findPackageInstallation(): Promise< | ||
Installation | undefined | ||
> { | ||
const isLocal = await isInstalledLocally(); | ||
if (isLocal) { | ||
const packageManager = await import("detect-package-manager").then( | ||
({ detect }) => { | ||
return detect(); | ||
}, | ||
); | ||
|
||
return { packageManager, isGlobal: false }; | ||
} | ||
|
||
const isGlobalNpm = await isInstalledGloballyWithNpm(); | ||
if (isGlobalNpm) { | ||
return { packageManager: "npm", isGlobal: true }; | ||
} | ||
|
||
const isGlobalYarn = await isInstalledGloballyWithYarn(); | ||
if (isGlobalYarn) { | ||
return { packageManager: "yarn", isGlobal: true }; | ||
} | ||
|
||
const isGlobalPnpm = await isInstalledGloballyWithPnpm(); | ||
if (isGlobalPnpm) { | ||
return { packageManager: "pnpm", isGlobal: true }; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
// For LOCAL packages, npm ls picks up things installed by yarn and pnpm too | ||
async function isInstalledLocally(): Promise<boolean> { | ||
const packages = await detectPackages("npm ls --depth=0"); | ||
return containsThirdweb(packages || []); | ||
} | ||
|
||
// But for GLOBAL packages, we need to use a separate command for each one | ||
async function isInstalledGloballyWithNpm(): Promise<boolean> { | ||
const packages = await detectPackages("npm ls -g --depth=0"); | ||
return containsThirdweb(packages || []); | ||
} | ||
|
||
async function isInstalledGloballyWithYarn(): Promise<boolean> { | ||
const packages = await detectPackages("yarn --cwd `yarn global dir` list"); | ||
return containsThirdweb(packages || []); | ||
} | ||
|
||
async function isInstalledGloballyWithPnpm(): Promise<boolean> { | ||
const packages: string[] = await new Promise((resolve) => { | ||
exec(`pnpm list -g --depth=0`, (err, stdout) => { | ||
var pkgs: string[] = []; | ||
pkgs = stdout.split("dependencies:")[1]?.trim().split("\n") || []; | ||
pkgs = pkgs.map((pkg) => pkg.replace(" ", "@")); | ||
resolve(pkgs); | ||
}); | ||
}); | ||
return containsThirdweb(packages || []); | ||
} | ||
|
||
function containsThirdweb(packages: string[]) { | ||
return packages.some((pkg) => pkg.match(/^thirdweb/)); | ||
} | ||
|
||
async function detectPackages(cmd: string): Promise<string[]> { | ||
return new Promise((resolve) => { | ||
exec(cmd, (err, stdout) => { | ||
var packages: string[] = []; | ||
packages = stdout.split("\n"); | ||
// We're one short on '-' to support both yarn and npm | ||
packages = packages.filter(function (item) { | ||
if (item.match(/^├─.+/g) !== null) { | ||
return true; | ||
} | ||
if (item.match(/^└─.+/g) !== null) { | ||
return true; | ||
} | ||
return undefined; | ||
}); | ||
packages = packages | ||
.map(function (item) { | ||
if (item.match(/^├─.+/g) !== null) { | ||
return item.replace(/(^├──\s|^├─\s)/g, ""); | ||
} | ||
if (item.match(/^└─.+/g) !== null) { | ||
return item.replace(/(^└──\s|^├─\s)/g, ""); | ||
} | ||
}) | ||
.filter((item) => !!item) as string[]; | ||
resolve(packages); | ||
}); | ||
}); | ||
} |
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