-
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.
feat: support other package managers -- add tests for pnpm
- Loading branch information
David CLEMENT
committed
Feb 20, 2024
1 parent
4b2e55d
commit 15a0ea4
Showing
6 changed files
with
288 additions
and
45 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
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,90 @@ | ||
import path from "node:path"; | ||
import { existsSync } from "node:fs"; | ||
|
||
/** | ||
* @typedef {'npm'|'pnpm'|'yarn'} PackageManager | ||
*/ | ||
|
||
/** | ||
* The list of configuration for package managers | ||
* @type {[{lockFile: string, packageManager: PackageManager, updateLockFileCommand: string[]}]} | ||
*/ | ||
const packageManagerConfigurations = [ | ||
{ | ||
packageManager: "npm", | ||
lockFile: "package-lock.json", | ||
updateLockFileCommand: [ | ||
"npm", | ||
"install", | ||
"--package-lock-only", | ||
"--ignore-scripts", | ||
"--no-audit", | ||
], | ||
}, | ||
{ | ||
packageManager: "pnpm", | ||
lockFile: "pnpm-lock.yaml", | ||
updateLockFileCommand: ["pnpm", "install", "--lockfile-only", "--ignore-scripts"], | ||
}, | ||
{ | ||
packageManager: "yarn", | ||
lockFile: "yarn.lock", | ||
updateLockFileCommand: ["yarn", "install"], | ||
}, | ||
]; | ||
|
||
/** | ||
* Detect package manager from lockFile | ||
* @param {string} location root of the project used to search for lockfile | ||
* @returns {PackageManager} | ||
*/ | ||
export function getPackageManagerFromLockFile(location) { | ||
return ( | ||
packageManagerConfigurations.find(({ lockFile }) => existsSync(path.join(location, lockFile))) | ||
?.packageManager ?? "npm" | ||
); | ||
} | ||
|
||
/** | ||
* define lockfile from package manager | ||
* @param {PackageManager} packageManager | ||
* @returns {string} | ||
*/ | ||
export function getLockFileFromPackageManager(packageManager) { | ||
return packageManagerConfiguration(packageManager).lockFile; | ||
} | ||
|
||
/** | ||
* @param {PackageManager} pm the package manager | ||
* @returns {{lockFile: string, packageManager: ("npm"|"pnpm"|"yarn"), updateLockFileCommand: string[]}} the associated configuration | ||
*/ | ||
function packageManagerConfiguration(pm) { | ||
return packageManagerConfigurations.find(({ packageManager }) => packageManager === pm); | ||
} | ||
|
||
/** | ||
* Add dynamic configuration to a command | ||
* @param {(any) => string[]} commandSelector | ||
* @param {PackageManager} packageManager | ||
* @param {string} npmrc | ||
* @returns {string[]} | ||
*/ | ||
function findAndPrepareCommand(commandSelector, packageManager, npmrc) { | ||
const command = commandSelector(packageManagerConfiguration(packageManager)); | ||
const extension = packageManager === "npm" ? ["--userconfig", npmrc] : []; | ||
return [...command, ...extension]; | ||
} | ||
|
||
/** | ||
* Get the update lockFile command | ||
* @param {PackageManager} packageManager | ||
* @param {string} npmrc | ||
* @returns {string[]} | ||
*/ | ||
export function getUpdateLockFileCommand(packageManager, npmrc) { | ||
return findAndPrepareCommand( | ||
(configuration) => configuration.updateLockFileCommand, | ||
packageManager, | ||
npmrc, | ||
); | ||
} |
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import path from "node:path"; | ||
import { execa } from "execa"; | ||
import { outputJson } from "fs-extra"; | ||
import { | ||
getLockFileFromPackageManager, | ||
getUpdateLockFileCommand, | ||
} from "../../lib/utils/package-manager-commands.js"; | ||
|
||
const MOCK_NAME = "Mock user"; | ||
const MOCK_EMAIL = "[email protected]"; | ||
|
@@ -18,13 +22,15 @@ const MOCK_EMAIL = "[email protected]"; | |
* @param {string} cwd - Project directory | ||
* @param {string} name - Package name | ||
* @param {string} version - Package initial version | ||
* @param {{private: boolean, lockfile: boolean}} [options] - Package options | ||
* @param {{private?: boolean, lockfile?: boolean, packageManager?: 'npm' | 'pnpm' | 'yarn', workspaces?: boolean}} [options] - Package options | ||
* @returns {Promise<Package>} | ||
*/ | ||
export async function createPackage(cwd, name, version, options = {}) { | ||
const pkgRoot = `packages/${name}`; | ||
const manifestLocation = path.resolve(cwd, pkgRoot, "package.json"); | ||
const lockfileLocation = path.resolve(cwd, pkgRoot, "package-lock.json"); | ||
const { lockfile, packageManager = "npm" } = options; | ||
const lockFileName = getLockFileFromPackageManager(packageManager); | ||
const lockfileLocation = lockfile ? path.resolve(cwd, pkgRoot, lockFileName) : null; | ||
const npmEnv = { | ||
...process.env, | ||
NPM_EMAIL: MOCK_EMAIL, | ||
|
@@ -38,8 +44,18 @@ export async function createPackage(cwd, name, version, options = {}) { | |
}; | ||
|
||
await outputJson(manifestLocation, { name, version, private: options.private }); | ||
|
||
if (options.lockfile) { | ||
await execa("npm", ["install", "--package-lock-only", "--ignore-scripts", "--no-audit"], { | ||
const [command, ...args] = getUpdateLockFileCommand( | ||
packageManager, | ||
path.resolve(cwd, ".npmrc"), | ||
); | ||
|
||
if (packageManager === "pnpm" && options.workspaces) { | ||
args.push("-w"); | ||
} | ||
|
||
await execa(command, args, { | ||
cwd: path.resolve(cwd, pkgRoot), | ||
env: npmEnv, | ||
}); | ||
|
@@ -51,9 +67,21 @@ export async function createPackage(cwd, name, version, options = {}) { | |
return { | ||
name, | ||
manifestLocation, | ||
lockfileLocation: options.lockfile ? lockfileLocation : null, | ||
lockfileLocation, | ||
async require(dep) { | ||
await execa("npm", ["install", "--workspace", this.name, dep.name], { cwd }); | ||
let args; | ||
switch (packageManager) { | ||
case "npm": | ||
args = ["install", "--workspace", this.name, dep.name]; | ||
break; | ||
case "pnpm": | ||
args = ["add", "--workspace", "--filter", this.name, dep.name]; | ||
break; | ||
case "yarn": | ||
args = ["add", this.name, dep.name]; | ||
break; | ||
} | ||
await execa(packageManager, args, { cwd }); | ||
}, | ||
resolve(...parts) { | ||
return path.resolve(cwd, pkgRoot, ...parts); | ||
|
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
Oops, something went wrong.