UNPKG

16.7 kBTypeScriptView Raw
1// Type definitions for metalsmith 2.3
2// Project: https://github.com/metalsmith/metalsmith
3// Definitions by: Brian Lagerman <https://github.com/brian-lagerman>, Kevin Van Lierde <https://github.com/webketje>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 3.1
6
7/// <reference types="node" />
8import { Mode, Stats } from 'fs';
9interface Metalsmith {
10 /**
11 * Set the path of the `working` directory.
12 * @param directory Relative or absolute `working` directory path.
13 * @example
14 * Set the relative `working` directory to './working'
15 * Metalsmith(__dirname).directory("working");
16 * @example
17 * Set the absolute `working` directory to 'C:\Projects\Metalsmith\'
18 * Metalsmith(__dirname).directory("C:\\Projects\\Metalsmith");
19 * @link [API] https://github.com/metalsmith/metalsmith#api
20 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L62
21 */
22 directory(directory: string): Metalsmith;
23 /**
24 * Get the absolute path of the `working` directory
25 * @example
26 * Retrieve the absolute `working` directory path
27 * const mwd:string = Metalsmith(__dirname).directory();
28 * @link [API] https://github.com/metalsmith/metalsmith#api
29 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L62
30 */
31 directory(): string;
32 /**
33 * Set the path of the `source` directory.
34 * @param path [path='src'] - Relative or absolute `source` directory path.
35 * @example
36 * Set the relative `source` directory to './src' (the default)
37 * Metalsmith(__dirname).source("src");
38 * @example
39 * Set the absolute `source` directory to 'C:\Projects\Site\'
40 * Metalsmith(__dirname).source("C:\\\Projects\\\Site");
41 * @link [API] https://github.com/metalsmith/metalsmith#sourcepath
42 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L90
43 */
44 source(path: string): Metalsmith;
45 /**
46 * Get the absolute path of the `source` directory.
47 * @example
48 * Retrieve the absolute `source` directory path
49 * var src:string = Metalsmith(__dirname).source();
50 * @link [API] https://github.com/metalsmith/metalsmith#sourcepath
51 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L90
52 */
53 source(): string;
54 /**
55 * Set the path of the `destination` directory.
56 * @param path [path='build'] - Relative or absolute `destination` directory path.
57 * @example
58 * Set the relative `destination` directory to './build' (the default)
59 * Metalsmith(__dirname).destination("build");
60 * @example
61 * Set the absolute `destination` directory to 'C:\Projects\Out\'
62 * Metalsmith(__dirname).destination("C:\\\Projects\\\Out");
63 * @link [API] https://github.com/metalsmith/metalsmith#destinationpath
64 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L104
65 */
66 destination(path: string): Metalsmith;
67 /**
68 * Get the absolute path of the `destination` directory.
69 * @example
70 * Retrieve the absolute `destination` directory path
71 * var dst:string = Metalsmith(__dirname).destination();
72 * @link [API] https://github.com/metalsmith/metalsmith#destinationpath
73 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L104
74 */
75 destination(): string;
76 /**
77 * Set the `maximum` number of files to open at once.
78 * @param max [max=Infinity] - The `maximum` number of open files.
79 * @example
80 * Set the `maximum` number of files to open at once to 50
81 * Metalsmith(__dirname).concurrency(50);
82 * @link [API] https://github.com/metalsmith/metalsmith#concurrencymax
83 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L118
84 */
85 concurrency(max: number): Metalsmith;
86 /**
87 * Get the `maximum` number of files to open at once.
88 * @example
89 * Retrieve the `maximum` number of files to open at once
90 * var max:number = Metalsmith(__dirname).concurrency();
91 * @link [API] https://github.com/metalsmith/metalsmith#concurrencymax
92 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L118
93 */
94 concurrency(): number;
95 /**
96 * Set whether the destination directory will be `cleaned` before writing.
97 * @param clean [clean=true] - Flag to `clean` destination directory.
98 * @example
99 * Set the flag to `clean` the destination directory to false
100 * Metalsmith(__dirname).clean(false);
101 * @link [API] https://github.com/metalsmith/metalsmith#cleanboolean
102 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L132
103 */
104 clean(clean: boolean): Metalsmith;
105 /**
106 * Get the flag on whether the destination directory will be `cleaned` before writing.
107 * @example
108 * Retrieve the `clean` flag indicating destination directory removal
109 * var clean:boolean = Metalsmith(__dirname).clean();
110 * @link [API] https://github.com/metalsmith/metalsmith#cleanboolean
111 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L132
112 */
113 clean(): boolean;
114 /**
115 * Set the flag on whether to parse YAML `frontmatter`
116 * @param frontmatter [frontmatter=true] - Flag to parse YAML `frontmatter`.
117 * @example
118 * Set the flag to parse YAML `frontmatter` to false
119 * Metalsmith(__dirname).frontmatter(false);
120 * @link [API] https://github.com/metalsmith/metalsmith#frontmatterboolean
121 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L145
122 */
123 frontmatter(frontmatter: boolean): Metalsmith;
124 /**
125 * Get the flag on whether to parse YAML `frontmatter`
126 * @example
127 * Retrieve the `frontmatter` flag indicating YAML parsing
128 * var parse:boolean = Metalsmith(__dirname).frontmatter();
129 * @link [API] https://github.com/metalsmith/metalsmith#frontmatterboolean
130 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L145
131 */
132 frontmatter(): boolean;
133 /**
134 * Set the global `metadata` object to pass to templates.
135 * @param metadata - The global metadata (json).
136 * @example
137 * Add 'sitename' to the global `metadata` object
138 * Metalsmith(__dirname).metadata({sitename: "My Static Site"});
139 * @link [API] https://github.com/metalsmith/metalsmith#metadatajson
140 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L76
141 */
142 metadata(metadata: object): Metalsmith;
143 /**
144 * Get the global `metadata` object passed to templates.
145 * @example
146 * Retrieve the `metadata` object passed to templates
147 * var meta:object = Metalsmith(__dirname).metadata();
148 * @link [API] https://github.com/metalsmith/metalsmith#metadatajson
149 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L76
150 */
151 metadata(): object;
152 /**
153 * Add a `plugin` function to the stack.
154 * @param plugin - The plugin to add.
155 * @example
156 * Add 'metalsmith-markdown' to the middleware stack
157 * Metalsmith(__dirname).use(markdown());
158 * @link [API] https://github.com/metalsmith/metalsmith#useplugin
159 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L50
160 */
161 use(plugin: Metalsmith.Plugin | Metalsmith.Plugin[]): Metalsmith;
162 /**
163 * Set the `Ignore` files/paths from being loaded into Metalsmith.
164 * @param files - The file(s) or directories to `ignore`.
165 * @example
166 * Add an `ignore` file to prevent load into Metalsmith
167 * Metalsmith(__dirname).ignore("corrupted.html");
168 * @example
169 * Add an `ignore` function to prevent load into Metalsmith
170 * Metalsmith(__dirname).ignore(ignore(function (filepath: string, stats: Stats) {
171 * return stats.isDirectory() && path.basename(filepath) === 'nested';
172 * });
173 * @link [API] https://github.com/metalsmith/metalsmith#ignorepath
174 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L159
175 */
176 ignore(files: string | string[] | Metalsmith.Ignore | Metalsmith.Ignore[]): Metalsmith;
177 /**
178 * Get the array of `Ignored` files/paths.
179 * @example
180 * Retrieve the `ignored` array of files in Metalsmith
181 * var ignored:string[] = Metalsmith(__dirname).ignore();
182 * @link [API] https://github.com/metalsmith/metalsmith#ignorepath
183 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L159
184 */
185 ignore(): string[];
186 /**
187 * Resolve `paths` relative to the root directory.
188 * @param paths - The `paths` to resolve.
189 * @example
190 * Retrieve the path after resolving sub-directies
191 * var path:string = Metalsmith(__dirname).path("path-a", "path-b");
192 * @link [API] https://github.com/metalsmith/metalsmith#pathpaths
193 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L171
194 */
195 path(...paths: string[]): string;
196 /**
197 * Perform the `build` with the current settings outputting to the destination directory.
198 * @param fn - Optional **(but strongly encouraged)** {@link Callback} function.
199 * @example
200 * Perform the `build` with the current settings
201 * Metalsmith(__dirname).build(
202 * function (err: Error): any {
203 * if (err) {throw err;}
204 * });
205 * @link [API] https://github.com/metalsmith/metalsmith#buildfn
206 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L184
207 */
208 build(fn?: Metalsmith.Callback): object;
209 /**
210 * `Process` files through plugins without writing out files.
211 * @param fn - Optional Callback function.
212 * @example
213 * `Process` the files like `build` without writing any files
214 * Metalsmith(__dirname).process(
215 * function (err: Error): any {
216 * if (err) {throw err;}
217 * });
218 * @link [API] https://github.com/metalsmith/metalsmith#processfn
219 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L200
220 */
221 process(fn?: Metalsmith.Callback): object;
222 /**
223 * `Run` a set of files through the plugins stack.
224 * @param files - The dictionary of files.
225 * @param fn - Optional Callback function.
226 * @example
227 * `Run` all of the middleware functions on a dictionary of files.
228 * var callback:Metalsmith.Callback = (err: Error, files: object) => {if (err) {throw err;}};
229 * Metalsmith(__dirname).run({fileA: "a.html"} , callback);
230 * @link [API] https://github.com/metalsmith/metalsmith#runfiles-fn
231 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L212
232 */
233 run(files: object, fn?: Metalsmith.Callback): object;
234 /**
235 * `Run` a set of files through the plugins stack.
236 * @param files - The dictionary of files.
237 * @param fn - Optional Callback function.
238 * @example
239 * `Run` all of the middleware functions on a dictionary of files.
240 * var callback:Metalsmith.Callback = (err: Error, files: object) => {if (err) {throw err;}};
241 * Metalsmith(__dirname).run({fileA: "a.html"} , callback);
242 * @link [API] https://github.com/metalsmith/metalsmith#runfiles-fn
243 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L212
244 */
245 run(files: object, plugins: Metalsmith.Plugin[], fn?: Metalsmith.Callback): object;
246 /**
247 * Read a dictionary of files from a `dir`, parsing frontmatter. If no directory
248 * is provided, it will default to the source directory.
249 * @param dir - Optional dictionary of files.
250 * @example
251 * Read a dictionary of files from a `dir`.
252 * var files:object = Metalsmith(__dirname).read("subdir");
253 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L227
254 */
255 read(dir: string, fn?: Metalsmith.Callback): object;
256 /**
257 * Read a dictionary of files from a `dir`, parsing frontmatter. If no directory
258 * is provided, it will default to the source directory.
259 * @param fn - Optional Callback function.
260 * @example
261 * Read a dictionary of files from a `dir`.
262 * var files:object = Metalsmith(__dirname).read("subdir");
263 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L227
264 */
265 read(fn: Metalsmith.Callback): object;
266 /**
267 * Read a `file` by path. If the path is not absolute, it will be resolved
268 * relative to the source directory.
269 * @param file - The file path.
270 * @example
271 * Read a `file` by path.
272 * var fileData:object = Metalsmith(__dirname).readFile("a.html");
273 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L261
274 */
275 readFile(file: string): object;
276 /**
277 * Write a dictionary of `files` to a destination `dir`. If no directory is
278 * provided, it will default to the destination directory.
279 * @param files - Dictionary of files.
280 * @param dir - Optional destination directory.
281 * @param fn - Optional Callback function.
282 * @example
283 * Write a dictionary of `files` to a destination `dir`.
284 * Metalsmith(__dirname).write({fileA: "a.html"} , "C:\\\OutDir");
285 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L308
286 */
287 write(files: object, dir?: string, fn?: Metalsmith.Callback): void;
288 /**
289 * Write a dictionary of `files` to a destination `dir`. If no directory is
290 * provided, it will default to the destination directory.
291 * @param files - Dictionary of files.
292 * @param dir - Optional destination directory.
293 * @example
294 * Write a dictionary of `files` to a destination `dir`.
295 * Metalsmith(__dirname).write({fileA: "a.html"} , "C:\\\OutDir");
296 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L308
297 */
298 write(files: object, fn: Metalsmith.Callback): void;
299 /**
300 * Write a `file` by path with `data`. If the path is not absolute, it will be
301 * resolved relative to the destination directory.
302 * @param file - File path.
303 * @param data - The file data.
304 * @example
305 * Write a `file` by path with `data`.
306 * Metalsmith(__dirname).writeFile("test.html", {contents: "File Contents"});
307 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L336
308 */
309 writeFile(file: string, data: object): void;
310
311 /**
312 * The (read-only) list of plugins `use`'d by the current metalsmith instance.
313 * When read from inside a plugin, the list is guaranteed to be complete
314 */
315 readonly plugins: Metalsmith.Plugin[];
316
317 /**
318 * The (read-only) list of ignores of the current metalsmith instance
319 */
320 readonly ignores: string[];
321}
322
323/**
324 * Initialize a new `Metalsmith` builder with a working `directory`.
325 * @param directory - The working directory.
326 * @example
327 * initialize Metalsmith with the node.js working directory
328 * Metalsmith(__dirname);
329 * @link [Metalsmith] http://www.metalsmith.io/
330 * @link [API] https://github.com/metalsmith/metalsmith#new-metalsmithdir
331 * @link [Source] https://github.com/metalsmith/metalsmith/blob/v2.3.0/lib/index.js#L30
332 */
333declare function Metalsmith(directory: string): Metalsmith;
334
335declare namespace Metalsmith {
336 /**
337 * A Metalsmith plugin is a function that is passed the file list, the metalsmith instance, and a `done` callback.
338 * Calling the callback is required for asynchronous plugins, and optional for synchronous plugins.
339 */
340 type Plugin = (files: Files, metalsmith: Metalsmith, callback: Callback) => void;
341 type Callback = (err: Error | null, files: Files, metalsmith: Metalsmith) => void;
342 type Ignore = (path: string, stat: Stats) => void;
343
344 /**
345 * Metalsmith representation of a file
346 */
347 interface File {
348 /** A Node {@link Buffer} that can be `.toString`'ed to obtain its human-readable contents */
349 contents: Buffer;
350 /** A Node {@link Stats} object with extra filesystem metadata and methods (like {@link Stats.isFile}) */
351 stats?: Stats;
352 /** Octal permission {@link Mode} of a file */
353 mode?: string;
354 [property: string]: any;
355 }
356 /**
357 * Metalsmith representation of the files in `metalsmith.source()`.
358 * The keys represent the file paths and the values are {@link Metalsmith.File} objects
359 */
360 interface Files {
361 [filepath: string]: File;
362 }
363}
364
365export = Metalsmith;