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 => {
modulesToAttempt.push(
formatModuleName(otherScope, typeName, name, true),
findConfigInWorkspaceRoot(root: PortablePath): ConfigPathOrObject | null {
const rootPath = Path.create(root);
let currentPath = rootPath.parent();
let currentDir = currentPath.path();
if (currentDir.includes('node_modules')) {
return null;
}
this.debug('Detecting if in a workspace');
let workspaceRoot: Path | null = null;
let workspacePackage: any = {};
let workspacePatterns: string[] = [];
// eslint-disable-next-line no-constant-condition
while (true) {
if (!currentDir || currentDir === '.' || currentDir === '/') {
getWorkspacePaths(options: WorkspaceOptions = {}): FilePath[] {
const rootPath = Path.create(options.root || this.options.root);
const pkgPath = rootPath.append('package.json');
const lernaPath = rootPath.append('lerna.json');
const workspacePaths = [];
// Yarn
if (pkgPath.exists()) {
const pkg = fs.readJsonSync(pkgPath.path());
if (pkg.workspaces) {
if (Array.isArray(pkg.workspaces)) {
workspacePaths.push(...pkg.workspaces);
} else if (Array.isArray(pkg.workspaces.packages)) {
workspacePaths.push(...pkg.workspaces.packages);
}
}
}
createWorkspaceMetadata(jsonPath: PortablePath): WorkspaceMetadata {
const metadata: any = {};
const filePath = Path.create(jsonPath);
const pkgPath = filePath.parent();
const wsPath = pkgPath.parent();
metadata.jsonPath = filePath.path();
metadata.packagePath = pkgPath.path();
metadata.packageName = pkgPath.name();
metadata.workspacePath = wsPath.path();
metadata.workspaceName = wsPath.name();
return metadata;
}