@tenonhq/dovetail-servicenow 0.0.55 → 0.0.57

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/README.md CHANGED
@@ -103,6 +103,12 @@ npx dove-sn view-flow --sys-id <id> --env .env.prod
103
103
  npx dove-sn add-choices --env ../envs/workshop.env --table ... --column ...
104
104
  ```
105
105
 
106
+ A bare instance name works too: `--env loft` → `.env.loft` in the current
107
+ directory, or — when it has none — the nearest `.env.loft` in a parent directory
108
+ (up to 8 levels, never the home directory or above, only a file owned by you; the
109
+ chosen path is printed to stderr), so a nested git worktree reaches the checkout's per-instance file. Paths
110
+ and dotted names (`.env.prod`, `loft.env`) never walk up.
111
+
106
112
  `--env` (alias `--env-file`) wins over the `DOVETAIL_ENV_FILE` env var, which in
107
113
  turn beats the default `.env`. Variables already present in the environment are
108
114
  never overridden, so an exported `SN_INSTANCE` still takes precedence over the
package/dist/loadEnv.d.ts CHANGED
@@ -16,10 +16,53 @@ export declare var SN_CONNECTION_KEYS: string[];
16
16
  * - a bare name that exists as a file in cwd (e.g. `--env my.env`) → that file;
17
17
  * - a bare name like `loft` → `<cwd>/.env.loft`, matching the MCP tool's
18
18
  * per-call `env` resolution; a `.env`-prefixed basename is used as-is.
19
+ * When `<cwd>/.env.loft` does not exist, the parent directories are
20
+ * searched (findEnvFileUpward) and the nearest existing `.env.loft` wins,
21
+ * so a nested git worktree can use the per-instance file of the checkout
22
+ * it lives in. Nothing found → `<cwd>/.env.loft`, as before. Same rule as
23
+ * `dove`'s resolveEnvArgPath (packages/core/src/envArg.ts).
19
24
  *
20
- * Returns the resolved path; does not check existence (loadEnvFile does).
25
+ * Only the bare-name case walks up. Existence is checked with stat only — no
26
+ * file is read here. Returns the resolved path; loadEnvFile checks existence.
21
27
  */
22
- export declare function resolveEnvSelection(raw: string, cwd?: string): string;
28
+ export declare function resolveEnvSelection(raw: string, cwd?: string, options?: EnvWalkUpOptions): string;
29
+ /**
30
+ * How many parent directories a bare instance name's file is looked for in,
31
+ * above the cwd. Mirrored in packages/core/src/envArg.ts.
32
+ */
33
+ export declare var ENV_WALK_UP_MAX_LEVELS: number;
34
+ /** Options for the parent-directory lookup of a bare instance name. */
35
+ export interface EnvWalkUpOptions {
36
+ /** Home directory to stop below (defaults to os.homedir()). Injectable for tests. */
37
+ homeDir?: string;
38
+ /** Called with the chosen path when the file was found in a parent directory. */
39
+ onParentMatch?: (foundPath: string) => void;
40
+ /**
41
+ * Owner a parent match must have (defaults to process.getuid(); a negative
42
+ * value, or a platform without getuid, skips the check). Injectable for tests.
43
+ */
44
+ ownerUid?: number;
45
+ }
46
+ /**
47
+ * Look for `fileName` in the parent directories of `startDir` (not in
48
+ * `startDir` itself) and return the nearest one that exists as a regular file
49
+ * owned by the current user. Bounds: at most `maxLevels` parents (default
50
+ * ENV_WALK_UP_MAX_LEVELS); never the home directory or anything above it;
51
+ * never the filesystem root. A file owned by someone else (e.g. planted in a
52
+ * shared or world-writable ancestor such as /tmp when the cwd is outside the
53
+ * home directory) is skipped, never selected. Uses stat only and never reads
54
+ * a file. Never throws: bad input or no match returns null.
55
+ *
56
+ * Keep behaviorally identical to findEnvFileUpward in
57
+ * packages/core/src/envArg.ts — tests/envWalkUpParity.test.ts runs both.
58
+ */
59
+ export declare function findEnvFileUpward(args: {
60
+ fileName: string;
61
+ startDir: string;
62
+ homeDir?: string;
63
+ maxLevels?: number;
64
+ ownerUid?: number;
65
+ }): string | null;
23
66
  /**
24
67
  * Loads ServiceNow credentials for the `dove-sn` CLI and its MCP server.
25
68
  *
package/dist/loadEnv.js CHANGED
@@ -3,11 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SN_CONNECTION_KEYS = void 0;
6
+ exports.ENV_WALK_UP_MAX_LEVELS = exports.SN_CONNECTION_KEYS = void 0;
7
7
  exports.resolveEnvSelection = resolveEnvSelection;
8
+ exports.findEnvFileUpward = findEnvFileUpward;
8
9
  exports.loadEnvFile = loadEnvFile;
9
10
  const fs_1 = __importDefault(require("fs"));
10
11
  const dotenv_1 = __importDefault(require("dotenv"));
12
+ const os_1 = __importDefault(require("os"));
11
13
  const path_1 = __importDefault(require("path"));
12
14
  const createClientFromEnvFile_1 = require("./createClientFromEnvFile");
13
15
  /**
@@ -48,10 +50,16 @@ exports.SN_CONNECTION_KEYS = [
48
50
  * - a bare name that exists as a file in cwd (e.g. `--env my.env`) → that file;
49
51
  * - a bare name like `loft` → `<cwd>/.env.loft`, matching the MCP tool's
50
52
  * per-call `env` resolution; a `.env`-prefixed basename is used as-is.
53
+ * When `<cwd>/.env.loft` does not exist, the parent directories are
54
+ * searched (findEnvFileUpward) and the nearest existing `.env.loft` wins,
55
+ * so a nested git worktree can use the per-instance file of the checkout
56
+ * it lives in. Nothing found → `<cwd>/.env.loft`, as before. Same rule as
57
+ * `dove`'s resolveEnvArgPath (packages/core/src/envArg.ts).
51
58
  *
52
- * Returns the resolved path; does not check existence (loadEnvFile does).
59
+ * Only the bare-name case walks up. Existence is checked with stat only — no
60
+ * file is read here. Returns the resolved path; loadEnvFile checks existence.
53
61
  */
54
- function resolveEnvSelection(raw, cwd) {
62
+ function resolveEnvSelection(raw, cwd, options) {
55
63
  if (typeof raw !== "string" || raw.trim().length === 0) {
56
64
  throw new Error("--env must be a non-empty env-file name or path.");
57
65
  }
@@ -67,7 +75,143 @@ function resolveEnvSelection(raw, cwd) {
67
75
  if (value.indexOf(".env") === 0) {
68
76
  return literal;
69
77
  }
70
- return path_1.default.resolve(base, ".env." + value);
78
+ var fileName = BARE_NAME_PREFIX + value;
79
+ var inCwd = path_1.default.resolve(base, fileName);
80
+ if (isExistingFile(inCwd)) {
81
+ return inCwd;
82
+ }
83
+ // A dotted value (`loft.env`) is not a bare instance name: keep the
84
+ // legacy `<cwd>/.env.<value>` result and never walk up for it.
85
+ if (value.indexOf(".") !== -1) {
86
+ return inCwd;
87
+ }
88
+ var opts = options || {};
89
+ var found = findEnvFileUpward({
90
+ fileName: fileName,
91
+ startDir: base,
92
+ homeDir: opts.homeDir,
93
+ ownerUid: opts.ownerUid,
94
+ });
95
+ if (found === null) {
96
+ return inCwd;
97
+ }
98
+ if (typeof opts.onParentMatch === "function") {
99
+ try {
100
+ opts.onParentMatch(found);
101
+ }
102
+ catch (e) {
103
+ // A logging callback must never change which file is selected.
104
+ }
105
+ }
106
+ return found;
107
+ }
108
+ // Prefix a bare instance name is expanded with (`loft` → `.env.loft`).
109
+ var BARE_NAME_PREFIX = ".env.";
110
+ /**
111
+ * How many parent directories a bare instance name's file is looked for in,
112
+ * above the cwd. Mirrored in packages/core/src/envArg.ts.
113
+ */
114
+ exports.ENV_WALK_UP_MAX_LEVELS = 8;
115
+ /**
116
+ * Look for `fileName` in the parent directories of `startDir` (not in
117
+ * `startDir` itself) and return the nearest one that exists as a regular file
118
+ * owned by the current user. Bounds: at most `maxLevels` parents (default
119
+ * ENV_WALK_UP_MAX_LEVELS); never the home directory or anything above it;
120
+ * never the filesystem root. A file owned by someone else (e.g. planted in a
121
+ * shared or world-writable ancestor such as /tmp when the cwd is outside the
122
+ * home directory) is skipped, never selected. Uses stat only and never reads
123
+ * a file. Never throws: bad input or no match returns null.
124
+ *
125
+ * Keep behaviorally identical to findEnvFileUpward in
126
+ * packages/core/src/envArg.ts — tests/envWalkUpParity.test.ts runs both.
127
+ */
128
+ function findEnvFileUpward(args) {
129
+ if (!args || typeof args.fileName !== "string" || typeof args.startDir !== "string") {
130
+ return null;
131
+ }
132
+ var fileName = args.fileName;
133
+ if (fileName.length === 0 || /[\\/]/.test(fileName) || fileName === "." || fileName === "..") {
134
+ return null;
135
+ }
136
+ if (args.startDir.length === 0) {
137
+ return null;
138
+ }
139
+ var maxLevels = typeof args.maxLevels === "number" && isFinite(args.maxLevels) && args.maxLevels >= 0
140
+ ? Math.floor(args.maxLevels)
141
+ : exports.ENV_WALK_UP_MAX_LEVELS;
142
+ var home = typeof args.homeDir === "string" ? args.homeDir : safeHomeDir();
143
+ var homeResolved = home.length > 0 ? path_1.default.resolve(home) : "";
144
+ var ownerUid = typeof args.ownerUid === "number" && isFinite(args.ownerUid) ? args.ownerUid : currentUid();
145
+ var dir = path_1.default.resolve(args.startDir);
146
+ for (var level = 0; level < maxLevels; level++) {
147
+ var parent = path_1.default.dirname(dir);
148
+ if (parent === dir) {
149
+ break; // already at the root
150
+ }
151
+ dir = parent;
152
+ if (path_1.default.dirname(dir) === dir) {
153
+ break; // never the filesystem root
154
+ }
155
+ if (homeResolved.length > 0 && isSameOrAncestor({ dir: dir, of: homeResolved })) {
156
+ break; // never the home directory or above it
157
+ }
158
+ var candidate = path_1.default.join(dir, fileName);
159
+ if (isOwnedFile({ candidate: candidate, ownerUid: ownerUid })) {
160
+ return candidate;
161
+ }
162
+ }
163
+ return null;
164
+ }
165
+ // True when the path is a regular file owned by `ownerUid` (any owner when
166
+ // `ownerUid` is negative). Stat only. Never throws.
167
+ function isOwnedFile(args) {
168
+ try {
169
+ var stat = fs_1.default.statSync(args.candidate);
170
+ if (!stat.isFile()) {
171
+ return false;
172
+ }
173
+ return args.ownerUid < 0 || stat.uid === args.ownerUid;
174
+ }
175
+ catch (e) {
176
+ return false;
177
+ }
178
+ }
179
+ // process.getuid(), or -1 where it does not exist (Windows), which disables
180
+ // the ownership check. Never throws.
181
+ function currentUid() {
182
+ try {
183
+ return typeof process.getuid === "function" ? process.getuid() : -1;
184
+ }
185
+ catch (e) {
186
+ return -1;
187
+ }
188
+ }
189
+ // True when `dir` is `of` itself or one of its ancestors. Both absolute.
190
+ function isSameOrAncestor(args) {
191
+ var rel = path_1.default.relative(args.dir, args.of);
192
+ if (rel === "") {
193
+ return true;
194
+ }
195
+ return rel.indexOf("..") !== 0 && !path_1.default.isAbsolute(rel);
196
+ }
197
+ // True when the path exists and is a regular file. Never throws.
198
+ function isExistingFile(candidate) {
199
+ try {
200
+ return fs_1.default.statSync(candidate).isFile();
201
+ }
202
+ catch (e) {
203
+ return false;
204
+ }
205
+ }
206
+ // os.homedir(), or "" when it cannot be determined. Never throws.
207
+ function safeHomeDir() {
208
+ try {
209
+ var home = os_1.default.homedir();
210
+ return typeof home === "string" ? home : "";
211
+ }
212
+ catch (e) {
213
+ return "";
214
+ }
71
215
  }
72
216
  /**
73
217
  * Loads ServiceNow credentials for the `dove-sn` CLI and its MCP server.
@@ -98,7 +242,13 @@ function loadEnvFile(explicitSelection) {
98
242
  dotenv_1.default.config();
99
243
  return undefined;
100
244
  }
101
- var resolved = resolveEnvSelection(raw);
245
+ var resolved = resolveEnvSelection(raw, undefined, {
246
+ onParentMatch: function (foundPath) {
247
+ // stderr, never stdout (stdout carries --json output and the MCP protocol).
248
+ // The path only — never anything from the file.
249
+ process.stderr.write("dove-sn: --env " + raw + ": using " + foundPath + " (found in a parent directory)\n");
250
+ },
251
+ });
102
252
  if (!fs_1.default.existsSync(resolved)) {
103
253
  throw new Error("env file not found: '" + resolved + "' (from --env '" + raw + "'). " +
104
254
  "Refusing to fall back to the default environment — pass an existing env-file " +
@@ -46,7 +46,11 @@ var KNOWN_INTERNAL = {
46
46
  reference: true, choice: true, translated_text: true, html: true, url: true,
47
47
  email: true, journal: true, journal_input: true, currency: true, price: true,
48
48
  percent_complete: true, sys_class_name: true, document_id: true, field_name: true,
49
- table_name: true, json: true, script: true
49
+ table_name: true, json: true, script: true,
50
+ // A condition-builder column (pair it with dependent_on_field = its table_name column),
51
+ // and the two domain-separation columns a domain-aware table carries (sys_domain /
52
+ // sys_domain_path).
53
+ conditions: true, domain_id: true, domain_path: true
50
54
  };
51
55
  /** Resolve a friendly-or-internal type string to the internal_type to write. */
52
56
  function resolveType(type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenonhq/dovetail-servicenow",
3
- "version": "0.0.55",
3
+ "version": "0.0.57",
4
4
  "engines": {
5
5
  "node": ">=22"
6
6
  },