UNPKG

4.51 kBMarkdownView Raw
1---
2id: api
3title: API References
4sidebar_label: API
5---
6
7There have a `TypeScript` type definitions which reflect the `api's` documentation
8
9An `api` is a instance of `Plugin`
10
11## Plugin Definitions
12
13```typescript
14/// <reference types="handlebars" />
15/// <reference types="inquirer" />
16/// <reference types="debug" />
17import program from 'commander'
18import fs, { WriteFileOptions } from 'fs-extra'
19import inquirer, { Questions, Answers } from 'inquirer'
20import * as npm from './npm'
21import prettier, { Options } from 'prettier'
22import debug from 'debug'
23import 'colors'
24import { PkgConf } from './config'
25import Handlebars from './handlebars'
26/**
27 * Action
28 */
29export interface Action {
30 /**
31 * path to new file write to
32 */
33 path: string
34 /**
35 * path to template file
36 */
37 templateFile: string
38 /**
39 * format code
40 * - provide a prettier configurations if is an object
41 * - true use default options
42 */
43 format?: Options | boolean
44}
45export interface Package {
46 name: string
47 version: string
48 description: string
49 dependencies: {
50 [key: string]: string
51 }
52 devDependencies: {
53 [key: string]: string
54 }
55 merry: PkgConf
56 [key: string]: any
57}
58export declare class Plugin {
59 private readonly program
60 readonly conf: PkgConf
61 /**
62 * absolutely package path
63 */
64 pkgCwd: string
65 readonly fs: typeof fs
66 readonly npm: typeof npm
67 readonly prettier: typeof prettier
68 readonly handlebars: typeof Handlebars
69 readonly inquirer: inquirer.Inquirer
70 readonly pkg: Package
71 /**
72 * plugin current working directory
73 */
74 readonly cwd: string
75 constructor(
76 program: program.CommanderStatic,
77 conf: PkgConf,
78 /**
79 * absolutely package path
80 */
81 pkgCwd: string
82 )
83 /**
84 * register command with version and description builtin
85 * @param command
86 */
87 command(command: string): program.Command
88 /**
89 * compile Handlebars template
90 * @param templateOrFilePath path to templates
91 * @param data data
92 * @param options
93 */
94 compile(
95 templateOrFilePath: string,
96 data: any,
97 options?: CompileOptions
98 ): string
99 /**
100 * write files to destination
101 * @param path
102 * @param data
103 * @param options
104 */
105 write(
106 path: string,
107 data: any,
108 options?: WriteFileOptions | string
109 ): Promise<void>
110 /**
111 * write file
112 * @param path
113 * @param data
114 * @param options
115 */
116 writeFile(
117 path: string,
118 data: any,
119 options?: WriteFileOptions | string
120 ): Promise<void>
121 /**
122 * compile and write file to destination
123 * @param template path/to/hbs
124 * @param dist path/file/will/write/to
125 * @param data data will pass to templates
126 */
127 tmpl(template: string, dist: string, data: any): Promise<void>
128 /**
129 * compile and write file to destination
130 * @param template path/to/hbs
131 * @param dist path/file/will/write/to
132 * @param data data will pass to templates
133 */
134 tmplWithFormat(
135 template: string,
136 dist: string,
137 data: any,
138 options?: Options
139 ): Promise<void>
140 /**
141 * compile and write file to destination
142 * @param template path/to/hbs
143 * @param data data will pass to templates
144 */
145 format(code: string, options?: Options): Promise<string>
146 /**
147 * questions
148 * @param questions
149 */
150 prompt<T = Answers>(questions: Questions): Promise<T>
151 expand(
152 file: string
153 ): Promise<{
154 overwrite: 'overwrite' | 'diff' | 'abort'
155 }>
156 /**
157 * get pretty name
158 * @param {string} name
159 */
160 getPrettyName(name: string): string
161 /**
162 * run actions
163 * @param {(Action|null)[]} actions
164 * @param {T} answers
165 */
166 runActions: <T>(actions: (Action | null)[], answers: T) => Promise<void>
167 /**
168 * log for plugin
169 *
170 * @param formatter
171 *
172 * ### Formatters
173 * Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.
174 * Below are the officially supported formatters:
175 *
176 * | Formatter | Representation |
177 * |-----------|----------------|
178 * | `%O` | Pretty-print an Object on multiple lines. |
179 * | `%o` | Pretty-print an Object all on a single line. |
180 * | `%s` | String. |
181 * | `%d` | Number (both integer and float). |
182 * | `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
183 * | `%%` | Single percent sign ('%'). This does not consume an argument. |
184 *
185 * @memberof Plugin
186 */
187 log: (formatter: string, ...args: any[]) => void
188 /**
189 * get a instance of node debug
190 * @param {string} namespace you can use a customized namespace or
191 * using the default one plugin:[your-plugin]
192 * @memberof Plugin
193 */
194 debug: (namespace?: string) => debug.IDebugger
195}
196```