@sanity/runtime-cli 8.2.0 → 8.3.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 +16 -16
- package/dist/actions/blueprints/assets.js +11 -5
- package/dist/actions/blueprints/resources.js +9 -22
- package/dist/server/static/components/run-panel.js +0 -1
- package/dist/utils/functions/packageJsonUtils.d.ts +4 -0
- package/dist/utils/functions/packageJsonUtils.js +37 -0
- package/dist/utils/functions/resolve-dependencies.d.ts +2 -0
- package/dist/utils/functions/resolve-dependencies.js +18 -0
- package/dist/utils/functions/resource-to-arc.d.ts +2 -0
- package/dist/utils/functions/resource-to-arc.js +12 -0
- package/dist/utils/functions/should-auto-resolve-deps.d.ts +2 -0
- package/dist/utils/functions/should-auto-resolve-deps.js +7 -0
- package/dist/utils/functions/should-transpile.d.ts +2 -0
- package/dist/utils/functions/{should-bundle.js → should-transpile.js} +4 -5
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/invoke-local.js +25 -7
- package/dist/utils/{bundle/bundle-function.d.ts → transpile/transpile-function.d.ts} +1 -1
- package/dist/utils/{bundle/bundle-function.js → transpile/transpile-function.js} +20 -16
- package/dist/utils/{bundle → transpile}/verify-handler.js +1 -1
- package/dist/utils/types.d.ts +2 -1
- package/oclif.manifest.json +1 -1
- package/package.json +6 -1
- package/dist/utils/functions/should-bundle.d.ts +0 -2
- /package/dist/utils/{bundle → transpile}/cleanup-source-maps.d.ts +0 -0
- /package/dist/utils/{bundle → transpile}/cleanup-source-maps.js +0 -0
- /package/dist/utils/{bundle → transpile}/find-up.d.ts +0 -0
- /package/dist/utils/{bundle → transpile}/find-up.js +0 -0
- /package/dist/utils/{bundle → transpile}/verify-handler.d.ts +0 -0
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ $ npm install -g @sanity/runtime-cli
|
|
|
20
20
|
$ sanity-run COMMAND
|
|
21
21
|
running command...
|
|
22
22
|
$ sanity-run (--version)
|
|
23
|
-
@sanity/runtime-cli/8.
|
|
23
|
+
@sanity/runtime-cli/8.3.0 linux-x64 node-v22.16.0
|
|
24
24
|
$ sanity-run --help [COMMAND]
|
|
25
25
|
USAGE
|
|
26
26
|
$ sanity-run COMMAND
|
|
@@ -86,7 +86,7 @@ EXAMPLES
|
|
|
86
86
|
$ sanity-run blueprints add function --name my-function --fn-type document-publish --lang js
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
89
|
+
_See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/add.ts)_
|
|
90
90
|
|
|
91
91
|
## `sanity-run blueprints config`
|
|
92
92
|
|
|
@@ -117,7 +117,7 @@ EXAMPLES
|
|
|
117
117
|
$ sanity-run blueprints config --edit --project-id <projectId> --stack-id <stackId>
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
-
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
120
|
+
_See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/config.ts)_
|
|
121
121
|
|
|
122
122
|
## `sanity-run blueprints deploy`
|
|
123
123
|
|
|
@@ -139,7 +139,7 @@ EXAMPLES
|
|
|
139
139
|
$ sanity-run blueprints deploy --no-wait
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
142
|
+
_See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/deploy.ts)_
|
|
143
143
|
|
|
144
144
|
## `sanity-run blueprints destroy`
|
|
145
145
|
|
|
@@ -164,7 +164,7 @@ EXAMPLES
|
|
|
164
164
|
$ sanity-run blueprints destroy --stack-id <stackId> --project-id <projectId> --force --no-wait
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
-
_See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
167
|
+
_See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/destroy.ts)_
|
|
168
168
|
|
|
169
169
|
## `sanity-run blueprints info`
|
|
170
170
|
|
|
@@ -186,7 +186,7 @@ EXAMPLES
|
|
|
186
186
|
$ sanity-run blueprints info --stack-id <stackId>
|
|
187
187
|
```
|
|
188
188
|
|
|
189
|
-
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
189
|
+
_See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/info.ts)_
|
|
190
190
|
|
|
191
191
|
## `sanity-run blueprints init [DIR]`
|
|
192
192
|
|
|
@@ -224,7 +224,7 @@ EXAMPLES
|
|
|
224
224
|
$ sanity-run blueprints init --blueprint-type <json|js|ts> --stack-name <stackName>
|
|
225
225
|
```
|
|
226
226
|
|
|
227
|
-
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
227
|
+
_See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/init.ts)_
|
|
228
228
|
|
|
229
229
|
## `sanity-run blueprints logs`
|
|
230
230
|
|
|
@@ -246,7 +246,7 @@ EXAMPLES
|
|
|
246
246
|
$ sanity-run blueprints logs --watch
|
|
247
247
|
```
|
|
248
248
|
|
|
249
|
-
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
249
|
+
_See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/logs.ts)_
|
|
250
250
|
|
|
251
251
|
## `sanity-run blueprints plan`
|
|
252
252
|
|
|
@@ -263,7 +263,7 @@ EXAMPLES
|
|
|
263
263
|
$ sanity-run blueprints plan
|
|
264
264
|
```
|
|
265
265
|
|
|
266
|
-
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
266
|
+
_See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/plan.ts)_
|
|
267
267
|
|
|
268
268
|
## `sanity-run blueprints stacks`
|
|
269
269
|
|
|
@@ -285,7 +285,7 @@ EXAMPLES
|
|
|
285
285
|
$ sanity-run blueprints stacks --project-id <projectId>
|
|
286
286
|
```
|
|
287
287
|
|
|
288
|
-
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
288
|
+
_See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/blueprints/stacks.ts)_
|
|
289
289
|
|
|
290
290
|
## `sanity-run functions dev`
|
|
291
291
|
|
|
@@ -305,7 +305,7 @@ EXAMPLES
|
|
|
305
305
|
$ sanity-run functions dev --port 8974
|
|
306
306
|
```
|
|
307
307
|
|
|
308
|
-
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
308
|
+
_See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/functions/dev.ts)_
|
|
309
309
|
|
|
310
310
|
## `sanity-run functions env add NAME KEY VALUE`
|
|
311
311
|
|
|
@@ -327,7 +327,7 @@ EXAMPLES
|
|
|
327
327
|
$ sanity-run functions env add MyFunction API_URL https://api.example.com/
|
|
328
328
|
```
|
|
329
329
|
|
|
330
|
-
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
330
|
+
_See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/functions/env/add.ts)_
|
|
331
331
|
|
|
332
332
|
## `sanity-run functions env list NAME`
|
|
333
333
|
|
|
@@ -347,7 +347,7 @@ EXAMPLES
|
|
|
347
347
|
$ sanity-run functions env list MyFunction
|
|
348
348
|
```
|
|
349
349
|
|
|
350
|
-
_See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
350
|
+
_See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/functions/env/list.ts)_
|
|
351
351
|
|
|
352
352
|
## `sanity-run functions env remove NAME KEY`
|
|
353
353
|
|
|
@@ -368,7 +368,7 @@ EXAMPLES
|
|
|
368
368
|
$ sanity-run functions env remove MyFunction API_URL
|
|
369
369
|
```
|
|
370
370
|
|
|
371
|
-
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
371
|
+
_See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/functions/env/remove.ts)_
|
|
372
372
|
|
|
373
373
|
## `sanity-run functions logs NAME`
|
|
374
374
|
|
|
@@ -402,7 +402,7 @@ EXAMPLES
|
|
|
402
402
|
$ sanity-run functions logs <name> --delete
|
|
403
403
|
```
|
|
404
404
|
|
|
405
|
-
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
405
|
+
_See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/functions/logs.ts)_
|
|
406
406
|
|
|
407
407
|
## `sanity-run functions test NAME`
|
|
408
408
|
|
|
@@ -435,7 +435,7 @@ EXAMPLES
|
|
|
435
435
|
$ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
|
|
436
436
|
```
|
|
437
437
|
|
|
438
|
-
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v8.
|
|
438
|
+
_See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v8.3.0/src/commands/functions/test.ts)_
|
|
439
439
|
|
|
440
440
|
## `sanity-run help [COMMAND]`
|
|
441
441
|
|
|
@@ -3,9 +3,11 @@ import path from 'node:path';
|
|
|
3
3
|
import { cwd } from 'node:process';
|
|
4
4
|
import AdmZip from 'adm-zip';
|
|
5
5
|
import config from '../../config.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { resolveResourceDependencies } from '../../utils/functions/resolve-dependencies.js';
|
|
7
|
+
import { shouldAutoResolveDependencies } from '../../utils/functions/should-auto-resolve-deps.js';
|
|
8
|
+
import { shouldTranspileFunction } from '../../utils/functions/should-transpile.js';
|
|
8
9
|
import getHeaders from '../../utils/get-headers.js';
|
|
10
|
+
import { transpileFunction } from '../../utils/transpile/transpile-function.js';
|
|
9
11
|
const { apiUrl } = config;
|
|
10
12
|
export const stashUrl = `${apiUrl}vX/blueprints/assets/stash`;
|
|
11
13
|
export async function stashAsset({ resource, auth, }) {
|
|
@@ -13,10 +15,10 @@ export async function stashAsset({ resource, auth, }) {
|
|
|
13
15
|
throw new Error('Resource src is required');
|
|
14
16
|
let functionPath = path.join(cwd(), resource.src);
|
|
15
17
|
let cleanup = async () => { };
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
18
|
+
const shouldTranspile = await shouldTranspileFunction(resource);
|
|
19
|
+
if (shouldTranspile) {
|
|
18
20
|
try {
|
|
19
|
-
const result = await
|
|
21
|
+
const result = await transpileFunction(resource);
|
|
20
22
|
functionPath = result.outputDir;
|
|
21
23
|
cleanup = result.cleanup;
|
|
22
24
|
}
|
|
@@ -24,6 +26,10 @@ export async function stashAsset({ resource, auth, }) {
|
|
|
24
26
|
return { success: false, error: err instanceof Error ? err.message : `${err}` };
|
|
25
27
|
}
|
|
26
28
|
}
|
|
29
|
+
const shouldResolveDependencies = await shouldAutoResolveDependencies(resource);
|
|
30
|
+
if (shouldResolveDependencies) {
|
|
31
|
+
await resolveResourceDependencies(resource, shouldTranspile);
|
|
32
|
+
}
|
|
27
33
|
try {
|
|
28
34
|
const stats = await fs.promises.stat(functionPath);
|
|
29
35
|
const zip = new AdmZip();
|
|
@@ -4,8 +4,7 @@ import { existsSync } from 'node:fs';
|
|
|
4
4
|
import { dirname, join } from 'node:path';
|
|
5
5
|
import { cwd } from 'node:process';
|
|
6
6
|
import chalk from 'chalk';
|
|
7
|
-
import {
|
|
8
|
-
import { addResourceToBlueprint } from './blueprint.js';
|
|
7
|
+
import { addResourceToBlueprint, writeOrUpdateNodeDependency } from './blueprint.js';
|
|
9
8
|
const DEFAULT_FUNCTION_TEMPLATE = /*js*/ `export async function handler({context, event}) {
|
|
10
9
|
const time = new Date().toLocaleTimeString()
|
|
11
10
|
console.log(\`👋 Your Sanity Function was called at \${time}\`)
|
|
@@ -16,11 +15,6 @@ export const handler = documentEventHandler(async ({ context, event }) => {
|
|
|
16
15
|
const time = new Date().toLocaleTimeString()
|
|
17
16
|
console.log(\`👋 Your Sanity Function was called at \${time}\`)
|
|
18
17
|
})`;
|
|
19
|
-
const DEFAULT_PACKAGE_JSON = {
|
|
20
|
-
name: '',
|
|
21
|
-
type: 'module',
|
|
22
|
-
main: '',
|
|
23
|
-
};
|
|
24
18
|
/**
|
|
25
19
|
* Creates a new function resource file and adds it to the blueprint
|
|
26
20
|
*/
|
|
@@ -48,23 +42,16 @@ export async function createFunctionResource(options) {
|
|
|
48
42
|
// Create index.<lang> with default template
|
|
49
43
|
const indexPath = join(functionDir, `index.${lang}`);
|
|
50
44
|
writeFileSync(indexPath, addHelpers ? DEFAULT_HELPER_FUNCTION_TEMPLATE : DEFAULT_FUNCTION_TEMPLATE);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
};
|
|
59
|
-
if (addHelpers) {
|
|
60
|
-
const helpersVersion = await getLatestNpmVersion('@sanity/functions');
|
|
61
|
-
packageContent.dependencies = {
|
|
62
|
-
'@sanity/functions': helpersVersion,
|
|
63
|
-
};
|
|
45
|
+
if (addHelpers && blueprintFilePath) {
|
|
46
|
+
try {
|
|
47
|
+
await writeOrUpdateNodeDependency({ blueprintFilePath, dependency: '@sanity/functions' });
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
throw new Error('Unable to add @sanity/functions to your project.', { cause: err });
|
|
51
|
+
}
|
|
64
52
|
}
|
|
65
|
-
writeFileSync(packagePath, JSON.stringify(packageContent, null, 2));
|
|
66
53
|
if (installCommand) {
|
|
67
|
-
const success = await runPackageInstall(
|
|
54
|
+
const success = await runPackageInstall(workingDir, installCommand);
|
|
68
55
|
if (!success) {
|
|
69
56
|
throw new Error(`Failed to install dependencies using \`${installCommand}\``);
|
|
70
57
|
}
|
|
@@ -16,7 +16,6 @@ class RunPanel extends ApiBaseElement {
|
|
|
16
16
|
invoke = () => {
|
|
17
17
|
this.api.store.result = {logs: '', time: 0}
|
|
18
18
|
const payloadText = this.createPayloadText(this.api.store.payload.state.doc)
|
|
19
|
-
console.log(payloadText)
|
|
20
19
|
const event = JSON.parse(payloadText)
|
|
21
20
|
const context = {
|
|
22
21
|
clientOptions: {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FunctionResource } from '../types.js';
|
|
2
|
+
export declare function doesPackageJsonExists(resource: FunctionResource): boolean;
|
|
3
|
+
export declare function createTempPackageJson(functionPath: string): boolean;
|
|
4
|
+
export declare function cleanupTempPackageJson(functionPath: string): boolean;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { existsSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { cwd } from 'node:process';
|
|
4
|
+
function getPackageJsonPath(functionPath) {
|
|
5
|
+
const functionDir = dirname(functionPath);
|
|
6
|
+
return join(functionDir, 'package.json');
|
|
7
|
+
}
|
|
8
|
+
export function doesPackageJsonExists(resource) {
|
|
9
|
+
if (!resource.src) {
|
|
10
|
+
// Cannot determine without a source path
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const absolutePath = resolve(cwd(), resource.src);
|
|
14
|
+
const packageJsonPath = join(absolutePath, 'package.json');
|
|
15
|
+
return existsSync(packageJsonPath);
|
|
16
|
+
}
|
|
17
|
+
export function createTempPackageJson(functionPath) {
|
|
18
|
+
if (!functionPath) {
|
|
19
|
+
// Cannot determine without a source path
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const packageJsonPath = getPackageJsonPath(functionPath);
|
|
23
|
+
const packageContent = { type: 'module' };
|
|
24
|
+
writeFileSync(packageJsonPath, JSON.stringify(packageContent, null, 2));
|
|
25
|
+
return !existsSync(packageJsonPath);
|
|
26
|
+
}
|
|
27
|
+
export function cleanupTempPackageJson(functionPath) {
|
|
28
|
+
if (!functionPath) {
|
|
29
|
+
// Cannot determine without a source path
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const packageJsonPath = getPackageJsonPath(functionPath);
|
|
33
|
+
if (existsSync(packageJsonPath)) {
|
|
34
|
+
rmSync(packageJsonPath);
|
|
35
|
+
}
|
|
36
|
+
return !existsSync(packageJsonPath);
|
|
37
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import hydrate from '@architect/hydrate';
|
|
2
|
+
import inventory from '@architect/inventory';
|
|
3
|
+
import { convertResourceToArcFormat } from './resource-to-arc.js';
|
|
4
|
+
export async function resolveResourceDependencies(resource, transpiled) {
|
|
5
|
+
const rawArc = await convertResourceToArcFormat(resource, transpiled);
|
|
6
|
+
const inv = await inventory({ rawArc });
|
|
7
|
+
try {
|
|
8
|
+
await hydrate.install({ inventory: inv, hydrateShared: false, quiet: true });
|
|
9
|
+
}
|
|
10
|
+
catch (err) {
|
|
11
|
+
// This is a temporary fix.
|
|
12
|
+
const regex = /ENOTDIR: not a directory, unlink ['"].*[/\\]node_modules['"]/;
|
|
13
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
14
|
+
if (!regex.test(errorMessage)) {
|
|
15
|
+
throw err;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { sep } from 'node:path';
|
|
2
|
+
export async function convertResourceToArcFormat(resource, transpiled) {
|
|
3
|
+
const srcPath = resource.src?.split(sep).join('/');
|
|
4
|
+
const functionPath = transpiled ? `${srcPath}/.build/function-${resource.name}` : srcPath;
|
|
5
|
+
return `@app
|
|
6
|
+
hydrate-function
|
|
7
|
+
|
|
8
|
+
@events
|
|
9
|
+
${resource.name}
|
|
10
|
+
src ${functionPath}
|
|
11
|
+
`;
|
|
12
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { findFunctionEntryPoint } from './find-entry-point.js';
|
|
2
|
-
export async function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return resource.bundle;
|
|
2
|
+
export async function shouldTranspileFunction(resource) {
|
|
3
|
+
if (typeof resource.transpile === 'boolean') {
|
|
4
|
+
return resource.transpile;
|
|
6
5
|
}
|
|
7
6
|
if (!resource.src) {
|
|
8
7
|
// Cannot determine without a source path
|
|
@@ -17,7 +16,7 @@ export async function shouldBundleFunction(resource) {
|
|
|
17
16
|
catch (err) {
|
|
18
17
|
// If we cannot find the entry point, we cannot determine if it's TS.
|
|
19
18
|
// Log a warning and default to false (don't bundle).
|
|
20
|
-
console.warn(`[warn] Could not determine entry point for function "${resource.displayName ?? resource.name}" while checking if
|
|
19
|
+
console.warn(`[warn] Could not determine entry point for function "${resource.displayName ?? resource.name}" while checking if transpiling is needed: ${err instanceof Error ? err.message : err}`);
|
|
21
20
|
return false;
|
|
22
21
|
}
|
|
23
22
|
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * as display from './display/index.js';
|
|
2
2
|
export * as findFunction from './find-function.js';
|
|
3
|
+
export * as invokeLocal from './invoke-local.js';
|
|
3
4
|
export * as types from './types.js';
|
|
4
5
|
export * as validate from './validate/index.js';
|
|
5
6
|
export * as vendor from './vendor/index.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * as display from './display/index.js';
|
|
2
2
|
export * as findFunction from './find-function.js';
|
|
3
|
+
export * as invokeLocal from './invoke-local.js';
|
|
3
4
|
export * as types from './types.js';
|
|
4
5
|
export * as validate from './validate/index.js';
|
|
5
6
|
export * as vendor from './vendor/index.js';
|
|
@@ -5,9 +5,12 @@ import { setTimeout } from 'node:timers';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import * as groq from 'groq-js';
|
|
7
7
|
import config from '../config.js';
|
|
8
|
-
import { bundleFunction } from './bundle/bundle-function.js';
|
|
9
8
|
import { findFunctionEntryPoint } from './functions/find-entry-point.js';
|
|
10
|
-
import {
|
|
9
|
+
import { cleanupTempPackageJson, createTempPackageJson, doesPackageJsonExists, } from './functions/packageJsonUtils.js';
|
|
10
|
+
import { resolveResourceDependencies } from './functions/resolve-dependencies.js';
|
|
11
|
+
import { shouldAutoResolveDependencies } from './functions/should-auto-resolve-deps.js';
|
|
12
|
+
import { shouldTranspileFunction } from './functions/should-transpile.js';
|
|
13
|
+
import { transpileFunction } from './transpile/transpile-function.js';
|
|
11
14
|
function getChildProcessWrapperPath() {
|
|
12
15
|
return fileURLToPath(new URL('./child-process-wrapper.js', import.meta.url));
|
|
13
16
|
}
|
|
@@ -67,15 +70,26 @@ export default async function invoke(resource, data, context, options) {
|
|
|
67
70
|
let cleanupBundle = async () => { };
|
|
68
71
|
let functionPath = '';
|
|
69
72
|
let bundleTimings = undefined;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
// If no package.json exists we will create a temporary one to avoid the
|
|
74
|
+
// Module Type warning from node
|
|
75
|
+
const existingPackageJson = doesPackageJsonExists(resource);
|
|
76
|
+
const shouldTranspile = await shouldTranspileFunction(resource);
|
|
77
|
+
if (shouldTranspile) {
|
|
78
|
+
const transpileResult = await transpileFunction(resource);
|
|
79
|
+
functionPath = await findFunctionEntryPoint(transpileResult.outputDir);
|
|
80
|
+
bundleTimings = transpileResult.timings;
|
|
81
|
+
cleanupBundle = transpileResult.cleanup;
|
|
75
82
|
}
|
|
76
83
|
else {
|
|
77
84
|
functionPath = await findFunctionEntryPoint(resource.src, resource.displayName ?? resource.name);
|
|
78
85
|
}
|
|
86
|
+
const shouldResolveDependencies = await shouldAutoResolveDependencies(resource);
|
|
87
|
+
if (shouldResolveDependencies && existingPackageJson) {
|
|
88
|
+
await resolveResourceDependencies(resource, shouldTranspile);
|
|
89
|
+
}
|
|
90
|
+
if (!existingPackageJson) {
|
|
91
|
+
createTempPackageJson(functionPath);
|
|
92
|
+
}
|
|
79
93
|
return new Promise((resolve, reject) => {
|
|
80
94
|
let child;
|
|
81
95
|
let timer;
|
|
@@ -146,6 +160,10 @@ export default async function invoke(resource, data, context, options) {
|
|
|
146
160
|
if (child && !child.killed) {
|
|
147
161
|
child.kill();
|
|
148
162
|
}
|
|
163
|
+
// If we previously created a temporary package.json, let's clean it up
|
|
164
|
+
if (!existingPackageJson) {
|
|
165
|
+
cleanupTempPackageJson(functionPath);
|
|
166
|
+
}
|
|
149
167
|
cleanupBundle().catch((err) => console.warn('Bundle cleanup failed:', err));
|
|
150
168
|
}
|
|
151
169
|
start();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FunctionResource } from '../types.js';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function transpileFunction(resource: FunctionResource): Promise<{
|
|
3
3
|
type: string;
|
|
4
4
|
outputDir: string;
|
|
5
5
|
warnings: string[];
|
|
@@ -8,25 +8,25 @@ import { findFunctionEntryPoint } from '../functions/find-entry-point.js';
|
|
|
8
8
|
import { cleanupSourceMaps } from './cleanup-source-maps.js';
|
|
9
9
|
import { findDirUp } from './find-up.js';
|
|
10
10
|
import { verifyHandler } from './verify-handler.js';
|
|
11
|
-
export async function
|
|
11
|
+
export async function transpileFunction(resource) {
|
|
12
12
|
if (!resource.src)
|
|
13
13
|
throw new Error('Resource src is required');
|
|
14
14
|
if (!resource.name)
|
|
15
15
|
throw new Error('Resource name is required');
|
|
16
16
|
const timings = {};
|
|
17
|
-
const
|
|
17
|
+
const transpileStart = performance.now();
|
|
18
18
|
const sourcePath = path.resolve(cwd(), resource.src);
|
|
19
19
|
const stats = await stat(sourcePath);
|
|
20
20
|
const fnDisplayName = resource.displayName ?? resource.name;
|
|
21
21
|
const findEntryStart = performance.now();
|
|
22
22
|
const entry = await findFunctionEntryPoint(sourcePath, fnDisplayName);
|
|
23
|
-
timings['
|
|
23
|
+
timings['transpile:findEntry'] = performance.now() - findEntryStart;
|
|
24
24
|
const entryDir = stats.isFile() ? path.dirname(sourcePath) : sourcePath;
|
|
25
25
|
const outputPathStart = performance.now();
|
|
26
|
-
const outputDir = await
|
|
26
|
+
const outputDir = await getTranspileOutputPath(entryDir, resource.name);
|
|
27
27
|
const outputFile = path.join(outputDir, getOutputFilename(entry));
|
|
28
28
|
const fnRootDir = (await findDirUp('node_modules', entryDir)) || entryDir;
|
|
29
|
-
timings['
|
|
29
|
+
timings['transpile:setupOutput'] = performance.now() - outputPathStart;
|
|
30
30
|
async function cleanupTmpDir() {
|
|
31
31
|
// Feel a certain way about leaving things uncleaned, but helps with debugging for now
|
|
32
32
|
// await rm(outputDir, {recursive: true, force: true}).catch(logCleanupFailure)
|
|
@@ -48,28 +48,32 @@ export async function bundleFunction(resource) {
|
|
|
48
48
|
output: {
|
|
49
49
|
format: 'esm',
|
|
50
50
|
entryFileNames: getOutputFilename(entry),
|
|
51
|
+
// Do NOT inline anything from node_modules
|
|
52
|
+
preserveModules: true, // Key setting
|
|
53
|
+
preserveModulesRoot: fnRootDir,
|
|
51
54
|
},
|
|
55
|
+
external: [/node_modules/], // treat all node_modules as external
|
|
52
56
|
},
|
|
53
57
|
},
|
|
54
58
|
ssr: {
|
|
55
|
-
noExternal:
|
|
59
|
+
noExternal: [], // Do NOT bundle node_modules
|
|
56
60
|
resolve: {
|
|
57
|
-
conditions: ['
|
|
61
|
+
conditions: ['node'],
|
|
58
62
|
},
|
|
59
63
|
},
|
|
60
64
|
plugins: [tsConfigPaths()],
|
|
61
65
|
});
|
|
62
|
-
timings['
|
|
66
|
+
timings['transpile:build'] = performance.now() - viteStart;
|
|
63
67
|
const verifyStart = performance.now();
|
|
64
68
|
await verifyHandler(result);
|
|
65
|
-
timings['
|
|
69
|
+
timings['transpile:verify'] = performance.now() - verifyStart;
|
|
66
70
|
const pkgStart = performance.now();
|
|
67
|
-
await
|
|
68
|
-
timings['
|
|
71
|
+
await writeTranspiledPackageJson(entryDir, outputFile);
|
|
72
|
+
timings['transpile:writePackage'] = performance.now() - pkgStart;
|
|
69
73
|
const cleanupStart = performance.now();
|
|
70
74
|
await cleanupSourceMaps(sourcePath, outputDir);
|
|
71
|
-
timings['
|
|
72
|
-
timings.bundle = performance.now() -
|
|
75
|
+
timings['transpile:cleanupMaps'] = performance.now() - cleanupStart;
|
|
76
|
+
timings.bundle = performance.now() - transpileStart;
|
|
73
77
|
return {
|
|
74
78
|
type: 'success',
|
|
75
79
|
outputDir,
|
|
@@ -80,12 +84,12 @@ export async function bundleFunction(resource) {
|
|
|
80
84
|
}
|
|
81
85
|
catch (err) {
|
|
82
86
|
await cleanupTmpDir();
|
|
83
|
-
throw new Error(`
|
|
87
|
+
throw new Error(`Transpiling of function failed: ${err instanceof Error ? err.message : err}`, {
|
|
84
88
|
cause: err,
|
|
85
89
|
});
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
|
-
async function
|
|
92
|
+
async function writeTranspiledPackageJson(inputDir, outputFilePath) {
|
|
89
93
|
const baseName = path.basename(outputFilePath);
|
|
90
94
|
let original;
|
|
91
95
|
try {
|
|
@@ -107,7 +111,7 @@ async function writeBundledPackageJson(inputDir, outputFilePath) {
|
|
|
107
111
|
const pkgJsonOutputPath = path.join(path.dirname(outputFilePath), 'package.json');
|
|
108
112
|
await writeFile(pkgJsonOutputPath, JSON.stringify(bundled, null, 2));
|
|
109
113
|
}
|
|
110
|
-
async function
|
|
114
|
+
async function getTranspileOutputPath(entryDir, fnName) {
|
|
111
115
|
const tmpPath = path.resolve(entryDir, '.build', `function-${fnName}`);
|
|
112
116
|
await rm(tmpPath, { recursive: true, force: true }).catch(logCleanupFailure);
|
|
113
117
|
await mkdir(tmpPath, { recursive: true });
|
|
@@ -3,7 +3,7 @@ export async function verifyHandler(result) {
|
|
|
3
3
|
throw new Error('Incorrect build output, got watcher');
|
|
4
4
|
}
|
|
5
5
|
const outputs = (Array.isArray(result) ? result : [result]).flatMap(({ output }) => output);
|
|
6
|
-
const bundledIndex = outputs.find((output) => output.type === 'chunk' && output.isEntry && output.
|
|
6
|
+
const bundledIndex = outputs.find((output) => output.type === 'chunk' && output.isEntry && output.fileName === 'index.js');
|
|
7
7
|
if (!bundledIndex || bundledIndex.type !== 'chunk') {
|
|
8
8
|
throw new Error('Unexpected build output, no bundled index found');
|
|
9
9
|
}
|
package/dist/utils/types.d.ts
CHANGED
|
@@ -71,7 +71,8 @@ export declare function isLocalFunctionResource(r: Resource): r is FunctionResou
|
|
|
71
71
|
/** @internal */
|
|
72
72
|
export interface FunctionResource extends Resource {
|
|
73
73
|
src?: string;
|
|
74
|
-
|
|
74
|
+
autoResolveDeps?: boolean;
|
|
75
|
+
transpile?: boolean;
|
|
75
76
|
memory?: number;
|
|
76
77
|
timeout?: number;
|
|
77
78
|
env?: Record<string, string>;
|
package/oclif.manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/runtime-cli",
|
|
3
3
|
"description": "Sanity's Runtime CLI for Blueprints and Functions",
|
|
4
|
-
"version": "8.
|
|
4
|
+
"version": "8.3.0",
|
|
5
5
|
"author": "Sanity Runtime Team",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -69,11 +69,14 @@
|
|
|
69
69
|
"postpack": "shx rm -f oclif.manifest.json",
|
|
70
70
|
"pretest": "cd test/integration && npm install",
|
|
71
71
|
"test": "vitest run",
|
|
72
|
+
"test:depmgmt": "vitest run --config ./test-depmgmt/vitest.config.ts",
|
|
72
73
|
"posttest": "npm run lint",
|
|
73
74
|
"test:watch": "vitest",
|
|
74
75
|
"watch": "tsc --watch"
|
|
75
76
|
},
|
|
76
77
|
"dependencies": {
|
|
78
|
+
"@architect/hydrate": "^4.0.8",
|
|
79
|
+
"@architect/inventory": "^4.0.9",
|
|
77
80
|
"@oclif/core": "^4.3.0",
|
|
78
81
|
"@oclif/plugin-help": "^6.2.28",
|
|
79
82
|
"adm-zip": "^0.5.16",
|
|
@@ -102,7 +105,9 @@
|
|
|
102
105
|
"@oclif/test": "^4.1.13",
|
|
103
106
|
"@playwright/test": "^1.52.0",
|
|
104
107
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
108
|
+
"@sanity/blueprints": "^0.1.0",
|
|
105
109
|
"@sanity/client": "^7.3.0",
|
|
110
|
+
"@sanity/functions": "^1.0.3",
|
|
106
111
|
"@types/adm-zip": "^0.5.7",
|
|
107
112
|
"@types/cardinal": "^2.1.1",
|
|
108
113
|
"@types/mime-types": "^2.1.4",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|