UNPKG

p5-analysis

Version:

API to find, create, and analyze p5.js sketch files.

187 lines (186 loc) 7.54 kB
import { HTMLElement } from 'node-html-parser'; import { Library } from './Library'; export declare type SketchStructureType = 'html' /** The main file is an HTML file */ | 'script'; /** The main file is a script file */ /** Sketch represents a p5.js Sketch. Is an interface to generate sketch files, * find associated files, infer libraries, and scan directories for sketches that * they contain. * * A sketch can be an HTML sketch, or a script sketch. */ export declare abstract class Sketch { /** The directory that contains the sketch files. Other xxxFile properties are relative to this. */ readonly dir: string; /** For an HTML sketch, the pathname of the HTML file, relative to dir. */ abstract readonly htmlFile: string | null; /** The main script file, relative to dir. */ readonly scriptFile: string; readonly description?: string; protected readonly _title?: string; protected _name?: string; protected constructor(dir: string, scriptFile?: string, options?: { title?: string; description?: string; }); /** * @category Sketch creation */ static create(mainFile: string, options?: { title?: string; description?: string; scriptFile?: string; }): Sketch; /** Create a sketch from an HTML file. * * @category Sketch creation */ static fromHtmlFile(htmlFile: string): Promise<Sketch>; /** Create a sketch from a JavaScript file. * * @category Sketch creation */ static fromScriptFile(scriptFile: string): Promise<Sketch>; /** Create a sketch from a directory. This method throws an exception if the * directory does not contain a sketch index.html file, or contains multiple * sketch files. * * @category Sketch creation */ static fromDirectory(dir: string, options?: { exclusions?: string[]; depth?: number; }): Promise<Sketch>; /** Create a sketch from a file. `filePath` should be path to an HTML sketch * file, a JavaScript sketch file, or a directory that contains exactly one * sketch file. * * @category Sketch detection */ static fromFile(filePath: string): Promise<Sketch>; /** Analyze the directory for sketch files. Returns a list of sketches, and * files that aren't associated with any sketch. * * @category Sketch detection */ static analyzeDirectory(dir: string, options?: { exclusions?: string[]; }): Promise<{ sketches: Sketch[]; allFiles: string[]; unassociatedFiles: string[]; }>; /** Tests whether a file is an HTML sketch file. It is a sketch file if it * includes the `p5.min.js` or `p5.js` script. * * @category Sketch detection */ static isSketchHtmlFile(htmlFile: string): Promise<boolean>; /** Tests whether a file is a JavaScript sketch file. It is recognized as a * sketch file if it includes a definition of the `setup()` function and a * call to the p5.js `createCanvas()`. * * @category Sketch detection */ static isSketchScriptFile(file: string): Promise<boolean>; /** Tests whether a file is an HTML or JavaScript sketch file. * * @category Sketch detection */ static isSketchFile(file: string): Promise<boolean>; /** Tests whether the directory is a sketch directory. It is a sketch * directory if it contains a single JavaScript sketch file, or a single HTML * sketch file named `index.html` that includes this file. * * @category Sketch detection */ static isSketchDir(dir: string, { exclusions, depth }?: { exclusions?: string[] | undefined; depth?: number | undefined; }): Promise<Sketch | null>; /** The file structure of the sketch. */ abstract get structureType(): SketchStructureType; /** For an HTML sketch, this is the HTML file. For a JavaScript sketch, this is * the JavaScript file. In either case, it is relative to dir. */ abstract get mainFile(): string; get mainFilePath(): string; get htmlFilePath(): string | null; get scriptFilePath(): string; get name(): string; set name(value: string); /** For an HTML sketch, this is the <title> element. Otherwise it is the base * name of the main file. */ get title(): string; protected getTitleFromFile(): string | null; /** The HTML file (for an HTML sketch); any JavaScript files; any files that * the HTML file includes; and any files that the JavaScript files include, * to the extent that this can be determined by static inspection. * * File names are relative to sketch.dirPath. */ abstract get files(): readonly string[]; /** The list of libraries. For a JavaScript sketch, this is the list of * libraries inferred from the undefined global variables that it references. * For an HTML sketch, this is the list of libraries named in the HTML file. * * @category Libraries */ get libraries(): readonly Library[]; protected impliedLibraries(): readonly Library[]; protected static readonly indexTemplateName = "index.pug"; /** Create and save the files for this sketch. This includes the script file; * for an HTML sketch, this also includes the HTML file. * * * @category file generation */ generate(force?: boolean, options?: Record<string, unknown>): Promise<string[]>; protected writeGeneratedFile(templateName: string, filename: string, force: boolean, templateOptions: Record<string, unknown>): Promise<string>; private getGeneratedFileContent; getHtmlContent(): Promise<string>; /** Convert an HTML sketch to a JavaScript-only sketch (by removing the HTML file), * or a JavaScript sketch to an HTML sketch (by adding the HTML file). * * Before modifying the file system, this method verifies that the set of libraries * will remain the same. Before removing an HTML file, it also verifies that the file * included only the single script file, and no other non-library files. * * @category Sketch conversion */ abstract convert(options: { type: SketchStructureType; }): Promise<void>; } export declare class HtmlSketch extends Sketch { readonly htmlFile: string; constructor(dir: string, htmlFile?: string, scriptFile?: string, options?: { title?: string; description?: string; }); static fromFile(htmlFilePath: string): Promise<Sketch>; static isSketchHtmlFile(htmlFilePath: string): Promise<boolean>; get structureType(): SketchStructureType; get mainFile(): string; get files(): readonly string[]; get libraries(): Library[]; private explicitLibraries; protected getTitleFromFile(): string | null; private getAssociatedFiles; protected getLocalScriptFiles(htmlRoot?: HTMLElement): readonly string[]; private static getLocalScriptFiles; convert(options: { type: SketchStructureType; }): Promise<void>; } export declare class ScriptSketch extends Sketch { static fromFile(scriptFile: string): Promise<Sketch>; static isSketchScriptFile(file: string): Promise<boolean>; get structureType(): SketchStructureType; get mainFile(): string; get htmlFile(): null; get files(): readonly string[]; convert(options: { type: SketchStructureType; }): Promise<void>; private static getDescriptionFromScript; }