@catafal/notion-cli 5.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +552 -0
- package/bin/dev +17 -0
- package/bin/dev.cmd +3 -0
- package/bin/run +14 -0
- package/bin/run.cmd +3 -0
- package/dist/base-command.d.ts +73 -0
- package/dist/base-command.js +179 -0
- package/dist/base-flags.d.ts +14 -0
- package/dist/base-flags.js +59 -0
- package/dist/cache.d.ts +84 -0
- package/dist/cache.js +351 -0
- package/dist/commands/append.d.ts +37 -0
- package/dist/commands/append.js +120 -0
- package/dist/commands/batch/delete.d.ts +42 -0
- package/dist/commands/batch/delete.js +199 -0
- package/dist/commands/batch/retrieve.d.ts +43 -0
- package/dist/commands/batch/retrieve.js +272 -0
- package/dist/commands/block/append.d.ts +42 -0
- package/dist/commands/block/append.js +219 -0
- package/dist/commands/block/delete.d.ts +30 -0
- package/dist/commands/block/delete.js +97 -0
- package/dist/commands/block/retrieve/children.d.ts +31 -0
- package/dist/commands/block/retrieve/children.js +177 -0
- package/dist/commands/block/retrieve.d.ts +30 -0
- package/dist/commands/block/retrieve.js +101 -0
- package/dist/commands/block/update.d.ts +45 -0
- package/dist/commands/block/update.js +242 -0
- package/dist/commands/bookmark/list.d.ts +30 -0
- package/dist/commands/bookmark/list.js +60 -0
- package/dist/commands/bookmark/remove.d.ts +26 -0
- package/dist/commands/bookmark/remove.js +47 -0
- package/dist/commands/bookmark/set.d.ts +29 -0
- package/dist/commands/bookmark/set.js +96 -0
- package/dist/commands/browse.d.ts +13 -0
- package/dist/commands/browse.js +44 -0
- package/dist/commands/cache/info.d.ts +19 -0
- package/dist/commands/cache/info.js +145 -0
- package/dist/commands/config/set-token.d.ts +22 -0
- package/dist/commands/config/set-token.js +137 -0
- package/dist/commands/daily/index.d.ts +32 -0
- package/dist/commands/daily/index.js +135 -0
- package/dist/commands/daily/setup.d.ts +42 -0
- package/dist/commands/daily/setup.js +149 -0
- package/dist/commands/db/create.d.ts +31 -0
- package/dist/commands/db/create.js +124 -0
- package/dist/commands/db/query.d.ts +41 -0
- package/dist/commands/db/query.js +360 -0
- package/dist/commands/db/retrieve.d.ts +33 -0
- package/dist/commands/db/retrieve.js +134 -0
- package/dist/commands/db/schema.d.ts +32 -0
- package/dist/commands/db/schema.js +308 -0
- package/dist/commands/db/update.d.ts +31 -0
- package/dist/commands/db/update.js +117 -0
- package/dist/commands/doctor.d.ts +50 -0
- package/dist/commands/doctor.js +420 -0
- package/dist/commands/init.d.ts +65 -0
- package/dist/commands/init.js +479 -0
- package/dist/commands/list.d.ts +29 -0
- package/dist/commands/list.js +219 -0
- package/dist/commands/open.d.ts +29 -0
- package/dist/commands/open.js +100 -0
- package/dist/commands/page/create.d.ts +33 -0
- package/dist/commands/page/create.js +261 -0
- package/dist/commands/page/delete.d.ts +36 -0
- package/dist/commands/page/delete.js +107 -0
- package/dist/commands/page/export.d.ts +38 -0
- package/dist/commands/page/export.js +120 -0
- package/dist/commands/page/retrieve/property_item.d.ts +24 -0
- package/dist/commands/page/retrieve/property_item.js +75 -0
- package/dist/commands/page/retrieve.d.ts +36 -0
- package/dist/commands/page/retrieve.js +244 -0
- package/dist/commands/page/update.d.ts +34 -0
- package/dist/commands/page/update.js +184 -0
- package/dist/commands/quick.d.ts +35 -0
- package/dist/commands/quick.js +168 -0
- package/dist/commands/search.d.ts +43 -0
- package/dist/commands/search.js +361 -0
- package/dist/commands/stats.d.ts +35 -0
- package/dist/commands/stats.js +274 -0
- package/dist/commands/sync.d.ts +24 -0
- package/dist/commands/sync.js +183 -0
- package/dist/commands/template/get.d.ts +28 -0
- package/dist/commands/template/get.js +59 -0
- package/dist/commands/template/list.d.ts +32 -0
- package/dist/commands/template/list.js +62 -0
- package/dist/commands/template/remove.d.ts +27 -0
- package/dist/commands/template/remove.js +48 -0
- package/dist/commands/template/save.d.ts +32 -0
- package/dist/commands/template/save.js +92 -0
- package/dist/commands/template/use.d.ts +34 -0
- package/dist/commands/template/use.js +142 -0
- package/dist/commands/user/list.d.ts +27 -0
- package/dist/commands/user/list.js +99 -0
- package/dist/commands/user/retrieve/bot.d.ts +28 -0
- package/dist/commands/user/retrieve/bot.js +96 -0
- package/dist/commands/user/retrieve.d.ts +30 -0
- package/dist/commands/user/retrieve.js +103 -0
- package/dist/commands/whoami.d.ts +19 -0
- package/dist/commands/whoami.js +175 -0
- package/dist/deduplication.d.ts +41 -0
- package/dist/deduplication.js +71 -0
- package/dist/envelope.d.ts +169 -0
- package/dist/envelope.js +257 -0
- package/dist/errors/enhanced-errors.d.ts +168 -0
- package/dist/errors/enhanced-errors.js +567 -0
- package/dist/errors/index.d.ts +18 -0
- package/dist/errors/index.js +33 -0
- package/dist/examples/cache-retry-examples.d.ts +64 -0
- package/dist/examples/cache-retry-examples.js +375 -0
- package/dist/helper.d.ts +102 -0
- package/dist/helper.js +885 -0
- package/dist/http-agent.d.ts +38 -0
- package/dist/http-agent.js +60 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/interface.d.ts +4 -0
- package/dist/interface.js +2 -0
- package/dist/notion.d.ts +144 -0
- package/dist/notion.js +547 -0
- package/dist/retry.d.ts +72 -0
- package/dist/retry.js +381 -0
- package/dist/utils/bookmarks.d.ts +32 -0
- package/dist/utils/bookmarks.js +98 -0
- package/dist/utils/daily-config.d.ts +22 -0
- package/dist/utils/daily-config.js +60 -0
- package/dist/utils/disk-cache.d.ts +80 -0
- package/dist/utils/disk-cache.js +291 -0
- package/dist/utils/fuzzy.d.ts +36 -0
- package/dist/utils/fuzzy.js +69 -0
- package/dist/utils/interactive-navigator.d.ts +63 -0
- package/dist/utils/interactive-navigator.js +123 -0
- package/dist/utils/markdown-to-blocks.d.ts +21 -0
- package/dist/utils/markdown-to-blocks.js +333 -0
- package/dist/utils/notion-resolver.d.ts +49 -0
- package/dist/utils/notion-resolver.js +278 -0
- package/dist/utils/notion-url-parser.d.ts +48 -0
- package/dist/utils/notion-url-parser.js +121 -0
- package/dist/utils/property-expander.d.ts +45 -0
- package/dist/utils/property-expander.js +323 -0
- package/dist/utils/schema-examples.d.ts +40 -0
- package/dist/utils/schema-examples.js +359 -0
- package/dist/utils/schema-extractor.d.ts +65 -0
- package/dist/utils/schema-extractor.js +235 -0
- package/dist/utils/shell-config.d.ts +30 -0
- package/dist/utils/shell-config.js +84 -0
- package/dist/utils/table-formatter.d.ts +36 -0
- package/dist/utils/table-formatter.js +125 -0
- package/dist/utils/templates.d.ts +30 -0
- package/dist/utils/templates.js +82 -0
- package/dist/utils/terminal-banner.d.ts +24 -0
- package/dist/utils/terminal-banner.js +34 -0
- package/dist/utils/token-validator.d.ts +42 -0
- package/dist/utils/token-validator.js +66 -0
- package/dist/utils/update-notifier.d.ts +26 -0
- package/dist/utils/update-notifier.js +54 -0
- package/dist/utils/workspace-cache.d.ts +58 -0
- package/dist/utils/workspace-cache.js +185 -0
- package/oclif.manifest.json +6471 -0
- package/package.json +118 -0
- package/scripts/banner.js +38 -0
- package/scripts/postinstall.js +44 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const core_1 = require("@oclif/core");
|
|
4
|
+
const workspace_cache_1 = require("../utils/workspace-cache");
|
|
5
|
+
const helper_1 = require("../helper");
|
|
6
|
+
const base_flags_1 = require("../base-flags");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
const table_formatter_1 = require("../utils/table-formatter");
|
|
9
|
+
const fuzzy_1 = require("../utils/fuzzy");
|
|
10
|
+
class List extends core_1.Command {
|
|
11
|
+
async run() {
|
|
12
|
+
const { flags } = await this.parse(List);
|
|
13
|
+
try {
|
|
14
|
+
// Load cache
|
|
15
|
+
const cache = await (0, workspace_cache_1.loadCache)();
|
|
16
|
+
if (!cache) {
|
|
17
|
+
// Use enhanced error factory for workspace not synced
|
|
18
|
+
throw errors_1.NotionCLIErrorFactory.workspaceNotSynced('');
|
|
19
|
+
}
|
|
20
|
+
// Calculate cache age
|
|
21
|
+
const lastSyncTime = new Date(cache.lastSync);
|
|
22
|
+
const cacheAgeMs = Date.now() - lastSyncTime.getTime();
|
|
23
|
+
const cacheAgeHours = cacheAgeMs / (1000 * 60 * 60);
|
|
24
|
+
const isStale = cacheAgeHours > 24;
|
|
25
|
+
let databases = cache.databases;
|
|
26
|
+
// Apply fuzzy name filter if --filter is provided.
|
|
27
|
+
// Checks substring on full title, then Levenshtein on individual words
|
|
28
|
+
// so "daly" matches "Daily Psychology Lecture" via the word "daily".
|
|
29
|
+
if (flags.filter) {
|
|
30
|
+
const query = flags.filter.toLowerCase().trim();
|
|
31
|
+
const threshold = Math.max(2, Math.floor(query.length / 4));
|
|
32
|
+
databases = databases
|
|
33
|
+
.map(db => {
|
|
34
|
+
// Substring match on full title (cheapest check)
|
|
35
|
+
if (db.titleNormalized.includes(query)) {
|
|
36
|
+
return { db, dist: 0 };
|
|
37
|
+
}
|
|
38
|
+
// Fuzzy match on individual words in the title
|
|
39
|
+
const words = db.titleNormalized.split(/[\s\-_]+/);
|
|
40
|
+
let bestWordDist = Infinity;
|
|
41
|
+
for (const word of words) {
|
|
42
|
+
const dist = (0, fuzzy_1.levenshtein)(query, word);
|
|
43
|
+
if (dist < bestWordDist)
|
|
44
|
+
bestWordDist = dist;
|
|
45
|
+
}
|
|
46
|
+
return { db, dist: bestWordDist };
|
|
47
|
+
})
|
|
48
|
+
.filter(({ dist }) => dist <= threshold)
|
|
49
|
+
.sort((a, b) => a.dist - b.dist)
|
|
50
|
+
.map(({ db }) => db);
|
|
51
|
+
}
|
|
52
|
+
// Build comprehensive metadata
|
|
53
|
+
const metadata = {
|
|
54
|
+
cache_info: {
|
|
55
|
+
last_sync: cache.lastSync,
|
|
56
|
+
cache_age_ms: cacheAgeMs,
|
|
57
|
+
cache_age_hours: parseFloat(cacheAgeHours.toFixed(2)),
|
|
58
|
+
is_stale: isStale,
|
|
59
|
+
stale_threshold_hours: 24,
|
|
60
|
+
cache_version: cache.version,
|
|
61
|
+
cache_location: await (0, workspace_cache_1.getCachePath)(),
|
|
62
|
+
},
|
|
63
|
+
ttls: {
|
|
64
|
+
workspace_cache: 'persists until next sync',
|
|
65
|
+
recommended_sync_interval_hours: 24,
|
|
66
|
+
in_memory: {
|
|
67
|
+
data_source_ms: parseInt(process.env.NOTION_CLI_CACHE_DS_TTL || '600000', 10),
|
|
68
|
+
page_ms: parseInt(process.env.NOTION_CLI_CACHE_PAGE_TTL || '60000', 10),
|
|
69
|
+
user_ms: parseInt(process.env.NOTION_CLI_CACHE_USER_TTL || '3600000', 10),
|
|
70
|
+
block_ms: parseInt(process.env.NOTION_CLI_CACHE_BLOCK_TTL || '30000', 10),
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
stats: {
|
|
74
|
+
total_databases: databases.length,
|
|
75
|
+
databases_with_urls: databases.filter(db => db.url).length,
|
|
76
|
+
total_aliases: databases.reduce((sum, db) => sum + db.aliases.length, 0),
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
// Add freshness warning if stale (non-JSON mode)
|
|
80
|
+
if (isStale && !flags.json && !flags['compact-json'] && !flags.markdown && !flags.pretty) {
|
|
81
|
+
this.warn(`Cache is ${cacheAgeHours.toFixed(1)} hours old. Consider running: notion-cli sync`);
|
|
82
|
+
}
|
|
83
|
+
if (databases.length === 0) {
|
|
84
|
+
if (flags.json) {
|
|
85
|
+
this.log(JSON.stringify({
|
|
86
|
+
success: true,
|
|
87
|
+
data: {
|
|
88
|
+
databases: [],
|
|
89
|
+
},
|
|
90
|
+
metadata,
|
|
91
|
+
}, null, 2));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.log('No databases found in cache.');
|
|
95
|
+
this.log('Your integration may not have access to any databases.');
|
|
96
|
+
}
|
|
97
|
+
process.exit(0);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// Define columns for table output
|
|
101
|
+
const columns = {
|
|
102
|
+
title: {
|
|
103
|
+
header: 'Title',
|
|
104
|
+
get: (row) => row.title,
|
|
105
|
+
},
|
|
106
|
+
id: {
|
|
107
|
+
header: 'ID',
|
|
108
|
+
get: (row) => row.id,
|
|
109
|
+
},
|
|
110
|
+
aliases: {
|
|
111
|
+
header: 'Aliases (first 3)',
|
|
112
|
+
get: (row) => row.aliases.slice(0, 3).join(', '),
|
|
113
|
+
},
|
|
114
|
+
url: {
|
|
115
|
+
header: 'URL',
|
|
116
|
+
get: (row) => row.url || '',
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
// Handle compact JSON output
|
|
120
|
+
if (flags['compact-json']) {
|
|
121
|
+
(0, helper_1.outputCompactJson)(databases);
|
|
122
|
+
process.exit(0);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Handle markdown table output
|
|
126
|
+
if (flags.markdown) {
|
|
127
|
+
(0, helper_1.outputMarkdownTable)(databases, columns);
|
|
128
|
+
process.exit(0);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
// Handle pretty table output
|
|
132
|
+
if (flags.pretty) {
|
|
133
|
+
(0, helper_1.outputPrettyTable)(databases, columns);
|
|
134
|
+
process.exit(0);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
// Handle JSON output for automation
|
|
138
|
+
if (flags.json) {
|
|
139
|
+
this.log(JSON.stringify({
|
|
140
|
+
success: true,
|
|
141
|
+
data: {
|
|
142
|
+
databases: databases.map(db => ({
|
|
143
|
+
id: db.id,
|
|
144
|
+
title: db.title,
|
|
145
|
+
aliases: db.aliases,
|
|
146
|
+
url: db.url,
|
|
147
|
+
lastEditedTime: db.lastEditedTime,
|
|
148
|
+
})),
|
|
149
|
+
},
|
|
150
|
+
metadata,
|
|
151
|
+
}, null, 2));
|
|
152
|
+
process.exit(0);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
// Handle table output (default)
|
|
156
|
+
this.log(`\nCached Databases (${databases.length} total)`);
|
|
157
|
+
this.log(`Last synced: ${lastSyncTime.toLocaleString()} (${cacheAgeHours.toFixed(1)} hours ago)`);
|
|
158
|
+
if (isStale) {
|
|
159
|
+
this.log(`⚠️ Cache is stale. Run: notion-cli sync`);
|
|
160
|
+
}
|
|
161
|
+
this.log('');
|
|
162
|
+
const options = {
|
|
163
|
+
printLine: this.log.bind(this),
|
|
164
|
+
...flags,
|
|
165
|
+
};
|
|
166
|
+
(0, table_formatter_1.formatTable)(databases, columns, options);
|
|
167
|
+
this.log(`\nTip: Run "notion-cli sync" to refresh the cache.`);
|
|
168
|
+
process.exit(0);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
const cliError = error instanceof errors_1.NotionCLIError
|
|
172
|
+
? error
|
|
173
|
+
: (0, errors_1.wrapNotionError)(error, {
|
|
174
|
+
endpoint: 'workspace.list'
|
|
175
|
+
});
|
|
176
|
+
if (flags.json) {
|
|
177
|
+
this.log(JSON.stringify(cliError.toJSON(), null, 2));
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
this.error(cliError.toHumanString());
|
|
181
|
+
}
|
|
182
|
+
process.exit(1);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
List.description = 'List all cached databases from your workspace';
|
|
187
|
+
List.aliases = ['db:list', 'ls'];
|
|
188
|
+
List.examples = [
|
|
189
|
+
{
|
|
190
|
+
description: 'List all cached databases',
|
|
191
|
+
command: 'notion-cli list',
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
description: 'Filter databases by name (fuzzy match)',
|
|
195
|
+
command: 'notion-cli list --filter "tasks"',
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
description: 'List databases in markdown format',
|
|
199
|
+
command: 'notion-cli list --markdown',
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
description: 'List databases in JSON format',
|
|
203
|
+
command: 'notion-cli list --json',
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
description: 'List databases in pretty table format',
|
|
207
|
+
command: 'notion-cli list --pretty',
|
|
208
|
+
},
|
|
209
|
+
];
|
|
210
|
+
List.flags = {
|
|
211
|
+
...table_formatter_1.tableFlags,
|
|
212
|
+
...base_flags_1.AutomationFlags,
|
|
213
|
+
...base_flags_1.OutputFormatFlags,
|
|
214
|
+
filter: core_1.Flags.string({
|
|
215
|
+
char: 'f',
|
|
216
|
+
description: 'Filter databases by name (supports fuzzy/typo-tolerant matching)',
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
219
|
+
exports.default = List;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
/**
|
|
3
|
+
* Search for a Notion page or database and open it in the browser.
|
|
4
|
+
* Accepts bookmark names, database names, IDs, URLs, or search queries.
|
|
5
|
+
*
|
|
6
|
+
* Uses execFile (not exec) to prevent shell injection — URL is passed
|
|
7
|
+
* as an argument array, never interpolated into a shell string.
|
|
8
|
+
*/
|
|
9
|
+
export default class Open extends Command {
|
|
10
|
+
static description: string;
|
|
11
|
+
static aliases: string[];
|
|
12
|
+
static examples: {
|
|
13
|
+
description: string;
|
|
14
|
+
command: string;
|
|
15
|
+
}[];
|
|
16
|
+
static args: {
|
|
17
|
+
target: import("@oclif/core/lib/interfaces").Arg<string, Record<string, unknown>>;
|
|
18
|
+
};
|
|
19
|
+
static flags: {
|
|
20
|
+
json: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
21
|
+
'page-size': import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
22
|
+
retry: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
23
|
+
timeout: import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
24
|
+
'no-cache': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
25
|
+
verbose: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
26
|
+
minimal: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
27
|
+
};
|
|
28
|
+
run(): Promise<void>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const core_1 = require("@oclif/core");
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const notion_resolver_1 = require("../utils/notion-resolver");
|
|
6
|
+
const notion = require("../notion");
|
|
7
|
+
const helper_1 = require("../helper");
|
|
8
|
+
const base_flags_1 = require("../base-flags");
|
|
9
|
+
const errors_1 = require("../errors");
|
|
10
|
+
/**
|
|
11
|
+
* Search for a Notion page or database and open it in the browser.
|
|
12
|
+
* Accepts bookmark names, database names, IDs, URLs, or search queries.
|
|
13
|
+
*
|
|
14
|
+
* Uses execFile (not exec) to prevent shell injection — URL is passed
|
|
15
|
+
* as an argument array, never interpolated into a shell string.
|
|
16
|
+
*/
|
|
17
|
+
class Open extends core_1.Command {
|
|
18
|
+
async run() {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
const { args, flags } = await this.parse(Open);
|
|
21
|
+
try {
|
|
22
|
+
// Try resolving as database first, then as page
|
|
23
|
+
let url;
|
|
24
|
+
let title = 'Untitled';
|
|
25
|
+
try {
|
|
26
|
+
const id = await (0, notion_resolver_1.resolveNotionId)(args.target, 'database');
|
|
27
|
+
const db = await notion.retrieveDataSource(id);
|
|
28
|
+
url = db.url;
|
|
29
|
+
title = ((_b = (_a = db.title) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.plain_text) || 'Untitled';
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Not a database — try as page
|
|
33
|
+
const id = await (0, notion_resolver_1.resolveNotionId)(args.target, 'page');
|
|
34
|
+
const page = await notion.retrievePage({ page_id: id });
|
|
35
|
+
url = page.url;
|
|
36
|
+
title = (0, helper_1.getPageTitle)(page);
|
|
37
|
+
}
|
|
38
|
+
if (!url) {
|
|
39
|
+
this.error('Could not find a URL for the resolved resource.');
|
|
40
|
+
process.exit(1);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
// Open in browser using execFile (safe — no shell injection)
|
|
44
|
+
const opener = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
45
|
+
(0, child_process_1.execFile)(opener, [url], (err) => {
|
|
46
|
+
if (err && !flags.json) {
|
|
47
|
+
this.warn(`Could not open browser: ${err.message}`);
|
|
48
|
+
this.log(`URL: ${url}`);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
if (flags.json) {
|
|
52
|
+
this.log(JSON.stringify({
|
|
53
|
+
success: true,
|
|
54
|
+
data: { title, url },
|
|
55
|
+
timestamp: new Date().toISOString()
|
|
56
|
+
}, null, 2));
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
this.log(`Opened: ${title}`);
|
|
60
|
+
this.log(url);
|
|
61
|
+
}
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
const cliError = error instanceof errors_1.NotionCLIError
|
|
66
|
+
? error
|
|
67
|
+
: (0, errors_1.wrapNotionError)(error, { endpoint: 'open', resourceType: 'page' });
|
|
68
|
+
if (flags.json) {
|
|
69
|
+
this.log(JSON.stringify(cliError.toJSON(), null, 2));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.error(cliError.toHumanString());
|
|
73
|
+
}
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
Open.description = 'Open a Notion page or database in your browser';
|
|
79
|
+
Open.aliases = ['o'];
|
|
80
|
+
Open.examples = [
|
|
81
|
+
{
|
|
82
|
+
description: 'Open a bookmarked database',
|
|
83
|
+
command: '$ notion-cli open inbox',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
description: 'Search and open a page',
|
|
87
|
+
command: '$ notion-cli open "weekly meeting"',
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
description: 'Open by ID or URL',
|
|
91
|
+
command: '$ notion-cli open PAGE_ID',
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
Open.args = {
|
|
95
|
+
target: core_1.Args.string({ required: true, description: 'Bookmark name, page/database name, ID, or URL' }),
|
|
96
|
+
};
|
|
97
|
+
Open.flags = {
|
|
98
|
+
...base_flags_1.AutomationFlags,
|
|
99
|
+
};
|
|
100
|
+
exports.default = Open;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Command } from '@oclif/core';
|
|
2
|
+
export default class PageCreate extends Command {
|
|
3
|
+
static description: string;
|
|
4
|
+
static aliases: string[];
|
|
5
|
+
static examples: {
|
|
6
|
+
description: string;
|
|
7
|
+
command: string;
|
|
8
|
+
}[];
|
|
9
|
+
static flags: {
|
|
10
|
+
json: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
11
|
+
'page-size': import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
12
|
+
retry: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
13
|
+
timeout: import("@oclif/core/lib/interfaces").OptionFlag<number, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
14
|
+
'no-cache': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
15
|
+
verbose: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
16
|
+
minimal: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
17
|
+
columns: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
18
|
+
sort: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
19
|
+
filter: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
20
|
+
csv: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
21
|
+
extended: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
22
|
+
'no-truncate': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
23
|
+
'no-header': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
24
|
+
parent_page_id: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
25
|
+
parent_data_source_id: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
26
|
+
file_path: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
27
|
+
title_property: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
28
|
+
properties: import("@oclif/core/lib/interfaces").OptionFlag<string, import("@oclif/core/lib/interfaces").CustomOptions>;
|
|
29
|
+
'simple-properties': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
30
|
+
raw: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
|
|
31
|
+
};
|
|
32
|
+
run(): Promise<void>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const core_1 = require("@oclif/core");
|
|
4
|
+
const table_formatter_1 = require("../../utils/table-formatter");
|
|
5
|
+
const notion = require("../../notion");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const markdown_to_blocks_1 = require("../../utils/markdown-to-blocks");
|
|
9
|
+
const helper_1 = require("../../helper");
|
|
10
|
+
const notion_resolver_1 = require("../../utils/notion-resolver");
|
|
11
|
+
const base_flags_1 = require("../../base-flags");
|
|
12
|
+
const errors_1 = require("../../errors");
|
|
13
|
+
const property_expander_1 = require("../../utils/property-expander");
|
|
14
|
+
class PageCreate extends core_1.Command {
|
|
15
|
+
async run() {
|
|
16
|
+
var _a;
|
|
17
|
+
const { flags } = await this.parse(PageCreate);
|
|
18
|
+
try {
|
|
19
|
+
let pageProps;
|
|
20
|
+
let pageParent;
|
|
21
|
+
if (flags.parent_page_id) {
|
|
22
|
+
// Resolve parent page ID from URL, direct ID, or name (future)
|
|
23
|
+
const parentPageId = await (0, notion_resolver_1.resolveNotionId)(flags.parent_page_id, 'page');
|
|
24
|
+
pageParent = {
|
|
25
|
+
page_id: parentPageId,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// Resolve parent database ID from URL, direct ID, or name (future)
|
|
30
|
+
const parentDataSourceId = await (0, notion_resolver_1.resolveNotionId)(flags.parent_data_source_id, 'database');
|
|
31
|
+
pageParent = {
|
|
32
|
+
data_source_id: parentDataSourceId,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// Auto-detect title property: sub-pages use "title", database pages use "Name"
|
|
36
|
+
// User can override with -t flag
|
|
37
|
+
const titleProperty = (_a = flags.title_property) !== null && _a !== void 0 ? _a : (flags.parent_page_id ? 'title' : 'Name');
|
|
38
|
+
// Build properties object
|
|
39
|
+
let properties = {};
|
|
40
|
+
// Handle properties flag
|
|
41
|
+
if (flags.properties) {
|
|
42
|
+
try {
|
|
43
|
+
const parsedProps = JSON.parse(flags.properties);
|
|
44
|
+
if (flags['simple-properties']) {
|
|
45
|
+
if (flags.parent_page_id) {
|
|
46
|
+
// Sub-pages only have a title property — expand it directly without schema
|
|
47
|
+
const titleValue = parsedProps[titleProperty] || parsedProps['title'] || parsedProps['Title'];
|
|
48
|
+
if (titleValue && typeof titleValue === 'string') {
|
|
49
|
+
properties[titleProperty] = {
|
|
50
|
+
title: [{ text: { content: titleValue } }]
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
throw new Error('Sub-page creation with -S requires a title property.\n' +
|
|
55
|
+
`Example: --properties '{"${titleProperty}": "My Page"}'`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (flags.parent_data_source_id) {
|
|
59
|
+
// Database pages — use schema-based expansion
|
|
60
|
+
const parentDataSourceId = await (0, notion_resolver_1.resolveNotionId)(flags.parent_data_source_id, 'database');
|
|
61
|
+
const dbSchema = await notion.retrieveDataSource(parentDataSourceId);
|
|
62
|
+
properties = await (0, property_expander_1.expandSimpleProperties)(parsedProps, dbSchema.properties);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
throw new Error('The --simple-properties flag requires either -p (sub-page) or -d (database) to be set.');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Use raw Notion format
|
|
70
|
+
properties = parsedProps;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (error.message.includes('Unexpected token') || error.message.includes('JSON')) {
|
|
75
|
+
throw new Error(`Invalid JSON in --properties flag: ${error.message}\n` +
|
|
76
|
+
`Example: --properties '{"Name": "Task", "Status": "Done"}'`);
|
|
77
|
+
}
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (flags.file_path) {
|
|
82
|
+
// Resolve path and prevent directory traversal outside cwd
|
|
83
|
+
const basePath = path.resolve('./');
|
|
84
|
+
const p = path.resolve(basePath, flags.file_path);
|
|
85
|
+
if (!p.startsWith(basePath)) {
|
|
86
|
+
throw new Error(`Invalid file path: must be within current directory`);
|
|
87
|
+
}
|
|
88
|
+
const fileName = path.basename(flags.file_path);
|
|
89
|
+
const md = fs.readFileSync(p, { encoding: 'utf-8' });
|
|
90
|
+
const blocks = (0, markdown_to_blocks_1.markdownToBlocks)(md);
|
|
91
|
+
// Extract title from H1 heading or use filename without extension
|
|
92
|
+
const extractTitle = (markdown, filename) => {
|
|
93
|
+
const h1Match = markdown.match(/^#\s+(.+)$/m);
|
|
94
|
+
if (h1Match && h1Match[1]) {
|
|
95
|
+
return h1Match[1].trim();
|
|
96
|
+
}
|
|
97
|
+
// Fallback: use filename without extension
|
|
98
|
+
return filename.replace(/\.md$/, '');
|
|
99
|
+
};
|
|
100
|
+
const pageTitle = extractTitle(md, fileName);
|
|
101
|
+
// If no properties were provided via flag, use extracted title
|
|
102
|
+
if (!flags.properties) {
|
|
103
|
+
properties = {
|
|
104
|
+
[titleProperty]: {
|
|
105
|
+
title: [{ text: { content: pageTitle } }],
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
// Merge with existing properties, but ensure title is set
|
|
111
|
+
if (!properties[titleProperty]) {
|
|
112
|
+
properties[titleProperty] = {
|
|
113
|
+
title: [{ text: { content: pageTitle } }],
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
pageProps = {
|
|
118
|
+
parent: pageParent,
|
|
119
|
+
properties,
|
|
120
|
+
children: blocks,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
pageProps = {
|
|
125
|
+
parent: pageParent,
|
|
126
|
+
properties,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const res = await notion.createPage(pageProps);
|
|
130
|
+
// Handle JSON output for automation
|
|
131
|
+
if (flags.json) {
|
|
132
|
+
this.log(JSON.stringify({
|
|
133
|
+
success: true,
|
|
134
|
+
data: res,
|
|
135
|
+
timestamp: new Date().toISOString()
|
|
136
|
+
}, null, 2));
|
|
137
|
+
process.exit(0);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// Handle raw JSON output (legacy)
|
|
141
|
+
if (flags.raw) {
|
|
142
|
+
(0, helper_1.outputRawJson)(res);
|
|
143
|
+
process.exit(0);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// Handle table output
|
|
147
|
+
const columns = {
|
|
148
|
+
title: {
|
|
149
|
+
get: (row) => {
|
|
150
|
+
return (0, helper_1.getPageTitle)(row);
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
object: {},
|
|
154
|
+
id: {},
|
|
155
|
+
url: {},
|
|
156
|
+
};
|
|
157
|
+
const options = {
|
|
158
|
+
printLine: this.log.bind(this),
|
|
159
|
+
...flags,
|
|
160
|
+
};
|
|
161
|
+
(0, table_formatter_1.formatTable)([res], columns, options);
|
|
162
|
+
process.exit(0);
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
const cliError = error instanceof errors_1.NotionCLIError
|
|
166
|
+
? error
|
|
167
|
+
: (0, errors_1.wrapNotionError)(error, {
|
|
168
|
+
resourceType: 'page',
|
|
169
|
+
endpoint: 'pages.create'
|
|
170
|
+
});
|
|
171
|
+
if (flags.json) {
|
|
172
|
+
this.log(JSON.stringify(cliError.toJSON(), null, 2));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.error(cliError.toHumanString());
|
|
176
|
+
}
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
PageCreate.description = 'Create a page';
|
|
182
|
+
PageCreate.aliases = ['page:c'];
|
|
183
|
+
PageCreate.examples = [
|
|
184
|
+
{
|
|
185
|
+
description: 'Create a page via interactive mode',
|
|
186
|
+
command: `$ notion-cli page create`,
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
description: 'Create a page with a specific parent_page_id',
|
|
190
|
+
command: `$ notion-cli page create -p PARENT_PAGE_ID`,
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
description: 'Create a page with a parent page URL',
|
|
194
|
+
command: `$ notion-cli page create -p https://notion.so/PARENT_PAGE_ID`,
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
description: 'Create a page with a specific parent_db_id',
|
|
198
|
+
command: `$ notion-cli page create -d PARENT_DB_ID`,
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
description: 'Create a page with simple properties (recommended for AI agents)',
|
|
202
|
+
command: `$ notion-cli page create -d DATA_SOURCE_ID -S --properties '{"Name": "My Task", "Status": "In Progress", "Due Date": "2025-12-31"}'`,
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
description: 'Create a page with simple properties using relative dates',
|
|
206
|
+
command: `$ notion-cli page create -d DATA_SOURCE_ID -S --properties '{"Name": "Review", "Due Date": "tomorrow", "Priority": "High"}'`,
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
description: 'Create a page with simple properties and multi-select',
|
|
210
|
+
command: `$ notion-cli page create -d DATA_SOURCE_ID -S --properties '{"Name": "Bug Fix", "Tags": ["urgent", "bug"], "Status": "Done"}'`,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
description: 'Create a page with a specific source markdown file and parent_page_id',
|
|
214
|
+
command: `$ notion-cli page create -f ./path/to/source.md -p PARENT_PAGE_ID`,
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
description: 'Create a page with a specific source markdown file and parent_db_id',
|
|
218
|
+
command: `$ notion-cli page create -f ./path/to/source.md -d PARENT_DB_ID`,
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
description: 'Create a page with a specific source markdown file and output raw json with parent_page_id',
|
|
222
|
+
command: `$ notion-cli page create -f ./path/to/source.md -p PARENT_PAGE_ID -r`,
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
description: 'Create a page and output JSON for automation',
|
|
226
|
+
command: `$ notion-cli page create -p PARENT_PAGE_ID --json`,
|
|
227
|
+
},
|
|
228
|
+
];
|
|
229
|
+
PageCreate.flags = {
|
|
230
|
+
parent_page_id: core_1.Flags.string({
|
|
231
|
+
char: 'p',
|
|
232
|
+
description: 'Parent page ID or URL (to create a sub-page)',
|
|
233
|
+
}),
|
|
234
|
+
parent_data_source_id: core_1.Flags.string({
|
|
235
|
+
char: 'd',
|
|
236
|
+
description: 'Parent data source ID or URL (to create a page in a table)',
|
|
237
|
+
}),
|
|
238
|
+
file_path: core_1.Flags.string({
|
|
239
|
+
char: 'f',
|
|
240
|
+
description: 'Path to a source markdown file',
|
|
241
|
+
}),
|
|
242
|
+
title_property: core_1.Flags.string({
|
|
243
|
+
char: 't',
|
|
244
|
+
description: 'Title property name (auto-detected: "title" for sub-pages, "Name" for databases)',
|
|
245
|
+
}),
|
|
246
|
+
properties: core_1.Flags.string({
|
|
247
|
+
description: 'Page properties as JSON string',
|
|
248
|
+
}),
|
|
249
|
+
'simple-properties': core_1.Flags.boolean({
|
|
250
|
+
char: 'S',
|
|
251
|
+
description: 'Use simplified property format (flat key-value pairs, recommended for AI agents)',
|
|
252
|
+
default: false,
|
|
253
|
+
}),
|
|
254
|
+
raw: core_1.Flags.boolean({
|
|
255
|
+
char: 'r',
|
|
256
|
+
description: 'output raw json',
|
|
257
|
+
}),
|
|
258
|
+
...table_formatter_1.tableFlags,
|
|
259
|
+
...base_flags_1.AutomationFlags,
|
|
260
|
+
};
|
|
261
|
+
exports.default = PageCreate;
|