diff --git a/packages/cli/src/notion/export.ts b/packages/cli/src/notion/export.ts index 866d49a5..697c3dcd 100644 --- a/packages/cli/src/notion/export.ts +++ b/packages/cli/src/notion/export.ts @@ -52,6 +52,9 @@ export class NotionExportCommand extends Command { wait = Option.Counter('-w,--wait', { description: 'Wait couter for missed collection view' }) + push = Option.Boolean('--push', { + description: 'Push exported data to remote repositories' + }) async execute() { const exporter = new NotionExporter({ @@ -64,9 +67,11 @@ export class NotionExportCommand extends Command { load: this.load, raw: this.raw, dataset: this.dataset, - token: this.token + token: this.token, + push: this.push }) await exporter.execute() + if (this.push) await exporter.pushRepos() if (this.treemap || this.stats) if (!exporter.pageTree) await exporter.loadRaw() diff --git a/packages/cli/src/notion/index.ts b/packages/cli/src/notion/index.ts index 7f88efbe..26fb19c0 100644 --- a/packages/cli/src/notion/index.ts +++ b/packages/cli/src/notion/index.ts @@ -16,6 +16,7 @@ import { mkdir, readFile } from 'fs/promises' import JSONStream from 'JSONStream' import fs from 'graceful-fs' import { promisify } from 'util' +import { execSync } from 'child_process' import stream from 'stream' import { format } from 'prettier' import { isCollection, isSpace } from '@texonom/ntypes' @@ -51,6 +52,7 @@ export class NotionExporter { load: boolean = false raw: boolean = false dataset: boolean = false + push: boolean = false debug: boolean = false wait: number = 5 token: string | undefined @@ -68,6 +70,7 @@ export class NotionExporter { raw?: boolean dataset?: boolean token?: string + push?: boolean }) { this.page = parsePageId(options.page) this.folder = options.folder ?? this.folder @@ -81,6 +84,7 @@ export class NotionExporter { this.raw = options.raw ?? this.raw this.dataset = options.dataset ?? this.dataset this.token = options.token + this.push = options.push ?? this.push this.notion = new NotionAPI({ authToken: this.token }) if (this.validation) writeFile = async () => {} @@ -713,6 +717,31 @@ export class NotionExporter { } return space } + + pushRepos() { + const run = (cmd: string, cwd: string) => { + try { + execSync(cmd, { cwd, stdio: 'ignore' }) + } catch {} + } + const message = new Date().toString() + + // push raw + run('git init', this.folder) + run('git add .', this.folder) + run(`git commit -m "${message}"`, this.folder) + run('git branch -M main', this.folder) + run('git remote add origin https://github.com/texonom/texonom-raw.git', this.folder) + run('git push -u origin main --force', this.folder) + + // push md + run('git init', this.md) + run('git add .', this.md) + run(`git commit -m "${message}"`, this.md) + run('git branch -M main', this.md) + run('git remote add origin https://github.com/texonom/texonom-md.git', this.md) + run('git push -u origin main --force', this.md) + } } export async function loadRaw(