Skip to content

Commit

Permalink
feat: update packages script [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric authored Nov 13, 2024
1 parent c6f6401 commit c409f7c
Show file tree
Hide file tree
Showing 18 changed files with 531 additions and 120 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"semver": "^7.5.4",
"ts-expect": "^1.3.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"typescript": "4.9.5",
"typescript-transform-paths": "^3.4.6",
"verdaccio": "^5.29.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-scaffold-admin-app-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"terminal-link": "^2.1.1"
},
"devDependencies": {
"@types/inquirer": "8.2.10",
"@types/inquirer": "^8.2.10",
"@types/ncp": "^2.0.4",
"@types/pluralize": "^0.0.29",
"@webiny/plugins": "0.0.0",
Expand Down
117 changes: 0 additions & 117 deletions scripts/updateBabelPackages.js

This file was deleted.

12 changes: 12 additions & 0 deletions scripts/updatePackages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { updatePackages, presets, getUserInput } = require("./updatePackagesLib/index");

(async () => {
const input = await getUserInput({
presets
});
if (!input) {
return;
}

return updatePackages(input);
})();
84 changes: 84 additions & 0 deletions scripts/updatePackagesLib/BasicPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import loadJsonFile from "load-json-file";
import { IBasicPackage, IPackageJson } from "./types";
import path from "path";
import fs from "fs";
import semver from "semver";

export interface IBasicPackageParams {
packages: string[];
matching: RegExp;
}

interface IFindPackagesParams {
collection: IBasicPackage[];
json: IPackageJson;
matching: RegExp;
}

const types = ["dependencies", "devDependencies", "peerDependencies"] as const;

export class BasicPackages {
public readonly packages: IBasicPackage[];

private constructor(packages: IBasicPackage[]) {
this.packages = packages;
}

public static async create(params: IBasicPackageParams): Promise<BasicPackages> {
const { packages, matching } = params;
const results = packages.reduce<IBasicPackage[]>((collection, pkg) => {
const target = path.resolve(pkg, "package.json");
if (!fs.existsSync(target)) {
console.log(`File not found: ${target}`);
return collection;
}
const json = loadJsonFile.sync<IPackageJson>(target);
return BasicPackages.findPackages({
collection,
json,
matching
});
}, []);

return new BasicPackages(results);
}

private static findPackages(params: IFindPackagesParams): IBasicPackage[] {
const { collection, json, matching } = params;
return types.reduce<IBasicPackage[]>((packages, type) => {
const deps = json[type];
if (!deps) {
return packages;
}
for (const name in deps) {
if (name.match(matching) === null) {
continue;
}
const existing = packages.find(p => p.name === name);
if (!existing) {
const version = semver.coerce(deps[name]);
if (!version) {
console.warn(`Could not coerce version "${deps[name]}" for ${name}`);
continue;
}
packages.push({
name,
version
});

continue;
}
const version = semver.coerce(deps[name]);
if (!version) {
continue;
}
if (!semver.gt(existing.version, version)) {
continue;
}
existing.version = version;
}

return packages;
}, Array.from(collection));
}
}
55 changes: 55 additions & 0 deletions scripts/updatePackagesLib/LatestVersionPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import execa from "execa";
import semver from "semver";
import { IBasicPackage, IVersionedPackage } from "./types";

export interface IGetUpdatableParams {
packages: IBasicPackage[];
}

export class LatestVersionPackages {
private readonly cache: WeakMap<IBasicPackage[], IVersionedPackage[]> = new WeakMap();

public async getUpdatable(params: IGetUpdatableParams): Promise<IVersionedPackage[]> {
const cache = this.cache.get(params.packages);
if (cache) {
return cache;
}

const results: IVersionedPackage[] = [];

for (const localPackage of params.packages) {
try {
const result = await execa("npm", ["show", localPackage.name, "version"]);
if (!result.stdout) {
console.log(`Could not find "${localPackage.name}" latest version on npm.`);
continue;
}
const npmPackageVersion = semver.coerce(result.stdout);
if (!npmPackageVersion) {
console.log(
`Could not coerce "${localPackage.name}" latest version "${result.stdout}" from npm.`
);
continue;
}
if (semver.gte(localPackage.version, npmPackageVersion)) {
continue;
}

results.push({
...localPackage,
version: localPackage.version,
latestVersion: npmPackageVersion
});
} catch (ex) {
console.error(`Could not find "${localPackage}" latest version on npm.`, ex);
}
}

this.cache.set(params.packages, results);
return results;
}

public static async create() {
return new LatestVersionPackages();
}
}
66 changes: 66 additions & 0 deletions scripts/updatePackagesLib/ResolutionPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { IPackageJson, IVersionedPackage } from "./types";
import loadJsonFile from "load-json-file";
import writeJsonFile from "write-json-file";
import execa from "execa";

export interface IResolutionPackagesParams {
packages: IVersionedPackage[];
path: string;
skip: boolean;
}

export class ResolutionPackages {
private readonly skip: boolean;
private readonly packages: IVersionedPackage[];
private readonly path: string;
private addedToResolutions: IVersionedPackage[] | undefined;

private constructor(params: IResolutionPackagesParams) {
this.skip = params.skip;
this.path = params.path;
this.packages = params.packages;
}

public static async create(params: IResolutionPackagesParams): Promise<ResolutionPackages> {
return new ResolutionPackages(params);
}

public async addToPackageJson(): Promise<void> {
if (this.skip) {
return;
} else if (this.addedToResolutions) {
throw new Error(`Cannot execute addToPackageJson() twice.`);
}
this.addedToResolutions = [];
const rootPackageJson = loadJsonFile.sync<IPackageJson>(this.path);
for (const pkg of this.packages) {
if (!rootPackageJson.resolutions[pkg.name]) {
rootPackageJson.resolutions[pkg.name] = `^${pkg.latestVersion.raw}`;
this.addedToResolutions.push(pkg);
}
if (!rootPackageJson.devDependencies) {
rootPackageJson.devDependencies = {};
}
if (rootPackageJson.devDependencies[pkg.name]) {
continue;
}
rootPackageJson.devDependencies[pkg.name] = `^${pkg.latestVersion.raw}`;
}
writeJsonFile.sync(this.path, rootPackageJson);
await execa("yarn");
}

public async removeFromPackageJson(): Promise<void> {
if (this.skip || !this.addedToResolutions?.length) {
return;
}
const rootPackageJsonUp = loadJsonFile.sync<IPackageJson>(this.path);
if (!rootPackageJsonUp.resolutions) {
return;
}
for (const pkg of this.packages) {
delete rootPackageJsonUp.resolutions[pkg.name];
}
writeJsonFile.sync(this.path, rootPackageJsonUp);
}
}
25 changes: 25 additions & 0 deletions scripts/updatePackagesLib/UpPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IVersionedPackage } from "./types";
import execa from "execa";

export interface IUpPackagesParams {
packages: IVersionedPackage[];
}

export class UpPackages {
private readonly packages: IVersionedPackage[];

private constructor(params: IUpPackagesParams) {
this.packages = params.packages;
}

public static async create(params: IUpPackagesParams): Promise<UpPackages> {
return new UpPackages(params);
}

public async process(): Promise<void> {
for (const pkg of this.packages) {
await execa("yarn", ["up", `${pkg}@^${pkg.latestVersion.raw}`]);
console.log(`${pkg}: ${pkg.version.raw} -> ${pkg.latestVersion.raw}`);
}
}
}
12 changes: 12 additions & 0 deletions scripts/updatePackagesLib/createPreset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface IPreset {
name: string;
matching: RegExp;
skipResolutions: boolean;
}
export interface ICreatePresetCb {
(): IPreset;
}

export const createPreset = (cb: ICreatePresetCb) => {
return cb();
};
Loading

0 comments on commit c409f7c

Please sign in to comment.