@till-ts/1password 0.0.0-stage → 0.1.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 +25 -2
- package/dist/index.d.ts +40 -0
- package/dist/index.js +169 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Gaudet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @till-ts/1password
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The 1Password store for Till: `op://<vault>/<item>/[<section>/]<field>` references, read with the 1Password CLI (`op`). It never writes.
|
|
4
|
+
|
|
5
|
+
Part of [Till](https://joegaudet.com/till/), one manifest for the secrets and the settings of an
|
|
6
|
+
app. Part of Ember Workbench.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pnpm add @till-ts/1password
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Use
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { onePassword } from '@till-ts/1password'
|
|
18
|
+
|
|
19
|
+
const providers = { op: onePassword({ concurrency: 4 }) }
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The guide: <https://joegaudet.com/till/guide/stores.html>
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { OpReference, Provider, StoreError } from '@till-ts/core';
|
|
2
|
+
export interface RunResult {
|
|
3
|
+
code: number;
|
|
4
|
+
stdout: string;
|
|
5
|
+
stderr: string;
|
|
6
|
+
}
|
|
7
|
+
/** Runs the op CLI. A test gives a fake. */
|
|
8
|
+
export type OpRunner = (args: string[], env: Record<string, string | undefined>) => Promise<RunResult>;
|
|
9
|
+
export interface OnePasswordOptions {
|
|
10
|
+
/** The op binary. Default: `op` on the PATH. */
|
|
11
|
+
bin?: string;
|
|
12
|
+
run?: OpRunner;
|
|
13
|
+
/** How many `op` processes at one time. Default 4. */
|
|
14
|
+
concurrency?: number;
|
|
15
|
+
}
|
|
16
|
+
interface OpField {
|
|
17
|
+
id?: string;
|
|
18
|
+
label?: string;
|
|
19
|
+
value?: string;
|
|
20
|
+
section?: {
|
|
21
|
+
id?: string;
|
|
22
|
+
label?: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
interface OpItem {
|
|
26
|
+
id?: string;
|
|
27
|
+
version?: number;
|
|
28
|
+
fields?: OpField[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The store error for a failed `op` call. The text of the CLI is matched but not copied, so no
|
|
32
|
+
* value can get into a message.
|
|
33
|
+
*/
|
|
34
|
+
export declare function opError(result: RunResult, what: string): StoreError;
|
|
35
|
+
/** The field of an item for a reference: by label or id, in the section if there is one. */
|
|
36
|
+
export declare function findField(item: OpItem, ref: OpReference): OpField[];
|
|
37
|
+
export declare function onePassword(options?: OnePasswordOptions): Provider;
|
|
38
|
+
/** The 1Password provider with the op CLI on the PATH. */
|
|
39
|
+
export declare const provider: Provider;
|
|
40
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { storeError, groupBy, mapLimit } from '@till-ts/core';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
function defaultRunner(bin) {
|
|
6
|
+
return (args, env) => new Promise((resolve) => {
|
|
7
|
+
const child = spawn(bin, args, {
|
|
8
|
+
env,
|
|
9
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
10
|
+
});
|
|
11
|
+
const out = [];
|
|
12
|
+
const err = [];
|
|
13
|
+
child.stdout.on("data", (d) => out.push(d));
|
|
14
|
+
child.stderr.on("data", (d) => err.push(d));
|
|
15
|
+
child.on(
|
|
16
|
+
"error",
|
|
17
|
+
(e) => resolve({
|
|
18
|
+
code: e.code === "ENOENT" ? 127 : 1,
|
|
19
|
+
stdout: "",
|
|
20
|
+
stderr: e.code ?? "spawn error"
|
|
21
|
+
})
|
|
22
|
+
);
|
|
23
|
+
child.on(
|
|
24
|
+
"close",
|
|
25
|
+
(code) => resolve({
|
|
26
|
+
code: code ?? 1,
|
|
27
|
+
stdout: Buffer.concat(out).toString("utf8"),
|
|
28
|
+
stderr: Buffer.concat(err).toString("utf8")
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function opError(result, what) {
|
|
34
|
+
const text = result.stderr.toLowerCase();
|
|
35
|
+
if (result.code === 127 || text.includes("enoent"))
|
|
36
|
+
return storeError(
|
|
37
|
+
"unavailable",
|
|
38
|
+
"the 1Password CLI (op) is not installed. Install it, then run `op signin`"
|
|
39
|
+
);
|
|
40
|
+
if (/locked|unlock/.test(text)) return storeError("locked", "the 1Password app is locked");
|
|
41
|
+
if (/not (currently )?signed in|no accounts configured|session expired|sign in|authorization prompt dismissed|authentication|unauthorized|invalid token/.test(
|
|
42
|
+
text
|
|
43
|
+
))
|
|
44
|
+
return storeError(
|
|
45
|
+
"unauthenticated",
|
|
46
|
+
"op is not signed in. Run `op signin`, or set OP_SERVICE_ACCOUNT_TOKEN"
|
|
47
|
+
);
|
|
48
|
+
if (/isn't a vault|vault .*not found|no vault/.test(text))
|
|
49
|
+
return storeError("not_found", `${what}: the vault is not found`);
|
|
50
|
+
if (/isn't an item|item .*not found|no item|not found/.test(text))
|
|
51
|
+
return storeError("not_found", `${what}: the item is not found`);
|
|
52
|
+
if (/permission|forbidden|access denied|not authorized/.test(text))
|
|
53
|
+
return storeError("access_denied", `${what}: access is denied`);
|
|
54
|
+
return storeError("unavailable", `op failed for ${what} (exit code ${result.code})`);
|
|
55
|
+
}
|
|
56
|
+
var lower = (s) => (s ?? "").toLowerCase();
|
|
57
|
+
function findField(item, ref) {
|
|
58
|
+
return (item.fields ?? []).filter((field) => {
|
|
59
|
+
const nameMatch = lower(field.label) === lower(ref.field) || field.id === ref.field;
|
|
60
|
+
if (!nameMatch) return false;
|
|
61
|
+
if (ref.section === null) return true;
|
|
62
|
+
return lower(field.section?.label) === lower(ref.section) || field.section?.id === ref.section;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function onePassword(options = {}) {
|
|
66
|
+
const run = options.run ?? defaultRunner(options.bin ?? "op");
|
|
67
|
+
const concurrency = options.concurrency ?? 4;
|
|
68
|
+
async function getItems(references, context) {
|
|
69
|
+
const refs = references;
|
|
70
|
+
const groups = groupBy(refs, (r) => JSON.stringify([r.vault, r.item]));
|
|
71
|
+
const results = /* @__PURE__ */ new Map();
|
|
72
|
+
await mapLimit([...groups.entries()], concurrency, async ([group, members]) => {
|
|
73
|
+
const { vault, item } = members[0];
|
|
74
|
+
const args = ["item", "get", item, "--vault", vault, "--format", "json"];
|
|
75
|
+
if (context.op?.account) args.push("--account", context.op.account);
|
|
76
|
+
const result = await run(args, context.env);
|
|
77
|
+
const what = `item "${item}" in vault "${vault}"`;
|
|
78
|
+
if (result.code !== 0) {
|
|
79
|
+
results.set(group, { error: opError(result, what) });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
results.set(group, { item: JSON.parse(result.stdout) });
|
|
84
|
+
} catch {
|
|
85
|
+
results.set(group, {
|
|
86
|
+
error: storeError("invalid", `op gave output that is not JSON for ${what}`)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return results;
|
|
91
|
+
}
|
|
92
|
+
async function resolve(references, context) {
|
|
93
|
+
const items = await getItems(references, context);
|
|
94
|
+
const out = /* @__PURE__ */ new Map();
|
|
95
|
+
for (const reference of references) {
|
|
96
|
+
const got = items.get(JSON.stringify([reference.vault, reference.item]));
|
|
97
|
+
if ("error" in got) {
|
|
98
|
+
out.set(reference.raw, { ok: false, error: got.error });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const fields = findField(got.item, reference);
|
|
102
|
+
const where = `field "${reference.field}" of item "${reference.item}"`;
|
|
103
|
+
if (fields.length === 0)
|
|
104
|
+
out.set(reference.raw, {
|
|
105
|
+
ok: false,
|
|
106
|
+
error: storeError("not_found", `${where} is not found`)
|
|
107
|
+
});
|
|
108
|
+
else if (fields.length > 1)
|
|
109
|
+
out.set(reference.raw, {
|
|
110
|
+
ok: false,
|
|
111
|
+
error: storeError(
|
|
112
|
+
"invalid",
|
|
113
|
+
`more than one field matches ${where}: add the section to the reference`
|
|
114
|
+
)
|
|
115
|
+
});
|
|
116
|
+
else
|
|
117
|
+
out.set(reference.raw, {
|
|
118
|
+
ok: true,
|
|
119
|
+
value: fields[0].value ?? "",
|
|
120
|
+
version: `${got.item.id ?? reference.item}@${got.item.version ?? 0}`
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
scheme: "op",
|
|
127
|
+
name: "1Password",
|
|
128
|
+
resolve,
|
|
129
|
+
// 1Password has no call that gives the version without the values, so the watcher needs
|
|
130
|
+
// read access to the items (ADR-0002). The values are dropped here.
|
|
131
|
+
async describe(references, context) {
|
|
132
|
+
const resolved = await resolve(references, context);
|
|
133
|
+
const out = /* @__PURE__ */ new Map();
|
|
134
|
+
for (const [raw, r] of resolved) out.set(raw, r.ok ? { ok: true, version: r.version } : r);
|
|
135
|
+
return out;
|
|
136
|
+
},
|
|
137
|
+
async doctor(context) {
|
|
138
|
+
const version = await run(["--version"], context.env);
|
|
139
|
+
if (version.code !== 0)
|
|
140
|
+
return [
|
|
141
|
+
{
|
|
142
|
+
ok: false,
|
|
143
|
+
label: "1Password CLI (op)",
|
|
144
|
+
hint: "Install the 1Password CLI: https://developer.1password.com/docs/cli/get-started/"
|
|
145
|
+
}
|
|
146
|
+
];
|
|
147
|
+
const checks = [
|
|
148
|
+
{ ok: true, label: `1Password CLI (op ${version.stdout.trim()})` }
|
|
149
|
+
];
|
|
150
|
+
const whoami = await run(["whoami", "--format", "json"], context.env);
|
|
151
|
+
checks.push(
|
|
152
|
+
whoami.code === 0 ? {
|
|
153
|
+
ok: true,
|
|
154
|
+
label: context.env.OP_SERVICE_ACCOUNT_TOKEN ? "op: signed in with a service account" : "op: signed in"
|
|
155
|
+
} : {
|
|
156
|
+
ok: false,
|
|
157
|
+
label: "op: not signed in",
|
|
158
|
+
hint: "Run `op signin` (or turn on the app integration), or set OP_SERVICE_ACCOUNT_TOKEN in CI"
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
return checks;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
var provider = onePassword();
|
|
166
|
+
|
|
167
|
+
export { findField, onePassword, opError, provider };
|
|
168
|
+
//# sourceMappingURL=index.js.map
|
|
169
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAkDA,SAAS,cAAc,GAAA,EAAuB;AAC5C,EAAA,OAAO,CAAC,IAAA,EAAM,GAAA,KACZ,IAAI,QAAQ,CAAA,OAAA,KAAW;AACrB,IAAA,MAAM,KAAA,GAAQ,KAAA,CAAM,GAAA,EAAK,IAAA,EAAM;AAAA,MAC7B,GAAA;AAAA,MACA,KAAA,EAAO,CAAC,QAAA,EAAU,MAAA,EAAQ,MAAM;AAAA,KACjC,CAAA;AACD,IAAA,MAAM,MAAgB,EAAC;AACvB,IAAA,MAAM,MAAgB,EAAC;AACvB,IAAA,KAAA,CAAM,MAAA,CAAO,GAAG,MAAA,EAAQ,CAAC,MAAc,GAAA,CAAI,IAAA,CAAK,CAAC,CAAC,CAAA;AAClD,IAAA,KAAA,CAAM,MAAA,CAAO,GAAG,MAAA,EAAQ,CAAC,MAAc,GAAA,CAAI,IAAA,CAAK,CAAC,CAAC,CAAA;AAClD,IAAA,KAAA,CAAM,EAAA;AAAA,MAAG,OAAA;AAAA,MAAS,CAAC,MACjB,OAAA,CAAQ;AAAA,QACN,IAAA,EAAM,CAAA,CAAE,IAAA,KAAS,QAAA,GAAW,GAAA,GAAM,CAAA;AAAA,QAClC,MAAA,EAAQ,EAAA;AAAA,QACR,MAAA,EAAQ,EAAE,IAAA,IAAQ;AAAA,OACnB;AAAA,KACH;AACA,IAAA,KAAA,CAAM,EAAA;AAAA,MAAG,OAAA;AAAA,MAAS,UAChB,OAAA,CAAQ;AAAA,QACN,MAAM,IAAA,IAAQ,CAAA;AAAA,QACd,QAAQ,M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A;AACpE,MAAA,MAAA,CAAO,IAAA;AAAA,QACL,MAAA,CAAO,SAAS,CAAA,GACZ;AAAA,UACE,EAAA,EAAI,IAAA;AAAA,UACJ,KAAA,EAAO,OAAA,CAAQ,GAAA,CAAI,wBAAA,GACf,sCAAA,GACA;AAAA,SACN,GACA;AAAA,UACE,EAAA,EAAI,KAAA;AAAA,UACJ,KAAA,EAAO,mBAAA;AAAA,UACP,IAAA,EAAM;AAAA;AACR,OACN;AACA,MAAA,OAAO,MAAA;AAAA,IACT;AAAA,GACF;AACF;AAGO,IAAM,WAAqB,WAAA","file":"index.js","sourcesContent":["// The 1Password store: op:// references, read with the 1Password CLI (`op`). One `op item get`\n// reads each item, so the fields of one item cost one call. The CLI uses the desktop app\n// integration on a developer's computer, and OP_SERVICE_ACCOUNT_TOKEN in CI.\nimport { spawn } from 'node:child_process'\n\nimport type {\n Description,\n DoctorCheck,\n OpReference,\n Provider,\n ProviderContext,\n Reference,\n Resolution,\n StoreError,\n} from '@till-ts/core'\nimport { groupBy, mapLimit, storeError } from '@till-ts/core'\n\nexport interface RunResult {\n code: number\n stdout: string\n stderr: string\n}\n\n/** Runs the op CLI. A test gives a fake. */\nexport type OpRunner = (\n args: string[],\n env: Record<string, string | undefined>,\n) => Promise<RunResult>\n\nexport interface OnePasswordOptions {\n /** The op binary. Default: `op` on the PATH. */\n bin?: string\n run?: OpRunner\n /** How many `op` processes at one time. Default 4. */\n concurrency?: number\n}\n\ninterface OpField {\n id?: string\n label?: string\n value?: string\n section?: { id?: string; label?: string }\n}\n\ninterface OpItem {\n id?: string\n version?: number\n fields?: OpField[]\n}\n\nfunction defaultRunner(bin: string): OpRunner {\n return (args, env) =>\n new Promise(resolve => {\n const child = spawn(bin, args, {\n env: env as NodeJS.ProcessEnv,\n stdio: ['ignore', 'pipe', 'pipe'],\n })\n const out: Buffer[] = []\n const err: Buffer[] = []\n child.stdout.on('data', (d: Buffer) => out.push(d))\n child.stderr.on('data', (d: Buffer) => err.push(d))\n child.on('error', (e: NodeJS.ErrnoException) =>\n resolve({\n code: e.code === 'ENOENT' ? 127 : 1,\n stdout: '',\n stderr: e.code ?? 'spawn error',\n }),\n )\n child.on('close', code =>\n resolve({\n code: code ?? 1,\n stdout: Buffer.concat(out).toString('utf8'),\n stderr: Buffer.concat(err).toString('utf8'),\n }),\n )\n })\n}\n\n/**\n * The store error for a failed `op` call. The text of the CLI is matched but not copied, so no\n * value can get into a message.\n */\nexport function opError(result: RunResult, what: string): StoreError {\n const text = result.stderr.toLowerCase()\n if (result.code === 127 || text.includes('enoent'))\n return storeError(\n 'unavailable',\n 'the 1Password CLI (op) is not installed. Install it, then run `op signin`',\n )\n if (/locked|unlock/.test(text)) return storeError('locked', 'the 1Password app is locked')\n if (\n /not (currently )?signed in|no accounts configured|session expired|sign in|authorization prompt dismissed|authentication|unauthorized|invalid token/.test(\n text,\n )\n )\n return storeError(\n 'unauthenticated',\n 'op is not signed in. Run `op signin`, or set OP_SERVICE_ACCOUNT_TOKEN',\n )\n if (/isn't a vault|vault .*not found|no vault/.test(text))\n return storeError('not_found', `${what}: the vault is not found`)\n if (/isn't an item|item .*not found|no item|not found/.test(text))\n return storeError('not_found', `${what}: the item is not found`)\n if (/permission|forbidden|access denied|not authorized/.test(text))\n return storeError('access_denied', `${what}: access is denied`)\n return storeError('unavailable', `op failed for ${what} (exit code ${result.code})`)\n}\n\nconst lower = (s: string | undefined) => (s ?? '').toLowerCase()\n\n/** The field of an item for a reference: by label or id, in the section if there is one. */\nexport function findField(item: OpItem, ref: OpReference): OpField[] {\n return (item.fields ?? []).filter(field => {\n const nameMatch = lower(field.label) === lower(ref.field) || field.id === ref.field\n if (!nameMatch) return false\n if (ref.section === null) return true\n return lower(field.section?.label) === lower(ref.section) || field.section?.id === ref.section\n })\n}\n\nexport function onePassword(options: OnePasswordOptions = {}): Provider {\n const run = options.run ?? defaultRunner(options.bin ?? 'op')\n const concurrency = options.concurrency ?? 4\n\n async function getItems(\n references: Reference[],\n context: ProviderContext,\n ): Promise<Map<string, { item: OpItem } | { error: StoreError }>> {\n const refs = references as (OpReference & { raw: string })[]\n const groups = groupBy(refs, r => JSON.stringify([r.vault, r.item]))\n const results = new Map<string, { item: OpItem } | { error: StoreError }>()\n await mapLimit([...groups.entries()], concurrency, async ([group, members]) => {\n const { vault, item } = members[0]!\n const args = ['item', 'get', item, '--vault', vault, '--format', 'json']\n if (context.op?.account) args.push('--account', context.op.account)\n const result = await run(args, context.env)\n const what = `item \"${item}\" in vault \"${vault}\"`\n if (result.code !== 0) {\n results.set(group, { error: opError(result, what) })\n return\n }\n try {\n results.set(group, { item: JSON.parse(result.stdout) as OpItem })\n } catch {\n results.set(group, {\n error: storeError('invalid', `op gave output that is not JSON for ${what}`),\n })\n }\n })\n return results\n }\n\n async function resolve(references: Reference[], context: ProviderContext) {\n const items = await getItems(references, context)\n const out = new Map<string, Resolution>()\n for (const reference of references as (OpReference & { raw: string })[]) {\n const got = items.get(JSON.stringify([reference.vault, reference.item]))!\n if ('error' in got) {\n out.set(reference.raw, { ok: false, error: got.error })\n continue\n }\n const fields = findField(got.item, reference)\n const where = `field \"${reference.field}\" of item \"${reference.item}\"`\n if (fields.length === 0)\n out.set(reference.raw, {\n ok: false,\n error: storeError('not_found', `${where} is not found`),\n })\n else if (fields.length > 1)\n out.set(reference.raw, {\n ok: false,\n error: storeError(\n 'invalid',\n `more than one field matches ${where}: add the section to the reference`,\n ),\n })\n else\n out.set(reference.raw, {\n ok: true,\n value: fields[0]!.value ?? '',\n version: `${got.item.id ?? reference.item}@${got.item.version ?? 0}`,\n })\n }\n return out\n }\n\n return {\n scheme: 'op',\n name: '1Password',\n resolve,\n\n // 1Password has no call that gives the version without the values, so the watcher needs\n // read access to the items (ADR-0002). The values are dropped here.\n async describe(references, context) {\n const resolved = await resolve(references, context)\n const out = new Map<string, Description>()\n for (const [raw, r] of resolved) out.set(raw, r.ok ? { ok: true, version: r.version } : r)\n return out\n },\n\n async doctor(context): Promise<DoctorCheck[]> {\n const version = await run(['--version'], context.env)\n if (version.code !== 0)\n return [\n {\n ok: false,\n label: '1Password CLI (op)',\n hint: 'Install the 1Password CLI: https://developer.1password.com/docs/cli/get-started/',\n },\n ]\n const checks: DoctorCheck[] = [\n { ok: true, label: `1Password CLI (op ${version.stdout.trim()})` },\n ]\n const whoami = await run(['whoami', '--format', 'json'], context.env)\n checks.push(\n whoami.code === 0\n ? {\n ok: true,\n label: context.env.OP_SERVICE_ACCOUNT_TOKEN\n ? 'op: signed in with a service account'\n : 'op: signed in',\n }\n : {\n ok: false,\n label: 'op: not signed in',\n hint: 'Run `op signin` (or turn on the app integration), or set OP_SERVICE_ACCOUNT_TOKEN in CI',\n },\n )\n return checks\n },\n }\n}\n\n/** The 1Password provider with the op CLI on the PATH. */\nexport const provider: Provider = onePassword()\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@till-ts/1password",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The 1Password store for Till: op:// references, read with the op CLI.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"till",
|
|
7
|
+
"secrets",
|
|
8
|
+
"env",
|
|
9
|
+
"1password",
|
|
10
|
+
"op",
|
|
11
|
+
"secrets"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Joe Gaudet joe@joegaudet.com",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/joegaudet/till.git",
|
|
18
|
+
"directory": "packages/1password"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://joegaudet.com/till/",
|
|
21
|
+
"bugs": "https://github.com/joegaudet/till/issues",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@till-ts/core": "0.1.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=22.12.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup",
|
|
48
|
+
"test": "vitest run"
|
|
49
|
+
}
|
|
6
50
|
}
|