Skip to content

Commit

Permalink
implement the run cli command
Browse files Browse the repository at this point in the history
* create cliUtils file to hold shared functions
  • Loading branch information
Jordan Shurmer committed Jan 30, 2021
1 parent 911a5c4 commit cc27a5c
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 49 deletions.
4 changes: 1 addition & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if (import.meta.main) {
try {
await cli(Deno.args);
} catch (error) {
printError("lume", error.message, error);
printError("lume", error.message);
}
}

Expand Down Expand Up @@ -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) ||
Expand All @@ -132,4 +131,3 @@ export default async function cli(args) {
Run ${brightGreen("lume --help")} for usage information
`);
}

45 changes: 3 additions & 42 deletions cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("-")) {
Expand All @@ -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("");
Expand Down
57 changes: 57 additions & 0 deletions cli/cliUtils.js
Original file line number Diff line number Diff line change
@@ -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
`);
}
}
13 changes: 13 additions & 0 deletions cli/cliUtils.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
3 changes: 3 additions & 0 deletions cli/init.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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";
Expand Down
30 changes: 27 additions & 3 deletions cli/run.js
Original file line number Diff line number Diff line change
@@ -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 <script>
`;
export async function run(args) {
unimplemented();
const options = parse(args, {
boolean: ["dev"],
string: ["src", "dest", "location", "root", "config"],
["--"]: true,
unknown(option) {
if (option.startsWith("-")) {
throw new Error(`Unknown option: ${option}`);
}
},
default: {
root: Deno.cwd(),
config: "_config.js",
},
});

// should be 2 arguments "run" and the thing to run
validateArgsCount("run", options._, 2);

// script name is the second argument ("run" is the first)
const script = options._[1];

const site = await buildSite(options);
console.log("");
site.run(script);
}
3 changes: 3 additions & 0 deletions cli/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { exists } from "../deps/fs.js";
import { error } from "../utils.js";
import { brightGreen, gray } from "../deps/colors.js";
import { version } from "../cli.js";
import { validateArgsCount } from "./cliUtils.js";

export const HELP = `
${
Expand All @@ -29,6 +30,8 @@ export async function run(args) {
},
});

validateArgsCount("update", options._, 1);

if (!await exists(options.config)) {
error("error", `The file ${options.config} does not exists`);
return;
Expand Down
4 changes: 3 additions & 1 deletion cli/upgrade.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { brightGreen, gray } from "../deps/colors.js";
import { version } from "../cli.js";
import { validateArgsCount } from "./cliUtils.js";

export const HELP = `
${
Expand All @@ -12,7 +13,8 @@ export const HELP = `
/**
* Command to upgrade lume to the latest version
*/
export async function run() {
export async function run(args) {
validateArgsCount("upgrade", args, 1);
const response = await fetch("https://cdn.deno.land/lume/meta/versions.json");
const versions = await response.json();
const { latest } = versions;
Expand Down

0 comments on commit cc27a5c

Please sign in to comment.