Skip to content

Commit

Permalink
feat(semver): support pnpm or npx or yarn with husky
Browse files Browse the repository at this point in the history
  • Loading branch information
edbzn committed Nov 19, 2023
1 parent e786dfe commit e253c7b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/semver/src/generators/install/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
writeJson,
type Tree,
logger,
detectPackageManager,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as inquirer from 'inquirer';
Expand All @@ -13,6 +14,14 @@ import install from '.';
import type { SchemaOptions } from './schema';

jest.mock('inquirer');
jest.mock('@nx/devkit', () => ({
...jest.requireActual('@nx/devkit'),
detectPackageManager: jest.fn().mockReturnValue('npm'),
}));

const detectPackageManagerMock = detectPackageManager as jest.MockedFunction<
typeof detectPackageManager
>;

const defaultOptions: SchemaOptions = {
syncVersions: false,
Expand Down Expand Up @@ -369,6 +378,31 @@ describe('@jscutlery/semver:install', () => {
expect(findProjectFile(tree, 'lib3', 'CHANGELOG.md')).toBeFalse();
});
});

it('use corresponding package manager (npx)', async () => {
await install(tree, defaultOptions);

const commitlintConfig = tree.read('.husky/commit-msg')?.toString();
expect(commitlintConfig).toContain('npx --no commitlint');
});

it('use corresponding package manager (yarn)', async () => {
detectPackageManagerMock.mockReturnValue('yarn');

await install(tree, defaultOptions);

const commitlintConfig = tree.read('.husky/commit-msg')?.toString();
expect(commitlintConfig).toContain('yarn commitlint');
});

it('use corresponding package manager (pnpm)', async () => {
detectPackageManagerMock.mockReturnValue('pnpm');

await install(tree, defaultOptions);

const commitlintConfig = tree.read('.husky/commit-msg')?.toString();
expect(commitlintConfig).toContain('pnpm commitlint');
});
});

function createProjectNames() {
Expand Down
10 changes: 9 additions & 1 deletion packages/semver/src/generators/install/utils/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
updateJson,
type Tree,
logger,
detectPackageManager,
} from '@nx/devkit';
import { constants } from 'fs';
import type { SchemaOptions } from '../schema';
Expand Down Expand Up @@ -103,9 +104,16 @@ function _addHuskyConfig(tree: Tree) {

function _addHuskyConfigMsg(tree: Tree) {
const hasConfigFile: boolean = tree.exists('.husky/commit-msg');
const packageManager = detectPackageManager(tree.root);
const command =
packageManager === 'npm'
? 'npx --no'
: packageManager === 'yarn'
? 'yarn'
: 'pnpm';

if (!hasConfigFile) {
const commitMsg = `#!/bin/sh\n. "$(dirname "$0")/_/husky.sh"\n\nnpx --no-install commitlint --edit $1\n`;
const commitMsg = `#!/bin/sh\n. "$(dirname "$0")/_/husky.sh"\n\n${command} commitlint --edit $1\n`;

tree.write('.husky/commit-msg', commitMsg, {
/* File mode indicating readable, writable, and executable by owner. */
Expand Down

0 comments on commit e253c7b

Please sign in to comment.