Path: blob/main/src/command/create/artifacts/document.ts
3587 views
/*1* document.ts2*3* Copyright (C) 2020-2022 Posit Software, PBC4*5*/67import { ArtifactCreator, CreateContext } from "../cmd.ts";8import {9CreateDirective,10ejsData,11renderAndCopyArtifacts,12} from "./artifact-shared.ts";1314import { Input } from "cliffy/prompt/mod.ts";15import { join } from "../../../deno_ral/path.ts";1617import { safeExistsSync } from "../../../core/path.ts";18import { resourcePath } from "../../../core/resources.ts";1920const kFormat = "format";21const kTitle = "title";2223const kTypeDocument = "document";2425export const documentArtifactCreator: ArtifactCreator = {26displayName: "Document",27type: kTypeDocument,28resolveOptions,29finalizeOptions,30nextPrompt,31createArtifact,32};3334function resolveOptions(args: string[]): Record<string, unknown> {35// The first argument is the extension type36// The second argument is the name37const formatRaw = args.length > 0 ? args[0] : undefined;38const titleRaw = args.length > 1 ? args[1] : undefined;3940const options: Record<string, unknown> = {};4142// Populate the type data43if (formatRaw) {44options[kFormat] = formatRaw;45}4647// Populate a directory, if provided48if (titleRaw) {49options[kTitle] = titleRaw;50}5152return options;53}5455function finalizeOptions(createOptions: CreateContext) {56// There should be a name57if (!createOptions.options.title) {58throw new Error("Required property 'title' is missing.");59}6061// Form a template62const template = createOptions.options[kFormat];6364// Provide a directory and title65return {66displayType: "document",67name: createOptions.options[kTitle],68directory: createOptions.cwd,69template,70options: createOptions.options,71} as CreateDirective;72}7374function nextPrompt(75createOptions: CreateContext,76) {77// First ensure that there is a type78if (!createOptions.options[kFormat]) {79return {80name: kFormat,81message: "Format",82type: Input,83};84}8586// Collect a title87if (!createOptions.options[kTitle]) {88return {89name: kTitle,90message: "Document Title",91type: Input,92};93}94}9596async function createArtifact(97createDirective: CreateDirective,98quiet?: boolean,99) {100const fileName = await createDocument(createDirective, quiet);101102return {103path: fileName,104openfiles: [fileName],105};106}107108async function createDocument(109createDirective: CreateDirective,110quiet?: boolean,111) {112// The folder for this extension113const artifact = templateFolder(createDirective);114115// The target directory116const target = createDirective.directory;117118// Data for this extension119const data = await ejsData(createDirective);120if (createDirective.options[kFormat]) {121data[kFormat] = createDirective.options[kFormat] as string;122}123124// Render or copy the artifact125const filesCreated = renderAndCopyArtifacts(126target,127artifact,128createDirective,129data,130quiet,131);132133return filesCreated[0];134}135136function templateFolder(createDirective: CreateDirective) {137const basePath = resourcePath(join("create", "documents"));138const formatSpecificPath = join(basePath, createDirective.template);139if (safeExistsSync(formatSpecificPath)) {140return formatSpecificPath;141} else {142return join(basePath, "default");143}144}145146147