UNPKG

templates-mo

Version:

Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting

97 lines (96 loc) • 3.78 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectoryNode = void 0; const path = __importStar(require("path")); const fs_1 = __importDefault(require("fs")); const minimatch_1 = require("minimatch"); const fileSystem_1 = require("../utilities/fileSystem"); const helpers_1 = require("../utilities/helpers"); const fileSystemNode_1 = require("./fileSystemNode"); const fileNode_1 = __importDefault(require("./fileNode")); class DirectoryNode extends fileSystemNode_1.FileSystemNode { constructor(name, parentDirNode = null, verbose = false) { let parentDir = parentDirNode; if (name && !parentDirNode) { parentDir = path.dirname(name); // eslint-disable-next-line no-param-reassign name = path.basename(name); } // this.verbose = verbose || false; super(name, 'dir', parentDir, verbose); this._renderChildren(); } toObject() { const obj = super.toObject(); obj.children = this.children.map((fileNode) => fileNode.toObject()); return obj; } _renderChildren() { let directoryChildren; try { directoryChildren = fs_1.default.readdirSync(this.path); } catch (e) { throw new Error(`[TPS ERROR] Path is not a directory (${this.path})`); } directoryChildren // TODO: should probably be done in the templates file instead // Filter out files that match the ignoreFiles pattern .filter((child) => { return !fileSystemNode_1.FileSystemNode.ignoreFiles.some((ignoreFile) => (0, minimatch_1.minimatch)(child, ignoreFile)); }) .forEach((name) => { const dirContentPath = path.join(this.path, name); const ContentType = (0, fileSystem_1.isDir)(dirContentPath) ? DirectoryNode : fileNode_1.default; const newFSNode = new ContentType(name, this, this.verbose); this.addChild(newFSNode); }); } eachChild(cb) { this.breathFirstEach((fsNode) => { if (this !== fsNode) { cb(fsNode); } }); } selectChildren(cb) { const found = []; this.eachChild((fsNode) => { if (cb(fsNode)) { found.push(fsNode); } }); return found; } find(selectBy = {}) { return this.selectChildren((fsNode) => (0, helpers_1.couldMatchObj)(selectBy, fsNode)); } } exports.DirectoryNode = DirectoryNode; exports.default = DirectoryNode;