Merge pull request #148 from Shopify/@cartogram/cli-debug-logger

feat: Pass instantiated Debug logger to cli command Env
This commit is contained in:
Matt Seccafien 2021-11-08 19:37:39 +01:00 committed by GitHub
commit 1435a58be9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View file

@ -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,
@ -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) {

View file

@ -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);
@ -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);
@ -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;

View file

@ -1,3 +1,4 @@
import debug from 'debug';
import {Workspace} from './workspace';
import {Ui} from './ui';
import {Fs} from './fs';
@ -9,6 +10,7 @@ export interface Env<Context = {}> {
workspace: Workspace;
fs: Fs;
context?: Context;
logger: debug.Debugger;
}
export enum ComponentType {