All files / bin cliFs.ts

100% Statements 22/22
87.5% Branches 7/8
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 872x   2x                                                               2x 5x 5x 5x 10x       5x 5x                         2x 4x   4x 5x     4x       9x     9x 2x 4x     4x         9x   7x 7x      
import Path
    from 'path';
import {
    getDirectoryContents,
    pathIsDirectory,
    pathIsFile,
    readFile,
} from '../lib/fs/fsUtils';
 
/**
 * Package.json file object
 */
export interface PackageJsonFile {
    /**
     * Package name.
     */
    name: string;
 
    /**
     * Package version.
     */
    version: string;
 
    [key: string]: unknown;
}
 
/**
 * Reads the package.json file at path and returns javascript object.
 * If path is not given, first package.json found in one of parent directories is used.
 *
 * @param path Optional path to package.json file
 * @returns PackageJsonFile Parsed data from package.json
 * @throws if path does not point to a file
 */
export async function readPackageJson(path?: string): Promise<PackageJsonFile> {
    Eif (path === undefined) {
        path = Path.resolve(__dirname, 'package.json');
        while (!await pathIsFile(path)) {
            path = Path.resolve(Path.dirname(path), '..', 'package.json');
        }
    }
 
    const packageJsonText = await readFile(path);
    return JSON.parse(packageJsonText);
}
 
/**
 * Scans a list of paths to extract all HTML files.
 * Paths can be any mix of files or directories.
 * Directories will be recursively searched.
 * Returned paths are all relative to basePath.
 *
 * @param paths List of paths to search
 * @param basePath Base path to compute relative paths
 * @returns Array of paths to all HTML files found in paths and subdirectories
 */
export async function expandPagePaths(paths: readonly string[], basePath: string): Promise<string[]> {
    const outPages: string[] = [];
 
    for (const rawPath of paths) {
        await expandPagePath(rawPath, basePath, outPages);
    }
 
    return outPages;
}
 
async function expandPagePath(pagePath: string, basePath: string, outPaths: string[]): Promise<void> {
    const realPath = Path.resolve(pagePath);
 
    // directories need to be recursively searched
    if (await pathIsDirectory(realPath)) {
        for (const subFile of (await getDirectoryContents(realPath))) {
            const subFilePath = Path.join(pagePath, subFile);
 
            // recurse with each directory content
           await expandPagePath(subFilePath, basePath, outPaths);
        }
    }
 
    // Add all HTML files
    if ((await pathIsFile(realPath)) && realPath.toLowerCase().endsWith('.html')) {
        // computer and save relative path
        const relativePath = Path.relative(basePath, pagePath);
        outPaths.push(relativePath);
    }
}