Skip to content

Commit

Permalink
Remove double logging in CLI (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Nov 14, 2022
1 parent 0f9294a commit cd30f4f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
5 changes: 3 additions & 2 deletions packages/snaps-cli/src/cmds/eval/evalHandler.test.ts
Expand Up @@ -30,7 +30,8 @@ describe('evalHandler', () => {
it('logs and throws errors', async () => {
evalBundleMock.mockRejectedValueOnce(new Error('foo'));

await expect(evalHandler(getMockArgv())).rejects.toThrow('foo');
expect(console.error).toHaveBeenCalledWith('Snap evaluation error: foo');
await expect(evalHandler(getMockArgv())).rejects.toThrow(
'Snap evaluation error: foo',
);
});
});
4 changes: 1 addition & 3 deletions packages/snaps-cli/src/cmds/eval/evalHandler.ts
@@ -1,6 +1,5 @@
import { evalBundle } from '@metamask/snaps-utils';
import { YargsArgs } from '../../types/yargs';
import { logError } from '../../utils';

/**
* Runs the snap in a worker, to ensure SES compatibility.
Expand All @@ -16,7 +15,6 @@ export async function evalHandler(argv: YargsArgs): Promise<void> {
await evalBundle(bundlePath as string);
console.log(`Eval Success: evaluated '${bundlePath}' in SES!`);
} catch (error) {
logError(`Snap evaluation error: ${error.message}`, error);
throw error;
throw new Error(`Snap evaluation error: ${error.message}`);
}
}
1 change: 0 additions & 1 deletion packages/snaps-cli/src/cmds/init/index.ts
Expand Up @@ -21,7 +21,6 @@ export = {
* @param argv - The Yargs arguments object.
*/
async function init(argv: YargsArgs): Promise<void> {
console.log();
const newArgs = await initHandler(argv);

process.chdir(newArgs.snapLocation);
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-cli/src/cmds/init/init.test.ts
Expand Up @@ -25,6 +25,6 @@ describe('init module', () => {
manifest: false,
eval: true,
});
expect(global.console.log).toHaveBeenCalledTimes(2);
expect(global.console.log).toHaveBeenCalledTimes(1);
});
});
15 changes: 7 additions & 8 deletions packages/snaps-cli/src/cmds/init/initHandler.test.ts
Expand Up @@ -185,15 +185,14 @@ describe('initialize', () => {
});

it('fails if the node version is not supported', async () => {
const satisfiesVersionRangeMock = jest
.spyOn(snapUtils, 'satisfiesVersionRange')
.mockImplementation(() => false);
global.process = {
...global.process,
version: 'v15.1.1',
};

await expect(initHandler({ ...getMockArgv() })).rejects.toThrow(
'Outdated node version.',
`Init Error: You are using an outdated version of Node (${process.version}). Please update to Node >=16.`,
);

expect(satisfiesVersionRangeMock).toHaveBeenCalledTimes(1);
});

