|
| 1 | +/* |
| 2 | +* config.ts |
| 3 | +* |
| 4 | +* Copyright (C) 2022 by RStudio, PBC |
| 5 | +* |
| 6 | +*/ |
| 7 | + |
| 8 | +import { stringify } from "encoding/yaml.ts"; |
| 9 | + |
| 10 | +import { Metadata } from "../config/types.ts"; |
| 11 | +import { mergeConfigs } from "../core/config.ts"; |
| 12 | +import { readYaml } from "../core/yaml.ts"; |
| 13 | +import { projectContext } from "../project/project-context.ts"; |
| 14 | +import { |
| 15 | + projectConfigFile, |
| 16 | + projectPublishFile, |
| 17 | +} from "../project/project-shared.ts"; |
| 18 | +import { ProjectContext, ProjectPublish } from "../project/types.ts"; |
| 19 | +import { lines } from "../core/text.ts"; |
| 20 | + |
| 21 | +export async function projectPublishConfig( |
| 22 | + target: string | ProjectContext, |
| 23 | +): Promise<ProjectPublish | undefined> { |
| 24 | + let project: ProjectContext | undefined; |
| 25 | + if (typeof (target) === "string") { |
| 26 | + if (target === ".") { |
| 27 | + target = Deno.cwd(); |
| 28 | + } |
| 29 | + project = await projectContext(target); |
| 30 | + } else { |
| 31 | + project = target; |
| 32 | + } |
| 33 | + return project ? project?.config?.publish : undefined; |
| 34 | +} |
| 35 | + |
| 36 | +export async function updateProjectPublishConfig( |
| 37 | + projectDir: string, |
| 38 | + config: ProjectPublish, |
| 39 | +) { |
| 40 | + const publishFile = projectPublishFile(projectDir); |
| 41 | + if (publishFile) { |
| 42 | + const baseConfig = (publishFile ? readYaml(publishFile) : {}) as Metadata; |
| 43 | + const updatedConfig = mergeConfigs(baseConfig, config); |
| 44 | + Deno.writeTextFileSync( |
| 45 | + publishFile, |
| 46 | + stringifyPublishConfig(updatedConfig, 2), |
| 47 | + ); |
| 48 | + } else { |
| 49 | + // read existing config and merge |
| 50 | + const baseConfig = await projectPublishConfig(projectDir) || {}; |
| 51 | + const updatedConfig = mergeConfigs(baseConfig, config) as ProjectPublish; |
| 52 | + |
| 53 | + // read proj config and detect indent level |
| 54 | + const projConfig = projectConfigFile(projectDir)!; |
| 55 | + const projConfigYaml = Deno.readTextFileSync(projConfig); |
| 56 | + const indent = detectIndentLevel(projConfigYaml); |
| 57 | + |
| 58 | + // yaml to write |
| 59 | + const configYaml = stringifyPublishConfig( |
| 60 | + { publish: updatedConfig }, |
| 61 | + indent, |
| 62 | + ); |
| 63 | + |
| 64 | + // do line based scan for update |
| 65 | + const yamlLines = lines(projConfigYaml.trimEnd()); |
| 66 | + const publishLine = yamlLines.findIndex((line) => |
| 67 | + line.match(/^publish:\s*$/) |
| 68 | + ); |
| 69 | + let updatedYamlLines = yamlLines; |
| 70 | + if (publishLine !== -1) { |
| 71 | + updatedYamlLines = yamlLines.slice(0, publishLine).concat(configYaml); |
| 72 | + // find the next top level key |
| 73 | + const nextKeyLine = yamlLines.slice(publishLine + 1).findIndex((line) => |
| 74 | + line.match(/^[^ \t]/) |
| 75 | + ); |
| 76 | + if (nextKeyLine !== -1) { |
| 77 | + updatedYamlLines.push( |
| 78 | + ...yamlLines.slice(publishLine + 1 + nextKeyLine), |
| 79 | + ); |
| 80 | + } |
| 81 | + } else { |
| 82 | + updatedYamlLines.push(""); |
| 83 | + updatedYamlLines = yamlLines.concat(configYaml); |
| 84 | + } |
| 85 | + Deno.writeTextFileSync(projConfig, updatedYamlLines.join("\n")); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function stringifyPublishConfig(config: Metadata, indent: number) { |
| 90 | + return stringify( |
| 91 | + config, |
| 92 | + { |
| 93 | + indent, |
| 94 | + sortKeys: false, |
| 95 | + skipInvalid: true, |
| 96 | + }, |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function detectIndentLevel(yaml: string) { |
| 101 | + const spaceMatch = yaml.match(/\n(\s+)/); |
| 102 | + return spaceMatch ? spaceMatch[1].length : 2; |
| 103 | +} |
0 commit comments