Skip to content

Commit

Permalink
Merge pull request #148 from Shopify/@cartogram/cli-debug-logger
Browse files Browse the repository at this point in the history
feat: Pass instantiated Debug logger to cli command Env
  • Loading branch information
Matt Seccafien authored Nov 8, 2021
2 parents cff98fa + fea2208 commit 1435a58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export enum Template {
* Create a new `@shopify/hydrogen` app.
*/
export async function init(env: Env) {
const {ui, fs, workspace} = env;
const {ui, fs, workspace, ...passThroughEnv} = env;

const name = await ui.ask('What do you want to name this app?', {
validate: validateProjectName,
Expand Down Expand Up @@ -43,8 +43,8 @@ export async function init(env: Env) {
);

if (template === Template.None) {
const context = {name};
await app({ui, fs, workspace, context});
const context = {name, ...passThroughEnv.context};
await app({...passThroughEnv, ui, fs, workspace, context});
}

if (template === Template.Default) {
Expand Down
24 changes: 22 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import {Cli} from './ui';
import {Workspace} from './workspace';
import {Fs} from './fs';
import {loadConfig} from './config';
import {Env} from './types';

const logger = debug('hydrogen');

interface ModuleType {
default: (env: Env) => Promise<void>;
}

(async () => {
const rawInputs = process.argv.slice(2);
const {command, root} = parseCliArguments(rawInputs);
Expand All @@ -22,10 +27,23 @@ const logger = debug('hydrogen');
throw new InputError();
}

async function runModule(module: ModuleType) {
logger('Run start');

await module.default({
ui,
fs,
workspace,
logger: debug(`hydrogen/${command}`),
});

logger('Run end');
}

try {
const module = await import(`./commands/${command}`);

await module.default({ui, fs, workspace});
await runModule(module);
} catch (error) {
logger(error);

Expand All @@ -36,7 +54,9 @@ const logger = debug('hydrogen');
ui.printFile(file);
}

await workspace.commit();
if (command === 'init') {
await workspace.commit();
}
})().catch((error) => {
logger(error);
process.exitCode = 1;
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import debug from 'debug';
import {Workspace} from './workspace';
import {Ui} from './ui';
import {Fs} from './fs';
Expand All @@ -9,6 +10,7 @@ export interface Env<Context = {}> {
workspace: Workspace;
fs: Fs;
context?: Context;
logger: debug.Debugger;
}

export enum ComponentType {
Expand Down

0 comments on commit 1435a58

Please sign in to comment.