Skip to content

Commit 5c07f7f

Browse files
committed
add packageManager property
1 parent 497f3f1 commit 5c07f7f

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Fixed for any bug fixes.
99
Security to invite users to upgrade in case of vulnerabilities.
1010
-->
1111

12+
## [0.2.0] - 2022/05/18
13+
14+
### Added
15+
16+
- add `packageManager` property to `getCurrentPackageManager` method
17+
1218
## [0.1.2] - 2022/05/18
1319

1420
### Changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ This command installs a package and any packages that it depends on. If the pack
161161

162162
### Add
163163

164-
This command, no arguments, will add a package to local `package.json` file.
164+
This command, no arguments, will add a package to local `package.json` file.
165165

166166
```bash
167167
swpm add <package> [--save-dev --global]
@@ -184,14 +184,15 @@ swpm add <package> [--save-dev --global]
184184

185185
`swpm` use two stages to infer what is the current Package Manager, following this order.
186186

187-
1. Search the `swpm` property on the `package.json` file
188-
1. Search for `lock`'s files.
187+
1. Search the `swpm` property on `package.json` file
188+
1. Search the `packageManager` property on `package.json` file
189+
1. Search for `lock`'s files
189190

190-
| Lock File | Package Manager |
191-
| ------------------- | --------------- |
192-
| `package-lock.json` | `npm` |
193-
| `yarn.lock` | `yarn` |
194-
| `pnpm-lock.yaml` | `pnpm` |
191+
| Lock File | Package Manager |
192+
| ------------------- | --------------- |
193+
| `package-lock.json` | `npm` |
194+
| `yarn.lock` | `yarn` |
195+
| `pnpm-lock.yaml` | `pnpm` |
195196

196197
[Back to menu](#menu)
197198

options/get.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
import { stripIndents } from 'common-tags'
12
import chalk from 'chalk'
23
import { getPackageJson, lockFileExists } from '../helpers/files.js'
34
import packagesList, { packageExists } from '../packages/list.js'
45

56
const packageName = 'package.json'
67

7-
const searchOnPackageJson = async () => {
8-
const packageJson = await getPackageJson()
9-
if (!packageJson || !('swpm' in packageJson)) {
8+
const searchForProperty = async (packageJson, property) => {
9+
if (!packageJson || !(property in packageJson)) {
1010
return undefined
1111
}
1212

13-
const pinned = packageJson?.swpm
14-
if (pinned && packageExists(pinned)) {
15-
return packageJson?.swpm
13+
const prop = packageJson?.[property]
14+
if (prop && packageExists(prop)) {
15+
return packageJson?.[property]
1616
}
1717

18-
console.log(`${chalk.red.bold('Error')}: Package Manager (${chalk.bold(pinned)}) pinned on ${chalk.bold(packageName)} file is not valid.`)
19-
console.log(`Use ${chalk.blue.bold('npm --pin <npm|yarn|pnpm>')} to fix it.`)
18+
console.log(stripIndents`
19+
${chalk.red.bold('Error')}: the value in (${chalk.bold(prop)}) property on ${chalk.bold(packageName)} file is not valid.
20+
Use ${chalk.blue.bold('npm --pin <npm|yarn|pnpm>')} to fix it.
21+
`)
2022
process.exit(1)
2123
}
2224

@@ -32,21 +34,29 @@ const searchForLockFiles = async () => {
3234
}
3335

3436
export const getCurrentPackageManager = async () => {
35-
const pinned = await searchOnPackageJson()
37+
const packageJson = await getPackageJson()
38+
39+
const pinned = await searchForProperty(packageJson, 'swpm')
3640
if (pinned) {
3741
return pinned
3842
}
3943

44+
// https://nodejs.org/api/corepack.html
45+
const packageManager = await searchForProperty(packageJson, 'packageManager')
46+
if (packageManager) {
47+
return packageManager
48+
}
49+
4050
const lock = await searchForLockFiles()
4151
if (lock) {
4252
return lock
4353
}
4454

45-
console.log(`
46-
${chalk.red.bold('Error')}: no Package Manager was found.
55+
console.log(stripIndents`
56+
${chalk.red.bold('Error')}: no Package Manager was found.
4757
48-
Please review if the current path has a ${chalk.bold('package.json')} or a ${chalk.bold('lock')} file.
49-
Highly recommend pin a Package Manager with ${chalk.blue.bold('swpm --pin <npm|yarn|pnpm>')} command.
50-
`)
58+
Please review if the current path has a ${chalk.bold('package.json')} or a ${chalk.bold('lock')} file.
59+
Highly recommend pin a Package Manager with ${chalk.blue.bold('swpm --pin <npm|yarn|pnpm>')} command.
60+
`)
5161
process.exit(1)
5262
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swpm",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"engines": {
55
"node": ">=16.15.0"
66
},

0 commit comments

Comments
 (0)