Skip to content

Commit c1f4142

Browse files
authored
feat(init): add pnpm support (#915)
pnpm reuses options available for npm, except `force` re: #893, re #858
1 parent 87138d3 commit c1f4142

File tree

4 files changed

+157
-149
lines changed

4 files changed

+157
-149
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ npm install commitizen -g
5858
Next, initialize your project to use the cz-conventional-changelog adapter by typing:
5959

6060
```sh
61+
# npm
6162
commitizen init cz-conventional-changelog --save-dev --save-exact
62-
```
63-
64-
Or if you are using Yarn:
6563

66-
```sh
64+
# yarn
6765
commitizen init cz-conventional-changelog --yarn --dev --exact
66+
67+
# pnpm
68+
commitizen init cz-conventional-changelog --pnpm --save-dev --save-exact
6869
```
6970

7071
Note that if you want to force install over the top of an old adapter, you can apply the `--force` argument. For more information on this, just run `commitizen help`.

src/commitizen/adapter.js

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ export {
1111
addPathToAdapterConfig,
1212
getNearestNodeModulesDirectory,
1313
getNearestProjectRootDirectory,
14-
getNpmInstallStringMappings,
14+
getInstallStringMappings,
1515
getPrompter,
16-
generateNpmInstallAdapterCommand,
16+
generateInstallAdapterCommand,
1717
resolveAdapterPath,
18-
getYarnAddStringMappings,
19-
generateYarnAddAdapterCommand,
2018
getGitRootPath,
2119
};
2220

@@ -55,40 +53,32 @@ function addPathToAdapterConfig (cliPath, repoPath, adapterNpmName) {
5553
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJsonContent, null, indent) + '\n');
5654
}
5755

