Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): add pnpm support #915

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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