UNPKG

majo

Version:

A minimal module to manipulate files.

145 lines (144 loc) 4.32 kB
/// <reference types="node" /> import fs from 'fs'; import glob from 'fast-glob'; import ensureDir from 'mkdirp'; export declare type Middleware = (ctx: Majo) => Promise<void> | void; declare const remove: (arg1: string) => Promise<void>; export interface File { /** The absolute path of the file */ path: string; stats: fs.Stats; contents: Buffer; } export declare type FilterHandler = (relativePath: string, file: File) => boolean; export declare type TransformHandler = (contents: string) => Promise<string> | string; export declare type OnWrite = (relativePath: string, outputPath: string) => void; export interface SourceOptions { /** * The base directory to search files from * @default `process.cwd()` */ baseDir?: string; /** * Whether to include dot files * @default `true` */ dotFiles?: boolean; /** This function is called when a file is written */ onWrite?: OnWrite; } export interface DestOptions { /** * The base directory to write files to * @default `process.cwd()` */ baseDir?: string; /** * Whether to clean output directory before writing files * @default `false` */ clean?: boolean; } export declare class Majo { middlewares: Middleware[]; /** * An object you can use across middleware to share states */ meta: { [k: string]: any; }; /** * Base directory * You normally set this by calling `.source` */ baseDir?: string; sourcePatterns?: string[]; dotFiles?: boolean; files: { [filename: string]: File; }; onWrite?: OnWrite; constructor(); /** * Find files from specific directory * @param source Glob patterns * @param opts * @param opts.baseDir The base directory to find files * @param opts.dotFiles Including dot files */ source(patterns: string | string[], options?: SourceOptions): this; /** * Use a middleware */ use(middleware: Middleware): this; /** * Process middlewares against files */ process(): Promise<this>; /** * Filter files * @param fn Filter handler */ filter(fn: FilterHandler): this; /** * Transform file at given path * @param relativePath Relative path * @param fn Transform handler */ transform(relativePath: string, fn: TransformHandler): Promise<void>; /** * Run middlewares and write processed files to disk * @param dest Target directory * @param opts * @param opts.baseDir Base directory to resolve target directory * @param opts.clean Clean directory before writing */ dest(dest: string, options?: DestOptions): Promise<this>; /** * Get file contents as a UTF-8 string * @param relativePath Relative path */ fileContents(relativePath: string): string; /** * Write contents to specific file * @param relativePath Relative path * @param string File content as a UTF-8 string */ writeContents(relativePath: string, contents: string): this; /** * Get the fs.Stats object of specified file * @para relativePath Relative path */ fileStats(relativePath: string): fs.Stats; /** * Get a file by relativePath path * @param relativePath Relative path */ file(relativePath: string): File; /** * Delete a file * @param relativePath Relative path */ deleteFile(relativePath: string): this; /** * Create a new file * @param relativePath Relative path * @param file */ createFile(relativePath: string, file: File): this; /** * Get an array of sorted file paths */ get fileList(): string[]; rename(fromPath: string, toPath: string): this; } declare const majo: () => Majo; export { majo, remove, glob, ensureDir }; /** * Ensure directory exists before writing file */ export declare const outputFile: (filepath: string, data: any, options?: string | { encoding?: string | null | undefined; mode?: string | number | undefined; flag?: string | undefined; } | null | undefined) => Promise<void>;