diff --git a/apps/update/src/app.ts b/apps/update/src/app.ts index 090df6ae..952c22a3 100644 --- a/apps/update/src/app.ts +++ b/apps/update/src/app.ts @@ -1,26 +1,9 @@ +import { parseArgs } from './lib.ts'; import { updateFirefoxAddons } from '././firefox.ts'; -/* Parse Args */ -const args = {} as Record; -let lastFlag: string | null = null; -for (let i = 2; i < process.argv.length; ++i) { - const arg = process.argv[i]; +const args = parseArgs(); - if (arg.toString().startsWith('-')) { - lastFlag = arg.toString().replace(/^-{1,2}/, ''); - args[lastFlag] = true; - } - else if (lastFlag) { - args[lastFlag] = arg; - lastFlag = null; - } - else { - console.error(`Could not parse args: ${arg.toString()}`); - } -} - -/* Exec functions based on args */ if (args['f'] || args['firefox']) { console.log(updateFirefoxAddons()); } diff --git a/apps/update/src/firefox.ts b/apps/update/src/firefox.ts index 2e1a8231..29a2f283 100644 --- a/apps/update/src/firefox.ts +++ b/apps/update/src/firefox.ts @@ -1,11 +1,26 @@ import { spawnSync } from 'node:child_process'; import { readFileSync } from 'node:fs'; +import { parseFetchurl } from './lib.ts'; + /* Constants */ const FLAKE = process.env.FLAKE; +const updateFFZ = () => { + const FILE = `${FLAKE}/pkgs/firefox-addons/default.nix`; + const URL = 'https://cdn.frankerfacez.com/script/frankerfacez-4.0-an+fx.xpi'; + + const HASH = parseFetchurl(URL); + + spawnSync('sed', ['-i', `'s,url = .*,url = \"${URL}\";,'`, FILE], { shell: true }); + spawnSync('sed', ['-i', `'s,sha256 = .*,sha256 = \"${HASH}\";,'`, FILE], { shell: true }); +}; + export const updateFirefoxAddons = () => { + console.log('Updating FFZ addon'); + updateFFZ(); + console.log('Updating firefox addons using mozilla-addons-to-nix'); const DIR = `${FLAKE}/pkgs/firefox-addons`; diff --git a/apps/update/src/lib.ts b/apps/update/src/lib.ts new file mode 100644 index 00000000..b59d6079 --- /dev/null +++ b/apps/update/src/lib.ts @@ -0,0 +1,30 @@ +import { spawnSync } from 'node:child_process'; + + +export const parseArgs = () => { + const args = {} as Record; + let lastFlag: string | null = null; + + for (let i = 2; i < process.argv.length; ++i) { + const arg = process.argv[i]; + + if (arg.toString().startsWith('-')) { + lastFlag = arg.toString().replace(/^-{1,2}/, ''); + args[lastFlag] = true; + } + else if (lastFlag) { + args[lastFlag] = arg; + lastFlag = null; + } + else { + console.error(`Could not parse args: ${arg.toString()}`); + } + } + + return args; +}; + +export const parseFetchurl = (url: string) => JSON.parse(spawnSync( + 'nix', ['store', 'prefetch-file', '--refresh', '--json', + '--hash-type', 'sha256', url, '--name', '"escaped"'], { shell: true }, +).stdout.toString()).hash;