Skip to content

Commit

Permalink
feat(app-builder): determine commit type based on upgraded versions
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Nov 7, 2023
1 parent 6639e04 commit c8353fb
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 17 deletions.
1 change: 1 addition & 0 deletions apps/app-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"inquirer": "^9.1.4",
"interpret": "^3.1.1",
"liftoff": "^4.0.0",
"semver": "^7.5.4",
"supports-color": "^9.3.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`exports > exports from index.ts 1`] = `
[
"COMMIT_TYPE_AUTO",
"PdkPlatformName",
"addPlatformToContext",
"copyFile",
Expand Down
58 changes: 58 additions & 0 deletions apps/app-builder/src/commands/upgrade/createCommitMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {describe, expect, it} from 'vitest';
import {createTestContext} from '../../__tests__/createTestContext';
import {type UpgradedEntry, UpgradeMode} from './types';
import {createCommitMessage} from './createCommitMessage';

describe('createCommitMessage', () => {
it('creates commit message for a single node dependency', () => {
const upgradedVersions = [
{
name: 'vue',
version: '3.3.0',
oldVersion: '3.2.0',
repository: 'https://github.com/vuejs/vue/',
},
] satisfies UpgradedEntry[];

const context = createTestContext();

const commitMessage = createCommitMessage(upgradedVersions, {
...context,
mode: UpgradeMode.Node,
packageName: 'vue',
});

expect(commitMessage).toEqual(`feat(deps): upgrade vue to v3.3.0
Compare changes:
- https://github.com/vuejs/vue/compare/v3.2.0...v3.3.0`);
});

it('creates commit message for multiple node dependencies', () => {
const upgradedVersions = [
{
name: '@myparcel-pdk/app-builder',
version: '1.1.5',
oldVersion: '1.1.1',
},
{
name: '@myparcel-pdk/admin',
version: '1.1.2',
oldVersion: '1.1.1',
},
] satisfies UpgradedEntry[];

const context = createTestContext();

const commitMessage = createCommitMessage(upgradedVersions, {
...context,
mode: UpgradeMode.Node,
packageName: '@myparcel-pdk/*',
});

expect(commitMessage).toEqual(`fix(deps): upgrade @myparcel-pdk/*
- upgrade @myparcel-pdk/app-builder to v1.1.5
- upgrade @myparcel-pdk/admin to v1.1.2`);
});
});
10 changes: 5 additions & 5 deletions apps/app-builder/src/commands/upgrade/createCommitMessage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {VerbosityLevel} from '../../constants';
import {type UpgradedEntry, type UpgradeSubContext} from './types';
import {getCommitType} from './getCommitType';

export const createCommitMessage = (upgradedVersions: UpgradedEntry[], context: UpgradeSubContext): string => {
const {packageName, debug, args} = context;

export const createCommitMessage = (
upgradedVersions: UpgradedEntry[],
{packageName, debug, args}: UpgradeSubContext,
): string => {
const lines: string[] = [];
const commitType = args.commitType ?? 'chore';
const commitType = getCommitType(context, upgradedVersions);

if (upgradedVersions.length === 1) {
lines.push(`${commitType}(deps): upgrade ${upgradedVersions[0].name} to v${upgradedVersions[0].version}`);
Expand Down
59 changes: 59 additions & 0 deletions apps/app-builder/src/commands/upgrade/getCommitType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {describe, expect, it} from 'vitest';
import {COMMIT_TYPE_AUTO} from '../../constants';
import {createTestContext} from '../../__tests__/createTestContext';
import {type UpgradedEntry} from './types';
import {getCommitType} from './getCommitType';

type TestInput = {
commitType: string;
versions: UpgradedEntry[];
result: string;
};

const createEntry = (newVersion: string, oldVersion?: string): UpgradedEntry => {
return {name: 'test', repository: '', oldVersion, version: newVersion};
};

describe('getCommitType', () => {
it.each([
{
commitType: 'chore',
versions: [createEntry('1.0.1', '1.0.0')],
result: 'chore',
},
{
commitType: 'chore',
versions: [createEntry('1.1.0', '1.0.0')],
result: 'chore',
},
{
commitType: COMMIT_TYPE_AUTO,
versions: [createEntry('1.0.1', '1.0.0'), createEntry('3.40.3', '3.40.0'), createEntry('4.14.3', '4.14.0')],
result: 'fix',
},
{
commitType: COMMIT_TYPE_AUTO,
versions: [createEntry('1.1.0', '1.0.0')],
result: 'feat',
},
{
commitType: COMMIT_TYPE_AUTO,
versions: [createEntry('1.1.0', '1.0.0'), createEntry('3.0.0', '2.0.0'), createEntry('4.14.3', '4.14.0')],
result: 'feat',
},
{
commitType: COMMIT_TYPE_AUTO,
versions: [createEntry('1.1.0'), createEntry('3.0.1', '3.0.0')],
result: 'feat',
},
{
commitType: COMMIT_TYPE_AUTO,
versions: [createEntry('1.0.0-alpha.62', '1.0.0-alpha.60')],
result: 'feat',
},
] satisfies TestInput[])('returns string', ({commitType, versions, result}) => {
const context = createTestContext({config: {commitType}});

expect(getCommitType(context, versions)).toBe(result);
});
});
22 changes: 22 additions & 0 deletions apps/app-builder/src/commands/upgrade/getCommitType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import diff from 'semver/functions/diff';
import {type PdkBuilderContext} from '../../types';
import {COMMIT_TYPE_AUTO} from '../../constants';
import {type UpgradedEntry} from './types';

export const getCommitType = (context: PdkBuilderContext, upgradedVersions: UpgradedEntry[]): string => {
if (context.config.commitType === COMMIT_TYPE_AUTO) {
const isPatchUpgrade = upgradedVersions.every((updatedVersion) => {
if (!updatedVersion.oldVersion) {
return false;
}

const difference = diff(updatedVersion.oldVersion, updatedVersion.version);

return difference === 'patch';
});

return isPatchUpgrade ? 'fix' : 'feat';
}

return context.config.commitType;
};
11 changes: 3 additions & 8 deletions apps/app-builder/src/commands/upgrade/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {type LiftoffEnv} from 'liftoff';
import {type MakeOptional, type PromiseOr} from '@myparcel/ts-utils';
import {type CommandArgs, type PdkDebugger, type ResolvedPdkBuilderConfig} from '../../types';
import {type CommandArgs, type PdkBuilderContext} from '../../types';

export interface ParsedEntry {
name: string;
Expand All @@ -10,7 +9,7 @@ export interface ParsedEntry {

export interface UpgradedEntry extends ParsedEntry {
oldVersion: string | undefined;
repository: string | undefined;
repository?: string | undefined;
}

export type UpgradeCommandArgs = CommandArgs & {
Expand All @@ -25,11 +24,7 @@ export type InputUpgradeCommandArgs = MakeOptional<
'composerCommand' | 'yarnCommand' | 'rootCommand'
>;

export interface UpgradeSubContext {
args: UpgradeCommandArgs;
config: ResolvedPdkBuilderConfig;
debug: PdkDebugger;
env: LiftoffEnv;
export interface UpgradeSubContext extends PdkBuilderContext<UpgradeCommandArgs> {
mode: UpgradeMode;
packageName: string;
}
Expand Down
2 changes: 2 additions & 0 deletions apps/app-builder/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ export const PLATFORM_SHEET_ID_MAP = {
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
[PdkPlatformName.MyParcelNl]: 1550017884,
} satisfies Record<PdkPlatformName, number>;

export const COMMIT_TYPE_AUTO = 'auto';
2 changes: 2 additions & 0 deletions apps/app-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type {PdkBuilderCommand, StringGenerator} from './types';

export type {PdkBuilderConfig};

export {COMMIT_TYPE_AUTO} from './constants';

export {PdkPlatformName} from './types';

export {
Expand Down
3 changes: 2 additions & 1 deletion apps/app-builder/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
COMMAND_TRANSLATIONS_NAME,
COMMAND_UPGRADE_NAME,
COMMAND_ZIP_NAME,
COMMIT_TYPE_AUTO,
TITLE,
} from './constants';

Expand Down Expand Up @@ -108,7 +109,7 @@ const COMMAND_UPGRADE: CommandDefinition = {
OPTION_QUIET,
OPTION_DRY_RUN,
['-l, --lockfile <lockfile>', 'Provide an alternative path to a lockfile.'],
['--commit-type <type>', 'Commit type', 'chore'],
['--commit-type <type>', 'Commit type', COMMIT_TYPE_AUTO],
['--no-check', 'Skip checking whether the lockfile is modified.'],
['--no-commit', 'Skip creating a commit.'],
],
Expand Down
6 changes: 6 additions & 0 deletions apps/app-builder/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ export type PdkBuilderConfig = {
vendorOutDir?: string;
};

/**
* Commit type to use when running the `upgrade` command. Set to auto to compute the commit type based on the
* upgraded versions.
*/
commitType?: string | 'auto' | 'chore' | 'feat' | 'fix';

additionalCommands?: CommandDefinition[];

hooks?: CommandHooksObject;
Expand Down
7 changes: 4 additions & 3 deletions apps/app-builder/src/utils/mergeDefaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {type PdkBuilderConfig, type ResolvedPdkBuilderConfig} from '../types';
import {DEFAULT_JSON_SPACES} from '../constants';
import {COMMIT_TYPE_AUTO, DEFAULT_JSON_SPACES} from '../constants';
import {NodePackageManager} from '../commands/upgrade/types';

export const mergeDefaultConfig = (config: PdkBuilderConfig): ResolvedPdkBuilderConfig => {
const resolvedConfig = {
additionalCommands: [],
archiveFilename: '{{platform}}-{{name}}-{{version}}.zip',
commitType: COMMIT_TYPE_AUTO,
composerCommand: 'composer',
debug: false,
jsonSpaces: DEFAULT_JSON_SPACES,
Expand Down Expand Up @@ -35,12 +36,12 @@ export const mergeDefaultConfig = (config: PdkBuilderConfig): ResolvedPdkBuilder
sheetId: 0,
...config.translations,
},
};
} satisfies PdkBuilderConfig;

return {
...resolvedConfig,
yarnCommand: resolvedConfig.yarnCommand ?? 'yarn',
nodePackageManagerCommand:
resolvedConfig.nodePackageManagerCommand ?? resolvedConfig.yarnCommand ?? resolvedConfig.nodePackageManager,
};
} satisfies ResolvedPdkBuilderConfig;
};
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,7 @@ __metadata:
inquirer: ^9.1.4
interpret: ^3.1.1
liftoff: ^4.0.0
semver: ^7.5.4
supports-color: ^9.3.1
tsup: ^7.1.0
bin:
Expand Down

0 comments on commit c8353fb

Please sign in to comment.