Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function createWorkUnit<input>(
titleOrWorkUnit: string | WorkUnit<{}, Input, Output>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
action?: Action,
scope?: unknown,
): WorkUnit<{}, Input, Output> {
if (titleOrWorkUnit instanceof WorkUnit) {
return titleOrWorkUnit;
}
if (typeof titleOrWorkUnit === 'string' && typeof action === 'function') {
return new Task(titleOrWorkUnit, scope ? action.bind(scope) : action);
}
throw new RuntimeError('pipeline', 'PL_INVALID_WORK_UNIT');
}
debug: debugOpt = false,
fallbackLocale = 'en',
locale,
lookupType,
resourceFormat = 'yaml',
}: TranslatorOptions = {},
): Translator {
const namespaces = toArray(namespace);
const resourcePaths = toArray(resourcePath).map(Path.create);
if (namespaces.length === 0) {
throw new RuntimeError('translate', 'TL_REQ_NAMESPACE');
} else if (resourcePaths.length === 0) {
throw new RuntimeError('translate', 'TL_REQ_RES_PATHS');
} else if (!autoDetect && !locale) {
throw new RuntimeError('translate', 'TL_REQ_MANUAL_LOCALE');
}
debug('New translator created: %s namespace(s)', namespaces.join(', '));
const translator = i18next.createInstance().use(new FileBackend());
if (autoDetect) {
translator.use(new LocaleDetector());
}
translator.init(
{
backend: {
format: resourceFormat,
paths: resourcePaths,
},
{
autoDetect = true,
debug: debugOpt = false,
fallbackLocale = 'en',
locale,
lookupType,
resourceFormat = 'yaml',
}: TranslatorOptions = {},
): Translator {
const namespaces = toArray(namespace);
const resourcePaths = toArray(resourcePath).map(Path.create);
if (namespaces.length === 0) {
throw new RuntimeError('translate', 'TL_REQ_NAMESPACE');
} else if (resourcePaths.length === 0) {
throw new RuntimeError('translate', 'TL_REQ_RES_PATHS');
} else if (!autoDetect && !locale) {
throw new RuntimeError('translate', 'TL_REQ_MANUAL_LOCALE');
}
debug('New translator created: %s namespace(s)', namespaces.join(', '));
const translator = i18next.createInstance().use(new FileBackend());
if (autoDetect) {
translator.use(new LocaleDetector());
}
translator.init(
{
backend: {
format: resourceFormat,
logFailureError(module: string, args?: unknown[]) {
this.logFailure(new RuntimeError('args', module, args).message);
}
get(name: ModuleName): Plugin {
const plugin = this.plugins.find(container => this.isMatchingName(container, name));
if (plugin) {
return plugin.plugin;
}
throw new RuntimeError('plugin', 'PG_MISSING_PLUGIN', [this.singularName, name]);
}
this.options.paths.forEach(path => {
if (path.exists() && !path.isDirectory()) {
throw new RuntimeError('translate', 'TL_INVALID_RES_PATH', [path.path()]);
}
});
}
getRegisteredType(typeName: K): PluginType {
const type = this.types[typeName];
if (!type) {
throw new RuntimeError('plugin', 'PG_MISSING_TYPE', [typeName]);
}
return type!;
}
return (target: Object) => {
if (!name.match(COMMAND_FORMAT)) {
throw new RuntimeError('args', 'AG_COMMAND_INVALID_FORMAT', [name]);
}
Reflect.defineMetadata(META_NAME, name, target);
};
}
switch (path.ext()) {
case '.js':
case '.jsx':
return requireModule(path);
case '.json':
case '.json5':
return JSON5.parse(fs.readFileSync(path.path(), 'utf8'));
case '.yml':
case '.yaml':
return YAML.safeLoad(fs.readFileSync(path.path(), 'utf8'));
default:
throw new RuntimeError('common', 'CM_PARSE_INVALID_EXT', [path.name()]);
}
}