UNPKG

p5-analysis

Version:

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

65 lines (64 loc) 2.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const __1 = require(".."); const printTree_1 = require("./helpers/printTree"); async function tree(dirs, options) { const depth = Number(options.level || Infinity); const printDescriptions = options.descriptions; const tabWidth = Number(options.tabWidth || 4); for (const dir of dirs.length ? dirs : [process.cwd()]) { const iter = sketchTreeIter(dir, { depth, printDescriptions }); await (0, printTree_1.printTree)(iter, tabWidth); } } exports.default = tree; function sketchTreeIter(file, { depth, printDescriptions }) { return visit(file, depth); async function* visit(file, depth) { if (fs_1.default.statSync(file).isDirectory()) { yield* visitDir(file, depth - 1); } else if (await __1.Sketch.isSketchFile(file)) { const sketch = await __1.Sketch.fromFile(file); yield* visitSketch(sketch, depth - 1); } else { yield path_1.default.basename(file); } } // Recursively visit directory async function* visitDir(dir, depth) { const { sketches, unassociatedFiles } = await __1.Sketch.analyzeDirectory(dir); yield* ['📁' + path_1.default.basename(dir), printTree_1.indentSymbol]; if (depth >= 0) { for (const sketch of sketches.sort((a, b) => a.name.localeCompare(b.name))) { yield* visitSketch(sketch, depth - 1); } const files = [...unassociatedFiles] .map(f => path_1.default.join(dir, f)) .sort((a, b) => a.localeCompare(b)) .sort((a, b) => Number(fs_1.default.statSync(b).isDirectory()) - Number(fs_1.default.statSync(a).isDirectory())); for (const file of files) { yield* visit(file, depth - 1); } } yield printTree_1.dedentSymbol; } // Recursively visit sketch async function* visitSketch(sketch, depth) { const mainFile = sketch.files.length === 1 ? ` (${sketch.mainFile})` : ''; const description = printDescriptions && sketch.description ? ` - ${sketch.description.replace(/\s*\n\s*/g, ' ')}` : ''; yield* ['🎨' + `${sketch.name}${mainFile}${description}`, printTree_1.indentSymbol]; if (depth >= 0 && sketch.files.length > 1) { yield* sketch.files; } yield printTree_1.dedentSymbol; } }