diff --git a/apps/update-sources/src/app.ts b/apps/update-sources/src/app.ts index 779085f4..4e9ca622 100644 --- a/apps/update-sources/src/app.ts +++ b/apps/update-sources/src/app.ts @@ -29,15 +29,15 @@ const main = async() => { } if (args['cp'] || args['caddy-plugins']) { - console.log(updateCaddyPlugins()); + console.log(updateCaddyPlugins() ?? ''); } if (args['d'] || args['docker']) { - console.log(updateDocker()); + console.log(updateDocker() ?? ''); } if (args['f'] || args['firefox']) { - console.log(updateFirefoxAddons()); + console.log(updateFirefoxAddons() ?? ''); } if (args['h'] || args['homepage']) { @@ -45,7 +45,7 @@ const main = async() => { } if (args['i'] || args['inputs']) { - console.log(updateFlakeInputs()); + console.log(updateFlakeInputs() ?? ''); } if (args['j'] || args['jmusicbot']) { @@ -57,7 +57,7 @@ const main = async() => { } if (args['n'] || args['node_modules']) { - console.log(await updateNodeModules()); + console.log((await updateNodeModules()) ?? ''); } if (args['p'] || args['pam-fprint-grosshack']) { @@ -77,43 +77,43 @@ const main = async() => { } if (args['v'] || args['vuetorrent']) { - console.log(updateVuetorrent()); + console.log(updateVuetorrent() ?? ''); } if (args['a'] || args['all']) { // Update this first because of nix run cmd const firefoxOutput = updateFirefoxAddons(); - console.log(firefoxOutput); + console.log(firefoxOutput ?? 'No updates'); const flakeOutput = updateFlakeInputs(); - console.log(flakeOutput); + console.log(flakeOutput ?? 'No updates'); const dockerOutput = updateDocker(); - console.log(dockerOutput); + console.log(dockerOutput ?? 'No updates'); const nodeModulesOutput = await updateNodeModules(); - console.log(nodeModulesOutput); + console.log(nodeModulesOutput ?? 'No updates'); const vuetorrentOutput = updateVuetorrent(); - console.log(vuetorrentOutput); + console.log(vuetorrentOutput ?? 'No updates'); const caddyPluginsOutput = updateCaddyPlugins(); - console.log(caddyPluginsOutput); + console.log(caddyPluginsOutput ?? 'No updates'); // nix-update executions - let nixUpdateOutputs = ''; + const nixUpdateOutputs: string[] = []; const updatePackage = ( attr: string, @@ -123,7 +123,7 @@ const main = async() => { const execution = runNixUpdate(attr, scope, scopeAttr); if (execution.changelog) { - nixUpdateOutputs += execution.changelog; + nixUpdateOutputs.push(execution.changelog); } console.log(execution.stderr); console.log(execution.stdout); @@ -139,6 +139,8 @@ const main = async() => { updatePackage('scopedPackages', 'lovelace-components', 'material-rounded-theme'); + spawnSync('alejandra', ['-q', FLAKE], { shell: true }); + spawnSync('nixFastBuild', [], { shell: true, stdio: [process.stdin, process.stdout, process.stderr], @@ -149,41 +151,40 @@ const main = async() => { }; const output = [ - 'chore: update sources\n\n', + 'chore: update sources', ]; - if (flakeOutput.length > 5) { - output.push(`Flake Inputs:\n${indentOutput(flakeOutput)}\n\n\n`); + if (flakeOutput) { + output.push(`Flake Inputs:\n${indentOutput(flakeOutput)}\n`); } - if (dockerOutput.length > 5) { - output.push(`Docker Images:\n${indentOutput(dockerOutput)}\n\n\n`); + if (dockerOutput) { + output.push(`Docker Images:\n${indentOutput(dockerOutput)}\n`); } - if (firefoxOutput.length > 5) { - output.push(`Firefox Addons:\n${indentOutput(firefoxOutput)}\n\n\n`); + if (firefoxOutput) { + output.push(`Firefox Addons:\n${indentOutput(firefoxOutput)}\n`); } - if (nodeModulesOutput.length > 5) { - output.push(`Node modules:\n${indentOutput(nodeModulesOutput)}\n\n\n`); + if (nodeModulesOutput) { + output.push(`Node modules:\n${indentOutput(nodeModulesOutput)}\n`); } - if (vuetorrentOutput.length > 5) { - output.push(`Misc Sources:\n${indentOutput(vuetorrentOutput)}\n\n\n`); + if (vuetorrentOutput) { + output.push(`qBittorrent Sources:\n${indentOutput(vuetorrentOutput)}\n`); } - if (caddyPluginsOutput !== '') { - output.push(`Caddy Plugins:\n${indentOutput(caddyPluginsOutput)}\n\n\n`); + if (caddyPluginsOutput) { + output.push(`Caddy Plugins:\n${indentOutput(caddyPluginsOutput)}\n`); } - if (nixUpdateOutputs !== '') { - output.push(`nix-update executions:\n${indentOutput(nixUpdateOutputs)}\n\n\n`); + if (nixUpdateOutputs.length > 0) { + output.push(`nix-update executions:\n${indentOutput(nixUpdateOutputs.join('\n'))}\n`); } if (args['f']) { - writeFileSync(args['f'] as string, output.join('')); + console.log(styleText(['magenta'], `\n\nWriting commit message to ${args['f']}\n`)); + writeFileSync(args['f'], output.join('\n\n')); } else { console.log(styleText(['magenta'], '\n\nCommit message:\n')); - console.log(output.join('')); + console.log(output.join('\n\n')); } } - - spawnSync('alejandra', ['-q', FLAKE], { shell: true }); }; main(); diff --git a/apps/update-sources/src/caddy.ts b/apps/update-sources/src/caddy.ts index 2f6f46a3..ca84f9f6 100644 --- a/apps/update-sources/src/caddy.ts +++ b/apps/update-sources/src/caddy.ts @@ -34,10 +34,10 @@ ${Object.entries(plugins) } `; -export default (): string => { +export default (): string | null => { console.log(styleText(['magenta'], '\nUpdating caddy plugins:\n')); - let updates = ''; + const updates: string[] = []; const dir = `${FLAKE}/configurations/cluster/modules/caddy`; // Setup workspace @@ -70,7 +70,7 @@ export default (): string => { .split(' ')[1]; if (plugins[key].version !== NEW_VERSION) { - updates += `${key}: ${plugins[key].version} -> ${NEW_VERSION}\n`; + updates.push(`${key}: ${plugins[key].version} -> ${NEW_VERSION}`); plugins[key].version = NEW_VERSION; } }); @@ -88,5 +88,5 @@ export default (): string => { replaceInFile(/hash = ".*";/, `hash = "${NEW_HASH}";`, `${dir}/plugins.nix`); - return updates; + return updates.length > 0 ? updates.join('\n') : null; }; diff --git a/apps/update-sources/src/docker.ts b/apps/update-sources/src/docker.ts index bfe33fde..a5683708 100644 --- a/apps/update-sources/src/docker.ts +++ b/apps/update-sources/src/docker.ts @@ -16,21 +16,36 @@ const updateImages = (imagePath: string): string | undefined => { } }; -export default () => { +export default (): string | null => { console.log(styleText(['magenta'], '\nUpdating docker images:\n')); - let updates = ''; + const updates: string[] = []; - updates += updateImages(`${FLAKE}/configurations/nos/modules/jellyfin`) ?? ''; - updates += updateImages(`${FLAKE}/configurations/homie/modules/home-assistant/netdaemon`) ?? ''; + const jellfyinUpdates = updateImages(`${FLAKE}/configurations/nos/modules/jellyfin`); + + if (jellfyinUpdates) { + updates.push(jellfyinUpdates); + } + + const hassUpdates = updateImages(`${FLAKE}/configurations/homie/modules/home-assistant/netdaemon`); + + if (hassUpdates) { + updates.push(hassUpdates); + } const DIR = `${FLAKE}/configurations/nos/modules/docker`; readdirSync(DIR, { withFileTypes: true, recursive: true }).forEach((path) => { if (path.name === 'compose.nix') { - updates += updateImages(path.parentPath) ?? ''; + const composeUpdates = updateImages(path.parentPath); + + if (composeUpdates) { + updates.push(composeUpdates); + } } }); - return updates; + return updates.length > 0 ? + updates.join('') : + null; }; diff --git a/apps/update-sources/src/firefox.ts b/apps/update-sources/src/firefox.ts index c363120d..8531404c 100644 --- a/apps/update-sources/src/firefox.ts +++ b/apps/update-sources/src/firefox.ts @@ -6,7 +6,7 @@ import { styleText } from 'node:util'; /* Constants */ const FLAKE = process.env.FLAKE; -export default () => { +export default (): string | null => { console.log(styleText(['magenta'], '\nUpdating firefox addons using mozilla-addons-to-nix:\n')); const DIR = `${FLAKE}/scopedPackages/firefox-addons`; @@ -54,9 +54,12 @@ export default () => { return [nameMap[pinfo[0]], pinfo[2]]; })); - return Object.keys(OLD_VERS) + const changelogs = Object.keys(OLD_VERS) .sort() .filter((pname) => OLD_VERS[pname] !== NEW_VERS[pname]) - .map((pname) => `${pname}: ${OLD_VERS[pname]} -> ${NEW_VERS[pname]}`) - .join('\n'); + .map((pname) => `${pname}: ${OLD_VERS[pname]} -> ${NEW_VERS[pname]}`); + + return changelogs.length > 0 ? + changelogs.join('\n') : + null; }; diff --git a/apps/update-sources/src/flake.ts b/apps/update-sources/src/flake.ts index d3f022c1..2809f96e 100644 --- a/apps/update-sources/src/flake.ts +++ b/apps/update-sources/src/flake.ts @@ -5,17 +5,18 @@ import { styleText } from 'node:util'; /* Constants */ const FLAKE = process.env.FLAKE; -export default () => { +export default (): string | null => { console.log(styleText(['magenta'], '\nUpdating flake inputs:\n')); - const output = spawnSync( + const output: string = spawnSync( `git restore flake.lock &> /dev/null; nix flake update --flake ${FLAKE}` + ' |& grep -v "warning: updating lock file" |& grep -v "unpacking"', [], { shell: true }, - ).stdout - .toString() - // Add an extra blank line between inputs + ).stdout.toString(); + + const inputsUpdates: string[] = output + // Get an array of each update / change .split('\n•') // Filter out some inputs .filter((input) => ![ @@ -29,9 +30,12 @@ export default () => { 'nix-gaming/umu', 'nix-github-actions', 'pre-commit-hooks', - ].some((inputName) => input.startsWith(` Updated input '${inputName}'`))) + ].some((inputName) => input.startsWith(` Updated input '${inputName}'`))); + + const formattedOutput: string = inputsUpdates + // Add an extra blank line between inputs .join('\n\n•') - // help readability of git revs + // Help readability of git revs .split('\n') .map((l) => l .replace( @@ -44,5 +48,7 @@ export default () => { )) .join('\n'); - return output; + return inputsUpdates.length > 0 ? + formattedOutput : + null; }; diff --git a/apps/update-sources/src/lib.ts b/apps/update-sources/src/lib.ts index 4d29931d..78642ad6 100644 --- a/apps/update-sources/src/lib.ts +++ b/apps/update-sources/src/lib.ts @@ -1,9 +1,15 @@ import { spawnSync } from 'node:child_process'; import { readFileSync, writeFileSync } from 'node:fs'; +/* Types */ +interface Args { + f: string | undefined + [name: string]: unknown +} -export const parseArgs = () => { - const args = {} as Record<string, unknown>; + +export const parseArgs = (): Args => { + const args = {} as Args; let lastFlag: string | null = null; for (let i = 2; i < process.argv.length; ++i) { @@ -25,12 +31,12 @@ export const parseArgs = () => { return args; }; -export const parseFetchurl = (url: string) => JSON.parse(spawnSync( +export const parseFetchurl = (url: string): string => JSON.parse(spawnSync( 'nix', ['store', 'prefetch-file', '--refresh', '--json', '--hash-type', 'sha256', url, '--name', '"escaped"'], { shell: true }, ).stdout.toString()).hash; -export const replaceInFile = (replace: RegExp, replacement: string, file: string) => { +export const replaceInFile = (replace: RegExp, replacement: string, file: string): void => { const fileContents = readFileSync(file); const replaced = fileContents.toString().replace(replace, replacement); @@ -38,6 +44,6 @@ export const replaceInFile = (replace: RegExp, replacement: string, file: string writeFileSync(file, replaced); }; -export const npmRun = (args: string[], workspaceDir: string) => spawnSync( +export const npmRun = (args: string[], workspaceDir: string): string => spawnSync( 'npm', args, { cwd: workspaceDir }, ).stdout.toString(); diff --git a/apps/update-sources/src/nix-update.ts b/apps/update-sources/src/nix-update.ts index 3b787294..7fb6a45d 100644 --- a/apps/update-sources/src/nix-update.ts +++ b/apps/update-sources/src/nix-update.ts @@ -31,7 +31,7 @@ export default ( return { changelog: OLD_VERSION !== NEW_VERSION ? - `${cleanAttr}: ${OLD_VERSION} -> ${NEW_VERSION}\n` : + `${cleanAttr}: ${OLD_VERSION} -> ${NEW_VERSION}` : null, stdout: execution.stdout.toString(), stderr: execution.stderr.toString(), diff --git a/apps/update-sources/src/node-modules.ts b/apps/update-sources/src/node-modules.ts index 6644494a..18264d02 100644 --- a/apps/update-sources/src/node-modules.ts +++ b/apps/update-sources/src/node-modules.ts @@ -71,7 +71,7 @@ const prefetchNpmDeps = (workspaceDir: string): string => { }; -export default async() => { +export default async(): Promise<string | null> => { console.log(styleText(['magenta'], '\nUpdating node modules:\n')); const updates = {}; @@ -115,7 +115,9 @@ export default async() => { } } - return Object.entries(updates) - .map(([key, dep]) => `${key}: ${dep}`) - .join('\n'); + return Object.entries(updates).length > 0 ? + Object.entries(updates) + .map(([key, dep]) => `${key}: ${dep}`) + .join('\n') : + null; }; diff --git a/apps/update-sources/src/vuetorrent.ts b/apps/update-sources/src/vuetorrent.ts index 85a5ff7f..fa1d23c5 100644 --- a/apps/update-sources/src/vuetorrent.ts +++ b/apps/update-sources/src/vuetorrent.ts @@ -20,7 +20,7 @@ const genVueText = ( } `; -export default () => { +export default (): string | null => { console.log(styleText(['magenta'], '\nUpdating Vuetorrent:\n')); const FILE = `${FLAKE}/configurations/nos/modules/qbittorrent/vuetorrent.nix`; @@ -40,5 +40,5 @@ export default () => { writeFileSync(FILE, fileText); - return OLD_VERSION !== VERSION ? `Vuetorrent: ${OLD_VERSION} -> ${VERSION}` : ''; + return OLD_VERSION !== VERSION ? `Vuetorrent: ${OLD_VERSION} -> ${VERSION}\n` : null; };