@jacebenson/jsn 5.2.0-dev.132.1 → 5.2.0-dev.133.1
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/package.json +1 -1
- package/src/cli.js +7 -1
- package/src/welcome.js +324 -0
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -10,6 +10,7 @@ import { App } from './app.js';
|
|
|
10
10
|
import { resolveSession, applySession } from './session.js';
|
|
11
11
|
import { renderHelp, renderTips } from './help.js';
|
|
12
12
|
import { isMutationCommand, refreshMutationCommands } from './mutations.js';
|
|
13
|
+
import { maybeShowWelcome } from './welcome.js';
|
|
13
14
|
import { skippedUpdatesCmd } from './commands/skipped-updates.js';
|
|
14
15
|
import { isLocalPreview, noInstanceCommands, dailyCheckSkipCommands, contextHeaderSkipCommands } from './capabilities.js';
|
|
15
16
|
import { guardExit, exitWithError, AppError, CodeUsage } from './errors.js';
|
|
@@ -585,7 +586,12 @@ export function buildCLI() {
|
|
|
585
586
|
}
|
|
586
587
|
process.exit(0);
|
|
587
588
|
}
|
|
588
|
-
|
|
589
|
+
// First-run orientation (issue #387) sits above the unchanged help.
|
|
590
|
+
// maybeShowWelcome decides for itself: human terminal, no machine
|
|
591
|
+
// format, no configured profile, and not shown before.
|
|
592
|
+
const config = loadConfig(formatArgv);
|
|
593
|
+
maybeShowWelcome({ argv: formatArgv, requestedFormat, cfg: config });
|
|
594
|
+
process.stdout.write(renderHelp(config, visibleCommandRegistry(cliInstance.commandRegistry(), config, formatArgv)) + '\n');
|
|
589
595
|
process.exit(0);
|
|
590
596
|
}
|
|
591
597
|
// Every other failure here (unknown command, missing required argument,
|
package/src/welcome.js
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
// First-run welcome (issue #387).
|
|
2
|
+
//
|
|
3
|
+
// A brand-new install that runs bare `jsn` gets grouped help, which assumes
|
|
4
|
+
// the reader already knows what to do with it. This adds a short orientation
|
|
5
|
+
// above that help, exactly once, only when a human is reading it.
|
|
6
|
+
//
|
|
7
|
+
// Invariants (see docs/DECISIONS.md):
|
|
8
|
+
// - at most once: a marker file in the XDG config dir records that the note
|
|
9
|
+
// was shown, and it is created exclusively so two racing first runs cannot
|
|
10
|
+
// both print it;
|
|
11
|
+
// - human terminal only: no --json/--quiet/--csv/--markdown/--get, no piped
|
|
12
|
+
// output, no agent/CI environments;
|
|
13
|
+
// - never instead of help: the grouped help below it is unchanged;
|
|
14
|
+
// - never an instance or auth operation: the note only points at local and
|
|
15
|
+
// interactive commands, so it cannot block setup or require credentials;
|
|
16
|
+
// - nothing on this path reaches the network, prompts, or reads an instance;
|
|
17
|
+
// - not shown to an already-configured installation (profiles exist);
|
|
18
|
+
// - plain ASCII at every width, so NO_COLOR, TERM=dumb and narrow terminals
|
|
19
|
+
// all read the same thing.
|
|
20
|
+
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import os from 'node:os';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import { globalConfigDir } from './config.js';
|
|
25
|
+
import { canPrompt } from './helpers.js';
|
|
26
|
+
import { getVersion } from './commands/version.js';
|
|
27
|
+
|
|
28
|
+
/** Marker file in the XDG config dir. Presence means "already welcomed". */
|
|
29
|
+
export const WELCOME_MARKER = '.welcomed';
|
|
30
|
+
|
|
31
|
+
/** Line budget for the note at 80 columns (issue #387 hard limit). */
|
|
32
|
+
export const WELCOME_MAX_LINES = 12;
|
|
33
|
+
|
|
34
|
+
/** The note never uses more than this many columns, however wide the terminal. */
|
|
35
|
+
const MAX_WIDTH = 80;
|
|
36
|
+
|
|
37
|
+
/** Below this, descriptions and prose get one word per line; commands never split. */
|
|
38
|
+
const MIN_WIDTH = 24;
|
|
39
|
+
|
|
40
|
+
const HUMAN_FORMATS = new Set(['', 'auto', 'styled']);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* What a first run actually needs next. Every command here exists, is visible
|
|
44
|
+
* to a profile-less install, and works without an instance:
|
|
45
|
+
* - `jsn setup` opens the interactive add-connection wizard (and defaults);
|
|
46
|
+
* - `jsn auth login <instance>` adds one connection without the wizard;
|
|
47
|
+
* - `jsn --help` is the grouped command list this note sits above;
|
|
48
|
+
* - `jsn docs search` queries ServiceNow documentation, not an instance;
|
|
49
|
+
* - `jsn skill install` writes the bundled agent skill from local files.
|
|
50
|
+
*/
|
|
51
|
+
const STEPS = [
|
|
52
|
+
{ command: 'jsn setup', detail: 'first connection + defaults, interactive' },
|
|
53
|
+
{ command: 'jsn auth login <instance>', detail: 'one connection, scriptable, no wizard' },
|
|
54
|
+
{ command: 'jsn --help', detail: 'every command and flag' },
|
|
55
|
+
{ command: 'jsn docs search <query>', detail: 'search ServiceNow documentation' },
|
|
56
|
+
{ command: 'jsn skill install', detail: 'install the jsn skill for your AI agent' },
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Resolve the terminal width this note may use. Absent or nonsense columns
|
|
61
|
+
* fall back to 80, so a non-interactive writer still gets the documented
|
|
62
|
+
* layout.
|
|
63
|
+
*
|
|
64
|
+
* @param {number|undefined} columns
|
|
65
|
+
* @returns {number}
|
|
66
|
+
*/
|
|
67
|
+
function layoutWidth(columns) {
|
|
68
|
+
const numeric = Number(columns);
|
|
69
|
+
if (!Number.isFinite(numeric) || numeric <= 0) return MAX_WIDTH;
|
|
70
|
+
return Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, Math.floor(numeric)));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Greedy word wrap that never truncates: a word longer than the width is left
|
|
75
|
+
* whole on its own line rather than cut. Command names are passed with
|
|
76
|
+
* `atomic: true` and are never split at all.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} text
|
|
79
|
+
* @param {number} width
|
|
80
|
+
* @param {string} [indent]
|
|
81
|
+
* @returns {string[]}
|
|
82
|
+
*/
|
|
83
|
+
function wrapText(text, width, indent = '') {
|
|
84
|
+
const words = String(text).split(/\s+/).filter(Boolean);
|
|
85
|
+
const lines = [];
|
|
86
|
+
let current = '';
|
|
87
|
+
for (const word of words) {
|
|
88
|
+
if (!current) {
|
|
89
|
+
current = `${indent}${word}`;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (current.length + 1 + word.length <= width) {
|
|
93
|
+
current += ` ${word}`;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
lines.push(current);
|
|
97
|
+
current = `${indent}${word}`;
|
|
98
|
+
}
|
|
99
|
+
if (current) lines.push(current);
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Candidate labels for "where config lives". The first honest label that fits
|
|
105
|
+
* the width wins, so a long XDG path is replaced by the switch that produced
|
|
106
|
+
* it rather than pushed off the screen.
|
|
107
|
+
*
|
|
108
|
+
* @param {object} env
|
|
109
|
+
* @param {string} configDir
|
|
110
|
+
* @returns {string[]}
|
|
111
|
+
*/
|
|
112
|
+
function configLabels(env, configDir) {
|
|
113
|
+
const home = os.homedir();
|
|
114
|
+
const replaceHome = (value) => (home && value.startsWith(`${home}${path.sep}`)
|
|
115
|
+
? `~${value.slice(home.length)}`
|
|
116
|
+
: value);
|
|
117
|
+
const labels = [`Config: ${replaceHome(path.join(configDir, 'config.json'))}`];
|
|
118
|
+
if (env.XDG_CONFIG_HOME) labels.push('Config: $XDG_CONFIG_HOME/servicenow/config.json');
|
|
119
|
+
labels.push('Config: ~/.config/servicenow/config.json');
|
|
120
|
+
return [...new Set(labels)];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Render the "Next steps" block. A wide terminal gets command and description
|
|
125
|
+
* on one aligned row; a narrow one puts the description underneath, indented,
|
|
126
|
+
* so the command name is always intact.
|
|
127
|
+
*
|
|
128
|
+
* @param {number} width
|
|
129
|
+
* @returns {string[]}
|
|
130
|
+
*/
|
|
131
|
+
function renderSteps(width) {
|
|
132
|
+
const commandWidth = Math.max(...STEPS.map((step) => step.command.length));
|
|
133
|
+
const alignedColumn = 4 + commandWidth + 2;
|
|
134
|
+
const oneLine = STEPS.every((step) => alignedColumn + step.detail.length <= width);
|
|
135
|
+
const lines = [];
|
|
136
|
+
for (const step of STEPS) {
|
|
137
|
+
if (oneLine) {
|
|
138
|
+
lines.push(` ${step.command.padEnd(commandWidth)} ${step.detail}`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
lines.push(` ${step.command}`);
|
|
142
|
+
lines.push(...wrapText(step.detail, width, ' '));
|
|
143
|
+
}
|
|
144
|
+
return lines;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The orientation text. Kept short: it must fit above the grouped help without
|
|
149
|
+
* pushing it off the first screen, and it must stay inside 12 lines at 80
|
|
150
|
+
* columns.
|
|
151
|
+
*
|
|
152
|
+
* Everything shown is derived from local state: the version comes from the
|
|
153
|
+
* installed package.json, the config path from the same resolution the CLI
|
|
154
|
+
* uses, and the update command from the version's release channel. No network.
|
|
155
|
+
*
|
|
156
|
+
* @param {object} [opts]
|
|
157
|
+
* @param {number} [opts.columns] — terminal width (defaults to process.stdout.columns)
|
|
158
|
+
* @param {object} [opts.env] — environment (defaults to process.env)
|
|
159
|
+
* @param {string} [opts.configDir] — config dir the marker lives in
|
|
160
|
+
* @param {string} [opts.version] — installed version (defaults to package.json)
|
|
161
|
+
* @returns {string[]} lines
|
|
162
|
+
*/
|
|
163
|
+
export function welcomeLines({
|
|
164
|
+
columns = process.stdout?.columns,
|
|
165
|
+
env = process.env,
|
|
166
|
+
configDir = globalConfigDir(),
|
|
167
|
+
version = getVersion(),
|
|
168
|
+
} = {}) {
|
|
169
|
+
const width = layoutWidth(columns);
|
|
170
|
+
const labels = configLabels(env, configDir);
|
|
171
|
+
const configLabel = labels.find((label) => label.length <= width)
|
|
172
|
+
|| labels.reduce((shortest, label) => (label.length < shortest.length ? label : shortest));
|
|
173
|
+
const title = version && version !== '0.0.0'
|
|
174
|
+
? `Welcome to jsn ${version} (ServiceNow from your terminal)`
|
|
175
|
+
: 'Welcome to jsn (ServiceNow from your terminal)';
|
|
176
|
+
return [
|
|
177
|
+
...wrapText(title, width),
|
|
178
|
+
...wrapText(configLabel, width),
|
|
179
|
+
'',
|
|
180
|
+
' Next steps',
|
|
181
|
+
...renderSteps(width),
|
|
182
|
+
'',
|
|
183
|
+
...wrapText('Nothing above needs an instance. jsn asks for credentials only when needed.', width),
|
|
184
|
+
...wrapText('Prints once. Hide it with JSN_NO_WELCOME=1. Update with jsn upgrade.', width),
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The orientation text as a single string (no trailing newline).
|
|
190
|
+
*
|
|
191
|
+
* @param {object} [opts] — same inputs as welcomeLines
|
|
192
|
+
* @returns {string}
|
|
193
|
+
*/
|
|
194
|
+
export function welcomeText(opts = {}) {
|
|
195
|
+
return welcomeLines(opts).join('\n');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Marker path for a given config dir (injectable for tests).
|
|
200
|
+
*
|
|
201
|
+
* @param {string} [configDir]
|
|
202
|
+
* @returns {string}
|
|
203
|
+
*/
|
|
204
|
+
export function welcomeMarkerPath(configDir = globalConfigDir()) {
|
|
205
|
+
return path.join(configDir, WELCOME_MARKER);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Decide whether this invocation may show the first-run welcome.
|
|
210
|
+
*
|
|
211
|
+
* Pure except for the marker existence check, so callers and tests can pass
|
|
212
|
+
* every input explicitly.
|
|
213
|
+
*
|
|
214
|
+
* `JSN_NO_WELCOME` is honoured only as the literal value `1`; `0`, an empty
|
|
215
|
+
* value and an unset variable all mean "normal first-run behavior".
|
|
216
|
+
*
|
|
217
|
+
* @param {object} [opts]
|
|
218
|
+
* @param {object} [opts.argv] — parsed args (for `--get` and global format flags)
|
|
219
|
+
* @param {string} [opts.requestedFormat] — format resolved from the same argv
|
|
220
|
+
* @param {object} [opts.cfg] — loaded profile config
|
|
221
|
+
* @param {object} [opts.env] — environment (defaults to process.env)
|
|
222
|
+
* @param {string} [opts.configDir] — config dir holding the marker
|
|
223
|
+
* @param {boolean} [opts.interactive] — a human is reachable (defaults to canPrompt())
|
|
224
|
+
* @returns {boolean}
|
|
225
|
+
*/
|
|
226
|
+
export function shouldShowWelcome({
|
|
227
|
+
argv = {},
|
|
228
|
+
requestedFormat = '',
|
|
229
|
+
cfg = {},
|
|
230
|
+
env = process.env,
|
|
231
|
+
configDir = globalConfigDir(),
|
|
232
|
+
interactive = canPrompt(),
|
|
233
|
+
} = {}) {
|
|
234
|
+
if (env.JSN_NO_WELCOME === '1') return false;
|
|
235
|
+
// Machine-readable output and one-shot extraction never get prose.
|
|
236
|
+
if (!HUMAN_FORMATS.has(requestedFormat || '')) return false;
|
|
237
|
+
if (argv && argv.get) return false;
|
|
238
|
+
if (!interactive) return false;
|
|
239
|
+
// An installation that already has a connection is not a first run. Profiles
|
|
240
|
+
// are the durable record of that (an env-provided instance URL is momentary).
|
|
241
|
+
if (cfg && (Object.keys(cfg.profiles || {}).length > 0 || cfg.defaultProfile)) return false;
|
|
242
|
+
try {
|
|
243
|
+
if (fs.existsSync(welcomeMarkerPath(configDir))) return false;
|
|
244
|
+
} catch {
|
|
245
|
+
// Unreadable config dir: showing the note once more is harmless.
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Exclusively record that the welcome was shown.
|
|
252
|
+
*
|
|
253
|
+
* Two first runs racing on the same config dir must not both print the note
|
|
254
|
+
* and must never leave a half-written marker, so the marker is created by
|
|
255
|
+
* hard-linking a fully written temp file: `link(2)` fails with EEXIST for every
|
|
256
|
+
* process but the winner, and the marker path only ever points at complete
|
|
257
|
+
* content.
|
|
258
|
+
*
|
|
259
|
+
* @param {string} [configDir]
|
|
260
|
+
* @returns {'claimed'|'taken'|'unwritable'} — 'taken' means another process
|
|
261
|
+
* already recorded (or recorded concurrently) the note.
|
|
262
|
+
*/
|
|
263
|
+
export function claimWelcome(configDir = globalConfigDir()) {
|
|
264
|
+
const marker = welcomeMarkerPath(configDir);
|
|
265
|
+
const temp = `${marker}.${process.pid}.tmp`;
|
|
266
|
+
const stamp = `${new Date().toISOString()} pid=${process.pid}\n`;
|
|
267
|
+
try {
|
|
268
|
+
fs.mkdirSync(configDir, { recursive: true, mode: 0o750 });
|
|
269
|
+
fs.writeFileSync(temp, stamp, { mode: 0o600 });
|
|
270
|
+
} catch {
|
|
271
|
+
// A read-only or missing config dir must not break the command; the note
|
|
272
|
+
// may be shown again on the next run.
|
|
273
|
+
try { fs.unlinkSync(temp); } catch { /* never created */ }
|
|
274
|
+
return 'unwritable';
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
fs.linkSync(temp, marker);
|
|
278
|
+
return 'claimed';
|
|
279
|
+
} catch (err) {
|
|
280
|
+
if (err && err.code === 'EEXIST') return 'taken';
|
|
281
|
+
// Filesystems without hard links: exclusive create is still a single
|
|
282
|
+
// winner, it just cannot guarantee the marker appears complete.
|
|
283
|
+
try {
|
|
284
|
+
fs.writeFileSync(marker, stamp, { flag: 'wx', mode: 0o600 });
|
|
285
|
+
return 'claimed';
|
|
286
|
+
} catch (fallbackError) {
|
|
287
|
+
if (fallbackError && fallbackError.code === 'EEXIST') return 'taken';
|
|
288
|
+
return 'unwritable';
|
|
289
|
+
}
|
|
290
|
+
} finally {
|
|
291
|
+
try { fs.unlinkSync(temp); } catch { /* already gone */ }
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Boolean form of claimWelcome for callers that only need "did this process
|
|
297
|
+
* record the note".
|
|
298
|
+
*
|
|
299
|
+
* @param {string} [configDir]
|
|
300
|
+
* @returns {boolean}
|
|
301
|
+
*/
|
|
302
|
+
export function markWelcomeSeen(configDir = globalConfigDir()) {
|
|
303
|
+
return claimWelcome(configDir) === 'claimed';
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Show the welcome (above the caller's own help output) when this is a first
|
|
308
|
+
* run, then record it. Returns the text that was shown, or '' when nothing was.
|
|
309
|
+
*
|
|
310
|
+
* The marker is claimed before anything is printed, so an interrupted run
|
|
311
|
+
* (Ctrl-C) can leave the note unshown but never printed twice.
|
|
312
|
+
*
|
|
313
|
+
* @param {object} [opts] — same inputs as shouldShowWelcome, plus the
|
|
314
|
+
* welcomeText inputs (columns, version)
|
|
315
|
+
* @returns {string}
|
|
316
|
+
*/
|
|
317
|
+
export function maybeShowWelcome(opts = {}) {
|
|
318
|
+
if (!shouldShowWelcome(opts)) return '';
|
|
319
|
+
const configDir = opts.configDir || globalConfigDir();
|
|
320
|
+
if (claimWelcome(configDir) === 'taken') return '';
|
|
321
|
+
const text = welcomeText({ ...opts, configDir });
|
|
322
|
+
process.stdout.write(`${text}\n\n`);
|
|
323
|
+
return text;
|
|
324
|
+
}
|