@sanity/blueprints 0.27.0 → 0.28.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/README.md +22 -0
- package/dist/definers/functions/durable.d.ts +0 -1
- package/dist/definers/functions/durable.js +3 -4
- package/dist/definers/functions/durable.js.map +1 -1
- package/dist/definers/functions/queue.js +3 -3
- package/dist/definers/functions/queue.js.map +1 -1
- package/dist/resolve/by-name.d.ts +7 -0
- package/dist/resolve/by-name.js +13 -0
- package/dist/resolve/by-name.js.map +1 -0
- package/dist/resolve/import.d.ts +16 -0
- package/dist/resolve/import.js +48 -0
- package/dist/resolve/import.js.map +1 -0
- package/dist/resolve/index.d.ts +18 -0
- package/dist/resolve/index.js +19 -0
- package/dist/resolve/index.js.map +1 -0
- package/dist/resolve/load.d.ts +53 -0
- package/dist/resolve/load.js +155 -0
- package/dist/resolve/load.js.map +1 -0
- package/dist/resolve/stack-config.d.ts +49 -0
- package/dist/resolve/stack-config.js +108 -0
- package/dist/resolve/stack-config.js.map +1 -0
- package/dist/resolve/stack.d.ts +23 -0
- package/dist/resolve/stack.js +91 -0
- package/dist/resolve/stack.js.map +1 -0
- package/dist/resolve/types.d.ts +165 -0
- package/dist/resolve/types.js +2 -0
- package/dist/resolve/types.js.map +1 -0
- package/dist/types/functions/index.d.ts +58 -10
- package/dist/utils/debounce.d.ts +20 -0
- package/dist/utils/debounce.js +46 -0
- package/dist/utils/debounce.js.map +1 -0
- package/dist/validation/functions.d.ts +9 -0
- package/dist/validation/functions.js +66 -9
- package/dist/validation/functions.js.map +1 -1
- package/package.json +16 -7
- package/src/definers/functions/durable.ts +3 -4
- package/src/definers/functions/queue.ts +3 -3
- package/src/resolve/by-name.ts +11 -0
- package/src/resolve/import.ts +52 -0
- package/src/resolve/index.ts +33 -0
- package/src/resolve/load.ts +166 -0
- package/src/resolve/stack-config.ts +116 -0
- package/src/resolve/stack.ts +100 -0
- package/src/resolve/types.ts +179 -0
- package/src/types/functions/index.ts +70 -19
- package/src/utils/debounce.ts +53 -0
- package/src/validation/functions.ts +78 -12
package/README.md
CHANGED
|
@@ -76,3 +76,25 @@ export default defineBlueprint({
|
|
|
76
76
|
| `defineRobotToken` β | Robot token for automated access |
|
|
77
77
|
|
|
78
78
|
Each definer validates its input at call time and returns a typed resource object. See the [reference docs](https://reference.sanity.io/_sanity/blueprints) for full configuration details and additional resource types.
|
|
79
|
+
|
|
80
|
+
## Reading a blueprint and its Stack
|
|
81
|
+
|
|
82
|
+
The Sanity CLI handles this for you. For scripts and tools, `@sanity/blueprints/resolve` reads a local blueprint and its deployed Stack.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import {readBlueprint, readStack} from '@sanity/blueprints/resolve'
|
|
86
|
+
|
|
87
|
+
const blueprint = await readBlueprint()
|
|
88
|
+
|
|
89
|
+
blueprint.path // '/path/to/project/sanity.blueprint.ts'
|
|
90
|
+
blueprint.resourcesByName['production-dataset']?.type // 'sanity.project.dataset'
|
|
91
|
+
blueprint.stackId // 'ST-abc123'
|
|
92
|
+
|
|
93
|
+
const {stack, resourcesByName, values} = await readStack({token: process.env.SANITY_AUTH_TOKEN})
|
|
94
|
+
|
|
95
|
+
stack.name // the Stack's name
|
|
96
|
+
resourcesByName['my-function']?.externalId // a function's external ID
|
|
97
|
+
values['production-dataset']?.name // the deployed dataset's name
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
See the [reference docs](https://reference.sanity.io/_sanity/blueprints) for options.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { validateDurableFunction } from '../../index.js';
|
|
2
|
+
import { createDebounceObject } from '../../utils/debounce.js';
|
|
2
3
|
import { parseDuration } from '../../utils/parse-duration.js';
|
|
3
4
|
import { runValidation } from '../../utils/validation.js';
|
|
4
5
|
import { defineFunction } from './index.js';
|
|
@@ -13,7 +14,6 @@ import { defineFunction } from './index.js';
|
|
|
13
14
|
* event: {type: 'document', on: ['create'], filter: "_type == 'post'"},
|
|
14
15
|
* concurrency: 5,
|
|
15
16
|
* debounce: 10,
|
|
16
|
-
* debounceKey: 'document._id',
|
|
17
17
|
* })
|
|
18
18
|
* ```
|
|
19
19
|
*
|
|
@@ -27,15 +27,14 @@ import { defineFunction } from './index.js';
|
|
|
27
27
|
* @returns The validated durable function resource
|
|
28
28
|
*/
|
|
29
29
|
export function defineDurableFunction(functionConfig) {
|
|
30
|
-
const { name, event, concurrency, debounce,
|
|
30
|
+
const { name, event, concurrency, debounce, src, durableTimeout } = functionConfig;
|
|
31
31
|
const functionResource = {
|
|
32
32
|
...defineFunction({ ...functionConfig, src: src ?? `functions/${name}` }, { skipValidation: true }),
|
|
33
33
|
type: 'sanity.function.durable',
|
|
34
34
|
...(event !== undefined && { event }),
|
|
35
35
|
...(durableTimeout !== undefined && { durableTimeout: parseDuration(durableTimeout, 's') }),
|
|
36
36
|
...(concurrency !== undefined && { concurrency }),
|
|
37
|
-
...(debounce !== undefined && { debounce }),
|
|
38
|
-
...(debounceKey !== undefined && { debounceKey }),
|
|
37
|
+
...(debounce !== undefined && { debounce: createDebounceObject(debounce) }),
|
|
39
38
|
};
|
|
40
39
|
runValidation(() => validateDurableFunction(functionResource));
|
|
41
40
|
return functionResource;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable.js","sourceRoot":"","sources":["../../../src/definers/functions/durable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqE,uBAAuB,EAAC,MAAM,gBAAgB,CAAA;AAC1H,OAAO,EAAC,aAAa,EAAC,MAAM,+BAA+B,CAAA;AAC3D,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAA;AAEzC
|
|
1
|
+
{"version":3,"file":"durable.js","sourceRoot":"","sources":["../../../src/definers/functions/durable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqE,uBAAuB,EAAC,MAAM,gBAAgB,CAAA;AAC1H,OAAO,EAAC,oBAAoB,EAAC,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAC,aAAa,EAAC,MAAM,+BAA+B,CAAA;AAC3D,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAA;AAEzC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,qBAAqB,CAAC,cAAsC;IAC1E,MAAM,EAAC,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,EAAE,cAAc,EAAC,GAAG,cAAc,CAAA;IAChF,MAAM,gBAAgB,GAAqC;QACzD,GAAG,cAAc,CAAC,EAAC,GAAG,cAAc,EAAE,GAAG,EAAE,GAAG,IAAI,aAAa,IAAI,EAAE,EAAC,EAAE,EAAC,cAAc,EAAE,IAAI,EAAC,CAAC;QAC/F,IAAI,EAAE,yBAAyB;QAC/B,GAAG,CAAC,KAAK,KAAK,SAAS,IAAI,EAAC,KAAK,EAAC,CAAC;QACnC,GAAG,CAAC,cAAc,KAAK,SAAS,IAAI,EAAC,cAAc,EAAE,aAAa,CAAC,cAAc,EAAE,GAAG,CAAC,EAAC,CAAC;QACzF,GAAG,CAAC,WAAW,KAAK,SAAS,IAAI,EAAC,WAAW,EAAC,CAAC;QAC/C,GAAG,CAAC,QAAQ,KAAK,SAAS,IAAI,EAAC,QAAQ,EAAE,oBAAoB,CAAC,QAAQ,CAAC,EAAC,CAAC;KAC1E,CAAA;IAED,aAAa,CAAC,GAAG,EAAE,CAAC,uBAAuB,CAAC,gBAAgB,CAAC,CAAC,CAAA;IAC9D,OAAO,gBAAgB,CAAA;AACzB,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { validateQueueFunction } from '../../index.js';
|
|
2
|
+
import { createDebounceObject } from '../../utils/debounce.js';
|
|
2
3
|
import { runValidation } from '../../utils/validation.js';
|
|
3
4
|
import { defineFunction } from './index.js';
|
|
4
5
|
/**
|
|
@@ -30,13 +31,12 @@ import { defineFunction } from './index.js';
|
|
|
30
31
|
* @returns The validated queue function resource
|
|
31
32
|
*/
|
|
32
33
|
export function defineQueueFunction(functionConfig) {
|
|
33
|
-
const { concurrency = 1, fifo = true, dlq = true, debounce,
|
|
34
|
+
const { concurrency = 1, fifo = true, dlq = true, debounce, event } = functionConfig;
|
|
34
35
|
const functionResource = {
|
|
35
36
|
...defineFunction(functionConfig, { skipValidation: true }),
|
|
36
37
|
type: 'sanity.function.queue',
|
|
37
38
|
...(concurrency !== undefined && { concurrency }),
|
|
38
|
-
...(debounce !== undefined && { debounce }),
|
|
39
|
-
...(debounceKey !== undefined && { debounceKey }),
|
|
39
|
+
...(debounce !== undefined && { debounce: createDebounceObject(debounce) }),
|
|
40
40
|
...(dlq !== undefined && { dlq }),
|
|
41
41
|
...(event !== undefined && { event }),
|
|
42
42
|
...(fifo !== undefined && { fifo }),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../src/definers/functions/queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyE,qBAAqB,EAAC,MAAM,gBAAgB,CAAA;AAC5H,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAA;AACzC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,mBAAmB,CAAC,cAA4C;IAC9E,MAAM,EAAC,WAAW,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,GAAG,GAAG,IAAI,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../src/definers/functions/queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyE,qBAAqB,EAAC,MAAM,gBAAgB,CAAA;AAC5H,OAAO,EAAC,oBAAoB,EAAC,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAC,aAAa,EAAC,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAA;AACzC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,mBAAmB,CAAC,cAA4C;IAC9E,MAAM,EAAC,WAAW,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,GAAG,GAAG,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAC,GAAG,cAAc,CAAA;IAElF,MAAM,gBAAgB,GAAmC;QACvD,GAAG,cAAc,CAAC,cAAc,EAAE,EAAC,cAAc,EAAE,IAAI,EAAC,CAAC;QACzD,IAAI,EAAE,uBAAuB;QAC7B,GAAG,CAAC,WAAW,KAAK,SAAS,IAAI,EAAC,WAAW,EAAC,CAAC;QAC/C,GAAG,CAAC,QAAQ,KAAK,SAAS,IAAI,EAAC,QAAQ,EAAE,oBAAoB,CAAC,QAAQ,CAAC,EAAC,CAAC;QACzE,GAAG,CAAC,GAAG,KAAK,SAAS,IAAI,EAAC,GAAG,EAAC,CAAC;QAC/B,GAAG,CAAC,KAAK,KAAK,SAAS,IAAI,EAAC,KAAK,EAAC,CAAC;QACnC,GAAG,CAAC,IAAI,KAAK,SAAS,IAAI,EAAC,IAAI,EAAC,CAAC;KAClC,CAAA;IAED,aAAa,CAAC,GAAG,EAAE,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,CAAC,CAAA;IAE5D,OAAO,gBAAgB,CAAA;AACzB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key resources by name. A resource without a string `name` is left out, and a later resource with the same name replaces an earlier one.
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export function byName(resources) {
|
|
6
|
+
const keyed = {};
|
|
7
|
+
for (const resource of resources) {
|
|
8
|
+
if (typeof resource?.name === 'string')
|
|
9
|
+
keyed[resource.name] = resource;
|
|
10
|
+
}
|
|
11
|
+
return keyed;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=by-name.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"by-name.js","sourceRoot":"","sources":["../../src/resolve/by-name.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,MAAM,CAA6B,SAAc;IAC/D,MAAM,KAAK,GAAsB,EAAE,CAAA;IACnC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,IAAI,OAAO,QAAQ,EAAE,IAAI,KAAK,QAAQ;YAAE,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAA;IACzE,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BlueprintLoader } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Import a file natively and return its default export
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare function importNative(path: string): Promise<unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Whether jiti may be able to import a file that the native import could not
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function canRetryWithJiti(error: unknown): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* A jiti loader, when jiti resolves from the blueprint file or from this package
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare function findJiti(path: string): Promise<BlueprintLoader | undefined>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
/*
|
|
4
|
+
* Error strings thrown by Node when importing a .ts file.
|
|
5
|
+
* Indicators to attempt again with jiti.
|
|
6
|
+
*/
|
|
7
|
+
const RETRY_WITH_JITI = new Set([
|
|
8
|
+
'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX',
|
|
9
|
+
'ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING',
|
|
10
|
+
'ERR_UNKNOWN_FILE_EXTENSION',
|
|
11
|
+
'ERR_MODULE_NOT_FOUND',
|
|
12
|
+
]);
|
|
13
|
+
/**
|
|
14
|
+
* Import a file natively and return its default export
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export async function importNative(path) {
|
|
18
|
+
const imported = await import(pathToFileURL(path).href);
|
|
19
|
+
return imported.default;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether jiti may be able to import a file that the native import could not
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export function canRetryWithJiti(error) {
|
|
26
|
+
return error instanceof Error && 'code' in error && RETRY_WITH_JITI.has(String(error.code));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A jiti loader, when jiti resolves from the blueprint file or from this package
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export async function findJiti(path) {
|
|
33
|
+
for (const from of [path, import.meta.url]) {
|
|
34
|
+
let resolved;
|
|
35
|
+
try {
|
|
36
|
+
resolved = createRequire(from).resolve('jiti');
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const { createJiti } = await import(pathToFileURL(resolved).href);
|
|
42
|
+
if (typeof createJiti !== 'function')
|
|
43
|
+
continue;
|
|
44
|
+
return (file) => createJiti(file).import(pathToFileURL(file).href, { default: true });
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=import.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import.js","sourceRoot":"","sources":["../../src/resolve/import.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAA;AACzC,OAAO,EAAC,aAAa,EAAC,MAAM,UAAU,CAAA;AAKtC;;;GAGG;AACH,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC;IAC9B,mCAAmC;IACnC,6CAA6C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
3
|
+
*
|
|
4
|
+
* A Blueprint declares resources. A Stack is its deployed counterpart.
|
|
5
|
+
* {@link readBlueprint} reads local blueprint.
|
|
6
|
+
* {@link readStack} reads remote stack.
|
|
7
|
+
*
|
|
8
|
+
* This entry point imports Node built-ins, so it is separate from `@sanity/blueprints`.
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @categoryDescription Resolve
|
|
13
|
+
* These functions read a blueprint from disk, resolve the Stack it deploys to, and read that Stack from the Blueprints API.
|
|
14
|
+
*/
|
|
15
|
+
export { BLUEPRINT_FILE_NAMES, findBlueprintFile, loadBlueprintFile, readBlueprint, } from './load.js';
|
|
16
|
+
export { DEFAULT_BLUEPRINTS_API_VERSION, readStack, } from './stack.js';
|
|
17
|
+
export { readStackConfigFile, resolveStackConfig, STACK_CONFIG_DIR, STACK_CONFIG_FILE, stackIdsFromEnv, } from './stack-config.js';
|
|
18
|
+
export * from './types.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sanity/blueprints/resolve`
|
|
3
|
+
*
|
|
4
|
+
* A Blueprint declares resources. A Stack is its deployed counterpart.
|
|
5
|
+
* {@link readBlueprint} reads local blueprint.
|
|
6
|
+
* {@link readStack} reads remote stack.
|
|
7
|
+
*
|
|
8
|
+
* This entry point imports Node built-ins, so it is separate from `@sanity/blueprints`.
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @categoryDescription Resolve
|
|
13
|
+
* These functions read a blueprint from disk, resolve the Stack it deploys to, and read that Stack from the Blueprints API.
|
|
14
|
+
*/
|
|
15
|
+
export { BLUEPRINT_FILE_NAMES, findBlueprintFile, loadBlueprintFile, readBlueprint, } from './load.js';
|
|
16
|
+
export { DEFAULT_BLUEPRINTS_API_VERSION, readStack, } from './stack.js';
|
|
17
|
+
export { readStackConfigFile, resolveStackConfig, STACK_CONFIG_DIR, STACK_CONFIG_FILE, stackIdsFromEnv, } from './stack-config.js';
|
|
18
|
+
export * from './types.js';
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/resolve/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;GAGG;AACH,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,aAAa,GACd,MAAM,WAAW,CAAA;AAClB,OAAO,EACL,8BAA8B,EAC9B,SAAS,GACV,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { BlueprintInfo, LoadBlueprintFileOptions, LoadedBlueprintFile, ReadBlueprintOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The file names {@link findBlueprintFile} looks for, in order
|
|
4
|
+
* @internal
|
|
5
|
+
* @private
|
|
6
|
+
* @category Resolve
|
|
7
|
+
*/
|
|
8
|
+
export declare const BLUEPRINT_FILE_NAMES: string[];
|
|
9
|
+
/**
|
|
10
|
+
* Find a blueprint file
|
|
11
|
+
*
|
|
12
|
+
* Returns a file path as is. Searches a directory, then its parents, for {@link BLUEPRINT_FILE_NAMES}.
|
|
13
|
+
*
|
|
14
|
+
* @param path A blueprint file or directory. Defaults to the current working directory.
|
|
15
|
+
* @returns The absolute path of the file, or `undefined` when none is found
|
|
16
|
+
* @internal
|
|
17
|
+
* @private
|
|
18
|
+
* @category Resolve
|
|
19
|
+
*/
|
|
20
|
+
export declare function findBlueprintFile(path?: string): string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Load a blueprint file
|
|
23
|
+
*
|
|
24
|
+
* Parses a JSON file. Imports a JS or TS file and calls its default export.
|
|
25
|
+
* TypeScript that Node cannot run, such as an enum, is loaded with jiti when it is installed.
|
|
26
|
+
* Imported modules are cached, so a later call in the same process does not see changes to the file. Pass a `loader` that skips the cache to re-read it.
|
|
27
|
+
*
|
|
28
|
+
* @param path The blueprint file
|
|
29
|
+
* @throws When the file cannot be loaded or does not produce a blueprint object
|
|
30
|
+
* @internal
|
|
31
|
+
* @private
|
|
32
|
+
* @category Resolve
|
|
33
|
+
*/
|
|
34
|
+
export declare function loadBlueprintFile(path: string, options?: LoadBlueprintFileOptions): Promise<LoadedBlueprintFile>;
|
|
35
|
+
/**
|
|
36
|
+
* Read a local blueprint and the ids of the Stack it deploys to
|
|
37
|
+
*
|
|
38
|
+
* Ids come from `overrides`, the environment, `defineBlueprint`, then `.sanity/blueprint.config.json`.
|
|
39
|
+
* Resources are not validated. No network access.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import {readBlueprint} from '@sanity/blueprints/resolve'
|
|
44
|
+
*
|
|
45
|
+
* const {resources, resourcesByName, stackId, sources} = await readBlueprint()
|
|
46
|
+
* resources.map((resource) => resource.name)
|
|
47
|
+
* resourcesByName['production-dataset']?.type // 'sanity.project.dataset'
|
|
48
|
+
* sources.stackId // 'config'
|
|
49
|
+
* ```
|
|
50
|
+
* @throws When no blueprint file is found, it cannot be loaded, or `resources` is not an array
|
|
51
|
+
* @category Resolve
|
|
52
|
+
*/
|
|
53
|
+
export declare function readBlueprint(options?: ReadBlueprintOptions): Promise<BlueprintInfo>;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, extname, join, resolve } from 'node:path';
|
|
3
|
+
import { cwd } from 'node:process';
|
|
4
|
+
import { byName } from './by-name.js';
|
|
5
|
+
import { canRetryWithJiti, findJiti, importNative } from './import.js';
|
|
6
|
+
import { readStackConfigFile, resolveStackConfig, stackIdsFromEnv } from './stack-config.js';
|
|
7
|
+
const EXTENSIONS = ['.json', '.js', '.mjs', '.ts'];
|
|
8
|
+
const BASE_NAMES = EXTENSIONS.map((ext) => `blueprint${ext}`);
|
|
9
|
+
/**
|
|
10
|
+
* The file names {@link findBlueprintFile} looks for, in order
|
|
11
|
+
* @internal
|
|
12
|
+
* @private
|
|
13
|
+
* @category Resolve
|
|
14
|
+
*/
|
|
15
|
+
export const BLUEPRINT_FILE_NAMES = [...BASE_NAMES, ...BASE_NAMES.map((name) => `sanity.${name}`)];
|
|
16
|
+
/**
|
|
17
|
+
* Find a blueprint file
|
|
18
|
+
*
|
|
19
|
+
* Returns a file path as is. Searches a directory, then its parents, for {@link BLUEPRINT_FILE_NAMES}.
|
|
20
|
+
*
|
|
21
|
+
* @param path A blueprint file or directory. Defaults to the current working directory.
|
|
22
|
+
* @returns The absolute path of the file, or `undefined` when none is found
|
|
23
|
+
* @internal
|
|
24
|
+
* @private
|
|
25
|
+
* @category Resolve
|
|
26
|
+
*/
|
|
27
|
+
export function findBlueprintFile(path = cwd()) {
|
|
28
|
+
let dir = resolve(path);
|
|
29
|
+
if (!existsSync(dir))
|
|
30
|
+
return undefined;
|
|
31
|
+
if (statSync(dir).isFile())
|
|
32
|
+
return dir;
|
|
33
|
+
while (true) {
|
|
34
|
+
for (const name of BLUEPRINT_FILE_NAMES) {
|
|
35
|
+
const candidate = join(dir, name);
|
|
36
|
+
if (existsSync(candidate))
|
|
37
|
+
return candidate;
|
|
38
|
+
}
|
|
39
|
+
const parent = dirname(dir);
|
|
40
|
+
if (parent === dir)
|
|
41
|
+
return undefined;
|
|
42
|
+
dir = parent;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Load a blueprint file
|
|
47
|
+
*
|
|
48
|
+
* Parses a JSON file. Imports a JS or TS file and calls its default export.
|
|
49
|
+
* TypeScript that Node cannot run, such as an enum, is loaded with jiti when it is installed.
|
|
50
|
+
* Imported modules are cached, so a later call in the same process does not see changes to the file. Pass a `loader` that skips the cache to re-read it.
|
|
51
|
+
*
|
|
52
|
+
* @param path The blueprint file
|
|
53
|
+
* @throws When the file cannot be loaded or does not produce a blueprint object
|
|
54
|
+
* @internal
|
|
55
|
+
* @private
|
|
56
|
+
* @category Resolve
|
|
57
|
+
*/
|
|
58
|
+
export async function loadBlueprintFile(path, options = {}) {
|
|
59
|
+
const extension = extname(path);
|
|
60
|
+
if (extension === '.json') {
|
|
61
|
+
let blueprint;
|
|
62
|
+
try {
|
|
63
|
+
blueprint = JSON.parse(readFileSync(path, 'utf8'));
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new Error(`Unable to parse ${path}: ${errorText(error)}`, { cause: error });
|
|
67
|
+
}
|
|
68
|
+
return { blueprint: asBlueprint(blueprint, path) };
|
|
69
|
+
}
|
|
70
|
+
if (!EXTENSIONS.includes(extension)) {
|
|
71
|
+
throw new Error(`Unsupported blueprint file extension "${extension}" for ${path}. Expected one of: ${EXTENSIONS.join(', ')}`);
|
|
72
|
+
}
|
|
73
|
+
const defaultExport = await importDefault(path, options.loader);
|
|
74
|
+
if (typeof defaultExport !== 'function') {
|
|
75
|
+
throw new Error(`${path} must export a default function. Wrap the blueprint in defineBlueprint().`);
|
|
76
|
+
}
|
|
77
|
+
const module = defaultExport;
|
|
78
|
+
let blueprint;
|
|
79
|
+
try {
|
|
80
|
+
blueprint = module();
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
throw new Error(`Error executing the default export of ${path}: ${errorText(error)}`, { cause: error });
|
|
84
|
+
}
|
|
85
|
+
return { blueprint: asBlueprint(blueprint, path), module };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Read a local blueprint and the ids of the Stack it deploys to
|
|
89
|
+
*
|
|
90
|
+
* Ids come from `overrides`, the environment, `defineBlueprint`, then `.sanity/blueprint.config.json`.
|
|
91
|
+
* Resources are not validated. No network access.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import {readBlueprint} from '@sanity/blueprints/resolve'
|
|
96
|
+
*
|
|
97
|
+
* const {resources, resourcesByName, stackId, sources} = await readBlueprint()
|
|
98
|
+
* resources.map((resource) => resource.name)
|
|
99
|
+
* resourcesByName['production-dataset']?.type // 'sanity.project.dataset'
|
|
100
|
+
* sources.stackId // 'config'
|
|
101
|
+
* ```
|
|
102
|
+
* @throws When no blueprint file is found, it cannot be loaded, or `resources` is not an array
|
|
103
|
+
* @category Resolve
|
|
104
|
+
*/
|
|
105
|
+
export async function readBlueprint(options = {}) {
|
|
106
|
+
const path = findBlueprintFile(options.path);
|
|
107
|
+
if (!path) {
|
|
108
|
+
const searched = resolve(options.path ?? cwd());
|
|
109
|
+
throw new Error(`No blueprint file found in ${searched} or its parent directories. Expected one of: ${BLUEPRINT_FILE_NAMES.join(', ')}`);
|
|
110
|
+
}
|
|
111
|
+
const { blueprint, module } = await loadBlueprintFile(path, { loader: options.loader });
|
|
112
|
+
const resources = blueprint.resources ?? [];
|
|
113
|
+
if (!Array.isArray(resources))
|
|
114
|
+
throw new Error(`Invalid blueprint in ${path}: \`resources\` must be an array`);
|
|
115
|
+
const file = readStackConfigFile(dirname(path));
|
|
116
|
+
const config = resolveStackConfig({
|
|
117
|
+
overrides: options.overrides,
|
|
118
|
+
env: options.env === undefined ? undefined : options.env && stackIdsFromEnv(options.env),
|
|
119
|
+
module,
|
|
120
|
+
config: file,
|
|
121
|
+
});
|
|
122
|
+
return { path, blueprint, module, resources, resourcesByName: byName(resources), ...config, configPath: file?.configPath };
|
|
123
|
+
}
|
|
124
|
+
async function importDefault(path, loader) {
|
|
125
|
+
let error;
|
|
126
|
+
try {
|
|
127
|
+
return await (loader ?? importNative)(path);
|
|
128
|
+
}
|
|
129
|
+
catch (caught) {
|
|
130
|
+
error = caught;
|
|
131
|
+
}
|
|
132
|
+
if (!loader && canRetryWithJiti(error)) {
|
|
133
|
+
const jiti = await findJiti(path);
|
|
134
|
+
if (!jiti)
|
|
135
|
+
throw new Error(`Unable to import ${path}: ${errorText(error)} Install jiti to load it, or pass a loader.`, { cause: error });
|
|
136
|
+
try {
|
|
137
|
+
return await jiti(path);
|
|
138
|
+
}
|
|
139
|
+
catch (caught) {
|
|
140
|
+
error = caught;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
throw new Error(`Unable to import ${path}: ${errorText(error)}`, { cause: error });
|
|
144
|
+
}
|
|
145
|
+
function errorText(error) {
|
|
146
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
147
|
+
return message.endsWith('.') ? message : `${message}.`;
|
|
148
|
+
}
|
|
149
|
+
function asBlueprint(value, path) {
|
|
150
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
151
|
+
throw new Error(`${path} did not produce a blueprint object`);
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=load.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load.js","sourceRoot":"","sources":["../../src/resolve/load.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAC,MAAM,SAAS,CAAA;AAC1D,OAAO,EAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAC,MAAM,WAAW,CAAA;AACzD,OAAO,EAAC,GAAG,EAAC,MAAM,cAAc,CAAA;AAEhC,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAA;AACnC,OAAO,EAAC,gBAAgB,EAAE,QAAQ,EAAE,YAAY,EAAC,MAAM,aAAa,CAAA;AACpE,OAAO,EAAC,mBAAmB,EAAE,kBAAkB,EAAE,eAAe,EAAC,MAAM,mBAAmB,CAAA;AAG1F,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,CAAA;AAClD,MAAM,UAAU,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,YAAY,GAAG,EA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
1
|
+
import type { StackConfig, StackConfigFile, StackIdSources, StackIds } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The directory next to the blueprint file that holds the Stack config
|
|
4
|
+
* @internal
|
|
5
|
+
* @private
|
|
6
|
+
* @category Resolve
|
|
7
|
+
*/
|
|
8
|
+
export declare const STACK_CONFIG_DIR = ".sanity";
|
|
9
|
+
/**
|
|
10
|
+
* The file in {@link STACK_CONFIG_DIR} that holds the Stack ids
|
|
11
|
+
* @internal
|
|
12
|
+
* @private
|
|
13
|
+
* @category Resolve
|
|
14
|
+
*/
|
|
15
|
+
export declare const STACK_CONFIG_FILE = "blueprint.config.json";
|
|
16
|
+
/**
|
|
17
|
+
* Read `.sanity/blueprint.config.json` from a blueprint directory
|
|
18
|
+
* @param dir The blueprint directory. Defaults to the current working directory.
|
|
19
|
+
* @returns The ids in the file and its path, or `undefined` when the file does not exist
|
|
20
|
+
* @throws When the file exists and is not valid JSON
|
|
21
|
+
* @internal
|
|
22
|
+
* @private
|
|
23
|
+
* @category Resolve
|
|
24
|
+
*/
|
|
25
|
+
export declare function readStackConfigFile(dir?: string): StackConfigFile | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Read the Stack ids from `SANITY_ORGANIZATION_ID`, `SANITY_PROJECT_ID`, and `SANITY_BLUEPRINT_STACK_ID`
|
|
28
|
+
* @param env The environment to read. Defaults to `process.env`.
|
|
29
|
+
* @internal
|
|
30
|
+
* @private
|
|
31
|
+
* @category Resolve
|
|
32
|
+
*/
|
|
33
|
+
export declare function stackIdsFromEnv(env?: NodeJS.ProcessEnv): StackIds;
|
|
34
|
+
/**
|
|
35
|
+
* Resolve the Stack ids and scope, and which source supplied each id
|
|
36
|
+
*
|
|
37
|
+
* Precedence: `overrides`, `env`, `module`, `config`. The scope is the project if known, else the organization.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const config = resolveStackConfig({config: readStackConfigFile()})
|
|
42
|
+
* config.stackId // 'ST-abc123'
|
|
43
|
+
* config.sources.stackId // 'config'
|
|
44
|
+
* ```
|
|
45
|
+
* @internal
|
|
46
|
+
* @private
|
|
47
|
+
* @category Resolve
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolveStackConfig(sources?: StackIdSources): StackConfig;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { cwd, env as processEnv } from 'node:process';
|
|
4
|
+
/**
|
|
5
|
+
* The directory next to the blueprint file that holds the Stack config
|
|
6
|
+
* @internal
|
|
7
|
+
* @private
|
|
8
|
+
* @category Resolve
|
|
9
|
+
*/
|
|
10
|
+
export const STACK_CONFIG_DIR = '.sanity';
|
|
11
|
+
/**
|
|
12
|
+
* The file in {@link STACK_CONFIG_DIR} that holds the Stack ids
|
|
13
|
+
* @internal
|
|
14
|
+
* @private
|
|
15
|
+
* @category Resolve
|
|
16
|
+
*/
|
|
17
|
+
export const STACK_CONFIG_FILE = 'blueprint.config.json';
|
|
18
|
+
const ID_KEYS = ['organizationId', 'projectId', 'stackId'];
|
|
19
|
+
/**
|
|
20
|
+
* Read `.sanity/blueprint.config.json` from a blueprint directory
|
|
21
|
+
* @param dir The blueprint directory. Defaults to the current working directory.
|
|
22
|
+
* @returns The ids in the file and its path, or `undefined` when the file does not exist
|
|
23
|
+
* @throws When the file exists and is not valid JSON
|
|
24
|
+
* @internal
|
|
25
|
+
* @private
|
|
26
|
+
* @category Resolve
|
|
27
|
+
*/
|
|
28
|
+
export function readStackConfigFile(dir = cwd()) {
|
|
29
|
+
const configPath = resolve(dir, STACK_CONFIG_DIR, STACK_CONFIG_FILE);
|
|
30
|
+
if (!existsSync(configPath))
|
|
31
|
+
return undefined;
|
|
32
|
+
let parsed;
|
|
33
|
+
try {
|
|
34
|
+
parsed = JSON.parse(readFileSync(configPath, 'utf8'));
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
throw new Error(`Unable to parse ${configPath}`, { cause: error });
|
|
38
|
+
}
|
|
39
|
+
return { configPath, ...pickIds(parsed) };
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Read the Stack ids from `SANITY_ORGANIZATION_ID`, `SANITY_PROJECT_ID`, and `SANITY_BLUEPRINT_STACK_ID`
|
|
43
|
+
* @param env The environment to read. Defaults to `process.env`.
|
|
44
|
+
* @internal
|
|
45
|
+
* @private
|
|
46
|
+
* @category Resolve
|
|
47
|
+
*/
|
|
48
|
+
export function stackIdsFromEnv(env = processEnv) {
|
|
49
|
+
return pickIds({
|
|
50
|
+
organizationId: env.SANITY_ORGANIZATION_ID,
|
|
51
|
+
projectId: env.SANITY_PROJECT_ID,
|
|
52
|
+
stackId: env.SANITY_BLUEPRINT_STACK_ID,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Resolve the Stack ids and scope, and which source supplied each id
|
|
57
|
+
*
|
|
58
|
+
* Precedence: `overrides`, `env`, `module`, `config`. The scope is the project if known, else the organization.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* const config = resolveStackConfig({config: readStackConfigFile()})
|
|
63
|
+
* config.stackId // 'ST-abc123'
|
|
64
|
+
* config.sources.stackId // 'config'
|
|
65
|
+
* ```
|
|
66
|
+
* @internal
|
|
67
|
+
* @private
|
|
68
|
+
* @category Resolve
|
|
69
|
+
*/
|
|
70
|
+
export function resolveStackConfig(sources = {}) {
|
|
71
|
+
const ordered = [
|
|
72
|
+
['overrides', sources.overrides],
|
|
73
|
+
['env', sources.env === undefined ? stackIdsFromEnv() : sources.env],
|
|
74
|
+
['module', sources.module],
|
|
75
|
+
['config', sources.config],
|
|
76
|
+
];
|
|
77
|
+
const config = { sources: {} };
|
|
78
|
+
for (const [source, values] of ordered) {
|
|
79
|
+
if (!values)
|
|
80
|
+
continue;
|
|
81
|
+
for (const key of ID_KEYS) {
|
|
82
|
+
const value = values[key];
|
|
83
|
+
if (!config[key] && value) {
|
|
84
|
+
config[key] = value;
|
|
85
|
+
config.sources[key] = source;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (config.projectId) {
|
|
90
|
+
config.scopeType = 'project';
|
|
91
|
+
config.scopeId = config.projectId;
|
|
92
|
+
}
|
|
93
|
+
else if (config.organizationId) {
|
|
94
|
+
config.scopeType = 'organization';
|
|
95
|
+
config.scopeId = config.organizationId;
|
|
96
|
+
}
|
|
97
|
+
return config;
|
|
98
|
+
}
|
|
99
|
+
function pickIds(values) {
|
|
100
|
+
const ids = {};
|
|
101
|
+
for (const key of ID_KEYS) {
|
|
102
|
+
const value = values[key];
|
|
103
|
+
if (typeof value === 'string' && value.length > 0)
|
|
104
|
+
ids[key] = value;
|
|
105
|
+
}
|
|
106
|
+
return ids;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=stack-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack-config.js","sourceRoot":"","sources":["../../src/resolve/stack-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,YAAY,EAAC,MAAM,SAAS,CAAA;AAChD,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAA;AACjC,OAAO,EAAC,GAAG,EAAE,GAAG,IAAI,UAAU,EAAC,MAAM,cAAc,CAAA;AAInD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,SAAS,CAAA;AAEzC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,uBAAuB,CAAA;AAExD,MAAM,OAAO,GAAkC,CAAC,gBAAgB,EAAE,WAAW,EAAE,SAAS,CAAC,CAAA;AAEzF;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAG,GAAW,GAAG,EAAE;IACrD,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,EAAE,gBAAgB,EAAE,iB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AM;YAAE,SAAQ;QACrB,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;YAC1B,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;YACzB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,KAAK,EAAE,CAAC;gBAC1B,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;gBACnB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAA;YAC9B,CAAC;QACH,CAAC;IACH,CAAC;IAED,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,MAAM,CAAC,SAAS,GAAG,SAAS,CAAA;QAC5B,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,SAAS,CAAA;IACnC,CAAC;SAAM,IAAI,MAAM,CAAC,cAAc,EAAE,CAAC;QACjC,MAAM,CAAC,SAAS,GAAG,cAAc,CAAA;QACjC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,cAAc,CAAA;IACxC,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED,SAAS,OAAO,CAAC,MAA+B;IAC9C,MAAM,GAAG,GAAa,EAAE,CAAA;IACxB,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;QAC1B,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;QACzB,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;IACrE,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ReadStackOptions, StackInfo } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The Blueprints API version {@link readStack} uses when none is configured
|
|
4
|
+
* @category Resolve
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_BLUEPRINTS_API_VERSION = "v2026-06-15";
|
|
7
|
+
/**
|
|
8
|
+
* Read a local blueprint and its deployed Stack
|
|
9
|
+
*
|
|
10
|
+
* Adds each deployed resource's `externalId` and `live` values to the declared resource with the same type and name.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {readStack} from '@sanity/blueprints/resolve'
|
|
15
|
+
*
|
|
16
|
+
* const {values, resourcesByName} = await readStack({token: process.env.SANITY_AUTH_TOKEN})
|
|
17
|
+
* values['production-dataset']?.['name'] // the deployed dataset's name
|
|
18
|
+
* resourcesByName['my-function']?.externalId
|
|
19
|
+
* ```
|
|
20
|
+
* @throws When {@link readBlueprint} throws, the Stack id or scope is unknown, or the API responds with an error
|
|
21
|
+
* @category Resolve
|
|
22
|
+
*/
|
|
23
|
+
export declare function readStack(options: ReadStackOptions): Promise<StackInfo>;
|