@catafal/notion-cli 5.9.1 → 5.9.2
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/dist/notion.js +10 -0
- package/dist/utils/shell-config.d.ts +19 -10
- package/dist/utils/shell-config.js +65 -24
- package/oclif.manifest.json +871 -871
- package/package.json +1 -1
package/dist/notion.js
CHANGED
|
@@ -6,6 +6,16 @@ const cache_1 = require("./cache");
|
|
|
6
6
|
const retry_1 = require("./retry");
|
|
7
7
|
const deduplication_1 = require("./deduplication");
|
|
8
8
|
const http_agent_1 = require("./http-agent");
|
|
9
|
+
const shell_config_1 = require("./utils/shell-config");
|
|
10
|
+
// Resolve token: env var takes priority, fall back to ~/.notion-cli/config.json.
|
|
11
|
+
// This ensures the CLI works in non-interactive environments (AI agents, cron, etc.)
|
|
12
|
+
// where shell profile files (~/.zshrc, ~/.zshenv) are not sourced.
|
|
13
|
+
if (!process.env.NOTION_TOKEN) {
|
|
14
|
+
const configToken = (0, shell_config_1.readTokenFromConfig)();
|
|
15
|
+
if (configToken) {
|
|
16
|
+
process.env.NOTION_TOKEN = configToken;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
9
19
|
/**
|
|
10
20
|
* Custom fetch function that uses our configured HTTPS agent and compression
|
|
11
21
|
*/
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shell Configuration
|
|
2
|
+
* Shell Configuration & Token Persistence
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Handles token storage across three layers:
|
|
5
|
+
* 1. Shell rc files (.zshrc, .bashrc) — for interactive terminal sessions
|
|
6
|
+
* 2. ~/.zshenv — for non-interactive zsh subprocesses
|
|
7
|
+
* 3. ~/.notion-cli/config.json — for environments with no shell at all
|
|
8
|
+
* (AI agent runtimes like OpenClaw, cron jobs, CI pipelines)
|
|
6
9
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
10
|
+
* Token resolution order (in notion.ts):
|
|
11
|
+
* process.env.NOTION_TOKEN > ~/.notion-cli/config.json
|
|
9
12
|
*/
|
|
10
13
|
/**
|
|
11
14
|
* Detect the current shell from the SHELL environment variable.
|
|
@@ -17,12 +20,18 @@ export declare function detectShell(): string;
|
|
|
17
20
|
*/
|
|
18
21
|
export declare function getRcFilePath(shell: string): string;
|
|
19
22
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
|
|
23
|
+
* Read the Notion token from ~/.notion-cli/config.json.
|
|
24
|
+
* Synchronous because it runs at module load time (before Notion client init).
|
|
25
|
+
* Returns null if file doesn't exist, is malformed, or has no token.
|
|
26
|
+
*/
|
|
27
|
+
export declare function readTokenFromConfig(): string | null;
|
|
28
|
+
/**
|
|
29
|
+
* Persist a Notion token to all storage layers:
|
|
30
|
+
* 1. Shell rc file (.zshrc / .bashrc / etc.) — interactive sessions
|
|
31
|
+
* 2. ~/.zshenv (zsh only) — non-interactive subprocesses
|
|
32
|
+
* 3. ~/.notion-cli/config.json — shell-free environments (AI agents, cron)
|
|
24
33
|
*
|
|
25
|
-
* Returns the shell name and rc file path so callers can inform the user.
|
|
34
|
+
* Returns the shell name and primary rc file path so callers can inform the user.
|
|
26
35
|
*/
|
|
27
36
|
export declare function persistToken(token: string): Promise<{
|
|
28
37
|
rcFile: string;
|
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Shell Configuration
|
|
3
|
+
* Shell Configuration & Token Persistence
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Handles token storage across three layers:
|
|
6
|
+
* 1. Shell rc files (.zshrc, .bashrc) — for interactive terminal sessions
|
|
7
|
+
* 2. ~/.zshenv — for non-interactive zsh subprocesses
|
|
8
|
+
* 3. ~/.notion-cli/config.json — for environments with no shell at all
|
|
9
|
+
* (AI agent runtimes like OpenClaw, cron jobs, CI pipelines)
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* Token resolution order (in notion.ts):
|
|
12
|
+
* process.env.NOTION_TOKEN > ~/.notion-cli/config.json
|
|
10
13
|
*/
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.detectShell = detectShell;
|
|
13
16
|
exports.getRcFilePath = getRcFilePath;
|
|
17
|
+
exports.readTokenFromConfig = readTokenFromConfig;
|
|
14
18
|
exports.persistToken = persistToken;
|
|
15
19
|
const fs = require("fs/promises");
|
|
20
|
+
const fsSync = require("fs");
|
|
16
21
|
const path = require("path");
|
|
17
22
|
const os = require("os");
|
|
23
|
+
// Config file path: ~/.notion-cli/config.json (same dir used for caches)
|
|
24
|
+
const CONFIG_DIR = path.join(os.homedir(), '.notion-cli');
|
|
25
|
+
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
18
26
|
/**
|
|
19
27
|
* Detect the current shell from the SHELL environment variable.
|
|
20
28
|
* Falls back to bash on Unix, powershell on Windows.
|
|
@@ -48,37 +56,70 @@ function getRcFilePath(shell) {
|
|
|
48
56
|
}
|
|
49
57
|
}
|
|
50
58
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* otherwise appends a new export line. Creates the file if it doesn't exist.
|
|
55
|
-
*
|
|
56
|
-
* Returns the shell name and rc file path so callers can inform the user.
|
|
59
|
+
* Read the Notion token from ~/.notion-cli/config.json.
|
|
60
|
+
* Synchronous because it runs at module load time (before Notion client init).
|
|
61
|
+
* Returns null if file doesn't exist, is malformed, or has no token.
|
|
57
62
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
function readTokenFromConfig() {
|
|
64
|
+
try {
|
|
65
|
+
const raw = fsSync.readFileSync(CONFIG_FILE, 'utf-8');
|
|
66
|
+
const config = JSON.parse(raw);
|
|
67
|
+
return typeof config.token === 'string' ? config.token : null;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Upsert a NOTION_TOKEN export line in a single shell file.
|
|
75
|
+
* Replaces existing line if found, otherwise appends a new one.
|
|
76
|
+
*/
|
|
77
|
+
async function upsertTokenInFile(filePath, token) {
|
|
78
|
+
let content = '';
|
|
63
79
|
try {
|
|
64
|
-
|
|
80
|
+
content = await fs.readFile(filePath, 'utf-8');
|
|
65
81
|
}
|
|
66
82
|
catch (error) {
|
|
67
83
|
if (error && typeof error === 'object' && 'code' in error && error.code !== 'ENOENT') {
|
|
68
84
|
throw error;
|
|
69
85
|
}
|
|
70
|
-
// File doesn't exist — will be created on write
|
|
71
86
|
}
|
|
72
|
-
// Upsert the NOTION_TOKEN export line
|
|
73
87
|
const tokenLineRegex = /^export\s+NOTION_TOKEN=.*/gm;
|
|
74
88
|
const newTokenLine = `export NOTION_TOKEN="${token}"`;
|
|
75
|
-
let
|
|
76
|
-
if (tokenLineRegex.test(
|
|
77
|
-
|
|
89
|
+
let updated;
|
|
90
|
+
if (tokenLineRegex.test(content)) {
|
|
91
|
+
updated = content.replace(tokenLineRegex, newTokenLine);
|
|
78
92
|
}
|
|
79
93
|
else {
|
|
80
|
-
|
|
94
|
+
updated = content.trim() + '\n\n# Notion CLI Token\n' + newTokenLine + '\n';
|
|
95
|
+
}
|
|
96
|
+
await fs.writeFile(filePath, updated, 'utf-8');
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Persist a Notion token to all storage layers:
|
|
100
|
+
* 1. Shell rc file (.zshrc / .bashrc / etc.) — interactive sessions
|
|
101
|
+
* 2. ~/.zshenv (zsh only) — non-interactive subprocesses
|
|
102
|
+
* 3. ~/.notion-cli/config.json — shell-free environments (AI agents, cron)
|
|
103
|
+
*
|
|
104
|
+
* Returns the shell name and primary rc file path so callers can inform the user.
|
|
105
|
+
*/
|
|
106
|
+
async function persistToken(token) {
|
|
107
|
+
const shell = detectShell();
|
|
108
|
+
const rcFile = getRcFilePath(shell);
|
|
109
|
+
// 1. Write to shell rc file (interactive sessions)
|
|
110
|
+
await upsertTokenInFile(rcFile, token);
|
|
111
|
+
// 2. For zsh: also write to ~/.zshenv (non-interactive subprocesses)
|
|
112
|
+
if (shell === 'zsh') {
|
|
113
|
+
await upsertTokenInFile(path.join(os.homedir(), '.zshenv'), token);
|
|
114
|
+
}
|
|
115
|
+
// 3. Write to ~/.notion-cli/config.json (shell-free environments)
|
|
116
|
+
await fs.mkdir(CONFIG_DIR, { recursive: true });
|
|
117
|
+
let config = {};
|
|
118
|
+
try {
|
|
119
|
+
config = JSON.parse(await fs.readFile(CONFIG_FILE, 'utf-8'));
|
|
81
120
|
}
|
|
82
|
-
|
|
121
|
+
catch { /* file doesn't exist yet — start fresh */ }
|
|
122
|
+
config.token = token;
|
|
123
|
+
await fs.writeFile(CONFIG_FILE, JSON.stringify(config, null, 2) + '\n', 'utf-8');
|
|
83
124
|
return { rcFile, shell };
|
|
84
125
|
}
|