refactor(update): properly separate code

This commit is contained in:
matt1432 2025-04-16 10:44:57 -04:00
parent 81b61228e0
commit a93ec6ad3f
8 changed files with 53 additions and 52 deletions

View file

@ -1,19 +1,16 @@
import { spawnSync } from 'node:child_process';
import { writeFileSync } from 'node:fs';
import { styleText } from 'node:util';
import { parseArgs } from './lib';
import { updateCaddyPlugins } from './caddy';
import { updateDocker } from './docker';
import { updateFirefoxAddons } from '././firefox';
import { updateFlakeInputs } from './flake';
import updateCaddyPlugins from './caddy';
import updateDocker from './docker';
import updateFirefoxAddons from '././firefox';
import updateFlakeInputs from './flake';
import runNixUpdate from './nix-update';
import updateNodeModules from './node-modules';
import {
runNixUpdate,
updateVuetorrent,
} from './misc';
import { styleText } from 'node:util';
import updateVuetorrent from './vuetorrent';
/* Constants */

View file

@ -34,7 +34,7 @@ ${Object.entries(plugins)
}
`;
export const updateCaddyPlugins = (): string => {
export default (): string => {
console.log(styleText(['magenta'], '\nUpdating caddy plugins:\n'));
let updates = '';

View file

@ -16,7 +16,7 @@ const updateImages = (imagePath: string): string | undefined => {
}
};
export const updateDocker = () => {
export default () => {
console.log(styleText(['magenta'], '\nUpdating docker images:\n'));
let updates = '';

View file

@ -6,7 +6,7 @@ import { styleText } from 'node:util';
/* Constants */
const FLAKE = process.env.FLAKE;
export const updateFirefoxAddons = () => {
export default () => {
console.log(styleText(['magenta'], '\nUpdating firefox addons using mozilla-addons-to-nix:\n'));
const DIR = `${FLAKE}/scopedPackages/firefox-addons`;

View file

@ -5,7 +5,7 @@ import { styleText } from 'node:util';
/* Constants */
const FLAKE = process.env.FLAKE;
export const updateFlakeInputs = () => {
export default () => {
console.log(styleText(['magenta'], '\nUpdating flake inputs:\n'));
const output = spawnSync(

View file

@ -0,0 +1,39 @@
import { spawnSync } from 'node:child_process';
import { styleText } from 'node:util';
/* Constants */
const FLAKE = process.env.FLAKE;
const getAttrVersion = (attr: string): string => spawnSync('nix',
['eval', '--raw', `${FLAKE}#${attr}.version`],
{ shell: true }).stdout.toString();
export default (
attr: string,
scope?: string,
scopeAttr?: string,
): {
changelog: string | null
stdout: string
stderr: string
} => {
const realAttr = scope ? `${attr}.x86_64-linux.${scope}.${scopeAttr}` : attr;
const cleanAttr = scope ? `${attr}.${scope}.${scopeAttr}` : attr;
console.log(styleText(['magenta'], `\nUpdating ${realAttr}:\n`));
const OLD_VERSION = getAttrVersion(realAttr);
const execution = spawnSync('nix-update', ['--flake', realAttr, '-u'], { cwd: FLAKE });
const NEW_VERSION = getAttrVersion(realAttr);
return {
changelog: OLD_VERSION !== NEW_VERSION ?
`${cleanAttr}: ${OLD_VERSION} -> ${NEW_VERSION}\n` :
null,
stdout: execution.stdout.toString(),
stderr: execution.stderr.toString(),
};
};

View file

@ -1,9 +1,9 @@
import { readPackageJSON, writePackageJSON } from 'pkg-types';
import { accessSync, constants, existsSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { styleText } from 'node:util';
import { replaceInFile, npmRun } from './lib';
import { styleText } from 'node:util';
/* Constants */
@ -11,7 +11,6 @@ const FLAKE = process.env.FLAKE as string;
const PINS = new Map([]);
const updatePackageJson = async(workspaceDir: string, updates: object) => {
const currentPackageJson = await readPackageJSON(`${workspaceDir}/package.json`);

View file

@ -1,8 +1,8 @@
import { writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { styleText } from 'node:util';
import { parseFetchurl } from './lib';
import { styleText } from 'node:util';
/* Constants */
@ -20,7 +20,7 @@ const genVueText = (
}
`;
export const updateVuetorrent = () => {
export default () => {
console.log(styleText(['magenta'], '\nUpdating Vuetorrent:\n'));
const FILE = `${FLAKE}/configurations/nos/modules/qbittorrent/vuetorrent.nix`;
@ -42,37 +42,3 @@ export const updateVuetorrent = () => {
return OLD_VERSION !== VERSION ? `Vuetorrent: ${OLD_VERSION} -> ${VERSION}` : '';
};
const getAttrVersion = (attr: string): string => spawnSync('nix',
['eval', '--raw', `${FLAKE}#${attr}.version`],
{ shell: true }).stdout.toString();
export const runNixUpdate = (
attr: string,
scope?: string,
scopeAttr?: string,
): {
changelog: string | null
stdout: string
stderr: string
} => {
const realAttr = scope ? `${attr}.x86_64-linux.${scope}.${scopeAttr}` : attr;
const cleanAttr = scope ? `${attr}.${scope}.${scopeAttr}` : attr;
console.log(styleText(['magenta'], `\nUpdating ${realAttr}:\n`));
const OLD_VERSION = getAttrVersion(realAttr);
const execution = spawnSync('nix-update', ['--flake', realAttr, '-u'], { cwd: FLAKE });
const NEW_VERSION = getAttrVersion(realAttr);
return {
changelog: OLD_VERSION !== NEW_VERSION ?
`${cleanAttr}: ${OLD_VERSION} -> ${NEW_VERSION}\n` :
null,
stdout: execution.stdout.toString(),
stderr: execution.stderr.toString(),
};
};