diff --git a/build/gen-patch.ts b/build/gen-patch.ts new file mode 100644 index 00000000..6c81164b --- /dev/null +++ b/build/gen-patch.ts @@ -0,0 +1,21 @@ +import child from 'child_process'; +import fs from 'fs'; +import path from 'path'; +import superagent from 'superagent'; + +if (!process.argv[2]) throw new Error('No target specified'); + +// TODO support module other than packages +const target = process.argv[2].startsWith('packages/') ? process.argv[2] : `packages/${process.argv[2]}`; + +const result = child.execSync(`git diff ${target}`); +const patch = result.toString().replace(new RegExp(`${target}/`, 'g'), ''); +const filename = `${path.basename(target)}.patch`; +fs.writeFileSync(filename, patch); +superagent.post('https://hydro.ac/paste') + .set('accept', 'application/json') + .send({ body: patch, filename }) + .end((err, res) => { + if (err) throw err; + console.log('Paste created on ', res.text); + }); diff --git a/package.json b/package.json index c129539f..226ae6eb 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "lint:ui:ci": "yarn workspace @hydrooj/ui-default lint --ext .js,.ts,.jsx,.tsx .", "debug": "node --trace-warnings --async-stack-traces --trace-deprecation packages/hydrooj/bin/hydrooj --debug --template", "start": "packages/hydrooj/bin/hydrooj", - "postinstall": "node build/prepare.js" + "postinstall": "node build/prepare.js", + "gen-patch": "node -r @hydrooj/utils/lib/register build/gen-patch.ts" }, "version": "1.0.0", "license": "AGPL-3.0-only", diff --git a/packages/hydrooj/bin/commands.ts b/packages/hydrooj/bin/commands.ts index 123763ad..7ce2fe44 100644 --- a/packages/hydrooj/bin/commands.ts +++ b/packages/hydrooj/bin/commands.ts @@ -204,6 +204,7 @@ if (!argv.args[0] || argv.args[0] === 'cli') { fs.removeSync(newAddonPath); console.log(`Successfully uninstalled ${name}.`); }); + require('../src/commands/patch').register(cli); cli.help(); cli.parse(); if (!cli.matchedCommand) { diff --git a/packages/hydrooj/src/commands/patch.ts b/packages/hydrooj/src/commands/patch.ts new file mode 100644 index 00000000..21f637e1 --- /dev/null +++ b/packages/hydrooj/src/commands/patch.ts @@ -0,0 +1,31 @@ +import child from 'child_process'; +import { CAC } from 'cac'; +import fs from 'fs-extra'; +import superagent from 'superagent'; +import { Logger } from '@hydrooj/utils'; + +const logger = new Logger('patch'); + +export function register(cli: CAC) { + cli.command('patch ').action(async (filename, patch) => { + const mod = require.resolve(filename); + if (!mod) { + logger.error('Module %s not found', filename); + return; + } + logger.info('Patching %s', mod); + const res = await superagent.get(patch); + logger.info('Downloaded patch'); + for (let i = 0; i <= 100; i++) { + const fp = `${mod}.${i}.patch`; + if (fs.existsSync(fp)) continue; + patch = fp; + break; + } + await fs.writeFile(patch, res.text); + child.execSync(`patch ${mod} -o ${mod} < ${patch}`); + logger.info('Patched %s', mod); + }); + + // TODO: support revert patch +}