All checks were successful
Deploy production / deploy (push) Successful in 2m33s
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
/**
|
|
* Sync a TickTick export (scripts/ticktick-export.ts) into the private `tasks`
|
|
* schema. Idempotent; rerun after a new export to pull in changes.
|
|
*
|
|
* bun scripts/ticktick-import.ts [--dry-run] ~/.cache/ticktick-export/all.json
|
|
*
|
|
* Links listed in data/tasks/ticktick-merges.json (plus exact title matches, see
|
|
* syncTickTick) attach a TickTick task to an existing task instead of duplicating it.
|
|
* Uses DB_URL (the app role on prod, personal_dev on dev).
|
|
*/
|
|
import { readFile } from 'node:fs/promises';
|
|
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
import postgres from 'postgres';
|
|
import { syncTickTick, type TtExport, type TtMerge } from '../src/lib/server/ticktick';
|
|
|
|
const args = process.argv.slice(2);
|
|
const dryRun = args.includes('--dry-run');
|
|
const files = args.filter((a) => !a.startsWith('--'));
|
|
if (files.length !== 1) {
|
|
console.error('usage: bun scripts/ticktick-import.ts [--dry-run] <export.json>');
|
|
process.exit(2);
|
|
}
|
|
const data = JSON.parse(await readFile(files[0], 'utf8')) as TtExport;
|
|
const merges = JSON.parse(
|
|
await readFile(new URL('../data/tasks/ticktick-merges.json', import.meta.url), 'utf8')
|
|
) as TtMerge[];
|
|
if (!process.env.DB_URL) throw new Error('DB_URL is required');
|
|
const client = postgres(process.env.DB_URL, { max: 1, onnotice: () => {} });
|
|
class DryRun extends Error {}
|
|
try {
|
|
const db = drizzle({ client });
|
|
let report;
|
|
try {
|
|
report = await db.transaction(async (tx) => {
|
|
const r = await syncTickTick(tx, data, { merges });
|
|
if (dryRun) {
|
|
console.log(JSON.stringify(r, null, 2));
|
|
throw new DryRun();
|
|
}
|
|
return r;
|
|
});
|
|
console.log(JSON.stringify(report, null, 2));
|
|
} catch (e) {
|
|
if (!(e instanceof DryRun)) throw e;
|
|
console.log('(dry run, rolled back)');
|
|
}
|
|
} finally {
|
|
await client.end();
|
|
}
|