Skip to content

Commit

Permalink
nt-87 improve get-config errors (#88)
Browse files Browse the repository at this point in the history
nt-87 improve get-config errors
  • Loading branch information
vordgi authored May 7, 2024
1 parent 8a821e8 commit 4d2a7c6
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions package/src/configuration/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,34 @@ const CONFIG_PATH = path.join(process.cwd(), "nimpl-i18n.js");
const dynamicImport = new Function("p", "return import(p)");

const getConfig = async (): Promise<Config> => {
try {
if (fs.existsSync(CONFIG_PATH)) {
const config: { default: Config } = await dynamicImport(pathToFileURL(CONFIG_PATH).href);
const { load, getLanguage, languages } = config.default;
if (fs.existsSync(CONFIG_PATH)) {
const config: { default: Config } = await dynamicImport(pathToFileURL(CONFIG_PATH).href);
const { load, getLanguage, languages } = config.default;

if (!load) {
throw new Error(
`Can't find load method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}

if (!languages) {
throw new Error(
`Can't find languages list in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}
if (!load) {
throw new Error(
`Can't find "load" method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}

if (!getLanguage) {
throw new Error(
`Can't find getLanguage method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}
if (!languages) {
throw new Error(
`Can't find "languages" list in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}

return config.default;
if (!getLanguage) {
throw new Error(
`Can't find "getLanguage" method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}
} catch (err) {
console.error(err);

return config.default;
} else {
throw new Error(
`Can't load config from "${CONFIG_PATH}". Read more - https://github.com/vordgi/nimpl-i18n#configuration`,
);
}
throw new Error("Can't load config - https://github.com/vordgi/nimpl-i18n#configuration");
};

export default getConfig;

0 comments on commit 4d2a7c6

Please sign in to comment.