caz
Version:
A simple yet powerful template-based Scaffolding tools.
554 lines (505 loc) • 15.6 kB
TypeScript
/// <reference types="node" />
import prompts from 'prompts';
import fs from 'fs';
import { RequestOptions } from 'http';
import { SpawnOptions } from 'child_process';
interface ListOptions {
json?: boolean;
short?: boolean;
}
/**
* Show all available templates.
*/
declare const _default$3: (owner?: string, options?: ListOptions) => Promise<void>;
/**
* Checks whether something exists on given path.
* @param input input path
*/
declare const exists: (input: string) => Promise<false | 'file' | 'dir' | 'other'>;
/**
* Check input is a file.
* @param input input path
*/
declare const isFile: (input: string) => Promise<boolean>;
/**
* Check input is a file.
* @param input input path
*/
declare const isDirectory: (input: string) => Promise<boolean>;
/**
* Check input is empty.
* @param input input path
*/
declare const isEmpty: (input: string) => Promise<boolean>;
/**
* Make directory recursive.
* require node >= v10.12
* @param input input path
* @param options recursive by default
*/
declare const mkdir: (input: string, options?: fs.MakeDirectoryOptions) => Promise<void>;
/**
* Remove input dir or file. recursive when dir
* require node >= v14.14.0
* @param input input path
* @param options recursive & force by default
* @todo https://github.com/sindresorhus/trash
*/
declare const remove: (input: string, options?: fs.RmOptions) => Promise<void>;
/**
* Read file as a buffer.
* @param input file name
*/
declare const read: (input: string) => Promise<Buffer>;
/**
* Write file with mkdir recursive.
* @param input file name
* @param contents file contents
*/
declare const write: (input: string, contents: string | Uint8Array) => Promise<void>;
/**
* Detect buffer is binary.
* @param input buffer
*/
declare const isBinary: (input: Uint8Array) => boolean;
/**
* Tildify absolute path.
* @param input absolute path
* @see https://github.com/sindresorhus/tildify
*/
declare const tildify: (input: string) => string;
/**
* Untildify tilde path.
* @param input tilde path
* @see https://github.com/sindresorhus/untildify
*/
declare const untildify: (input: string) => string;
/**
* Extract zip file.
* @param input input path or stream
* @param output output path
* @param strip strip output path
* @see https://github.com/shinnn/node-strip-dirs
*/
declare const extract: (input: string, output: string, strip?: number) => Promise<void>;
declare const file_exists: typeof exists;
declare const file_extract: typeof extract;
declare const file_isBinary: typeof isBinary;
declare const file_isDirectory: typeof isDirectory;
declare const file_isEmpty: typeof isEmpty;
declare const file_isFile: typeof isFile;
declare const file_mkdir: typeof mkdir;
declare const file_read: typeof read;
declare const file_remove: typeof remove;
declare const file_tildify: typeof tildify;
declare const file_untildify: typeof untildify;
declare const file_write: typeof write;
declare namespace file {
export {
file_exists as exists,
file_extract as extract,
file_isBinary as isBinary,
file_isDirectory as isDirectory,
file_isEmpty as isEmpty,
file_isFile as isFile,
file_mkdir as mkdir,
file_read as read,
file_remove as remove,
file_tildify as tildify,
file_untildify as untildify,
file_write as write,
};
}
declare const FormData: {
new (): FormData;
prototype: FormData;
};
/** @type {typeof globalThis.Blob} */
declare const Blob: typeof globalThis.Blob;
type AbortSignal = {
readonly aborted: boolean;
addEventListener: (type: 'abort', listener: (this: AbortSignal) => void) => void;
removeEventListener: (type: 'abort', listener: (this: AbortSignal) => void) => void;
};
type HeadersInit = Headers | Record<string, string> | Iterable<readonly [string, string]> | Iterable<Iterable<string>>;
/**
* This Fetch API interface allows you to perform various actions on HTTP request and response headers.
* These actions include retrieving, setting, adding to, and removing.
* A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs.
* You can add to this using methods like append() (see Examples.)
* In all methods of this interface, header names are matched by case-insensitive byte sequence.
* */
declare class Headers {
constructor(init?: HeadersInit);
append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | null;
has(name: string): boolean;
set(name: string, value: string): void;
forEach(
callbackfn: (value: string, key: string, parent: Headers) => void,
thisArg?: any
): void;
[Symbol.iterator](): IterableIterator<[string, string]>;
/**
* Returns an iterator allowing to go through all key/value pairs contained in this object.
*/
entries(): IterableIterator<[string, string]>;
/**
* Returns an iterator allowing to go through all keys of the key/value pairs contained in this object.
*/
keys(): IterableIterator<string>;
/**
* Returns an iterator allowing to go through all values of the key/value pairs contained in this object.
*/
values(): IterableIterator<string>;
/** Node-fetch extension */
raw(): Record<string, string[]>;
}
interface RequestInit {
/**
* A BodyInit object or null to set request's body.
*/
body?: BodyInit | null;
/**
* A Headers object, an object literal, or an array of two-item arrays to set request's headers.
*/
headers?: HeadersInit;
/**
* A string to set request's method.
*/
method?: string;
/**
* A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.
*/
redirect?: RequestRedirect;
/**
* An AbortSignal to set request's signal.
*/
signal?: AbortSignal | null;
/**
* A string whose value is a same-origin URL, "about:client", or the empty string, to set request’s referrer.
*/
referrer?: string;
/**
* A referrer policy to set request’s referrerPolicy.
*/
referrerPolicy?: ReferrerPolicy;
// Node-fetch extensions to the whatwg/fetch spec
agent?: RequestOptions['agent'] | ((parsedUrl: URL) => RequestOptions['agent']);
compress?: boolean;
counter?: number;
follow?: number;
hostname?: string;
port?: number;
protocol?: string;
size?: number;
highWaterMark?: number;
insecureHTTPParser?: boolean;
}
interface ResponseInit {
headers?: HeadersInit;
status?: number;
statusText?: string;
}
type BodyInit =
| Blob
| Buffer
| URLSearchParams
| FormData
| NodeJS.ReadableStream
| string;
declare class BodyMixin {
constructor(body?: BodyInit, options?: {size?: number});
readonly body: NodeJS.ReadableStream | null;
readonly bodyUsed: boolean;
readonly size: number;
/** @deprecated Use `body.arrayBuffer()` instead. */
buffer(): Promise<Buffer>;
arrayBuffer(): Promise<ArrayBuffer>;
formData(): Promise<FormData>;
blob(): Promise<Blob>;
json(): Promise<unknown>;
text(): Promise<string>;
}
type RequestRedirect = 'error' | 'follow' | 'manual';
type ReferrerPolicy = '' | 'no-referrer' | 'no-referrer-when-downgrade' | 'same-origin' | 'origin' | 'strict-origin' | 'origin-when-cross-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
type RequestInfo = string | Request;
declare class Request extends BodyMixin {
constructor(input: URL | RequestInfo, init?: RequestInit);
/**
* Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.
*/
readonly headers: Headers;
/**
* Returns request's HTTP method, which is "GET" by default.
*/
readonly method: string;
/**
* Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default.
*/
readonly redirect: RequestRedirect;
/**
* Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler.
*/
readonly signal: AbortSignal;
/**
* Returns the URL of request as a string.
*/
readonly url: string;
/**
* A string whose value is a same-origin URL, "about:client", or the empty string, to set request’s referrer.
*/
readonly referrer: string;
/**
* A referrer policy to set request’s referrerPolicy.
*/
readonly referrerPolicy: ReferrerPolicy;
clone(): Request;
}
type ResponseType = 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect';
declare class Response extends BodyMixin {
constructor(body?: BodyInit | null, init?: ResponseInit);
readonly headers: Headers;
readonly ok: boolean;
readonly redirected: boolean;
readonly status: number;
readonly statusText: string;
readonly type: ResponseType;
readonly url: string;
clone(): Response;
static error(): Response;
static redirect(url: string, status?: number): Response;
static json(data: any, init?: ResponseInit): Response;
}
/**
* Send a http request.
* @param url url
* @param init init
*/
declare const request: (url: RequestInfo, init?: RequestInit) => Promise<Response>;
/**
* Download remote resource to temporary file.
* @param url remote url
* @returns temporary filename
*/
declare const download: (url: string) => Promise<string>;
declare const http_download: typeof download;
declare const http_request: typeof request;
declare namespace http {
export {
http_download as download,
http_request as request,
};
}
interface Paths {
/**
Directory for data files.
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Application Support/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Data` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Data`)
- Linux: `~/.local/share/MyApp-nodejs` (or `$XDG_DATA_HOME/MyApp-nodejs`)
*/
readonly data: string;
/**
Directory for data files.
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Preferences/MyApp-nodejs`
- Windows: `%APPDATA%\MyApp-nodejs\Config` (for example, `C:\Users\USERNAME\AppData\Roaming\MyApp-nodejs\Config`)
- Linux: `~/.config/MyApp-nodejs` (or `$XDG_CONFIG_HOME/MyApp-nodejs`)
*/
readonly config: string;
/**
Directory for non-essential data files.
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Caches/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Cache` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Cache`)
- Linux: `~/.cache/MyApp-nodejs` (or `$XDG_CACHE_HOME/MyApp-nodejs`)
*/
readonly cache: string;
/**
Directory for log files.
Example locations (with the default `nodejs` suffix):
- macOS: `~/Library/Logs/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\MyApp-nodejs\Log` (for example, `C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Log`)
- Linux: `~/.local/state/MyApp-nodejs` (or `$XDG_STATE_HOME/MyApp-nodejs`)
*/
readonly log: string;
/**
Directory for temporary files.
Example locations (with the default `nodejs` suffix):
- macOS: `/var/folders/jf/f2twvvvs5jl_m49tf034ffpw0000gn/T/MyApp-nodejs`
- Windows: `%LOCALAPPDATA%\Temp\MyApp-nodejs` (for example, `C:\Users\USERNAME\AppData\Local\Temp\MyApp-nodejs`)
- Linux: `/tmp/USERNAME/MyApp-nodejs`
*/
readonly temp: string;
}
declare const _default$2: {
npm: Record<string, any> | undefined;
git: Record<string, any> | undefined;
paths: Paths;
ini: (filename: string) => Record<string, any> | undefined;
registry: string;
official: string;
branch: string;
proxy: string | undefined;
commitMessage: string;
};
/**
* Execute a command.
* @param command command name
* @param args command arguments
* @param options command options
*/
declare const _default$1: (command: string, args: string[], options: SpawnOptions) => Promise<void>;
type Middleware<S> = (state: S) => Promise<void> | void;
/**
* Middleware Async.
* @template S state type
*/
declare class Ware<S> {
private readonly middlewares;
/**
* Use the given middleware.
* @param middleware middleware func
*/
use(middleware: Middleware<S>): Ware<S>;
/**
* Run all middlewares.
* @param state initial state
*/
run(state: S): Promise<void>;
}
interface Options {
/**
* Force mode, overwrite if the target exists.
* @default false
*/
force?: boolean;
/**
* Offline mode, try to use an offline template.
* @default false
*/
offline?: boolean;
/**
* cli options to override prompts
*/
[key: string]: unknown;
}
/**
* Template config.
*/
interface Template {
/**
* Template name.
*/
name: string;
/**
* Template version.
*/
version?: string;
/**
* Template source dirname.
*/
source?: string;
/**
* Template metadata.
*/
metadata?: Record<string, unknown>;
/**
* Template prompts.
*/
prompts?: prompts.PromptObject | prompts.PromptObject[];
/**
* Template file filters.
*/
filters?: Record<string, (answers: Record<string, unknown>) => boolean>;
/**
* Template engine helpers.
*/
helpers?: Record<string, unknown>;
/**
* Auto install dependencies.
*/
install?: false | 'npm' | 'yarn' | 'pnpm';
/**
* Auto init git repository.
*/
init?: boolean;
/**
* Template setup hook, execute after template loaded & inquire completed.
*/
setup?: (ctx: Context) => Promise<void>;
/**
* Template prepare hook, execute after template files prepare, before rename & render.
*/
prepare?: (ctx: Context) => Promise<void>;
/**
* Template emit hook, execute after all files emit to the destination.
*/
emit?: (ctx: Context) => Promise<void>;
/**
* Template all completed.
*/
complete?: ((ctx: Context) => string | Promise<string> | Promise<void>) | string;
}
/**
* File info.
*/
interface File {
/**
* File full path
*/
path: string;
/**
* File contents (buffer)
*/
contents: Buffer;
}
/**
* Creator context.
*/
interface Context {
/**
* Template name.
* e.g.
* - offlical short name: `nm`
* - offlical short name with branch: `nm#master`
* - custom full name: `zce/nm`
* - custom full name with branch: `zce/nm#master`
* - local directory path: `~/templates/nm`
* - full url: `https://github.com/zce/nm/archive/master.zip`
*/
readonly template: string;
/**
* Project name, which is also the project directory.
*/
readonly project: string;
/**
* More options, most of the cases come from CLI..
*/
readonly options: Options;
/**
* The source directory where the template (absolute).
*/
src: string;
/**
* Generated result output destination directory (absolute).
*/
dest: string;
/**
* Template config.
*/
readonly config: Template;
/**
* Template prompts answers.
*/
readonly answers: Record<string, unknown>;
/**
* Template files.
*/
readonly files: File[];
}
declare const inject: typeof prompts.inject;
declare const _default: (template: string, project?: string, options?: Options) => Promise<void>;
export { Context, ListOptions, Middleware, Options, Template, Ware, _default$2 as config, _default as default, _default$1 as exec, file, http, inject, _default$3 as list };