Skip to content

Commit

Permalink
Merge pull request #19276 from abpframework/issue-19275
Browse files Browse the repository at this point in the history
Fix invalid lepton-x package version in all repository after deployment
  • Loading branch information
ebicoglu committed Mar 13, 2024
2 parents 63aacdb + 8db8041 commit 7ea9d98
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 100 deletions.
10 changes: 7 additions & 3 deletions npm/publish-ng.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
param(
[string]$Version,
[string]$Registry
[string]$Registry,
[string]$LeptonXVersion
)

yarn install
Expand All @@ -15,10 +16,11 @@ if (-Not $Version) {
if (-Not $Registry) {
$Registry = "https://registry.npmjs.org";
}

$UpdateNgPacksCommand = "yarn update-version $Version"
$NgPacksPublishCommand = "npm run publish-packages -- --nextVersion $Version --skipGit --registry $Registry --skipVersionValidation"
$UpdateGulpCommand = "yarn update-gulp --registry $Registry"

$UpdateLeptonXCommand = "yarn update-lepton-x-versions -v $LeptonXVersion";

$IsPrerelease = $(node publish-utils.js --prerelease --customVersion $Version) -eq "true";

Expand All @@ -38,7 +40,9 @@ $commands = (
"cd scripts",
"yarn remove-lock-files",
"cd ..",
$UpdateGulpCommand
$UpdateGulpCommand,
"cd scripts",
$UpdateLeptonXCommand
)

foreach ($command in $commands) {
Expand Down
167 changes: 90 additions & 77 deletions npm/scripts/change-package-version.ts
Original file line number Diff line number Diff line change
@@ -1,101 +1,114 @@
import glob from "glob";
import fse from "fs-extra";
import {program} from "commander";
import { program } from "commander";

(function findPackageJsonFiles() {
setupCommander();
const options = {
ignore: [
"../../**/node_modules/**",
"../../**/dist/**",
"../../**/build/**",
"../../**/scripts/**",
"../../**/wwwroot/**"
]
};
export const semverRegex =
/\d+\.\d+\.\d+(?:-[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?(?:\+[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?$/;

const workingDir = "../../";
glob(`${workingDir}**/package.json`, options, (err, files) => {
if (err) throw err;
function setupCommander() {
program
.option("-n, --packageName <packageName>", "Package name")
.option(
"-v, --targetVersion <targetVersion>",
"Version number of the package"
);

//Todo @masumulu28: check options value and throw error if not provided
const {packageName,targetVersion}= program.opts();

for (const file of files) {
readPackageJsonFile(file, packageName, targetVersion);
}
});
})();
program.parse(process.argv);
}

function readPackageJsonFile(path, key, newVersion) {
const replace = (block, key, newVersion) => {
const founded = Object.keys(block).filter(x => x === key);
const founded = Object.keys(block).filter((x) => x === key);

if (founded.length > 0) {
let value = block[key];
value = value.replace(semverRegex, newVersion);
return [true, {
...block,
[key]:value
}];

return [
true,
{
...block,
[key]: value,
},
];
}

return [false, block];
};

fse.readJson(path, (err, packageObj) => {
if (err) throw err;

const { dependencies, peerDependencies, devDependencies } = packageObj;
const results = [];

let result = { ...packageObj };
if (dependencies) {
const [founded, d] = replace(dependencies, key, newVersion);
results.push(founded);
result = {
...result, dependencies: d
};
}
if (peerDependencies) {
const [founded, p] = replace(peerDependencies, key, newVersion);
results.push(founded);
result = {
...result, peerDependencies: p
};
}
if (devDependencies) {
const [founded, d] = replace(devDependencies, key, newVersion);
results.push(founded);
result = {
...result, devDependencies: d
};
}
const anyChanges = !results.some(x => x);
if (anyChanges) {
return;
}
console.log("changed", path);
writeFile(path, result);
if (err) {
throw err;
}

const { dependencies, peerDependencies, devDependencies } = packageObj;
const results = [];

let result = { ...packageObj };
if (dependencies) {
const [founded, d] = replace(dependencies, key, newVersion);
results.push(founded);
result = {
...result,
dependencies: d,
};
}

if (peerDependencies) {
const [founded, p] = replace(peerDependencies, key, newVersion);
results.push(founded);
result = {
...result,
peerDependencies: p,
};
}
)
;

if (devDependencies) {
const [founded, d] = replace(devDependencies, key, newVersion);
results.push(founded);
result = {
...result,
devDependencies: d,
};
}

const anyChanges = !results.some((x) => x);
if (anyChanges) {
return;
}

console.log("changed", path);
writeFile(path, result);
});
}
export const semverRegex =
/\d+\.\d+\.\d+(?:-[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?(?:\+[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?$/;

function writeFile(path, result) {
return fse.writeJson(path, result, { spaces: 2 });
}

(function findPackageJsonFiles() {
setupCommander();
const options = {
ignore: [
"../../**/node_modules/**",
"../../**/dist/**",
"../../**/build/**",
"../../**/scripts/**",
"../../**/wwwroot/**",
],
};

const workingDir = "../../";
glob(`${workingDir}**/package.json`, options, (err, files) => {
if (err) {
throw err;
}

function setupCommander() {
program
.option(
"-n, --packageName <packageName>",
"Package name"
)
.option(
"-v, --targetVersion <targetVersion>",
"Version number of the package"
);
//Todo @masumulu28: check options value and throw error if not provided
const { packageName, targetVersion } = program.opts();

program.parse(process.argv);
}
for (const file of files) {
readPackageJsonFile(file, packageName, targetVersion);
}
});
})();
3 changes: 2 additions & 1 deletion npm/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"scripts": {
"remove-lock-files": "yarn && ts-node -r tsconfig-paths/register remove-lock-files.ts",
"validate-versions": "yarn && ts-node -r tsconfig-paths/register validate-versions.ts",
"change-package-version": "ts-node -r tsconfig-paths/register change-package-version.ts"
"change-package-version": "ts-node -r tsconfig-paths/register change-package-version.ts",
"update-lepton-x-versions": "ts-node -r tsconfig-paths/register update-lepton-x-versions.ts"
},
"dependencies": {
"axios": "^0.24.0",
Expand Down
55 changes: 55 additions & 0 deletions npm/scripts/update-lepton-x-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { program } from "commander";
import childProcess from "child_process";

/**
* This script is used to update the version of the LeptonX (angular |mvc | blazor) npm packages.
* Depending to **change-package-version.ts** file. (Should we move this to a single script?)
*
* I'm not sure about depending "commander" package. We might need to get options from process.env directly ?
*
* Example
* -Set env
* $env:targetVersion = "1.0.0"
* -Read from nodejs
* process.env.targetVersion
* -Use as argument in commands
* const command = `yarn change-package-version -n ${packageName} -v ${process.env.targetVersion}`;
*/

//All lepton-x-lite packages for open source (angular | mvc | blazor) UI frameworks
const LEPTON_X_PACKAGE_NAMES = [
"@abp/ng.theme.lepton-x",
"@abp/aspnetcore.mvc.ui.theme.leptonxlite",
"@abp/aspnetcore.components.server.leptonxlitetheme",
];

function validateVersion(targetVersion) {
if (!targetVersion) {
console.log("\x1b[31m", "Error: lepton-x targetVersion is not defined");
process.exit(1);
}
}

function initCommander() {
program.option(
"-v, --targetVersion <targetVersion>",
"Version number of the package"
);

program.parse(process.argv);
}

(() => {
initCommander();

const { targetVersion } = program.opts();

validateVersion(targetVersion);

LEPTON_X_PACKAGE_NAMES.forEach((packageName) => {
const command = `yarn change-package-version -n ${packageName} -v ${targetVersion}`;
const result = childProcess.execSync(command).toString();

console.log(result);
});
})();
42 changes: 23 additions & 19 deletions npm/scripts/validate-versions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { program } from 'commander';
import fse from 'fs-extra';
import * as path from 'path';
import { log } from './utils/log';
import { program } from "commander";
import fse from "fs-extra";
import * as path from "path";
import { log } from "./utils/log";

let excludedPackages = [];

Expand All @@ -12,29 +12,31 @@ let excludedPackages = [];

function initCommander() {
program.requiredOption(
'-v, --compareVersion <version>',
'version to compare'
"-v, --compareVersion <version>",
"version to compare"
);
program.requiredOption('-p, --path <path>', 'NPM packages folder path');
program.requiredOption("-p, --path <path>", "NPM packages folder path");
program.option(
'-ep, --excludedPackages <excludedpackages>',
'Packages that will not be checked. Can be passed with separeted comma (like @abp/utils,@abp/core)',
''
"-ep, --excludedPackages <excludedpackages>",
"Packages that will not be checked. Can be passed with separeted comma (like @abp/utils,@abp/core)",
""
);
program.parse(process.argv);

excludedPackages = program.opts().excludedPackages.split(',');
excludedPackages = program.opts().excludedPackages.split(",");
}

async function compare() {
let { compareVersion, path: packagesPath } = program.opts();
packagesPath = path.resolve(packagesPath);

const packageFolders = await fse.readdir(packagesPath);

for (let i = 0; i < packageFolders.length; i++) {
const folder = packageFolders[i];
const pkgJsonPath = `${packagesPath}/${folder}/package.json`;
let pkgJson;

try {
pkgJson = await fse.readJSON(pkgJsonPath);
} catch (error) {}
Expand All @@ -46,11 +48,10 @@ async function compare() {
throwError(pkgJsonPath, pkgJson.name, pkgJson.version);
}

const { dependencies, peerDependencies } = pkgJson;
if (dependencies) await compareDependencies(dependencies, pkgJsonPath);
// if (peerDependencies) { // TODO: update peerDependencies while updating version
// await compareDependencies(peerDependencies, pkgJsonPath);
// }
const { dependencies } = pkgJson;
if (dependencies) {
await compareDependencies(dependencies, pkgJsonPath);
}
}
}

Expand All @@ -66,6 +67,7 @@ async function compareDependencies(
const packageName = entry[0];
const version = getCleanVersionName(entry[1]);
const cleanCompareVersion = getCleanVersionName(compareVersion);

if (
!excludedPackages.includes(entry[0]) &&
packageName.match(/@(abp|volo)/)?.length &&
Expand All @@ -79,11 +81,13 @@ async function compareDependencies(
function throwError(filePath: string, pkg: string, version?: string) {
const { compareVersion } = program.opts();

log.error(`${filePath}: ${pkg} version is not ${compareVersion}. it is ${version}`);
log.error(
`${filePath}: ${pkg} version is not ${compareVersion}. it is ${version}`
);
process.exit(1);
}

function getCleanVersionName(version) {
// Remove caret (^) or tilde (~) from the beginning of the version number
return version.replace(/^[\^~]+/, '');
}
return version.replace(/^[\^~]+/, "");
}

0 comments on commit 7ea9d98

Please sign in to comment.