it('fails if git is not installed', async () => {
Expand All @@ -206,7 +205,7 @@ describe('initialize', () => {
.mockImplementation(() => false);

await expect(initHandler({ ...getMockArgv() })).rejects.toThrow(
'Git is not installed.',
'Init Error: git is not installed. Please install git to continue.',
);

expect(isGitInstalledMock).toHaveBeenCalledTimes(1);
Expand All @@ -226,7 +225,7 @@ describe('initialize', () => {
});

await expect(initHandler({ ...getMockArgv() })).rejects.toThrow(
'error message',
'Init Error: Failed to create template.',
);

expect(cloneTemplateMock).toHaveBeenCalledTimes(1);
Expand Down
10 changes: 3 additions & 7 deletions packages/snaps-cli/src/cmds/init/initHandler.ts
Expand Up @@ -8,7 +8,6 @@ import {
NpmSnapPackageJson,
} from '@metamask/snaps-utils';
import { YargsArgs } from '../../types/yargs';
import { logError } from '../../utils';
import {
cloneTemplate,
gitInit,
Expand Down Expand Up @@ -40,18 +39,16 @@ export async function initHandler(argv: YargsArgs) {
);

if (!isVersionSupported) {
logError(
throw new Error(
`Init Error: You are using an outdated version of Node (${process.version}). Please update to Node ${SATISFIED_VERSION}.`,
);
throw new Error('Outdated node version.');
}

const gitExists = isGitInstalled();
if (!gitExists) {
logError(
throw new Error(
`Init Error: git is not installed. Please install git to continue.`,
);
throw new Error('Git is not installed.');
}

const directoryToUse = directory
Expand All @@ -71,8 +68,7 @@ export async function initHandler(argv: YargsArgs) {
recursive: true,
});
} catch (err) {
logError('Init Error: Failed to create template, cleaning...');
throw err;
throw new Error('Init Error: Failed to create template.');
}

console.log('Installing dependencies...');
Expand Down
16 changes: 11 additions & 5 deletions packages/snaps-cli/src/cmds/init/initUtils.test.ts
Expand Up @@ -47,7 +47,7 @@ describe('initUtils', () => {
});

await expect(prepareWorkingDirectory('bar')).rejects.toThrow(
'error message',
'Init Error: Failed to prepare working directory with message: Init Error: Failed to create new directory.',
);
});

Expand All @@ -59,7 +59,7 @@ describe('initUtils', () => {
await fs.appendFile(filePath, 'test');

await expect(prepareWorkingDirectory('bar')).rejects.toThrow(
'Directory not empty: bar',
'Init Error: Failed to prepare working directory with message: Directory bar not empty.',
);
});
});
Expand Down Expand Up @@ -88,7 +88,9 @@ describe('initUtils', () => {
throw new Error('error message');
});

await expect(cloneTemplate('foo')).rejects.toThrow('error message');
await expect(cloneTemplate('foo')).rejects.toThrow(
'Init Error: Failed to clone the template.',
);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
Expand Down Expand Up @@ -177,7 +179,9 @@ describe('initUtils', () => {
throw new Error('error message');
});

await expect(gitInit('foo')).rejects.toThrow('error message');
await expect(gitInit('foo')).rejects.toThrow(
'Init Error: Failed to init a new git repository.',
);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
Expand All @@ -204,7 +208,9 @@ describe('initUtils', () => {
throw new Error('error message');
});

await expect(yarnInstall('foo')).rejects.toThrow('error message');
await expect(yarnInstall('foo')).rejects.toThrow(
'Init Error: Failed to install dependencies.',
);
expect(execSyncMock).toHaveBeenCalledTimes(1);
});
});
Expand Down
20 changes: 8 additions & 12 deletions packages/snaps-cli/src/cmds/init/initUtils.ts
@@ -1,7 +1,6 @@
import { promises as fs } from 'fs';
import { execSync } from 'child_process';
import pathUtils from 'path';
import { logError } from '../../utils';

export const TEMPLATE_GIT_URL =
'https://github.com/MetaMask/template-snap-monorepo.git';
Expand All @@ -23,19 +22,19 @@ export async function prepareWorkingDirectory(
try {
await fs.mkdir(directory, { recursive: true });
} catch (err) {
logError('Init Error: Failed to create new directory.', err);
throw err;
throw new Error('Init Error: Failed to create new directory.');
}
}

const existingFiles = await fs.readdir(directory);

if (existingFiles.length > 0) {
throw new Error(`Directory not empty: ${directory}.`);
throw new Error(`Directory ${directory} not empty.`);
}
} catch (err) {
logError('Init Error: Failed to prepare working directory.', err);
throw err;
throw new Error(
`Init Error: Failed to prepare working directory with message: ${err.message}`,
);
}
}

Expand All @@ -50,8 +49,7 @@ export async function cloneTemplate(directory: string) {
stdio: [2],
});
} catch (err) {
logError('Init Error: Failed to clone the template.', err);
throw err;
throw new Error('Init Error: Failed to clone the template.');
}
}

Expand Down Expand Up @@ -99,8 +97,7 @@ export async function gitInit(directory: string) {
cwd: pathUtils.resolve(__dirname, directory),
});
} catch (err) {
logError('Init Error: Failed to init a new git repository.', err);
throw err;
throw new Error('Init Error: Failed to init a new git repository.');
}
}

Expand All @@ -116,7 +113,6 @@ export async function yarnInstall(directory: string) {
cwd: pathUtils.resolve(__dirname, directory),
});
} catch (err) {
logError('Init Error: Failed to install dependencies.', err);
throw err;
throw new Error('Init Error: Failed to install dependencies.');
}
}

0 comments on commit cd30f4f

Please sign in to comment.