58-
/**
59-
* Generates an npm install command given a map of strings and a package name
56+
/*
57+
* Get additional options for install command
6058
*/
61-
function generateNpmInstallAdapterCommand (stringMappings, adapterNpmName) {
62-
63-
// Start with an initial npm install command
64-
let installAdapterCommand = `npm install ${adapterNpmName}`;
59+
function getInstallOptions(stringMappings) {
60+
return Array.from(stringMappings.values()).filter(Boolean).join(" ")
61+
}
6562

66-
// Append the neccesary arguments to it based on user preferences
67-
for (let value of stringMappings.values()) {
68-
if (value) {
69-
installAdapterCommand = installAdapterCommand + ' ' + value;
70-
}
71-
}
63+
/*
64+
* Get specific install command for passed package manager
65+
*/
66+
function getInstallCommand(packageManager) {
67+
const fallbackCommand = 'install';
68+
const commandByPackageManager = {
69+
npm: 'install',
70+
yarn: 'add',
71+
pnpm: 'add',
72+
};
7273

73-
return installAdapterCommand;
74+
return commandByPackageManager[packageManager] || fallbackCommand;
7475
}
7576

7677
/**
77-
* Generates an yarn add command given a map of strings and a package name
78+
* Generates an npm install command given a map of strings and a package name
7879
*/
79-
function generateYarnAddAdapterCommand (stringMappings, adapterNpmName) {
80-
81-
// Start with an initial yarn add command
82-
let installAdapterCommand = `yarn add ${adapterNpmName}`;
83-
84-
// Append the necessary arguments to it based on user preferences
85-
for (let value of stringMappings.values()) {
86-
if (value) {
87-
installAdapterCommand = installAdapterCommand + ' ' + value;
88-
}
89-
}
90-
91-
return installAdapterCommand;
80+
function generateInstallAdapterCommand(stringMappings, adapterNpmName, packageManager = "npm") {
81+
return `${packageManager} ${getInstallCommand(packageManager)} ${adapterNpmName} ${getInstallOptions(stringMappings)}`;
9282
}
9383

9484
/**
@@ -117,24 +107,28 @@ function getNearestProjectRootDirectory (repoPath, options) {
117107
}
118108

119109
/**
120-
* Gets a map of arguments where the value is the corresponding npm strings
110+
* Gets a map of arguments where the value is the corresponding (to passed package manager) string
121111
*/
122-
function getNpmInstallStringMappings (save, saveDev, saveExact, force) {
123-
return new Map()
124-
.set('save', (save && !saveDev) ? '--save' : undefined)
112+
function getInstallStringMappings({ save, dev, saveDev, exact, saveExact, force }, packageManager) {
113+
const npm = new Map()
114+
.set('save', save && !saveDev ? '--save' : undefined)
125115
.set('saveDev', saveDev ? '--save-dev' : undefined)
126116
.set('saveExact', saveExact ? '--save-exact' : undefined)
127117
.set('force', force ? '--force' : undefined);
128-
}
129118

130-
/**
131-
* Gets a map of arguments where the value is the corresponding yarn strings
132-
*/
133-
function getYarnAddStringMappings (dev, exact, force) {
134-
return new Map()
119+
const yarn = new Map()
135120
.set('dev', dev ? '--dev' : undefined)
136121
.set('exact', exact ? '--exact' : undefined)
137122
.set('force', force ? '--force' : undefined);
123+
124+
const pnpm = new Map()
125+
.set('save', save && !saveDev ? '--save-prod' : undefined)
126+
.set('dev', saveDev ? '--save-dev' : undefined)
127+
.set('exact', saveExact ? '--save-exact' : undefined);
128+
129+
const map = { npm, yarn, pnpm };
130+
131+
return map[packageManager] || npm;
138132
}
139133

140134
/**

src/commitizen/init.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import * as adapter from './adapter';
55

66
let {
77
addPathToAdapterConfig,
8-
generateNpmInstallAdapterCommand,
9-
getNpmInstallStringMappings,
10-
generateYarnAddAdapterCommand,
11-
getYarnAddStringMappings,
8+
generateInstallAdapterCommand,
9+
getInstallStringMappings,
1210
} = adapter;
1311

1412
export default init;
@@ -43,6 +41,8 @@ const defaultInitOptions = {
4341
yarn: false,
4442
dev: true,
4543
exact: false, // should add trailing comma, thus next developer doesn't got blamed for this line
44+
45+
pnpm: false, // reuses `save`, `saveDev`, `saveExact`
4646
};
4747

4848
/**
@@ -56,6 +56,7 @@ function init (repoPath, adapterNpmName, {
5656
yarn = false,
5757
dev = false,
5858
exact = false,
59+
pnpm = false,
5960
includeCommitizen = false
6061
} = defaultInitOptions) {
6162

@@ -65,13 +66,15 @@ function init (repoPath, adapterNpmName, {
6566
// Load the current adapter config
6667
let adapterConfig = loadAdapterConfig(repoPath);
6768

69+
const packageManager = yarn ? 'yarn' : pnpm ? 'pnpm' : 'npm';
70+
6871
// Get the npm string mappings based on the arguments provided
69-
let stringMappings = yarn ? getYarnAddStringMappings(dev, exact, force) : getNpmInstallStringMappings(save, saveDev, saveExact, force);
72+
const stringMappings = getInstallStringMappings({ save, dev, saveDev, saveExact, force }, packageManager);
7073

7174
// Generate a string that represents the npm install command
72-
let installAdapterCommand = yarn ? generateYarnAddAdapterCommand(stringMappings, adapterNpmName) : generateNpmInstallAdapterCommand(stringMappings, adapterNpmName);
75+
const installAdapterCommand = generateInstallAdapterCommand(stringMappings, adapterNpmName, packageManager);
7376

74-
let installCommitizenCommand = yarn ? generateYarnAddAdapterCommand(stringMappings, "commitizen") : generateNpmInstallAdapterCommand(stringMappings, "commitizen");
77+
const installCommitizenCommand = generateInstallAdapterCommand(stringMappings, 'commitizen', packageManager);
7578

7679
// Check for previously installed adapters
7780
if (adapterConfig && adapterConfig.path && adapterConfig.path.length > 0 && !force) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy