cmpl
Version:
no deps, no operations, no vowels - just a tiny compiler
77 lines (76 loc) • 3.11 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
export interface Path {
join: (...segments: string[]) => string;
basename: (path: string, ext?: string) => string;
extname: (path: string) => string;
relative: (a: string, b: string) => string;
dirname: (path: string) => string;
}
export interface Fs {
readdir: (dir: string) => Promise<string[]>;
stat: (dirOrFile: string) => Promise<{
isDirectory: () => boolean;
mtimeMs: number;
}>;
readFile: (path: string) => Promise<Buffer>;
mkdir: (path: string, opts?: {
recursive: true;
}) => Promise<string | undefined>;
writeFile: (path: string, contents: Buffer) => Promise<void>;
}
export interface Crypto {
createHash: (algorithm: 'sha256') => {
update: (content: Buffer) => {
digest: (encoding: 'hex') => string;
};
};
}
export declare type TransformResult = Buffer | {
content: Buffer;
name: string;
} | {
content: Buffer;
name: string;
}[] | null;
export declare type TransformFn = (content: Buffer, file: string) => TransformResult | Promise<TransformResult>;
export declare type FileNamerFn = (originalName: string, contents: Buffer) => string | Promise<string>;
export interface BasePrcssr {
outDir: string;
recursive?: boolean;
include?: (name: string, isDir: boolean, getContents?: () => Promise<Buffer | null>) => boolean | Promise<boolean>;
}
export interface RenamePrcssr extends BasePrcssr {
rename?: FileNamerFn;
}
export interface TransformPrcssr extends BasePrcssr {
transform: TransformFn;
}
export declare type Prcssr = RenamePrcssr | TransformPrcssr;
export interface CmplOptions {
entry: string;
processors: (Prcssr | Promise<Prcssr>)[];
path?: Path | Promise<Path>;
fs?: Fs | Promise<Fs>;
}
export declare const cntntHsh: (length?: number, crypto?: Crypto | Promise<Crypto>, path?: Path | Promise<Path>) => FileNamerFn;
export declare const cntntChngd: (crypto?: Crypto | Promise<Crypto>) => (name: string, isDir: boolean, getContents?: (() => Promise<Buffer | null>) | undefined) => Promise<boolean>;
export declare function prcss(file: string, { entry, fs, path, }: Pick<CmplOptions, 'entry' | 'fs' | 'path'>, processors: (Prcssr | null)[]): Promise<(null | Record<string, string | string[]>)[]>;
export declare function cmpl({ entry, processors, fs, path, }: CmplOptions): Promise<Record<string, string> | Record<string, string>[]>;
export interface WatchEvent {
eventType: 'rename' | 'change';
filename: string;
}
export interface WatchFs extends Fs {
watch: (path: string, opts?: {
recursive: boolean;
signal?: AbortSignal;
}) => AsyncIterable<WatchEvent>;
}
export interface WtchOpts extends Omit<CmplOptions, 'fs'> {
signal?: AbortSignal;
fs?: WatchFs | Promise<WatchFs>;
poll?: boolean | number;
onError?: (err: unknown) => void;
}
export declare function wtch({ signal, entry, processors, poll, onError, fs, path, }: WtchOpts): AsyncGenerator<Record<string, string> | Record<string, string>[], void, unknown>;