Skip to content

Commit 3aa9b19

Browse files
committed
feat(cli): add push option for export
1 parent bc882cd commit 3aa9b19

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/cli/src/notion/export.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class NotionExportCommand extends Command {
4444
wait = Option.Counter('-w,--wait', {
4545
description: 'Wait couter for missed collection view'
4646
})
47+
push = Option.Boolean('--push', {
48+
description: 'Push exported data to remote repositories'
49+
})
4750

4851
async execute() {
4952
const exporter = new NotionExporter({
@@ -56,8 +59,10 @@ export class NotionExportCommand extends Command {
5659
load: this.load,
5760
raw: this.raw,
5861
dataset: this.dataset,
59-
token: this.token
62+
token: this.token,
63+
push: this.push
6064
})
6165
await exporter.execute()
66+
if (this.push) await exporter.push()
6267
}
6368
}

packages/cli/src/notion/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { mkdir, readFile } from 'fs/promises'
1616
import JSONStream from 'JSONStream'
1717
import fs from 'graceful-fs'
1818
import { promisify } from 'util'
19+
import { execSync } from 'child_process'
1920
import stream from 'stream'
2021
import { format } from 'prettier'
2122
import { isCollection, isSpace } from '@texonom/ntypes'
@@ -51,6 +52,7 @@ export class NotionExporter {
5152
load: boolean = false
5253
raw: boolean = false
5354
dataset: boolean = false
55+
push: boolean = false
5456
debug: boolean = false
5557
wait: number = 5
5658
token: string | undefined
@@ -68,6 +70,7 @@ export class NotionExporter {
6870
raw?: boolean
6971
dataset?: boolean
7072
token?: string
73+
push?: boolean
7174
}) {
7275
this.page = parsePageId(options.page)
7376
this.folder = options.folder ?? this.folder
@@ -81,6 +84,7 @@ export class NotionExporter {
8184
this.raw = options.raw ?? this.raw
8285
this.dataset = options.dataset ?? this.dataset
8386
this.token = options.token
87+
this.push = options.push ?? this.push
8488

8589
this.notion = new NotionAPI({ authToken: this.token })
8690
if (this.validation) writeFile = async () => {}
@@ -713,6 +717,31 @@ export class NotionExporter {
713717
}
714718
return space
715719
}
720+
721+
push() {
722+
const run = (cmd: string, cwd: string) => {
723+
try {
724+
execSync(cmd, { cwd, stdio: 'ignore' })
725+
} catch {}
726+
}
727+
const message = new Date().toString()
728+
729+
// push raw
730+
run('git init', this.folder)
731+
run('git add .', this.folder)
732+
run(`git commit -m "${message}"`, this.folder)
733+
run('git branch -M main', this.folder)
734+
run('git remote add origin https://github.com/texonom/texonom-raw.git', this.folder)
735+
run('git push -u origin main --force', this.folder)
736+
737+
// push md
738+
run('git init', this.md)
739+
run('git add .', this.md)
740+
run(`git commit -m "${message}"`, this.md)
741+
run('git branch -M main', this.md)
742+
run('git remote add origin https://github.com/texonom/texonom-md.git', this.md)
743+
run('git push -u origin main --force', this.md)
744+
}
716745
}
717746

718747
export async function loadRaw(

0 commit comments

Comments
 (0)