Skip to content

Commit

Permalink
feat(init): add pnpm support (#915)
Browse files Browse the repository at this point in the history
pnpm reuses options available for npm, except `force`

re: #893, re #858
  • Loading branch information
chenaski authored Jan 19, 2023
1 parent 87138d3 commit c1f4142
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 149 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ npm install commitizen -g
Next, initialize your project to use the cz-conventional-changelog adapter by typing:

```sh
# npm
commitizen init cz-conventional-changelog --save-dev --save-exact
```

Or if you are using Yarn:

```sh
# yarn
commitizen init cz-conventional-changelog --yarn --dev --exact

# pnpm
commitizen init cz-conventional-changelog --pnpm --save-dev --save-exact
```

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`.
Expand Down
76 changes: 35 additions & 41 deletions src/commitizen/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ export {
addPathToAdapterConfig,
getNearestNodeModulesDirectory,
getNearestProjectRootDirectory,
getNpmInstallStringMappings,
getInstallStringMappings,
getPrompter,
generateNpmInstallAdapterCommand,
generateInstallAdapterCommand,
resolveAdapterPath,
getYarnAddStringMappings,
generateYarnAddAdapterCommand,
getGitRootPath,
};

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

/**
* Generates an npm install command given a map of strings and a package name
/*
* Get additional options for install command
*/
function generateNpmInstallAdapterCommand (stringMappings, adapterNpmName) {

// Start with an initial npm install command
let installAdapterCommand = `npm install ${adapterNpmName}`;
function getInstallOptions(stringMappings) {
return Array.from(stringMappings.values()).filter(Boolean).join(" ")
}

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

return installAdapterCommand;
return commandByPackageManager[packageManager] || fallbackCommand;
}

/**
* Generates an yarn add command given a map of strings and a package name
* Generates an npm install command given a map of strings and a package name
*/
function generateYarnAddAdapterCommand (stringMappings, adapterNpmName) {

// Start with an initial yarn add command
let installAdapterCommand = `yarn add ${adapterNpmName}`;

// Append the necessary arguments to it based on user preferences
for (let value of stringMappings.values()) {
if (value) {
installAdapterCommand = installAdapterCommand + ' ' + value;
}
}

return installAdapterCommand;
function generateInstallAdapterCommand(stringMappings, adapterNpmName, packageManager = "npm") {
return `${packageManager} ${getInstallCommand(packageManager)} ${adapterNpmName} ${getInstallOptions(stringMappings)}`;
}

/**
Expand Down Expand Up @@ -117,24 +107,28 @@ function getNearestProjectRootDirectory (repoPath, options) {
}

/**
* Gets a map of arguments where the value is the corresponding npm strings
* Gets a map of arguments where the value is the corresponding (to passed package manager) string
*/
function getNpmInstallStringMappings (save, saveDev, saveExact, force) {
return new Map()
.set('save', (save && !saveDev) ? '--save' : undefined)
function getInstallStringMappings({ save, dev, saveDev, exact, saveExact, force }, packageManager) {
const npm = new Map()
.set('save', save && !saveDev ? '--save' : undefined)
.set('saveDev', saveDev ? '--save-dev' : undefined)
.set('saveExact', saveExact ? '--save-exact' : undefined)
.set('force', force ? '--force' : undefined);
}

/**
* Gets a map of arguments where the value is the corresponding yarn strings
*/
function getYarnAddStringMappings (dev, exact, force) {
return new Map()
const yarn = new Map()
.set('dev', dev ? '--dev' : undefined)
.set('exact', exact ? '--exact' : undefined)
.set('force', force ? '--force' : undefined);

const pnpm = new Map()
.set('save', save && !saveDev ? '--save-prod' : undefined)
.set('dev', saveDev ? '--save-dev' : undefined)
.set('exact', saveExact ? '--save-exact' : undefined);

const map = { npm, yarn, pnpm };

return map[packageManager] || npm;
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/commitizen/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import * as adapter from './adapter';

let {
addPathToAdapterConfig,
generateNpmInstallAdapterCommand,
getNpmInstallStringMappings,
generateYarnAddAdapterCommand,
getYarnAddStringMappings,
generateInstallAdapterCommand,
getInstallStringMappings,
} = adapter;

export default init;
Expand Down Expand Up @@ -43,6 +41,8 @@ const defaultInitOptions = {
yarn: false,
dev: true,
exact: false, // should add trailing comma, thus next developer doesn't got blamed for this line

pnpm: false, // reuses `save`, `saveDev`, `saveExact`
};

/**
Expand All @@ -56,6 +56,7 @@ function init (repoPath, adapterNpmName, {
yarn = false,
dev = false,
exact = false,
pnpm = false,
includeCommitizen = false
} = defaultInitOptions) {

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

const packageManager = yarn ? 'yarn' : pnpm ? 'pnpm' : 'npm';

// Get the npm string mappings based on the arguments provided
let stringMappings = yarn ? getYarnAddStringMappings(dev, exact, force) : getNpmInstallStringMappings(save, saveDev, saveExact, force);
const stringMappings = getInstallStringMappings({ save, dev, saveDev, saveExact, force }, packageManager);

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

let installCommitizenCommand = yarn ? generateYarnAddAdapterCommand(stringMappings, "commitizen") : generateNpmInstallAdapterCommand(stringMappings, "commitizen");
const installCommitizenCommand = generateInstallAdapterCommand(stringMappings, 'commitizen', packageManager);

// Check for previously installed adapters
if (adapterConfig && adapterConfig.path && adapterConfig.path.length > 0 && !force) {
Expand Down
Loading

1 comment on commit c1f4142

@jamesbullock21451334
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never give up

Please sign in to comment.