|
| 1 | +// Copyright 2021 Deno Land Inc. All rights reserved. MIT license. |
| 2 | + |
| 3 | +import { dirname } from "https://deno.land/[email protected]/path/win32.ts"; |
| 4 | +import { join } from "../deps.ts"; |
| 5 | +import { error } from "./error.ts"; |
| 6 | +import { wait } from "./utils/spinner.ts"; |
| 7 | + |
| 8 | +const DEFAULT_FILENAME = "deno.json"; |
| 9 | + |
| 10 | +/** Arguments persisted in the deno.json config file */ |
| 11 | +interface ConfigArgs { |
| 12 | + project?: string; |
| 13 | +} |
| 14 | + |
| 15 | +class ConfigFile { |
| 16 | + #path: string; |
| 17 | + #content: { deploy?: ConfigArgs }; |
| 18 | + |
| 19 | + constructor(path: string, content: { deploy?: ConfigArgs }) { |
| 20 | + this.#path = path; |
| 21 | + this.#content = content; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new `ConfigFile` using an object that _at least_ contains the `ConfigArgs`. |
| 26 | + * |
| 27 | + * Ignores any property in `args` not meant to be persisted. |
| 28 | + */ |
| 29 | + static create(path: string, args: ConfigArgs) { |
| 30 | + const config = new ConfigFile(path, { deploy: {} }); |
| 31 | + // Use override to clean-up args |
| 32 | + config.override(args); |
| 33 | + return config; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Override the `ConfigArgs` of this ConfigFile. |
| 38 | + * |
| 39 | + * Ignores any property in `args` not meant to be persisted. |
| 40 | + */ |
| 41 | + override(args: ConfigArgs) { |
| 42 | + if (this.#content.deploy === undefined) { |
| 43 | + this.#content.deploy = {}; |
| 44 | + } |
| 45 | + // Update only specific properties as args might contain properties we don't want in the config file |
| 46 | + this.#content.deploy.project = args.project; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * For every arg in `ConfigArgs`, if the `args` argument object does not contain |
| 51 | + * the arg, fill it with the value in this `ConfigFile`, if any. |
| 52 | + */ |
| 53 | + useAsDefaultFor(args: ConfigArgs) { |
| 54 | + if (args.project === undefined && this.#content.deploy?.project) { |
| 55 | + args.project = this.#content.deploy?.project; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** Check if the `ConfigArgs` in this `ConfigFile` match `args` |
| 60 | + * |
| 61 | + * Ignores any property in `args` not meant to be persisted. |
| 62 | + */ |
| 63 | + eq(args: ConfigArgs) { |
| 64 | + const thisContent = (this.#content.deploy ?? {}) as { |
| 65 | + [x: string]: unknown; |
| 66 | + }; |
| 67 | + const otherConfig = ConfigFile.create(this.#path, args); |
| 68 | + for (const [key, otherValue] of Object.entries(otherConfig.args())) { |
| 69 | + if (thisContent[key] !== otherValue) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + /** Return whether the `ConfigFile` has the `deploy` namespace */ |
| 77 | + hasDeployConfig() { |
| 78 | + return this.#content.deploy !== undefined; |
| 79 | + } |
| 80 | + |
| 81 | + stringify() { |
| 82 | + return JSON.stringify(this.#content, null, 2); |
| 83 | + } |
| 84 | + |
| 85 | + path() { |
| 86 | + return this.#path; |
| 87 | + } |
| 88 | + |
| 89 | + args() { |
| 90 | + return (this.#content.deploy ?? {}); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +export default { |
| 95 | + /** Read a `ConfigFile` from disk */ |
| 96 | + async read( |
| 97 | + path: string | Iterable<string>, |
| 98 | + ): Promise<ConfigFile | null> { |
| 99 | + const paths = typeof path === "string" ? [path] : path; |
| 100 | + for (const filepath of paths) { |
| 101 | + let content; |
| 102 | + try { |
| 103 | + content = await Deno.readTextFile(filepath); |
| 104 | + } catch { |
| 105 | + // File not found, try next |
| 106 | + continue; |
| 107 | + } |
| 108 | + try { |
| 109 | + const parsedContent = JSON.parse(content); |
| 110 | + return new ConfigFile(filepath, parsedContent); |
| 111 | + } catch (e) { |
| 112 | + error(e); |
| 113 | + } |
| 114 | + } |
| 115 | + // config file not found |
| 116 | + return null; |
| 117 | + }, |
| 118 | + |
| 119 | + /** |
| 120 | + * Write `ConfigArgs` to the config file. |
| 121 | + * |
| 122 | + * @param path {string | null} path where to write the config file. If the file already exists and |
| 123 | + * `override` is `true`, its content will be merged with the `args` |
| 124 | + * argument. If null, will default to `DEFAULT_FILENAME`. |
| 125 | + * @param args {ConfigArgs} args to be upserted into the config file. |
| 126 | + * @param overwrite {boolean} control whether an existing config file should be overwritten. |
| 127 | + */ |
| 128 | + maybeWrite: async function ( |
| 129 | + path: string | null, |
| 130 | + args: ConfigArgs, |
| 131 | + overwrite: boolean, |
| 132 | + ): Promise<void> { |
| 133 | + const pathOrDefault = path ?? DEFAULT_FILENAME; |
| 134 | + const existingConfig = await this.read(pathOrDefault); |
| 135 | + let config; |
| 136 | + if (existingConfig && existingConfig.hasDeployConfig() && !overwrite) { |
| 137 | + if (!existingConfig.eq(args)) { |
| 138 | + wait("").info( |
| 139 | + `Some of the config used differ from the config found in '${existingConfig.path()}'. Use --save-config to overwrite it.`, |
| 140 | + ); |
| 141 | + } |
| 142 | + return; |
| 143 | + } else if (existingConfig) { |
| 144 | + existingConfig.override(args); |
| 145 | + config = existingConfig; |
| 146 | + } else { |
| 147 | + config = ConfigFile.create(pathOrDefault, args); |
| 148 | + } |
| 149 | + await Deno.writeTextFile( |
| 150 | + config.path(), |
| 151 | + (config satisfies ConfigFile).stringify(), |
| 152 | + ); |
| 153 | + wait("").succeed( |
| 154 | + `${ |
| 155 | + existingConfig ? "Updated" : "Created" |
| 156 | + } config file '${config.path()}'.`, |
| 157 | + ); |
| 158 | + }, |
| 159 | + |
| 160 | + cwdOrAncestors: function* () { |
| 161 | + let wd = Deno.cwd(); |
| 162 | + while (wd) { |
| 163 | + yield join(wd, DEFAULT_FILENAME); |
| 164 | + const newWd = dirname(wd); |
| 165 | + if (newWd === wd) { |
| 166 | + return; |
| 167 | + } else { |
| 168 | + wd = newWd; |
| 169 | + } |
| 170 | + } |
| 171 | + }, |
| 172 | +}; |
0 commit comments