@sanity/runtime-cli 17.2.0-next.ga4b9ae7 → 17.3.0-blueprinit.g6a63623
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 +36 -27
- package/SKILL.md +21 -0
- package/dist/actions/blueprinit/manifest.d.ts +13 -0
- package/dist/actions/blueprinit/manifest.js +57 -0
- package/dist/actions/blueprinit/studio.d.ts +6 -0
- package/dist/actions/blueprinit/studio.js +43 -0
- package/dist/actions/blueprinit/templates.d.ts +34 -0
- package/dist/actions/blueprinit/templates.js +83 -0
- package/dist/actions/blueprinit/workspace.d.ts +15 -0
- package/dist/actions/blueprinit/workspace.js +142 -0
- package/dist/actions/sanity/datasets.d.ts +15 -0
- package/dist/actions/sanity/datasets.js +26 -0
- package/dist/commands/blueprinit.d.ts +17 -0
- package/dist/commands/blueprinit.js +59 -0
- package/dist/commands/functions/env/add.d.ts +3 -0
- package/dist/commands/functions/env/add.js +6 -2
- package/dist/commands/functions/env/list.d.ts +3 -0
- package/dist/commands/functions/env/list.js +9 -3
- package/dist/commands/functions/env/remove.d.ts +3 -0
- package/dist/commands/functions/env/remove.js +9 -3
- package/dist/cores/blueprinit/adopt.d.ts +6 -0
- package/dist/cores/blueprinit/adopt.js +108 -0
- package/dist/cores/blueprinit/finish.d.ts +14 -0
- package/dist/cores/blueprinit/finish.js +64 -0
- package/dist/cores/blueprinit/fresh.d.ts +3 -0
- package/dist/cores/blueprinit/fresh.js +108 -0
- package/dist/cores/blueprinit/preview.d.ts +14 -0
- package/dist/cores/blueprinit/preview.js +13 -0
- package/dist/cores/blueprinit/prompts.d.ts +4 -0
- package/dist/cores/blueprinit/prompts.js +16 -0
- package/dist/cores/blueprinit/selection.d.ts +15 -0
- package/dist/cores/blueprinit/selection.js +90 -0
- package/dist/cores/blueprinit/types.d.ts +16 -0
- package/dist/cores/blueprinit/types.js +1 -0
- package/dist/cores/blueprinit.d.ts +7 -0
- package/dist/cores/blueprinit.js +48 -0
- package/dist/server/static/vendor/vendor.bundle.js +10 -14
- package/oclif.manifest.json +135 -7
- package/package.json +19 -15
- package/skills/blueprinit.md +148 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Logger } from '../../utils/logger.js';
|
|
2
|
+
import type { ActionResponse } from '../../utils/types.js';
|
|
3
|
+
export interface Dataset {
|
|
4
|
+
name: string;
|
|
5
|
+
aclMode: string;
|
|
6
|
+
}
|
|
7
|
+
interface ListDatasetsResponse extends ActionResponse {
|
|
8
|
+
datasets: Dataset[];
|
|
9
|
+
}
|
|
10
|
+
export declare function listDatasets({ token, projectId, logger, }: {
|
|
11
|
+
token: string;
|
|
12
|
+
projectId: string;
|
|
13
|
+
logger: Logger;
|
|
14
|
+
}): Promise<ListDatasetsResponse>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createTracedFetch } from '../../utils/traced-fetch.js';
|
|
2
|
+
import { projectsApiPath } from './projects.js';
|
|
3
|
+
export async function listDatasets({ token, projectId, logger, }) {
|
|
4
|
+
const fetchFn = createTracedFetch(logger);
|
|
5
|
+
const url = `${projectsApiPath}/${encodeURIComponent(projectId)}/datasets`;
|
|
6
|
+
const response = await fetchFn(url, {
|
|
7
|
+
method: 'GET',
|
|
8
|
+
headers: {
|
|
9
|
+
Authorization: `Bearer ${token}`,
|
|
10
|
+
'Content-Type': 'application/json',
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
const body = await response.json();
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
return {
|
|
16
|
+
ok: false,
|
|
17
|
+
error: body.error?.message ?? response.statusText,
|
|
18
|
+
datasets: [],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
ok: true,
|
|
23
|
+
error: null,
|
|
24
|
+
datasets: body,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RuntimeCommand } from '../baseCommands.js';
|
|
2
|
+
export default class BlueprinitCommand extends RuntimeCommand<typeof BlueprinitCommand> {
|
|
3
|
+
static hidden: boolean;
|
|
4
|
+
static summary: string;
|
|
5
|
+
static description: string;
|
|
6
|
+
static examples: string[];
|
|
7
|
+
static flags: {
|
|
8
|
+
llms: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
9
|
+
yes: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
10
|
+
'organization-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
'project-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
dataset: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
'stack-name': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
14
|
+
'studio-name': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
15
|
+
};
|
|
16
|
+
run(): Promise<Record<string, unknown> | undefined>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Flags } from '@oclif/core';
|
|
2
|
+
import { llmsFlagConfig } from '@sanity-labs/oclif-plugin-skills-flag';
|
|
3
|
+
import { RuntimeCommand } from '../baseCommands.js';
|
|
4
|
+
import { blueprinitCore } from '../cores/blueprinit.js';
|
|
5
|
+
import { Logger } from '../utils/logger.js';
|
|
6
|
+
export default class BlueprinitCommand extends RuntimeCommand {
|
|
7
|
+
static hidden = true;
|
|
8
|
+
static summary = 'Experimental blueprints-first project init';
|
|
9
|
+
static description = `Scaffolds a Blueprint, Studio, and starter function around an existing Sanity project.
|
|
10
|
+
|
|
11
|
+
This hidden command is an experiment; its interface and generated files are not stable.`;
|
|
12
|
+
static examples = [
|
|
13
|
+
'<%= config.bin %> <%= command.id %>',
|
|
14
|
+
'<%= config.bin %> <%= command.id %> --yes',
|
|
15
|
+
'<%= config.bin %> <%= command.id %> --project-id <id> --dataset <name> --yes',
|
|
16
|
+
];
|
|
17
|
+
static flags = {
|
|
18
|
+
llms: Flags.boolean(llmsFlagConfig),
|
|
19
|
+
yes: Flags.boolean({
|
|
20
|
+
char: 'y',
|
|
21
|
+
description: 'Skip confirmation prompts',
|
|
22
|
+
default: false,
|
|
23
|
+
}),
|
|
24
|
+
'organization-id': Flags.string({
|
|
25
|
+
description: 'Organization to use when the account has several',
|
|
26
|
+
aliases: ['organization', 'organizationId', 'org'],
|
|
27
|
+
}),
|
|
28
|
+
'project-id': Flags.string({
|
|
29
|
+
description: 'Project to use when the organization has several',
|
|
30
|
+
aliases: ['project', 'projectId'],
|
|
31
|
+
}),
|
|
32
|
+
dataset: Flags.string({
|
|
33
|
+
description: 'Dataset to attach',
|
|
34
|
+
}),
|
|
35
|
+
'stack-name': Flags.string({
|
|
36
|
+
description: 'Name for the new organization-scoped Stack',
|
|
37
|
+
}),
|
|
38
|
+
'studio-name': Flags.string({
|
|
39
|
+
description: 'Name for the Studio resource',
|
|
40
|
+
}),
|
|
41
|
+
};
|
|
42
|
+
async run() {
|
|
43
|
+
const result = await blueprinitCore({
|
|
44
|
+
bin: this.config.bin,
|
|
45
|
+
log: Logger(this.log.bind(this), this.flags),
|
|
46
|
+
flags: {
|
|
47
|
+
yes: this.flags.yes,
|
|
48
|
+
organizationId: this.flags['organization-id'],
|
|
49
|
+
projectId: this.flags['project-id'],
|
|
50
|
+
datasetName: this.flags.dataset,
|
|
51
|
+
stackName: this.flags['stack-name'],
|
|
52
|
+
studioName: this.flags['studio-name'],
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
if (!result.success)
|
|
56
|
+
this.coreError(result);
|
|
57
|
+
return result.json;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -8,6 +8,9 @@ export default class EnvAddCommand extends ResolvedCommand<typeof EnvAddCommand>
|
|
|
8
8
|
key: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
9
9
|
value: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
10
10
|
};
|
|
11
|
+
static flags: {
|
|
12
|
+
stack: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
|
+
};
|
|
11
14
|
static examples: string[];
|
|
12
15
|
run(): Promise<Record<string, unknown> | undefined>;
|
|
13
16
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Args } from '@oclif/core';
|
|
2
|
-
import { ResolvedCommand } from '../../../baseCommands.js';
|
|
1
|
+
import { Args, Flags } from '@oclif/core';
|
|
2
|
+
import { ResolvedCommand, stackFlagConfig } from '../../../baseCommands.js';
|
|
3
3
|
import { functionEnvAddCore } from '../../../cores/functions/env/add.js';
|
|
4
4
|
import { Logger } from '../../../utils/logger.js';
|
|
5
5
|
export default class EnvAddCommand extends ResolvedCommand {
|
|
@@ -13,8 +13,12 @@ Environment variables are useful for API keys, configuration values, and other s
|
|
|
13
13
|
key: Args.string({ description: 'The name of the environment variable', required: true }),
|
|
14
14
|
value: Args.string({ description: 'The value of the environment variable', required: true }),
|
|
15
15
|
};
|
|
16
|
+
static flags = {
|
|
17
|
+
stack: Flags.string({ ...stackFlagConfig }),
|
|
18
|
+
};
|
|
16
19
|
static examples = [
|
|
17
20
|
'<%= config.bin %> <%= command.id %> MyFunction API_URL https://api.example.com/',
|
|
21
|
+
'<%= config.bin %> <%= command.id %> --stack <name-or-id> MyFunction API_URL https://api.example.com/',
|
|
18
22
|
];
|
|
19
23
|
async run() {
|
|
20
24
|
const result = await functionEnvAddCore({
|
|
@@ -6,6 +6,9 @@ export default class EnvListCommand extends ResolvedCommand<typeof EnvListComman
|
|
|
6
6
|
static args: {
|
|
7
7
|
name: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
8
8
|
};
|
|
9
|
+
static flags: {
|
|
10
|
+
stack: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
11
|
+
};
|
|
9
12
|
static examples: string[];
|
|
10
13
|
run(): Promise<Record<string, unknown> | undefined>;
|
|
11
14
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Args } from '@oclif/core';
|
|
2
|
-
import { ResolvedCommand } from '../../../baseCommands.js';
|
|
1
|
+
import { Args, Flags } from '@oclif/core';
|
|
2
|
+
import { ResolvedCommand, stackFlagConfig } from '../../../baseCommands.js';
|
|
3
3
|
import { functionEnvListCore } from '../../../cores/functions/env/list.js';
|
|
4
4
|
import { Logger } from '../../../utils/logger.js';
|
|
5
5
|
export default class EnvListCommand extends ResolvedCommand {
|
|
@@ -11,7 +11,13 @@ Use 'functions env add' to set variables or 'functions env remove' to delete the
|
|
|
11
11
|
static args = {
|
|
12
12
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
13
13
|
};
|
|
14
|
-
static
|
|
14
|
+
static flags = {
|
|
15
|
+
stack: Flags.string({ ...stackFlagConfig }),
|
|
16
|
+
};
|
|
17
|
+
static examples = [
|
|
18
|
+
'<%= config.bin %> <%= command.id %> MyFunction',
|
|
19
|
+
'<%= config.bin %> <%= command.id %> --stack <name-or-id> MyFunction',
|
|
20
|
+
];
|
|
15
21
|
async run() {
|
|
16
22
|
const result = await functionEnvListCore({
|
|
17
23
|
bin: this.config.bin,
|
|
@@ -7,6 +7,9 @@ export default class EnvRemoveCommand extends ResolvedCommand<typeof EnvRemoveCo
|
|
|
7
7
|
name: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
8
8
|
key: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
|
|
9
9
|
};
|
|
10
|
+
static flags: {
|
|
11
|
+
stack: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
|
+
};
|
|
10
13
|
static examples: string[];
|
|
11
14
|
run(): Promise<Record<string, unknown> | undefined>;
|
|
12
15
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Args } from '@oclif/core';
|
|
2
|
-
import { ResolvedCommand } from '../../../baseCommands.js';
|
|
1
|
+
import { Args, Flags } from '@oclif/core';
|
|
2
|
+
import { ResolvedCommand, stackFlagConfig } from '../../../baseCommands.js';
|
|
3
3
|
import { functionEnvRemoveCore } from '../../../cores/functions/env/remove.js';
|
|
4
4
|
import { Logger } from '../../../utils/logger.js';
|
|
5
5
|
export default class EnvRemoveCommand extends ResolvedCommand {
|
|
@@ -12,7 +12,13 @@ Use 'functions env list' to see current variables before removing.`;
|
|
|
12
12
|
name: Args.string({ description: 'The name of the Sanity Function', required: true }),
|
|
13
13
|
key: Args.string({ description: 'The name of the environment variable', required: true }),
|
|
14
14
|
};
|
|
15
|
-
static
|
|
15
|
+
static flags = {
|
|
16
|
+
stack: Flags.string({ ...stackFlagConfig }),
|
|
17
|
+
};
|
|
18
|
+
static examples = [
|
|
19
|
+
'<%= config.bin %> <%= command.id %> MyFunction API_URL',
|
|
20
|
+
'<%= config.bin %> <%= command.id %> --stack <name-or-id> MyFunction API_URL',
|
|
21
|
+
];
|
|
16
22
|
async run() {
|
|
17
23
|
const result = await functionEnvRemoveCore({
|
|
18
24
|
bin: this.config.bin,
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DetectedStudio } from '../../actions/blueprinit/studio.js';
|
|
2
|
+
import type { CoreResult } from '../index.js';
|
|
3
|
+
import type { BlueprinitFlowOptions } from './types.js';
|
|
4
|
+
export declare function adoptFlow(options: BlueprinitFlowOptions & {
|
|
5
|
+
studio: DetectedStudio;
|
|
6
|
+
}): Promise<CoreResult>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { slugify } from '../../actions/blueprinit/manifest.js';
|
|
4
|
+
import { adoptWorkspace } from '../../actions/blueprinit/workspace.js';
|
|
5
|
+
import { listDatasets } from '../../actions/sanity/datasets.js';
|
|
6
|
+
import { groupProjectsByOrganization } from '../../actions/sanity/projects.js';
|
|
7
|
+
import { check } from '../../utils/display/presenters.js';
|
|
8
|
+
import { getLatestNpmVersion } from '../../utils/external/npmjs.js';
|
|
9
|
+
import { isInteractive } from '../../utils/interactive.js';
|
|
10
|
+
import { createStackAndFinish } from './finish.js';
|
|
11
|
+
import { printBlueprintPreview } from './preview.js';
|
|
12
|
+
import { confirmUnlessSkipped, promptForStudioName } from './prompts.js';
|
|
13
|
+
import { findProject } from './selection.js';
|
|
14
|
+
export async function adoptFlow(options) {
|
|
15
|
+
const { bin, dir, flags, log, studio, token } = options;
|
|
16
|
+
if (flags.projectId && studio.projectId && flags.projectId !== studio.projectId) {
|
|
17
|
+
return { success: false, error: `The Studio uses project "${studio.projectId}".` };
|
|
18
|
+
}
|
|
19
|
+
if (flags.datasetName && studio.dataset && flags.datasetName !== studio.dataset) {
|
|
20
|
+
return { success: false, error: `The Studio uses dataset "${studio.dataset}".` };
|
|
21
|
+
}
|
|
22
|
+
const projectId = studio.projectId ?? flags.projectId;
|
|
23
|
+
const datasetName = studio.dataset ?? flags.datasetName;
|
|
24
|
+
if (!projectId || !datasetName) {
|
|
25
|
+
return {
|
|
26
|
+
success: false,
|
|
27
|
+
error: `Found ${studio.configFile}, but could not read a projectId and dataset from it.`,
|
|
28
|
+
suggestions: ['Use literal values in the Studio config or pass --project-id and --dataset.'],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
log(check(`Studio detected (${studio.configFile})`));
|
|
32
|
+
const grouped = await groupProjectsByOrganization({ token, logger: log });
|
|
33
|
+
if (!grouped.ok) {
|
|
34
|
+
return { success: false, error: `Could not list organizations: ${grouped.error}` };
|
|
35
|
+
}
|
|
36
|
+
const match = findProject(grouped.organizations, projectId);
|
|
37
|
+
if (!match) {
|
|
38
|
+
return {
|
|
39
|
+
success: false,
|
|
40
|
+
error: `Project "${projectId}" from ${studio.configFile} was not found in your account.`,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (flags.organizationId && flags.organizationId !== match.organization.id) {
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
error: `Project "${studio.projectId}" belongs to organization "${match.organization.id}".`,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const datasets = await listDatasets({ token, projectId: match.project.id, logger: log });
|
|
50
|
+
if (!datasets.ok) {
|
|
51
|
+
return { success: false, error: `Could not list datasets: ${datasets.error}` };
|
|
52
|
+
}
|
|
53
|
+
if (!datasets.datasets.some(({ name }) => name === datasetName)) {
|
|
54
|
+
return {
|
|
55
|
+
success: false,
|
|
56
|
+
error: `Dataset "${datasetName}" was not found in project ${match.project.displayName}.`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
log(check(`Matches project ${match.project.displayName} (${match.project.id}), dataset ${datasetName}`));
|
|
60
|
+
log('');
|
|
61
|
+
const studioName = await promptForStudioName(flags, isInteractive);
|
|
62
|
+
const stackName = flags.stackName
|
|
63
|
+
? slugify(flags.stackName) || match.project.id
|
|
64
|
+
: slugify(match.project.displayName) || match.project.id;
|
|
65
|
+
const marker = (file) => (existsSync(join(dir, file)) ? '~' : '+');
|
|
66
|
+
printBlueprintPreview({
|
|
67
|
+
log,
|
|
68
|
+
organizationName: match.organization.name,
|
|
69
|
+
projectId: match.project.id,
|
|
70
|
+
projectName: match.project.displayName,
|
|
71
|
+
datasetName,
|
|
72
|
+
studioName,
|
|
73
|
+
studioSource: '.',
|
|
74
|
+
stackName,
|
|
75
|
+
});
|
|
76
|
+
log('Changes:');
|
|
77
|
+
log(" + sanity.blueprint.ts attach project + dataset; adopt Studio at '.'");
|
|
78
|
+
log(` + .sanity/blueprint.config.json org scope, stack ${stackName}`);
|
|
79
|
+
log(` ${marker('.gitignore')} .gitignore`);
|
|
80
|
+
log(` ${marker('package.json')} package.json`);
|
|
81
|
+
log('');
|
|
82
|
+
const confirmed = await confirmUnlessSkipped(flags, isInteractive, 'Adopt this Studio into the Blueprint?');
|
|
83
|
+
if (!confirmed)
|
|
84
|
+
return { success: false, error: 'Initialization cancelled.' };
|
|
85
|
+
const adoption = adoptWorkspace({
|
|
86
|
+
dir,
|
|
87
|
+
projectId: match.project.id,
|
|
88
|
+
displayName: match.project.displayName,
|
|
89
|
+
datasetName,
|
|
90
|
+
stackName,
|
|
91
|
+
studioName,
|
|
92
|
+
blueprintsVersion: await getLatestNpmVersion('@sanity/blueprints', log),
|
|
93
|
+
});
|
|
94
|
+
return await createStackAndFinish({
|
|
95
|
+
...options,
|
|
96
|
+
organizationId: match.organization.id,
|
|
97
|
+
projectId: match.project.id,
|
|
98
|
+
datasetName,
|
|
99
|
+
stackName,
|
|
100
|
+
blueprintFilePath: adoption.blueprintFilePath,
|
|
101
|
+
files: adoption.files,
|
|
102
|
+
nextSteps: [
|
|
103
|
+
` npx ${bin} blueprints plan`,
|
|
104
|
+
` npx ${bin} blueprints deploy`,
|
|
105
|
+
` npx ${bin} blueprints info`,
|
|
106
|
+
],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CoreResult } from '../index.js';
|
|
2
|
+
import type { BlueprinitFlowOptions } from './types.js';
|
|
3
|
+
interface FinishOptions extends BlueprinitFlowOptions {
|
|
4
|
+
organizationId: string;
|
|
5
|
+
projectId: string;
|
|
6
|
+
datasetName: string;
|
|
7
|
+
stackName: string;
|
|
8
|
+
blueprintFilePath: string;
|
|
9
|
+
files: string[];
|
|
10
|
+
nextSteps: string[];
|
|
11
|
+
}
|
|
12
|
+
export declare function createStackAndFinish(options: FinishOptions): Promise<CoreResult>;
|
|
13
|
+
export declare function alignSteps(steps: Array<[command: string, description?: string]>): string[];
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { writeConfigFile } from '../../actions/blueprints/config.js';
|
|
2
|
+
import { createEmptyStack } from '../../actions/blueprints/stacks.js';
|
|
3
|
+
import { check } from '../../utils/display/presenters.js';
|
|
4
|
+
export async function createStackAndFinish(options) {
|
|
5
|
+
let stack;
|
|
6
|
+
try {
|
|
7
|
+
stack = await createEmptyStack({
|
|
8
|
+
token: options.token,
|
|
9
|
+
scopeType: 'organization',
|
|
10
|
+
scopeId: options.organizationId,
|
|
11
|
+
name: options.stackName,
|
|
12
|
+
logger: options.log,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
17
|
+
return {
|
|
18
|
+
success: false,
|
|
19
|
+
error: `Local files were written, but Stack creation failed: ${message}`,
|
|
20
|
+
suggestions: [
|
|
21
|
+
'Resolve the Stack error, then finish setup with:',
|
|
22
|
+
`npx ${options.bin} blueprints init --organization-id ${options.organizationId} --stack-name ${options.stackName}`,
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
writeConfigFile(options.blueprintFilePath, {
|
|
28
|
+
organizationId: options.organizationId,
|
|
29
|
+
stackId: stack.id,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
34
|
+
return {
|
|
35
|
+
success: false,
|
|
36
|
+
error: `Stack ${stack.id} was created, but the local Blueprint config could not be written: ${message}`,
|
|
37
|
+
suggestions: [
|
|
38
|
+
'Finish setup with:',
|
|
39
|
+
`npx ${options.bin} blueprints init --organization-id ${options.organizationId} --stack-id ${stack.id}`,
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
options.log(check(`Stack ${stack.name} created (org scope)`));
|
|
44
|
+
options.log('The Stack is empty; Blueprint resources have not been deployed.');
|
|
45
|
+
options.log('');
|
|
46
|
+
options.log('Next steps:');
|
|
47
|
+
for (const step of options.nextSteps)
|
|
48
|
+
options.log(step);
|
|
49
|
+
return {
|
|
50
|
+
success: true,
|
|
51
|
+
json: {
|
|
52
|
+
organizationId: options.organizationId,
|
|
53
|
+
projectId: options.projectId,
|
|
54
|
+
dataset: options.datasetName,
|
|
55
|
+
stackId: stack.id,
|
|
56
|
+
stackName: stack.name,
|
|
57
|
+
files: [...options.files, '.sanity/blueprint.config.json'],
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function alignSteps(steps) {
|
|
62
|
+
const width = Math.max(...steps.map(([command]) => command.length));
|
|
63
|
+
return steps.map(([command, description]) => description ? ` ${command.padEnd(width + 2)}${description}` : ` ${command}`);
|
|
64
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { STARTER_FUNCTION_NAME, slugify } from '../../actions/blueprinit/manifest.js';
|
|
2
|
+
import { scaffoldWorkspace } from '../../actions/blueprinit/workspace.js';
|
|
3
|
+
import { listDatasets } from '../../actions/sanity/datasets.js';
|
|
4
|
+
import { groupProjectsByOrganization } from '../../actions/sanity/projects.js';
|
|
5
|
+
import { check } from '../../utils/display/presenters.js';
|
|
6
|
+
import { getLatestNpmVersion } from '../../utils/external/npmjs.js';
|
|
7
|
+
import { isInteractive } from '../../utils/interactive.js';
|
|
8
|
+
import { alignSteps, createStackAndFinish } from './finish.js';
|
|
9
|
+
import { printBlueprintPreview } from './preview.js';
|
|
10
|
+
import { confirmUnlessSkipped, promptForStudioName } from './prompts.js';
|
|
11
|
+
import { chooseDataset, chooseOrganization, chooseProject } from './selection.js';
|
|
12
|
+
export async function freshFlow(options) {
|
|
13
|
+
const { bin, dir, flags, log, token } = options;
|
|
14
|
+
const grouped = await groupProjectsByOrganization({ token, logger: log });
|
|
15
|
+
if (!grouped.ok) {
|
|
16
|
+
return { success: false, error: `Could not list organizations: ${grouped.error}` };
|
|
17
|
+
}
|
|
18
|
+
const organizationResult = await chooseOrganization(grouped.organizations, flags.organizationId, isInteractive);
|
|
19
|
+
if (!organizationResult.ok) {
|
|
20
|
+
return {
|
|
21
|
+
success: false,
|
|
22
|
+
error: organizationResult.error.message,
|
|
23
|
+
suggestions: organizationResult.error.suggestions,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const { organization, projects = [] } = organizationResult.value;
|
|
27
|
+
const projectResult = await chooseProject(projects, flags.projectId, organization.name, isInteractive);
|
|
28
|
+
if (!projectResult.ok) {
|
|
29
|
+
return {
|
|
30
|
+
success: false,
|
|
31
|
+
error: projectResult.error.message,
|
|
32
|
+
suggestions: projectResult.error.suggestions,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const project = projectResult.value;
|
|
36
|
+
const datasets = await listDatasets({ token, projectId: project.id, logger: log });
|
|
37
|
+
if (!datasets.ok) {
|
|
38
|
+
return { success: false, error: `Could not list datasets: ${datasets.error}` };
|
|
39
|
+
}
|
|
40
|
+
const datasetResult = await chooseDataset(datasets.datasets, flags.datasetName, project.displayName, isInteractive);
|
|
41
|
+
if (!datasetResult.ok) {
|
|
42
|
+
return {
|
|
43
|
+
success: false,
|
|
44
|
+
error: datasetResult.error.message,
|
|
45
|
+
suggestions: datasetResult.error.suggestions,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const dataset = datasetResult.value;
|
|
49
|
+
log(check(`Org: ${organization.name} (${organization.id})`));
|
|
50
|
+
log(check(`Project: ${project.displayName} (${project.id}), dataset: ${dataset.name}`));
|
|
51
|
+
log('');
|
|
52
|
+
const stackName = flags.stackName
|
|
53
|
+
? slugify(flags.stackName) || project.id
|
|
54
|
+
: slugify(project.displayName) || project.id;
|
|
55
|
+
const studioName = await promptForStudioName(flags, isInteractive);
|
|
56
|
+
printBlueprintPreview({
|
|
57
|
+
log,
|
|
58
|
+
organizationName: organization.name,
|
|
59
|
+
projectId: project.id,
|
|
60
|
+
projectName: project.displayName,
|
|
61
|
+
datasetName: dataset.name,
|
|
62
|
+
studioName,
|
|
63
|
+
studioSource: `studios/${studioName}`,
|
|
64
|
+
stackName,
|
|
65
|
+
functionName: STARTER_FUNCTION_NAME,
|
|
66
|
+
});
|
|
67
|
+
log('Files to write:');
|
|
68
|
+
log(' + sanity.blueprint.ts project + dataset (attached), studio, function');
|
|
69
|
+
log(` + ${`studios/${studioName}/`.padEnd(32)}Studio with a "post" type`);
|
|
70
|
+
log(` + ${`functions/${STARTER_FUNCTION_NAME}/`.padEnd(32)}sets a post slug from its title`);
|
|
71
|
+
log(` + .sanity/blueprint.config.json org scope, stack ${stackName}`);
|
|
72
|
+
log(' + .gitignore');
|
|
73
|
+
log(' + package.json');
|
|
74
|
+
log('');
|
|
75
|
+
const confirmed = await confirmUnlessSkipped(flags, isInteractive, `Create this Blueprint workspace for "${project.displayName}"?`);
|
|
76
|
+
if (!confirmed)
|
|
77
|
+
return { success: false, error: 'Initialization cancelled.' };
|
|
78
|
+
const scaffold = scaffoldWorkspace({
|
|
79
|
+
dir,
|
|
80
|
+
projectId: project.id,
|
|
81
|
+
displayName: project.displayName,
|
|
82
|
+
datasetName: dataset.name,
|
|
83
|
+
stackName,
|
|
84
|
+
studioName,
|
|
85
|
+
blueprintsVersion: await getLatestNpmVersion('@sanity/blueprints', log),
|
|
86
|
+
});
|
|
87
|
+
return await createStackAndFinish({
|
|
88
|
+
...options,
|
|
89
|
+
organizationId: organization.id,
|
|
90
|
+
projectId: project.id,
|
|
91
|
+
datasetName: dataset.name,
|
|
92
|
+
stackName,
|
|
93
|
+
blueprintFilePath: scaffold.blueprintFilePath,
|
|
94
|
+
files: scaffold.files,
|
|
95
|
+
nextSteps: [
|
|
96
|
+
...alignSteps([
|
|
97
|
+
['npm install'],
|
|
98
|
+
[`npm run dev -w studios/${studioName}`, 'open the Studio and add a post'],
|
|
99
|
+
[`npx ${bin} blueprints plan`, 'preview the deployment'],
|
|
100
|
+
[`npx ${bin} blueprints deploy`, 'deploy the resources'],
|
|
101
|
+
]),
|
|
102
|
+
'',
|
|
103
|
+
'Test the function on that post:',
|
|
104
|
+
` npx ${bin} functions test ${STARTER_FUNCTION_NAME} --document-id <id>` +
|
|
105
|
+
` --project-id ${project.id} --dataset ${dataset.name} --with-user-token`,
|
|
106
|
+
],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Logger } from '../../utils/logger.js';
|
|
2
|
+
interface BlueprintPreviewOptions {
|
|
3
|
+
log: Logger;
|
|
4
|
+
organizationName: string;
|
|
5
|
+
projectId: string;
|
|
6
|
+
projectName: string;
|
|
7
|
+
datasetName: string;
|
|
8
|
+
studioName: string;
|
|
9
|
+
studioSource: string;
|
|
10
|
+
stackName: string;
|
|
11
|
+
functionName?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function printBlueprintPreview(options: BlueprintPreviewOptions): void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function printBlueprintPreview(options) {
|
|
2
|
+
options.log('Blueprint plan:');
|
|
3
|
+
options.log(` attach Project ${options.projectName} (${options.projectId}) on deploy; retain on Stack removal`);
|
|
4
|
+
options.log(` attach Dataset ${options.datasetName} on deploy; retain on Stack removal`);
|
|
5
|
+
options.log(` create Studio ${options.studioName} from ${options.studioSource} on deploy`);
|
|
6
|
+
if (options.functionName) {
|
|
7
|
+
options.log(` create Function ${options.functionName} on deploy`);
|
|
8
|
+
}
|
|
9
|
+
options.log('');
|
|
10
|
+
options.log('Remote action now:');
|
|
11
|
+
options.log(` create Stack ${options.stackName} in ${options.organizationName} (empty, organization scope)`);
|
|
12
|
+
options.log('');
|
|
13
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { BlueprinitFlags } from './types.js';
|
|
2
|
+
export declare const DEFAULT_STUDIO_NAME = "studio";
|
|
3
|
+
export declare function confirmUnlessSkipped(flags: BlueprinitFlags, interactive: boolean, message: string): Promise<boolean>;
|
|
4
|
+
export declare function promptForStudioName(flags: BlueprinitFlags, interactive: boolean): Promise<string>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { confirm, input } from '@inquirer/prompts';
|
|
2
|
+
import { slugify } from '../../actions/blueprinit/manifest.js';
|
|
3
|
+
export const DEFAULT_STUDIO_NAME = 'studio';
|
|
4
|
+
export async function confirmUnlessSkipped(flags, interactive, message) {
|
|
5
|
+
if (flags.yes || !interactive)
|
|
6
|
+
return true;
|
|
7
|
+
return await confirm({ message, default: true });
|
|
8
|
+
}
|
|
9
|
+
export async function promptForStudioName(flags, interactive) {
|
|
10
|
+
if (flags.studioName)
|
|
11
|
+
return slugify(flags.studioName) || DEFAULT_STUDIO_NAME;
|
|
12
|
+
if (flags.yes || !interactive)
|
|
13
|
+
return DEFAULT_STUDIO_NAME;
|
|
14
|
+
const answer = await input({ message: 'Name your studio?', default: DEFAULT_STUDIO_NAME });
|
|
15
|
+
return slugify(answer) || DEFAULT_STUDIO_NAME;
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Dataset } from '../../actions/sanity/datasets.js';
|
|
2
|
+
import type { GroupedProjects, Project } from '../../actions/sanity/projects.js';
|
|
3
|
+
import type { Result } from '../../utils/types.js';
|
|
4
|
+
interface SelectionError {
|
|
5
|
+
message: string;
|
|
6
|
+
suggestions?: string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function chooseOrganization(groups: GroupedProjects[], organizationId: string | undefined, interactive: boolean): Promise<Result<GroupedProjects, SelectionError>>;
|
|
9
|
+
export declare function chooseProject(projects: Project[], projectId: string | undefined, organizationName: string, interactive: boolean): Promise<Result<Project, SelectionError>>;
|
|
10
|
+
export declare function chooseDataset(datasets: Dataset[], datasetName: string | undefined, projectName: string, interactive: boolean): Promise<Result<Dataset, SelectionError>>;
|
|
11
|
+
export declare function findProject(groups: GroupedProjects[], projectId: string): {
|
|
12
|
+
organization: GroupedProjects['organization'];
|
|
13
|
+
project: Project;
|
|
14
|
+
} | undefined;
|
|
15
|
+
export {};
|