From cc27a5c6a4ace479d99a7b065f1a28194d97473c Mon Sep 17 00:00:00 2001 From: Jordan Shurmer Date: Fri, 29 Jan 2021 22:11:37 -0500 Subject: [PATCH] implement the run cli command * create cliUtils file to hold shared functions --- cli.js | 4 +--- cli/build.js | 45 +++------------------------------- cli/cliUtils.js | 57 ++++++++++++++++++++++++++++++++++++++++++++ cli/cliUtils.test.js | 13 ++++++++++ cli/init.js | 3 +++ cli/run.js | 30 ++++++++++++++++++++--- cli/update.js | 3 +++ cli/upgrade.js | 4 +++- 8 files changed, 110 insertions(+), 49 deletions(-) create mode 100644 cli/cliUtils.js create mode 100644 cli/cliUtils.test.js diff --git a/cli.js b/cli.js index 67395586..b7383755 100644 --- a/cli.js +++ b/cli.js @@ -56,7 +56,7 @@ if (import.meta.main) { try { await cli(Deno.args); } catch (error) { - printError("lume", error.message, error); + printError("lume", error.message); } } @@ -113,7 +113,6 @@ export default async function cli(args) { return false; } - // Check each command. If any of them ran, then return if ( await maybeRun("build", build) || @@ -132,4 +131,3 @@ export default async function cli(args) { Run ${brightGreen("lume --help")} for usage information `); } - diff --git a/cli/build.js b/cli/build.js index 3ffaf193..ada1dc5f 100644 --- a/cli/build.js +++ b/cli/build.js @@ -3,8 +3,7 @@ import { server } from "../server.js"; import { brightGreen, gray } from "../deps/colors.js"; import { error } from "../utils.js"; import { join, relative } from "../deps/path.js"; -import { existsSync } from "../deps/fs.js"; -import lume from "../mod.js"; +import { buildSite, validateArgsCount } from "./cliUtils.js"; export const HELP = ` ${brightGreen("lume build")}: Build the site and optionally serve it @@ -28,7 +27,6 @@ export async function run(args) { const options = parse(args, { boolean: ["serve", "dev"], string: ["port", "src", "dest", "location", "root", "config"], - alias: { help: "h" }, ["--"]: true, unknown(option) { if (option.startsWith("-")) { @@ -41,46 +39,9 @@ export async function run(args) { }, }); - if (options._.length > 1) { - console.log(`Too many arguments: ${options._.join(", ")}`); - console.log(`Run ${brightGreen("lume --help")} for usage information`); - console.log(""); - Deno.exit(1); - } - - const configFile = join(options.root, options.config); - - let site; - if (existsSync(configFile)) { - const mod = await import(`file://${configFile}`); - site = mod.default; - } else { - site = lume({ cwd: options.root }); - } - - site.options.cwd = options.root; - - if (options.dev) { - site.options.dev = options.dev; - } - - if (options.location) { - site.options.location = new URL(options.location); - } - - if (options.src) { - site.options.src = options.src; - } - - if (options.dest) { - site.options.dest = options.dest; - } - - if (options["--"]) { - site.options.flags = options["--"]; - } + validateArgsCount("build", options._, 1); - // validate the options for this argument + const site = await buildSite(options); console.log(""); await site.build(); console.log(""); diff --git a/cli/cliUtils.js b/cli/cliUtils.js new file mode 100644 index 00000000..e5fc6b78 --- /dev/null +++ b/cli/cliUtils.js @@ -0,0 +1,57 @@ +import { existsSync } from "../deps/fs.js"; +import lume from "../mod.js"; +import { join } from "../deps/path.js"; +import { brightGreen } from "../deps/colors.js"; + +/** + * @return {Promise<*>} a lume instance - ready to build, run, etc. + */ +export async function buildSite(options) { + const configFile = join(options.root, options.config); + + let site; + if (existsSync(configFile)) { + const mod = await import(`file://${configFile}`); + site = mod.default; + } else { + site = lume({ cwd: options.root }); + } + + site.options.cwd = options.root; + + if (options.dev) { + site.options.dev = options.dev; + } + + if (options.location) { + site.options.location = new URL(options.location); + } + + if (options.src) { + site.options.src = options.src; + } + + if (options.dest) { + site.options.dest = options.dest; + } + + if (options["--"]) { + site.options.flags = options["--"]; + } + + return site; +} + +/** + * @param command name of the command you're validating + * @param args array of cli arguments (i.e. options._) + * @param expected the expected number of args + */ +export function validateArgsCount(command, args, expected) { + if (args.length !== expected) { + throw new Error(`Unexpected arguments: ${args.join(", ")} + + Run ${brightGreen(`lume ${command} --help`)} for usage information + `); + } +} diff --git a/cli/cliUtils.test.js b/cli/cliUtils.test.js new file mode 100644 index 00000000..b8768c80 --- /dev/null +++ b/cli/cliUtils.test.js @@ -0,0 +1,13 @@ +import { assert, assertThrows } from "../deps/asserts.js"; +import { validateArgsCount } from "./cliUtils.js"; + +Deno.test("valid counts don't throw", () => { + validateArgsCount(["build"], 1); + validateArgsCount(["run", "script"], 2); + + assert(true); // just ensure it makes it here without throwing +}); + +assertThrows(() => { + validateArgsCount(["build", "the", "world"], 1); +}); diff --git a/cli/init.js b/cli/init.js index 53599aaf..6753f967 100644 --- a/cli/init.js +++ b/cli/init.js @@ -1,6 +1,7 @@ import { parse } from "../deps/flags.js"; import { brightGreen } from "../deps/colors.js"; import { version } from "../cli.js"; +import { validateArgsCount } from "./cliUtils.js"; export const HELP = ` ${brightGreen("lume init")}: create a config file for a new site @@ -24,6 +25,8 @@ export async function run(args) { config: "_config.js", }, }); + validateArgsCount("init", options._, 1); + Deno.writeTextFileSync( options.config, `import lume from "https://deno.land/x/lume@${version}/mod.js"; diff --git a/cli/run.js b/cli/run.js index e3b31fad..a9c837a3 100644 --- a/cli/run.js +++ b/cli/run.js @@ -1,12 +1,36 @@ -import { unimplemented } from "../deps/asserts.js"; import { brightGreen } from "../deps/colors.js"; +import { parse } from "../deps/flags.js"; +import { buildSite, validateArgsCount } from "./cliUtils.js"; export const HELP = ` ${brightGreen("lume run")}: run a script in your site USAGE: - lume run + lume run