libs/nx/src/generators/journey-src/lib/shared/bb-path-utils.ts
Properties |
compilerOptions | |
Type |
literal type
|
import { parseJson } from '@nx/devkit';
import { existsSync, readFileSync } from 'fs';
import { globSync } from 'glob';
import { posix } from 'path';
export interface TsconfigJson {
compilerOptions: {
paths?: Record<string, string[]>;
};
}
export function buildImportStatements(exportNamesGroupByPackage: Record<string, Set<string>>) {
const importStatements: string = Object.entries(exportNamesGroupByPackage)
.map(([packageName, exportNames]) => {
const stringifiedExportNames = Array.from(exportNames).join(', ');
return `import { ${stringifiedExportNames} } from '${packageName}';`;
})
.join('\n');
return importStatements;
}
export function getLocalLibraryPackagePath(packageName: string): string | undefined {
let baseTsconfigJson;
if (existsSync('tsconfig.base.json')) {
baseTsconfigJson = parseJson(readFileSync('tsconfig.base.json', 'utf8'));
}
if (!baseTsconfigJson) return;
const localPackagePath = baseTsconfigJson.compilerOptions.paths?.[packageName]?.[0];
if (!localPackagePath) return;
const pathToSrcFolderInLocalLib = localPackagePath.substring(
0,
localPackagePath.lastIndexOf('/')
);
return pathToSrcFolderInLocalLib;
}
export function getNodeModulesLibraryPackagePath(packageName: string): string | undefined {
const baseDir = posix.join('node_modules', packageName);
if (!existsSync(baseDir)) return;
return baseDir;
}
function extractBbFilePath(
basePath: string,
bbModuleName: string,
bbModuleFileExtension: string,
bbFileExtension: string
): string | undefined {
const filePattern = posix.join(basePath, '**', `*${bbModuleFileExtension}`);
const filePaths = globSync(filePattern);
const classNameRegex = new RegExp(`class ${bbModuleName}`, 'g');
for (const file of filePaths) {
const content = readFileSync(file, 'utf-8');
if (classNameRegex.test(content)) {
const filePath = file.replace(bbModuleFileExtension, bbFileExtension);
return filePath;
}
}
return;
}
export function getLocalPathToBbFile(
basePath: string,
bbModuleName: string,
bbFileExtension: string
): { found: boolean; path: string } {
const moduleFileExtension = '.module.ts';
const bbFilePath = extractBbFilePath(
basePath,
bbModuleName,
moduleFileExtension,
bbFileExtension
);
if (bbFilePath) {
return {
found: existsSync(bbFilePath),
path: bbFilePath
};
}
return {
found: false,
path: basePath
};
}
export function getNodeModulesPathToBbFile(
basePath: string,
bbModuleName: string,
bbFileExtension: string
): { found: boolean; path: string } {
const moduleFileExtension = '.module.d.ts';
const bbFilePath = extractBbFilePath(
basePath,
bbModuleName,
moduleFileExtension,
bbFileExtension
);
if (bbFilePath) {
return {
found: existsSync(bbFilePath),
path: bbFilePath
};
}
return {
found: false,
path: basePath
};
}