File
skipped
|
Type
|
BuildingBlockFolderStats[]
|
import { existsSync, readdirSync, statSync } from 'fs';
import { basename, join } from 'path';
export interface BuildingBlockFoldersCollection {
result: string[];
skipped: BuildingBlockFolderStats[];
}
export interface BuildingBlockFolderStats {
folderName: string;
success: boolean;
componentExists: boolean;
moduleExists: boolean;
markdownExists: boolean;
}
/**
* Find all suitable folders that contain a Building Block.
* A Building needs to be an Angular component, with a Markdown Files and Module.
* All with the same name
*/
export async function collectBuildingBlockFolders(
source: string
): Promise<BuildingBlockFoldersCollection> {
const sourceFolder = join(source, 'src', 'lib');
const files = readdirSync(sourceFolder);
const result: string[] = [];
const skipped: BuildingBlockFolderStats[] = [];
for (const folderName of files) {
const currentFolderPath = join(sourceFolder, folderName);
const stats = statSync(currentFolderPath);
if (stats.isDirectory()) {
const stats = statsBuildingBlockFolder(currentFolderPath);
if (false === stats.success) {
skipped.push(stats);
} else {
result.push(currentFolderPath);
}
}
}
return { result, skipped };
}
/**
* Create a small stat report about the given folder.
* Check if all required files for a Building Block do exist
* and return details of the check for a later analysis/logging
* to provide some verbose output.
*/
function statsBuildingBlockFolder(folderPath: string): BuildingBlockFolderStats {
const folderName = basename(folderPath);
const expectedComponentName = `${folderName}.component.ts`;
const componentExists = existsSync(join(folderPath, expectedComponentName));
const expectedModuleName = `${folderName}.module.ts`;
const moduleExists = existsSync(join(folderPath, expectedModuleName));
const expectedMarkdownFile = `${folderName}.md`;
const markdownExists = existsSync(join(folderPath, expectedMarkdownFile));
const isBuildingBlock = componentExists && moduleExists && markdownExists;
return {
folderName,
success: isBuildingBlock,
componentExists,
moduleExists,
markdownExists
};
}