Skip to content

Commit

Permalink
removed Deno.run calls
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Apr 28, 2023
1 parent a2643ab commit c0b2d09
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 46 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Any BREAKING CHANGE between minor versions will be documented here in upper case
- `deno task serve --quiet` no longer logs the http server requests.
- BREAKING: The `includes` option of `sass` plugin accepts only a string
(previously `string[]` was also accepted).
- BREAKING: Removed all `Deno.run` calls and replaced with the new `Deno.Command` API.
- Removed the `ScriptOptions` argument of `lume.run()`.

### Fixed
- `multilanguage` plugin:
Expand Down
14 changes: 6 additions & 8 deletions ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ export default async function main(args: string[]) {
checkDenoVersion();

const [lumeArgs, denoArgs] = await getArgs(args, quiet);
const process = Deno.run({
cmd: [
Deno.execPath(),
const { code } = new Deno.Command(Deno.execPath(), {
args: [
"run",
...denoArgs,
import.meta.resolve("./cli.ts"),
...lumeArgs,
],
});
stdout: "inherit",
stderr: "inherit",
}).outputSync();

const status = await process.status();
process.close();

if (!status.success) {
if (code !== 0) {
addEventListener("unload", () => Deno.exit(1));
}
}
Expand Down
6 changes: 5 additions & 1 deletion cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export async function build(
windows: "explorer",
};

Deno.run({ cmd: [commands[Deno.build.os], `http://localhost:${port}/`] });
new Deno.Command(commands[Deno.build.os], {
args: [`http://localhost:${port}/`],
stdout: "inherit",
stderr: "inherit",
}).output();
}

site.dispatchEvent({ type: "afterStartServer" });
Expand Down
10 changes: 5 additions & 5 deletions cli/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ export async function importMap(url: URL, plugins: string[] = []) {
if (configFile) {
console.log("Reloading Deno cache...");

const process = Deno.run({
cmd: [
Deno.execPath(),
const command = new Deno.Command(Deno.execPath(), {
args: [
"cache",
"--unstable",
"--reload",
configFile,
],
stdout: "inherit",
stderr: "inherit",
});

await process.status();
process.close();
await command.output();
}
}
7 changes: 1 addition & 6 deletions core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import type {
EventOptions,
} from "./core/events.ts";

import type {
default as Scripts,
ScriptOptions,
ScriptOrFunction,
} from "./core/scripts.ts";
import type { default as Scripts, ScriptOrFunction } from "./core/scripts.ts";

import type { default as FS, Entry, Loader } from "./core/fs.ts";
import type Logger from "./core/logger.ts";
Expand Down Expand Up @@ -135,7 +131,6 @@ export type {
RequestHandler,
ScopeFilter,
Scopes,
ScriptOptions,
ScriptOrFunction,
Scripts,
Server,
Expand Down
44 changes: 22 additions & 22 deletions core/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export interface Options {
/** The logger to use */
logger: Logger;

/** The default cwd for scripts */
options: ScriptOptions;
/** The current working directory */
cwd?: string;
}

/**
Expand All @@ -16,15 +16,15 @@ export default class Scripts {
/** The logger to output messages in the terminal */
logger: Logger;

/** The default options to execute the scripts */
options: ScriptOptions;
/** The current working directory */
cwd: string;

/** All registered scripts and functions */
scripts = new Map<string, ScriptOrFunction[]>();

constructor(options: Options) {
this.logger = options.logger;
this.options = options.options;
this.cwd = options.cwd || Deno.cwd();
}

/** Register one or more scripts under a specific name */
Expand All @@ -34,13 +34,10 @@ export default class Scripts {

/** Run one or more commands */
async run(
options: ScriptOptions,
...names: ScriptOrFunction[]
): Promise<boolean> {
options = { ...this.options, ...options };

for (const name of names) {
const success = await this.#run(options, name);
const success = await this.#run(name);

if (!success) {
return false;
Expand All @@ -51,16 +48,16 @@ export default class Scripts {
}

/** Run an individual script or function */
async #run(options: ScriptOptions, name: ScriptOrFunction): Promise<unknown> {
async #run(name: ScriptOrFunction): Promise<unknown> {
if (typeof name === "string" && this.scripts.has(name)) {
this.logger.log(`⚡️ <green>${name}</green>`);
const command = this.scripts.get(name)!;
return this.run(options, ...command);
return this.run(...command);
}

if (Array.isArray(name)) {
const results = await Promise.all(
name.map((n) => this.#run(options, n)),
name.map((n) => this.#run(n)),
);
return results.every((success) => success);
}
Expand All @@ -69,7 +66,7 @@ export default class Scripts {
return this.#runFunction(name);
}

return this.#runScript(options, name);
return this.#runScript(name);
}

/** Run a function */
Expand All @@ -82,15 +79,21 @@ export default class Scripts {
}

/** Run a shell command */
async #runScript(options: ScriptOptions, script: string) {
async #runScript(script: string) {
this.logger.log(`⚡️ <dim>${script}</dim>`);

const cmd = shArgs(script);
const process = Deno.run({ cmd, ...options });
const status = await process.status();
process.close();
const args = shArgs(script);
const cmd = args.shift()!;

const command = new Deno.Command(cmd, {
args,
stdout: "inherit",
stderr: "inherit",
cwd: this.cwd,
});

return status.success;
const output = await command.output();
return output.code === 0;
}
}

Expand All @@ -103,6 +106,3 @@ function shArgs(script: string) {

/** A script or function */
export type ScriptOrFunction = string | (() => unknown) | ScriptOrFunction[];

/** The options for a script */
export type ScriptOptions = Omit<Deno.RunOptions, "cmd">;
7 changes: 3 additions & 4 deletions core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import type {
Plugin,
Processor,
ScopeFilter,
ScriptOptions,
ScriptOrFunction,
StaticFile,
} from "../core.ts";
Expand Down Expand Up @@ -183,7 +182,7 @@ export default class Site {
// Other stuff
const events = new Events<SiteEvent>();
const logger = new Logger({ quiet });
const scripts = new Scripts({ logger, options: { cwd } });
const scripts = new Scripts({ logger, cwd });
const writer = new Writer({ src, dest, logger });

// Save everything in the site instance
Expand Down Expand Up @@ -274,8 +273,8 @@ export default class Site {
}

/** Runs a script or function registered previously */
async run(name: string, options: ScriptOptions = {}): Promise<boolean> {
return await this.scripts.run(options, name);
async run(name: string): Promise<boolean> {
return await this.scripts.run(name);
}

/**
Expand Down

0 comments on commit c0b2d09

Please sign in to comment.