Skip to content

Commit

Permalink
🔊 Use consola when error.
Browse files Browse the repository at this point in the history
  • Loading branch information
yakisova41 committed Apr 28, 2024
1 parent 748f900 commit fb4f2d9
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 37 deletions.
13 changes: 5 additions & 8 deletions packages/crx-monkey/src/node/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises } from 'fs';
import { CrxMonkeyConfig } from './types';
import path from 'path';
import consola from 'consola';

const configFileNameMatch = ['crx-monkey.config.js'];

Expand Down Expand Up @@ -38,11 +39,7 @@ async function getConfigPath(): Promise<string | null> {
} else {
const splited = dir.split('/');
if (splited.length === 1) {
reject(
new Error(
['Config file not found.', 'Please create "crx-monkey-config.js"'].join('\n'),
),
);
reject(new Error('Config file not found. Please create "crx-monkey-config.js"'));
} else {
splited.pop();
dir = splited.join('/');
Expand Down Expand Up @@ -134,11 +131,11 @@ export async function loadConfig(): Promise<CrxMonkeyConfig> {
resolve(configCahce);
});
} else {
throw new Error('Can not import config');
throw consola.error(new Error('Can not import config'));
}
})
.catch((e) => {
throw e;
throw consola.error(e);
});
});
}
Expand All @@ -152,6 +149,6 @@ export function getConfig() {
if (configCahce !== null) {
return configCahce;
} else {
throw new Error('Config has not been loaded. Please using "loadConfig()"');
throw consola.error(new Error('Config has not been loaded. Please using "loadConfig()"'));
}
}
3 changes: 2 additions & 1 deletion packages/crx-monkey/src/node/handlers/build/Build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import consola from 'consola';
import { BuildOptions, BuildResult, Plugin, build } from 'esbuild';
import { getConfig } from 'src/node/config';
import { ManifestFactory } from 'src/node/manifest-factory';
Expand All @@ -14,7 +15,7 @@ export class Build {

const config = getConfig();
if (config === undefined) {
throw new Error(['Config is undefined.'].join('\n'));
throw consola.error(new Error('Config is undefined.'));
}
this.config = config;
}
Expand Down
15 changes: 5 additions & 10 deletions packages/crx-monkey/src/node/handlers/build/BuildUserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ export class BuildUserScript extends Build implements BuildImplements {
* Build userscript
*/
public async build() {
if (this.config.devServer === undefined) {
throw new Error('Dev Server is not enabled');
}

/**
* Build and output content scripts.
*/
Expand Down Expand Up @@ -158,14 +154,13 @@ export class BuildUserScript extends Build implements BuildImplements {
const base64 = convertImgToBase64(iconPath);
this.headerFactory.push('@icon', base64);
} else {
throw new Error('No size 48 icons were found in the icons item');
throw consola.error(new Error('No size 48 icons were found in the icons item'));
}
} else {
throw new Error(
[
'No "icons" entry found in manifest',
'Disable the "importIconToUserscript" entry in the config file or add an "icons" entry to manifest',
].join('\n'),
throw consola.error(
new Error(
'No "icons" entry found in manifest. Disable the "importIconToUserscript" entry in the config file or add an "icons" entry to manifest.',
),
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/crx-monkey/src/node/handlers/dev/Watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ManifestFactory } from 'src/node/manifest-factory';
import { CrxMonkeyConfig } from 'src/node/types';
import chokidar from 'chokidar';
import { ReloadServer } from './server/reloadServer';
import consola from 'consola';

export class Watch {
protected readonly manifest: chrome.runtime.ManifestV3;
Expand All @@ -22,7 +23,7 @@ export class Watch {

const config = getConfig();
if (config === undefined) {
throw new Error(['Config is undefined.'].join('\n'));
throw consola.error(new Error('Config is undefined.'));
}
this.config = config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ function devServiceWorkerPlugin(config: CrxMonkeyConfig) {
name: 'dev-sw-plugin',
setup: (build) => {
const { devServer } = config;
if (devServer === undefined) {
throw new Error('');
}

build.onEnd((res) => {
const meta = res.metafile;
Expand Down
15 changes: 5 additions & 10 deletions packages/crx-monkey/src/node/handlers/dev/WatchUserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export class WatchUserScript extends Watch implements WatchImplements {
}

public async watch() {
if (this.config.devServer === undefined) {
throw new Error('Dev Server is not enabled');
}

const contentScripts = this.manifest.content_scripts;
if (contentScripts !== undefined) {
const { jsFiles, cssFiles } = getAllJsAndCSSByContentScripts(contentScripts);
Expand Down Expand Up @@ -146,14 +142,13 @@ export class WatchUserScript extends Watch implements WatchImplements {
const base64 = convertImgToBase64(iconPath);
this.headerFactory.push('@icon', base64);
} else {
throw new Error('No size 48 icons were found in the icons item');
throw consola.error(new Error('No size 48 icons were found in the icons item'));
}
} else {
throw new Error(
[
'No "icons" entry found in manifest',
'Disable the "importIconToUserscript" entry in the config file or add an "icons" entry to manifest',
].join('\n'),
throw consola.error(
new Error(
'No "icons" entry found in manifest. Disable the "importIconToUserscript" entry in the config file or add an "icons" entry to manifest.',
),
);
}
}
Expand Down
7 changes: 3 additions & 4 deletions packages/crx-monkey/src/node/manifest-factory/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fsExtra from 'fs-extra/esm';
import fs from 'fs';
import path from 'path';
import { getlocalesPath } from '../handlers/utils';
import consola from 'consola';

/**
* Enumerate all js and css paths from conetnt_scripts in manifestjson
Expand Down Expand Up @@ -131,9 +132,7 @@ async function getMessage(langKey: string, key: string) {
if (message !== undefined) {
return message;
} else {
throw new Error(
['Can not found message property', `lang: ${langKey}`, `key: ${key}`].join('\n'),
);
throw consola.error(new Error(`Can not found message property. lang: ${langKey} key: ${key}`));
}
}

Expand All @@ -147,7 +146,7 @@ function getEnableLangs(localesPath: string) {
const langs = fs.readdirSync(localesPath);

if (!langs.includes('en')) {
throw new Error('No en in _locales folder');
throw consola.error(new Error('There is no en directory in the _locales directory.'));
}

return langs;
Expand Down

0 comments on commit fb4f2d9

Please sign in to comment.