-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detect package manager when spawning dmno dev for config loader client (
#84)
- Loading branch information
1 parent
33ad7d6
commit cff6475
Showing
9 changed files
with
170 additions
and
76 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,6 @@ | ||
--- | ||
"@dmno/astro-integration": patch | ||
"dmno": patch | ||
--- | ||
|
||
spawn dmno dev using correct package manager, astro integration cleanup, dynamic item prerender warning |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
"compilerOptions": { | ||
"jsx": "preserve" | ||
}, | ||
"exclude": [ "dist", "~partytown" ] | ||
} |
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
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,138 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import kleur from 'kleur'; | ||
import { asyncMapValues } from './async-utils'; | ||
|
||
export type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun' | 'moon'; | ||
|
||
|
||
export async function pathExists(p: string) { | ||
try { | ||
await fs.promises.access(p); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
function pathExistsSync(p:string) { | ||
try { | ||
fs.accessSync(p); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
// TODO: nx and lerna support? (lerna.json has packages array) | ||
// TODO: deno? | ||
const PACKAGE_MANAGER_RELEVANT_FILES = { | ||
packageJson: 'package.json', | ||
yarnLock: 'yarn.lock', | ||
npmLock: 'package-lock.json', | ||
pnpmLock: 'pnpm-lock.yaml', | ||
pnpmWorkspace: 'pnpm-workspace.yaml', | ||
bunLock: 'bun.lockb', | ||
moonWorkspace: '.moon/workspace.yml', | ||
}; | ||
|
||
// SEE SYNC VERSION BELOW - UPDATE BOTH IF ANY CHANGES ARE MADE! | ||
export async function detectPackageManager() { | ||
let cwd = process.cwd(); | ||
|
||
const cwdParts = cwd.split('/'); | ||
|
||
let packageManager: PackageManager | undefined; | ||
let possibleRootPackage: string | undefined; | ||
|
||
while (!packageManager) { | ||
// we could also try to detect the current package manager via env vars (ex: process.env.PNPM_PACKAGE_NAME) | ||
// and then not check for all of the lockfiles...? | ||
|
||
|
||
const filesFound = await asyncMapValues( | ||
PACKAGE_MANAGER_RELEVANT_FILES, | ||
// eslint-disable-next-line @typescript-eslint/no-loop-func | ||
async (filePath) => pathExists(path.resolve(cwd, filePath)), | ||
); | ||
|
||
if (filesFound.packageJson) possibleRootPackage = cwd; | ||
|
||
if (filesFound.pnpmLock || filesFound.pnpmWorkspace) packageManager = 'pnpm'; | ||
else if (filesFound.npmLock) packageManager = 'npm'; | ||
else if (filesFound.yarnLock) packageManager = 'yarn'; | ||
else if (filesFound.bunLock) packageManager = 'bun'; | ||
else if (filesFound.moonWorkspace) packageManager = 'moon'; | ||
|
||
if (!packageManager) { | ||
cwdParts.pop(); | ||
cwd = cwdParts.join('/'); | ||
} | ||
// show some hopefully useful error messaging if we hit the root folder without finding anything | ||
if (cwd === '') { | ||
console.log(kleur.red('Unable to find detect your package manager and workspace root!')); | ||
if (possibleRootPackage) { | ||
console.log(`But it looks like your workspace root might be ${kleur.green().italic(possibleRootPackage)}`); | ||
} | ||
console.log('We look for lock files (ex: package-lock.json) so you may just need to run a dependency install (ie `npm install`)'); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
return { | ||
packageManager, | ||
rootWorkspacePath: cwd, | ||
}; | ||
} | ||
|
||
|
||
// sync version of above fn, probably dont want this... but fine for now | ||
export function detectPackageManagerSync() { | ||
let cwd = process.cwd(); | ||
|
||
const cwdParts = cwd.split('/'); | ||
|
||
let packageManager: PackageManager | undefined; | ||
let possibleRootPackage: string | undefined; | ||
|
||
while (!packageManager) { | ||
// we could also try to detect the current package manager via env vars (ex: process.env.PNPM_PACKAGE_NAME) | ||
// and then not check for all of the lockfiles...? | ||
|
||
const filesFound: Partial<Record<keyof typeof PACKAGE_MANAGER_RELEVANT_FILES, boolean>> = {}; | ||
for (const fileKey of Object.keys(PACKAGE_MANAGER_RELEVANT_FILES)) { | ||
const key = fileKey as keyof typeof PACKAGE_MANAGER_RELEVANT_FILES; | ||
const filePath = path.resolve(cwd, PACKAGE_MANAGER_RELEVANT_FILES[key]); | ||
filesFound[key] = pathExistsSync(filePath); | ||
} | ||
|
||
if (filesFound.packageJson) possibleRootPackage = cwd; | ||
|
||
if (filesFound.pnpmLock || filesFound.pnpmWorkspace) packageManager = 'pnpm'; | ||
else if (filesFound.npmLock) packageManager = 'npm'; | ||
else if (filesFound.yarnLock) packageManager = 'yarn'; | ||
else if (filesFound.bunLock) packageManager = 'bun'; | ||
else if (filesFound.moonWorkspace) packageManager = 'moon'; | ||
|
||
if (!packageManager) { | ||
cwdParts.pop(); | ||
cwd = cwdParts.join('/'); | ||
} | ||
// show some hopefully useful error messaging if we hit the root folder without finding anything | ||
if (cwd === '') { | ||
console.log(kleur.red('Unable to find detect your package manager and workspace root!')); | ||
if (possibleRootPackage) { | ||
console.log(`But it looks like your workspace root might be ${kleur.green().italic(possibleRootPackage)}`); | ||
} | ||
console.log('We look for lock files (ex: package-lock.json) so you may just need to run a dependency install (ie `npm install`)'); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
return { | ||
packageManager, | ||
rootWorkspacePath: cwd, | ||
}; | ||
} | ||
|
||
|
||
|
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