-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathcmd.ts
More file actions
293 lines (261 loc) · 8.5 KB
/
cmd.ts
File metadata and controls
293 lines (261 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { extensionArtifactCreator } from "./artifacts/extension.ts";
import { projectArtifactCreator } from "./artifacts/project.ts";
import { kEditorInfos, scanForEditors } from "./editor.ts";
import {
isInteractiveTerminal,
isPositWorkbench,
} from "../../core/platform.ts";
import { runningInCI } from "../../core/ci-info.ts";
import { Command } from "cliffy/command/mod.ts";
import { prompt, Select, SelectValueOptions } from "cliffy/prompt/mod.ts";
import { readLines } from "../../deno_ral/io.ts";
import { info } from "../../deno_ral/log.ts";
import { ArtifactCreator, CreateDirective, CreateResult } from "./cmd-types.ts";
// The registered artifact creators
const kArtifactCreators: ArtifactCreator[] = [
projectArtifactCreator,
extensionArtifactCreator,
// documentArtifactCreator, CT: Disabled for 1.2 as it arrived too late on the scene
];
export const createCommand = new Command()
.name("create")
.description("Create a Quarto project or extension")
.option(
"--open [editor:string]",
`Open new artifact in this editor (${
kEditorInfos.map((info) => info.id).join(", ")
})`,
)
.option("--no-open", "Do not open in an editor")
.option("--no-prompt", "Do not prompt to confirm actions")
.option("--json", "Pass serialized creation options via stdin", {
hidden: true,
})
.arguments("[type] [commands...]")
.action(
async (
options: {
prompt: boolean;
json?: boolean;
open?: string | boolean;
},
type?: string,
...commands: string[]
) => {
if (options.json) {
await createFromStdin();
} else {
// Compute a sane default for prompting
const isInteractive = isInteractiveTerminal() && !runningInCI();
const allowPrompt = isInteractive && !!options.prompt && !options.json;
// Specific case where opening automatically in an editor is not allowed
if (options.open !== false && isPositWorkbench()) {
if (options.open !== undefined) {
info(
`The --open option is not supported in Posit Workbench - ignoring`,
);
}
options.open = false;
}
// Resolve the type into an artifact
const resolved = await resolveArtifact(
type,
options.prompt,
);
const resolvedArtifact = resolved.artifact;
if (resolvedArtifact) {
// Resolve the arguments that the user provided into options
// for the artifact provider
// If we aliased the type, shift the args (including what was
// the type alias in the list of args for the artifact creator
// to resolve)
const args = commands;
const commandOpts = resolvedArtifact.resolveOptions(args);
const createOptions = {
cwd: Deno.cwd(),
options: commandOpts,
};
if (allowPrompt) {
// Prompt the user until the options have been fully realized
let nextPrompt = resolvedArtifact.nextPrompt(createOptions);
while (nextPrompt !== undefined) {
if (nextPrompt) {
const result = await prompt([nextPrompt]);
createOptions.options = {
...createOptions.options,
...result,
};
}
nextPrompt = resolvedArtifact.nextPrompt(createOptions);
}
}
// Complete the defaults
const createDirective = resolvedArtifact.finalizeOptions(
createOptions,
);
// Create the artifact using the options
const createResult = await resolvedArtifact.createArtifact(
createDirective,
);
// Now that the article was created, offer to open the item
if (allowPrompt && options.open !== false) {
const resolvedEditor = await resolveEditor(
createResult,
typeof (options.open) === "string" ? options.open : undefined,
);
if (resolvedEditor) {
resolvedEditor.open();
}
}
}
}
},
);
// Resolves the artifact string (or undefined) into an
// Artifact interface which will provide the functions
// needed to complete the creation
const resolveArtifact = async (type?: string, prompt?: boolean) => {
// Finds an artifact
const findArtifact = (type: string) => {
return kArtifactCreators.find((artifact) =>
artifact.type === type && artifact.enabled !== false
);
};
// Use the provided type to search (or prompt the user)
let artifact = type ? findArtifact(type) : undefined;
while (artifact === undefined) {
if (!prompt) {
// We can't prompt to resolve this, so just throw an Error
if (type) {
throw new Error(`Failed to create ${type} - the type isn't recognized`);
} else {
throw new Error(
`Creation failed - you must provide a type to create when using '--no-prompt'`,
);
}
}
if (type) {
// The user provided a type, but it isn't recognized
info(`Unknown type ${type} - please select from the following:`);
}
// Prompt the user to select a type
type = await promptForType();
// Find the type (this should always work since we provided the id)
artifact = findArtifact(type);
}
return {
artifact,
};
};
// Wrapper that will provide keyboard selection hint (if necessary)
async function promptSelect(
message: string,
options: SelectValueOptions,
) {
return await Select.prompt({
message,
options,
});
}
// Prompts from the type of artifact to create
const promptForType = async () => {
return await promptSelect(
"Create",
kArtifactCreators.map((artifact) => {
return {
name: artifact.displayName.toLowerCase(),
value: artifact.type,
};
}),
);
};
// Determine the selected editor that should be used to open
// the artifact once created
const resolveEditor = async (createResult: CreateResult, editor?: string) => {
// Find supported editors
const editors = await scanForEditors(kEditorInfos, createResult);
const defaultEditor = editors.find((ed) => {
return ed.id === editor;
});
if (defaultEditor) {
// If an editor is specified, use that
return defaultEditor;
} else {
// See if we are executing inside of an editor, and just use
// that editor
const inEditor = editors.find((ed) => ed.inEditor);
if (inEditor) {
return inEditor;
} else if (editors.length > 0) {
// Prompt the user to select an editor
const editorOptions = editors.map((editor) => {
return {
name: editor.name.toLowerCase(),
value: editor.name,
};
});
// Add an option to not open
const options = [...editorOptions, {
name: "(don't open)",
value: "do not open",
}];
const name = await promptSelect("Open With", options);
// Return the matching editor (if any)
const selectedEditor = editors.find((edit) => edit.name === name);
return selectedEditor;
} else {
return undefined;
}
}
};
async function createFromStdin() {
/* Read a single line (should be json) like:
{
type: "project",
directive: {
"directory" : "",
"template" : "",
"name": ""
}
}
*/
// Read the stdin and then close it
const { value: input } = await readLines(Deno.stdin).next();
Deno.stdin.close();
// Parse options
const jsonOptions = JSON.parse(input);
// A type is required in the JSON no matter what
const type = jsonOptions.type;
if (!type) {
throw new Error(
"The provided json for create artifacts must include a valid type",
);
}
// Validate other required fields
if (
!jsonOptions.directive || !jsonOptions.directive.name ||
!jsonOptions.directive.directory || !jsonOptions.directive.template
) {
throw new Error(
"The provided json for create artifacts must include a directive with a name, directory, and template.",
);
}
// Find the artifact creator
const resolved = await resolveArtifact(
type,
false,
);
const createDirective = jsonOptions.directive as CreateDirective;
// Create the artifact using the options
const createResult = await resolved.artifact.createArtifact(
createDirective,
true,
);
const resultJSON = JSON.stringify(createResult, undefined);
Deno.stdout.writeSync(new TextEncoder().encode(resultJSON));
}