Skip to content

Commit

Permalink
feat: add a simply way to force pm config
Browse files Browse the repository at this point in the history
  • Loading branch information
liwuhou committed Dec 18, 2021
1 parent ea9e59e commit 8dc5c8e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 36 deletions.
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
# pm-keeper

A keeper of package manager. Make sure a specific version and package manager to be used in project.
A simple way to force package manager in your project.

# usage

Add a `preinstall` script in your project's `package.json`.
Add a `preinstall` script in your project's `package.json`, link this:

```json
{
"scripts": {
"preinstall": "npx pm-keeper"
"preinstall": "npx pm-keeper npm"
}
}
```

And force the pm's version

```json
{
"scripts": {
"preinstall": "npx pm-keeper [email protected]"
}
}
```

And add a `pmKeeper` options in ypur `package.json`.
or

```json
{
"scripts": {
"preinstall": "npx pm-keeper npm 6.14.11"
}
}
```

Of course, you can set a options in your `package.json`, link this:

```json
{
"scripts": {
"preinstall": "npx pm-keeper"
},
"pmKeeper": {
"name": "npm", // package manager name
// "version": "6.14.1" // package manager version
"name": "npm",
"version": "6.14.1" // optional
}
}
```

enjoy
have fun
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pm-keeper",
"version": "1.1.1",
"version": "1.1.10",
"description": "package version keeper",
"author": "liwuhou <[email protected]>",
"license": "MIT",
Expand All @@ -13,9 +13,9 @@
],
"bin": "bin/index.js",
"scripts": {
"test": "test",
"build": "rimraf dist && tsup src --format cjs,esm,iife --dts",
"watch": "npm run build -- --watch"
"dev": "npm run build -- --watch",
"prebuild": "rimraf dist",
"build": "tsup src --format cjs,esm,iife --dts"
},
"keywords": [
"npm",
Expand Down
5 changes: 5 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const AGENT = [
'npm',
'yarn',
'pnpm'
]
53 changes: 29 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import boxen from 'boxen'
import process from 'node:process'
import { getAgentInfo, getPackageJSON } from "./utils"
import {exit} from 'node:process'
import { getAgentInfo, getAgentConfig, checkVersionValid } from "./utils"
import { AGENT } from './consts'

const pmInfo = getAgentInfo()
const { pmKeeper: { name: targetPm = '', version: targetVersion = '' } = {} } = getPackageJSON() ?? {}
const { name: targetPm, version: targetVersion, } = getAgentConfig() ?? {}
const boxenAlertOpts = {
title: 'Package Manager Error',
titleAlignment: 'center' as 'center',
Expand All @@ -13,28 +14,32 @@ const boxenAlertOpts = {
padding: 1
}

if (pmInfo && targetPm && pmInfo.name !== targetPm) {
switch (targetPm) {
case 'npm':
console.log(boxen('Use "npm install" for installation in this project', boxenAlertOpts))
break
case 'pnpm':
console.log(boxen(`Use "pnpm install" for installation in this project.
If you don't have pnpm, install it via "npm i -g pnpm".
if (targetPm && AGENT.includes(targetPm) && checkVersionValid(targetVersion)) {
if (pmInfo && targetPm && pmInfo.name !== targetPm) {
const versionFix = targetVersion ? `@${targetVersion}` : ''
switch (targetPm) {
case 'npm':
console.log(boxen('Use "npm install" for installation in this project' + (
targetVersion ? `\nAnd the specific version: ${targetVersion}` : ''
), boxenAlertOpts))
break
case 'pnpm':
console.log(boxen(`Use "pnpm install" for installation in this project.\n
If you don't have pnpm, install it via "npm i -g pnpm${versionFix}".
For more details, go to https://pnpm.js.org/`, boxenAlertOpts))
break
case 'yarn':
console.log(boxen(`Use "yarn" for installation in this project.
If you don't have yarn, install it via "npm i -g yarn".
break
case 'yarn':
console.log(boxen(`Use "yarn" for installation in this project.\n
If you don't have yarn, install it via "npm i -g yarn${versionFix}".
For more details, go to https://yarnpkg.com/`, boxenAlertOpts))
break
break
}
exit(1)
} else if (pmInfo && targetVersion && pmInfo.version !== targetVersion) {
console.log(boxen(`Use version "${targetVersion}" of the ${targetPm} for installation in this project.\n
you can change version via "npm install -g ${targetPm}@${targetVersion}".`, { ...boxenAlertOpts, title: 'Package Manager Version Error', borderStyle: 'single' }))
exit(1)
}
process.exit(1)
} else if (pmInfo && targetVersion && pmInfo.version !== targetVersion) {
console.log(boxen(`Use version "${targetVersion}" of the ${targetPm} for installation in this project.
you can change version via "npm install ${targetPm}@${targetVersion}".`, { ...boxenAlertOpts, title: 'Package Manager Version Error', borderStyle: 'single' }))
process.exit(1)
}


26 changes: 25 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolve } from 'node:path'
import process from 'node:process'

export interface Agentinfo {
nodeVersion: string
nodeVersion?: string
version: string
name: string
}
Expand Down Expand Up @@ -34,4 +34,28 @@ export const getPackageJSON = (cwd = process.cwd()) => {
process.exit(0)
}
}
}

const getArgvConf = (argv: string[]): Agentinfo => {
const [name, version] = argv.length >= 2 ? argv : argv[0].split('@')

return {
name,
version
}
}

export const getAgentConfig = (): void | Agentinfo => {
const argv = process.argv.slice(2)
if (argv.length) {
return getArgvConf(argv)
} else {
const { pmKeeper } = getPackageJSON()
return pmKeeper
}
}

export const checkVersionValid = (version?: string): boolean => {
if (!version) return true
return /^([1-9]\d|[1-9])(\.([1-9]\d|\d)){2}$/.test(version)
}

0 comments on commit 8dc5c8e

Please sign in to comment.