|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 | + } |
| 8 | + Object.defineProperty(o, k2, desc); |
| 9 | +}) : (function(o, m, k, k2) { |
| 10 | + if (k2 === undefined) k2 = k; |
| 11 | + o[k2] = m[k]; |
| 12 | +})); |
| 13 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 | +}) : function(o, v) { |
| 16 | + o["default"] = v; |
| 17 | +}); |
| 18 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 19 | + if (mod && mod.__esModule) return mod; |
| 20 | + var result = {}; |
| 21 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 22 | + __setModuleDefault(result, mod); |
| 23 | + return result; |
| 24 | +}; |
| 25 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 26 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 27 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 28 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 29 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 30 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 31 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 32 | + }); |
| 33 | +}; |
| 34 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 35 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 36 | +}; |
| 37 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 38 | +const client_1 = require("@notionhq/client"); |
| 39 | +const dotenv_1 = __importDefault(require("dotenv")); |
| 40 | +const promises_1 = require("fs/promises"); |
| 41 | +const path_1 = __importDefault(require("path")); |
| 42 | +const page_1 = require("./page"); |
| 43 | +const fs = __importStar(require("node:fs")); |
| 44 | +dotenv_1.default.config(); |
| 45 | +function main() { |
| 46 | + var _a; |
| 47 | + return __awaiter(this, void 0, void 0, function* () { |
| 48 | + const notion = new client_1.Client({ |
| 49 | + auth: process.env.NOTION_TOKEN, |
| 50 | + }); |
| 51 | + // Function to write a page to a file |
| 52 | + function writePageToFile(page, directory) { |
| 53 | + return __awaiter(this, void 0, void 0, function* () { |
| 54 | + const pageId = page.id; |
| 55 | + const pageContent = yield (0, page_1.getPageContent)(notion, pageId); |
| 56 | + const fileDir = path_1.default.join(directory, pageId.toString()); |
| 57 | + yield (0, promises_1.mkdir)(fileDir, { recursive: true }); |
| 58 | + const filePath = getPath(directory, page); |
| 59 | + fs.writeFileSync(filePath, pageContent, "utf8"); |
| 60 | + }); |
| 61 | + } |
| 62 | + function getPath(directory, page) { |
| 63 | + var _a, _b, _c, _d, _e, _f; |
| 64 | + const pageId = page.id; |
| 65 | + const fileDir = path_1.default.join(directory, pageId.toString()); |
| 66 | + let title = (_f = (_e = (_d = (((_b = (_a = page.properties) === null || _a === void 0 ? void 0 : _a.title) !== null && _b !== void 0 ? _b : (_c = page.properties) === null || _c === void 0 ? void 0 : _c.Name))) === null || _d === void 0 ? void 0 : _d.title[0]) === null || _e === void 0 ? void 0 : _e.plain_text) === null || _f === void 0 ? void 0 : _f.trim().replaceAll(/\//g, "-"); |
| 67 | + if (!title) { |
| 68 | + title = pageId.toString(); |
| 69 | + } |
| 70 | + return path_1.default.join(fileDir, title + ".md"); |
| 71 | + } |
| 72 | + // Function to fetch all pages |
| 73 | + function fetchAllPages() { |
| 74 | + var _a; |
| 75 | + return __awaiter(this, void 0, void 0, function* () { |
| 76 | + let pages = []; |
| 77 | + let cursor = undefined; |
| 78 | + while (true) { |
| 79 | + const response = yield notion.search({ |
| 80 | + filter: { |
| 81 | + property: "object", |
| 82 | + value: "page", |
| 83 | + }, |
| 84 | + start_cursor: cursor, |
| 85 | + }); |
| 86 | + pages = pages.concat(response.results); |
| 87 | + if (!response.has_more) { |
| 88 | + break; |
| 89 | + } |
| 90 | + cursor = (_a = response.next_cursor) !== null && _a !== void 0 ? _a : undefined; |
| 91 | + } |
| 92 | + return pages; |
| 93 | + }); |
| 94 | + } |
| 95 | + // Fetch all pages |
| 96 | + const pages = yield fetchAllPages(); |
| 97 | + let metadata = new Map(); |
| 98 | + const outputDir = path_1.default.join(process.env.WORKSPACE_DIR, 'knowledge', 'integrations', 'notion'); |
| 99 | + yield (0, promises_1.mkdir)(outputDir, { recursive: true }); |
| 100 | + const metadataPath = path_1.default.join(outputDir, 'metadata.json'); |
| 101 | + if (fs.existsSync(metadataPath)) { |
| 102 | + metadata = new Map(Object.entries(JSON.parse(fs.readFileSync(metadataPath, 'utf8').toString()))); |
| 103 | + } |
| 104 | + let updatedPages = 0; |
| 105 | + for (const page of pages) { |
| 106 | + if (metadata.has(page.id)) { |
| 107 | + const entry = metadata.get(page.id); |
| 108 | + if ((entry === null || entry === void 0 ? void 0 : entry.updatedAt) === page.last_edited_time && fs.existsSync(getPath(outputDir, page))) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + if (entry === null || entry === void 0 ? void 0 : entry.sync) { |
| 112 | + updatedPages++; |
| 113 | + yield writePageToFile(page, outputDir); |
| 114 | + } |
| 115 | + metadata.set(page.id, { |
| 116 | + url: page.url, |
| 117 | + filename: path_1.default.basename(getPath(outputDir, page)), |
| 118 | + updatedAt: page.last_edited_time, |
| 119 | + sync: (_a = entry === null || entry === void 0 ? void 0 : entry.sync) !== null && _a !== void 0 ? _a : false, |
| 120 | + }); |
| 121 | + } |
| 122 | + metadata.set(page.id, { |
| 123 | + url: page.url, |
| 124 | + filename: path_1.default.basename(getPath(outputDir, page)), |
| 125 | + updatedAt: page.last_edited_time, |
| 126 | + sync: false, |
| 127 | + }); |
| 128 | + } |
| 129 | + for (const [key, _] of metadata) { |
| 130 | + if (!pages.find((page) => page.id === key)) { |
| 131 | + fs.rmSync(path_1.default.join(outputDir, key), { recursive: true }); |
| 132 | + console.log(`Removed ${key} from ${outputDir}`); |
| 133 | + metadata.delete(key); |
| 134 | + } |
| 135 | + } |
| 136 | + yield (0, promises_1.writeFile)(metadataPath, JSON.stringify(Object.fromEntries(metadata)), 'utf8'); |
| 137 | + console.log(`Finished writing ${updatedPages} pages to ${outputDir}`); |
| 138 | + }); |
| 139 | +} |
| 140 | +main() |
| 141 | + .then(() => process.exit(0)) |
| 142 | + .catch((err) => { |
| 143 | + console.error(err); |
| 144 | + process.exit(1); |
| 145 | +}); |
0 commit comments