Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
importModule(name: string | Path, args: any[] = []): Tm {
const { typeName } = this;
const { appName, scoped } = this.tool.options;
// Determine modules to attempt to load
const modulesToAttempt = [];
let isFilePath = false;
let importedModule: any = null;
let moduleName;
// File path
if (name instanceof Path || name.match(/^\.|\/|\\|[A-Z]:/u)) {
this.debug('Locating %s from path %s', typeName, color.filePath(name));
modulesToAttempt.push(Path.create(name).path());
isFilePath = true;
// Module name
} else {
this.debug('Locating %s module %s', typeName, color.moduleName(name));
if (scoped) {
modulesToAttempt.push(formatModuleName(appName, typeName, name, true));
}
modulesToAttempt.push(formatModuleName(appName, typeName, name));
// Additional scopes to load
this.scopes.forEach(otherScope => {
parseFile(basePath: PortablePath, args: any[] = [], options: ParseOptions = {}): T {
const filePath = Path.create(basePath);
const name = filePath.name();
const ext = filePath.ext();
let value: any = null;
this.debug('Parsing file %s', color.filePath(filePath));
if (!filePath.isAbsolute()) {
throw new Error(this.tool.msg('errors:absolutePathRequired'));
}
if (ext === '.json' || ext === '.json5') {
value = JSON5.parse(fs.readFileSync(filePath.path(), 'utf8'));
} else if (ext === '.js') {
value = requireModule(filePath);
if (typeof value === 'function') {
if (options.errorOnFunction) {
throw new Error(this.tool.msg('errors:configNoFunction', { name }));
} else {
value = value(...args);
}
absolute: true,
cwd: String(root),
onlyFiles: true,
});
this.debug.invariant(
configPaths.length === 1,
`Looking for local config file: ${relPath}`,
'Found',
'Not found',
);
if (configPaths.length === 1) {
const localPath = new Path(configPaths[0]);
this.debug('Found %s', color.filePath(localPath.name()));
return localPath;
}
if (configPaths.length > 1) {
throw new Error(this.tool.msg('errors:multipleConfigFiles', { configName }));
}
return null;
}
}
const ModuleClass = importedModule as ConcreteConstructor;
const module = new ModuleClass(...args);
if (!instanceOf(module, this.contract)) {
throw new TypeError(
this.tool.msg('errors:moduleExportInvalid', {
moduleName,
typeName,
}),
);
}
if (isFilePath) {
this.debug('Found with file path %s', color.filePath(moduleName));
} else {
this.debug('Found with module %s', color.moduleName(moduleName));
(module as any).name = name;
(module as any).moduleName = moduleName;
}
return module;
}
`Matching patterns: ${workspacePatterns.map(p => color.filePath(p)).join(', ')}`,
'Match found',
loadPackageJSON(): PackageConfig {
const rootPath = new Path(this.tool.options.root);
const filePath = rootPath.append('package.json');
this.debug('Locating package.json in %s', color.filePath(rootPath.path()));
if (!filePath.exists()) {
throw new Error(this.tool.msg('errors:packageJsonNotFound'));
}
this.package = optimal(
this.parseFile(filePath),
{
name: string().notEmpty(),
version: string('0.0.0'),
},
{
file: 'package.json',
name: 'ConfigLoader',
unknown: true,
},