Skip to content

πŸš€ Add command to increase project version and implement version increment logic, Closes #94 #387

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

Merged
merged 14 commits into from
Mar 7, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@
"title": "Rename project",
"category": "SharePoint Framework Toolkit"
},
{
"command": "spfx-toolkit.increaseVersion",
"title": "Increase project version",
"category": "SharePoint Framework Toolkit"
},
{
"command": "spfx-toolkit.grantAPIPermissions",
"title": "Grant API permissions",
Expand Down
3 changes: 3 additions & 0 deletions src/constants/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const Commands = {
// Rename
renameProject: `${EXTENSION_NAME}.renameProject`,

// Increase version
increaseVersion: `${EXTENSION_NAME}.increaseVersion`,

// Grant API permissions
grantAPIPermissions: `${EXTENSION_NAME}.grantAPIPermissions`,

Expand Down
24 changes: 24 additions & 0 deletions src/panels/CommandPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Subscription } from '../models';
import { Extension } from '../services/dataType/Extension';
import { getExtensionSettings } from '../utils';
import { Notifications } from '../services/dataType/Notifications';
import { increaseVersion } from '../utils/increaseVersion';


export class CommandPanel {
Expand All @@ -29,6 +30,9 @@ export class CommandPanel {
subscriptions.push(
commands.registerCommand(Commands.welcome, () => commands.executeCommand('workbench.action.openWalkthrough', 'm365pnp.viva-connections-toolkit#spfx-toolkit-intro', false))
);
subscriptions.push(
commands.registerCommand(Commands.increaseVersion, CommandPanel.increaseVersion)
);

CommandPanel.init();
}
Expand Down Expand Up @@ -303,6 +307,7 @@ export class CommandPanel {
actionCommands.push(new ActionTreeItem('Upgrade project SPFx version', '', { name: 'arrow-up', custom: false }, undefined, Commands.upgradeProject));
actionCommands.push(new ActionTreeItem('Validate project correctness', '', { name: 'check-all', custom: false }, undefined, Commands.validateProject));
actionCommands.push(new ActionTreeItem('Rename project', '', { name: 'whole-word', custom: false }, undefined, Commands.renameProject));
actionCommands.push(new ActionTreeItem('Increase project version', '', { name: 'fold-up', custom: false }, undefined, Commands.increaseVersion));

if (EnvironmentInformation.account) {
actionCommands.push(new ActionTreeItem('Grant API permissions', '', { name: 'workspace-trusted', custom: false }, undefined, Commands.grantAPIPermissions));
Expand Down Expand Up @@ -353,4 +358,23 @@ export class CommandPanel {
private static showWelcome() {
commands.executeCommand('setContext', ContextKeys.showWelcome, true);
}

/**
* Increases the version of the project.
*/
public static async increaseVersion() {
const versionType = await window.showQuickPick(['major', 'minor', 'patch'], {
placeHolder: 'Select the version type to increase',
ignoreFocusOut: true,
canPickMany: false,
title: 'Increase Version'
});

if (!versionType) {
return;
}

await increaseVersion(versionType as 'major' | 'minor' | 'patch');
Notifications.info('Version increased successfully.');
}
}
54 changes: 54 additions & 0 deletions src/utils/increaseVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { workspace } from 'vscode';
import { TeamsToolkitIntegration } from '../services/dataType/TeamsToolkitIntegration';


export async function increaseVersion(versionType: 'major' | 'minor' | 'patch') {
const wsFolder = workspace.workspaceFolders?.[0];
if (!wsFolder) {
throw new Error('Workspace folder not found');
}

let packageSolutionFiles = [];
if (TeamsToolkitIntegration.isTeamsToolkitProject) {
packageSolutionFiles = await workspace.findFiles('src/config/package-solution.json', '**/node_modules/**');
} else {
packageSolutionFiles = await workspace.findFiles('config/package-solution.json', '**/node_modules/**');
}

const packageSolutionPath = packageSolutionFiles[0].fsPath;
const packageSolution = JSON.parse(readFileSync(packageSolutionPath, 'utf8'));

const packageJsonPath = join(wsFolder.uri.fsPath, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));

const newVersion = incrementVersion(packageJson.version, versionType);
packageJson.version = newVersion;
packageSolution.solution.version = `${newVersion}.0`;

writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
writeFileSync(packageSolutionPath, JSON.stringify(packageSolution, null, 2));

if (TeamsToolkitIntegration.isTeamsToolkitProject) {
const packageJsonSrcPath = join(wsFolder.uri.fsPath, 'src', 'package.json');
const packageJsonSrc = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
packageJsonSrc.version = newVersion;
writeFileSync(packageJsonSrcPath, JSON.stringify(packageJsonSrc, null, 2));
}
}

function incrementVersion(version: string, versionType: 'major' | 'minor' | 'patch'): string {
const [major, minor, patch] = version.split('.').map(Number);

switch (versionType) {
case 'major':
return `${major + 1}.0.0`;
case 'minor':
return `${major}.${minor + 1}.0`;
case 'patch':
return `${major}.${minor}.${patch + 1}`;
default:
throw new Error('Invalid version type');
}
}