diff --git a/src/commands/init.ts b/src/commands/init.ts index f1f973f..cd9a7de 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,7 +1,7 @@ import { Command } from "https://deno.land/x/cliffy@v0.25.5/command/mod.ts"; import { Variables } from "../config.ts"; -import { initializeProject, TemplateOptions } from "../init.ts"; +import { initializeProject } from "../init.ts"; import { templateCompletion, varOptions } from "./utils.ts"; export const command = new Command() diff --git a/src/commands/new.ts b/src/commands/new.ts index 1b16b1e..1f219a5 100644 --- a/src/commands/new.ts +++ b/src/commands/new.ts @@ -1,7 +1,7 @@ import { Command } from "https://deno.land/x/cliffy@v0.25.5/command/mod.ts"; import { Variables } from "../config.ts"; -import { initializeProject, TemplateOptions } from "../init.ts"; +import { initializeProject } from "../init.ts"; import { templateCompletion, varOptions } from "./utils.ts"; export const command = new Command() diff --git a/src/init.ts b/src/init.ts index 6a9f3c2..52663d5 100644 --- a/src/init.ts +++ b/src/init.ts @@ -13,7 +13,7 @@ import { import * as log from "https://deno.land/std@0.167.0/log/mod.ts"; import { Output, Template, Variable, Variables } from "./config.ts"; -import { asBytes, asString } from "./utils.ts"; +import { asBytes } from "./utils.ts"; import { writeOutput } from "./process.ts"; export async function initializeProject( @@ -31,9 +31,11 @@ export async function initializeProject( for (const generated of outputs) { let exists = false; try { - const stat = await Deno.stat(generated.path); + const _stat = await Deno.stat(generated.path); exists = true; - } catch {} + } catch (_e) { + // Ignore + } if (exists && !options.isNew) { log.debug(`Skipping ${generated.path} as it already exists`); continue; @@ -90,7 +92,7 @@ export async function getTemplateSources( variables = variables || {}; - const unresolved = mergeVariables( + const unresolved = getUnresolved( variables || {}, templateConfig.variables || [], ); @@ -181,31 +183,29 @@ export function renderTemplate( return contents; } -export function mergeVariables( +export function getUnresolved( variables: Record, definitions: Variable[], ): Variable[] { const unresolved: Variable[] = []; for (const variable of definitions) { - if (variables[variable.name] === undefined) { - if (variable.default) { - const type = variable.type || "input"; - switch (type) { - case "number": - variables[variable.name] = parseFloat(variable.default as string); - break; - case "confirm": - variables[variable.name] = - (variable.default as string).toLowerCase() == "true"; - break; - default: - variables[variable.name] = variable.default; - } - continue; + const value = variables[variable.name]; + if (value !== undefined) { + // Make sure the value provided on the command line + // is the type we expect. + const type = variable.type || "input"; + switch (type) { + case "number": + variables[variable.name] = parseFloat(value as string); + break; + case "confirm": + variables[variable.name] = ("" + value).toLowerCase() == "true"; + break; } + } else { + unresolved.push(variable); } - unresolved.push(variable); } return unresolved; } diff --git a/src/process.ts b/src/process.ts index 43e0bcd..ebad9cf 100644 --- a/src/process.ts +++ b/src/process.ts @@ -43,6 +43,7 @@ export async function process(config: Configuration): Promise { const output = new TextDecoder().decode(rawOutput); log.debug(`Generator output: ${output}`); const fromJson = JSON.parse(output) as JsonOutput[]; + // deno-lint-ignore no-explicit-any const parsedOutput = fromJson.map((o: any) => { o.contents = Uint8Array.from(o.contents); return o as Output; diff --git a/test/init.test.ts b/test/init.test.ts index 765b51b..8f8ba92 100644 --- a/test/init.test.ts +++ b/test/init.test.ts @@ -1,8 +1,7 @@ -import * as apex from "https://deno.land/x/apex_core@v0.1.1/mod.ts"; import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts"; -import { getTemplateSources, mergeVariables } from "../src/init.ts"; +import { getTemplateSources, getUnresolved } from "../src/init.ts"; import * as path from "https://deno.land/std@0.167.0/path/mod.ts"; -import { asBytes, asString, setupLogger } from "../src/utils.ts"; +import { asBytes, setupLogger } from "../src/utils.ts"; import { Variable } from "../src/config.ts"; const __dirname = new URL(".", import.meta.url).pathname; @@ -60,9 +59,11 @@ Deno.test( required: true, loop: false, }]; - const variables = {}; - const unresolved = mergeVariables(variables, definitions); - assertEquals(variables, { "module": "default value" }); + const variables = { + module: "I am provided", + }; + const unresolved = getUnresolved(variables, definitions); + assertEquals(variables, { "module": "I am provided" }); assertEquals(unresolved, [{ name: "needed", description: "a required var",