-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create cliUtils file to hold shared functions
- Loading branch information
Jordan Shurmer
committed
Jan 30, 2021
1 parent
911a5c4
commit 1336e46
Showing
8 changed files
with
110 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters