@theholocron/holocron-plugin-1password 5.0.0-alpha.415 → 5.0.0-alpha.416

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/index.d.mts CHANGED
@@ -121,6 +121,8 @@ export interface OpPluginOptions extends VerifyInput {
121
121
  environment?: string;
122
122
  /** Optional 1P account UUID; passed as `--account` on every op call. Useful when several accounts are signed in. */
123
123
  account?: string;
124
+ /** The repository root, handed in by the CLI: where `export-environment` writes `.credentials` and finds a layer word's remote. */
125
+ repoRoot?: string;
124
126
  }
125
127
  /** Shared state for one 1Password plugin instance: its options and the `op` shell wrapper. */
126
128
  export interface PluginContext {
@@ -135,11 +137,12 @@ export declare function createContext(options: OpPluginOptions): PluginContext;
135
137
  export declare function vault(ctx: PluginContext): Vault;
136
138
  /** Builds the `credentials` capability: the variables of a 1Password Environment, read for the token resolvers. */
137
139
  export declare function credentials(ctx: PluginContext): Credentials;
138
- /** Creates the 1Password plugin: its `connection` probe and the `vault` and `credentials` capabilities. */
140
+ /** Creates the 1Password plugin: its `connection` and `export-environment` probes and the `vault` and `credentials` capabilities. */
139
141
  export declare function createPlugin(options?: OpPluginOptions): {
140
142
  name: string;
141
143
  probes: {
142
144
  connection: import("@theholocron/cli").Probe;
145
+ "export-environment": import("@theholocron/cli").Probe;
143
146
  };
144
147
  capabilities: {
145
148
  vault: () => Vault;
package/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
- import { ProviderApiError, connectionProbe } from "@theholocron/cli";
1
+ import { ProviderApiError, connectionProbe, resolveEnvironmentId, stdoutKind } from "@theholocron/cli";
2
2
  import { spawnSync } from "node:child_process";
3
+ import { resolve } from "node:path";
4
+ import { copyToClipboard, dotenvLine, finishCredentialsFile, isGitIgnored, shredFile, writeCredentialsFile } from "@theholocron/kyber/secrets";
3
5
  // src/auth.ts
4
6
  /**
5
7
  * 1Password plugin "auth" is just verifying the `op` binary is on
@@ -264,6 +266,168 @@ function parseReference(reference) {
264
266
  field: fieldParts.join("/")
265
267
  };
266
268
  }
269
+ // src/probes/parse-export-args.ts
270
+ const USAGE = "usage: holocron probe 1password export-environment <environment> [--only NAME]... [--copy]";
271
+ /** Reads the words after `export-environment`: one Environment, any number of `--only NAME`, and `--copy`. */
272
+ function parseExportArgs(args) {
273
+ const only = [];
274
+ const positional = [];
275
+ let copy = false;
276
+ for (let i = 0; i < args.length; i++) {
277
+ const arg = args[i];
278
+ if (arg === "--copy") copy = true;
279
+ else if (arg === "--only") {
280
+ const name = args[++i];
281
+ if (name === void 0) throw new Error(`--only needs a variable name. ${USAGE}`);
282
+ only.push(name);
283
+ } else if (arg.startsWith("--only=")) only.push(arg.slice(7));
284
+ else if (arg.startsWith("--")) throw new Error(`unknown option ${arg}. ${USAGE}`);
285
+ else positional.push(arg);
286
+ }
287
+ if (positional.length !== 1) throw new Error(`name one Environment. ${USAGE}`);
288
+ return {
289
+ environment: positional[0],
290
+ only,
291
+ copy
292
+ };
293
+ }
294
+ // src/probes/export-environment.ts
295
+ /** The file the lines go to, next to the config. */
296
+ const FILE_NAME = ".credentials";
297
+ /** Plain code-unit order: the same on every machine, whatever its locale. The names it sorts are all different. */
298
+ const compare = (a, b) => a < b ? -1 : 1;
299
+ /** Where the lines may go. Throws (with no value in the message) when nowhere is safe. */
300
+ function chooseDestination(deps, copy) {
301
+ if (copy) return "clipboard";
302
+ const kind = deps.outKind();
303
+ if (kind === "pipe") return "stdout";
304
+ if (kind === "other") throw new Error("refusing to write secrets to a redirected file: it would leave them on disk with no protection. Run it on a terminal for the private, short-lived .credentials file, or pipe the output to a tool, or use --copy");
305
+ if (deps.platform === "win32") throw new Error("cannot make the file private on Windows: its mode bits are not enforced, so other users may read it. Use --copy, or pipe the output to a tool");
306
+ if (!deps.interactive) throw new Error("the .credentials file is deleted once you confirm the import, which needs a terminal to ask on. Pipe the output to a tool, or use --copy");
307
+ return "file";
308
+ }
309
+ /** Writes the lines to the private file, waits for the import and deletes it. Registers the deletion for a failure or Ctrl-C. */
310
+ async function writeToFile(probe, deps, text) {
311
+ const path = resolve(deps.repoRoot, FILE_NAME);
312
+ const git = deps.isIgnored(path);
313
+ if (git === "tracked" || git === "unknown") throw new Error(git === "tracked" ? `refusing to write secrets to ${path}: git does not ignore it, so it could be committed. Add it to .gitignore` : `refusing to write secrets to ${path}: git could not say whether it ignores it. Add it to .gitignore`);
314
+ let identity;
315
+ try {
316
+ identity = deps.writeFile(path, text, false);
317
+ } catch (err) {
318
+ const reason = err instanceof Error ? err.message : "unknown error";
319
+ throw new Error(`could not write ${path}: ${reason}. An existing file is never overwritten: delete it first`, { cause: err });
320
+ }
321
+ probe.cleanup(`the ${FILE_NAME} file`, async () => {
322
+ deps.shred(path, identity);
323
+ });
324
+ if (!await finishCredentialsFile({
325
+ path,
326
+ identity,
327
+ print: (line) => probe.say({
328
+ status: "info",
329
+ label: line
330
+ }),
331
+ holds: "the Environment's secrets",
332
+ importHint: "import it in the 1Password app: Developer, View Environments, the target Environment, Import .env file",
333
+ interactive: deps.interactive,
334
+ shred: deps.shred,
335
+ waitForImport: deps.waitForImport
336
+ })) probe.say({
337
+ status: "fail",
338
+ label: `${path} is still on disk and holds secrets in plain text: delete it`
339
+ });
340
+ }
341
+ /**
342
+ * The `export-environment` probe: reads one 1Password Environment in memory and writes its variables as `NAME=value`
343
+ * to a private `.credentials` file (deleted once the import into another Environment is done), a pipe or the clipboard.
344
+ * Names and counts are all it ever reports; a value is only ever in the text handed to the destination.
345
+ */
346
+ function exportEnvironmentProbe(deps) {
347
+ return {
348
+ summary: "Read one 1Password Environment in memory and write its variables as NAME=value to a private .credentials file (deleted after the import), a pipe or the clipboard.",
349
+ writes: true,
350
+ reportsTo: "stderr",
351
+ async run(probe) {
352
+ const args = parseExportArgs(probe.args ?? []);
353
+ const destination = chooseDestination(deps, args.copy);
354
+ const environment = await deps.resolveId(args.environment, deps.repoRoot);
355
+ const all = await deps.read(environment);
356
+ const present = Object.keys(all);
357
+ const unknown = args.only.filter((name) => !present.includes(name));
358
+ if (unknown.length > 0) throw new Error(`${environment} has no variable named ${unknown.join(", ")}`);
359
+ const names = (args.only.length > 0 ? [...new Set(args.only)] : present).sort(compare);
360
+ if (names.length === 0) {
361
+ probe.say({
362
+ status: "info",
363
+ label: environment,
364
+ detail: "has no variables, so there is nothing to export"
365
+ });
366
+ return;
367
+ }
368
+ const lines = [];
369
+ const exported = [];
370
+ for (const name of names) {
371
+ const value = all[name];
372
+ probe.secret(value);
373
+ const line = dotenvLine(name, value);
374
+ if (line === null) {
375
+ probe.say({
376
+ status: "fail",
377
+ label: name,
378
+ detail: "a value dotenv cannot carry unchanged, so add it by hand"
379
+ });
380
+ continue;
381
+ }
382
+ lines.push(line);
383
+ exported.push(name);
384
+ }
385
+ if (lines.length === 0) return;
386
+ const text = `${lines.join("\n")}\n`;
387
+ probe.say({
388
+ status: "heading",
389
+ label: `${environment}: ${exported.length} of ${names.length} variables`
390
+ });
391
+ for (const name of exported) probe.say({
392
+ status: "info",
393
+ label: name
394
+ });
395
+ const label = `write ${exported.length} variables to ${destination === "file" ? FILE_NAME : destination}`;
396
+ await probe.write(label, async () => {
397
+ if (destination === "clipboard") {
398
+ await deps.copy(text);
399
+ probe.say({
400
+ status: "ok",
401
+ label: "copied to the clipboard: paste it, then clear the clipboard"
402
+ });
403
+ } else if (destination === "stdout") {
404
+ deps.out(text);
405
+ probe.say({
406
+ status: "ok",
407
+ label: "wrote the variables to stdout"
408
+ });
409
+ } else await writeToFile(probe, deps, text);
410
+ });
411
+ }
412
+ };
413
+ }
414
+ // src/probes/export-environment-deps.ts
415
+ /** The real pieces `export-environment` runs on: the plugin's own reader, the repo root, this process's stdout and the shared secret-file helpers. */
416
+ function exportEnvironmentDeps(credentials, repoRoot) {
417
+ return {
418
+ read: (environment) => credentials.read(environment),
419
+ repoRoot,
420
+ resolveId: (id, root) => resolveEnvironmentId(id, { repoRoot: root }),
421
+ outKind: () => stdoutKind(process.stdout.isTTY === true),
422
+ out: (text) => void process.stdout.write(text),
423
+ copy: copyToClipboard,
424
+ writeFile: writeCredentialsFile,
425
+ shred: shredFile,
426
+ isIgnored: isGitIgnored,
427
+ platform: process.platform,
428
+ interactive: process.stdin.isTTY === true
429
+ };
430
+ }
267
431
  // src/shell.ts
268
432
  /**
269
433
  * Thin wrapper around `spawnSync` for `op` CLI shell-outs.
@@ -403,16 +567,19 @@ function vault(ctx) {
403
567
  function credentials(ctx) {
404
568
  return new OpCredentials(ctx.shell);
405
569
  }
406
- /** Creates the 1Password plugin: its `connection` probe and the `vault` and `credentials` capabilities. */
570
+ /** Creates the 1Password plugin: its `connection` and `export-environment` probes and the `vault` and `credentials` capabilities. */
407
571
  function createPlugin(options = {}) {
408
572
  const ctx = createContext(options);
409
573
  return {
410
574
  name: "@theholocron/holocron-plugin-1password",
411
- probes: { connection: connectionProbe({
412
- provider: "1Password",
413
- token: () => "",
414
- verify: (token) => verifyToken(token, { spawn: options.spawn })
415
- }) },
575
+ probes: {
576
+ connection: connectionProbe({
577
+ provider: "1Password",
578
+ token: () => "",
579
+ verify: (token) => verifyToken(token, { spawn: options.spawn })
580
+ }),
581
+ "export-environment": exportEnvironmentProbe(exportEnvironmentDeps(new OpCredentials(ctx.shell), options.repoRoot ?? process.cwd()))
582
+ },
416
583
  capabilities: {
417
584
  vault: () => vault(ctx),
418
585
  credentials: () => credentials(ctx)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-1password",
3
- "version": "5.0.0-alpha.415",
3
+ "version": "5.0.0-alpha.416",
4
4
  "description": "Holocron plugin for 1Password. Implements the vault capability via shell-out to the `op` CLI (https://developer.1password.com/docs/cli).",
5
5
  "keywords": [
6
6
  "1password",
@@ -29,6 +29,9 @@
29
29
  "files": [
30
30
  "dist"
31
31
  ],
32
+ "dependencies": {
33
+ "@theholocron/kyber": "^5.0.0-alpha.416"
34
+ },
32
35
  "devDependencies": {
33
36
  "@types/node": "^26",
34
37
  "@vitest/coverage-v8": "^5.1.0",
@@ -40,14 +43,14 @@
40
43
  "tsx": "4.23.15",
41
44
  "typescript": "^5.9.3",
42
45
  "vitest": "^5.1.0",
43
- "@theholocron/eslint-config": "^8.9.0-alpha.16",
44
- "@theholocron/cli": "5.0.0-alpha.415",
45
- "@theholocron/tsconfig": "^8.9.0-alpha.16",
46
+ "@theholocron/cli": "5.0.0-alpha.416",
46
47
  "@theholocron/tsdown-config": "^8.9.0-alpha.16",
47
- "@theholocron/vitest-config": "^8.9.0-alpha.16"
48
+ "@theholocron/tsconfig": "^8.9.0-alpha.16",
49
+ "@theholocron/vitest-config": "^8.9.0-alpha.16",
50
+ "@theholocron/eslint-config": "^8.9.0-alpha.16"
48
51
  },
49
52
  "peerDependencies": {
50
- "@theholocron/cli": "5.0.0-alpha.415"
53
+ "@theholocron/cli": "5.0.0-alpha.416"
51
54
  },
52
55
  "engines": {
53
56
  "node": ">=22"