Files
suwayomi-material-you-webui/tools/scripts/updateDependencies.ts

142 lines
4.4 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2026-03-11 00:11:29 +01:00
import type { ExecSyncOptions } from 'node:child_process';
import { execSync } from 'node:child_process';
import { writeFileSync } from 'node:fs';
import packageJson from '../../package.json';
type TPackageJson = typeof packageJson;
type PackageType = keyof Pick<TPackageJson, 'dependencies' | 'devDependencies'>;
type CaretMode = 'add' | 'remove';
type OutdatedDependency = {
name: string;
packageType: PackageType;
isMajorUpdate: boolean;
};
2026-03-08 23:25:02 +01:00
const PACKAGE_TYPES: PackageType[] = ['dependencies', 'devDependencies'];
const setCaretForDependency = <DependencyType extends PackageType>(
caretMode: CaretMode,
type: DependencyType,
dependency: keyof TPackageJson[DependencyType],
) => {
const versionRange = caretMode === 'add' ? '^' : '';
// maybe something is typed wrong, but typescript is not able to infer that the value of a dependency is of type string
packageJson[type][dependency] = `${versionRange}${packageJson[type][dependency]}` as any;
};
const setCaretForDependencies = (caretMode: CaretMode, dependencies: OutdatedDependency[]) => {
dependencies.forEach(({ name, packageType }) =>
setCaretForDependency(caretMode, packageType, name as keyof TPackageJson[typeof packageType]),
);
writeFileSync('package.json', `${JSON.stringify(packageJson, null, 2)}\n`);
};
/**
* @param asJson
* @param stdio - only considered in case asJson is falsy
*/
const execYarnOutdated = ({ asJson = false, stdio }: { asJson?: boolean; stdio?: ExecSyncOptions['stdio'] } = {}):
| string
| null => {
try {
2026-05-15 15:11:47 +02:00
execSync(`pnpm outdated ${asJson ? '--json' : ''}`, { stdio: !asJson ? stdio : undefined }).toString();
return null;
} catch (e: any) {
if (e?.stdout === null) {
return null;
}
if (!Buffer.isBuffer(e.stdout)) {
throw e;
}
return e.stdout.toString();
}
};
const getOutdatedDependencies = (): OutdatedDependency[] => {
const output = execYarnOutdated({ asJson: true });
if (output === null) {
return [];
}
const lines = output.split('\n');
const outdatedPackages = lines
.map((line) => {
try {
return JSON.parse(line);
} catch (error) {
return null;
}
})
.filter(Boolean)
.filter((item) => item.type === 'table')
.flatMap((item) => item.data.body);
return outdatedPackages
.map(([name, current, , latest, packageType]) => ({
name,
packageType,
isMajorUpdate: current.split('.')[0] !== latest.split('.')[0],
}))
2026-03-08 23:25:02 +01:00
.filter(({ packageType }) => PACKAGE_TYPES.includes(packageType))
.toSorted((a, b) => a.packageType.localeCompare(b.packageType));
};
2026-03-11 00:11:29 +01:00
const log = (...args: Parameters<typeof console.log>) => console.log('updateDependencies:', ...args);
2026-03-11 00:11:29 +01:00
const updateDependencies = () => {
log('checking for outdated dependencies...');
const outdatedDependencies = getOutdatedDependencies();
const autoUpdatableDependencies = outdatedDependencies.filter(({ isMajorUpdate }) => !isMajorUpdate);
if (!outdatedDependencies.length) {
log('all dependencies are up-to-date');
return;
}
execYarnOutdated({ stdio: 'inherit' });
if (!autoUpdatableDependencies.length) {
log('no automatically updatable dependencies found');
return;
}
log('updating dependencies with non breaking changes...');
setCaretForDependencies('add', autoUpdatableDependencies);
2026-05-15 15:11:47 +02:00
execSync('pnpm', { stdio: 'inherit' });
log('syncing package.json with yarn.lock...');
2026-05-15 15:11:47 +02:00
execSync('pnpm up --latest --lockfile-only', { stdio: 'inherit' });
log('updating yarn.lock after syncing package.json...');
2026-05-15 15:11:47 +02:00
execSync('pnpm', { stdio: 'inherit' });
log('commiting changes...');
execSync('git add package.json yarn.lock && git commit -m "Update dependencies"', { stdio: 'inherit' });
log('updated dependencies with non breaking changes. Check dependencies with breaking changes listed below:');
execYarnOutdated({ stdio: 'inherit' });
};
updateDependencies();