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

116 lines (115 loc) • 4.43 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileSystemNode = void 0; const is = __importStar(require("is")); const path = __importStar(require("path")); const stack_1 = require("../data-structures/stack"); const tree_1 = require("../data-structures/tree"); // eslint-disable-next-line @typescript-eslint/no-explicit-any class FileSystemNode extends tree_1.Tree { constructor(name, type, parentDirectory, verbose) { super(); this.name = name; this.type = type; const isFSNode = this.isFileSystemNode(parentDirectory); // parse file path to get name and path/to/file if (!isFSNode && !is.string(parentDirectory)) { throw new TypeError(`Argument must be a String`); } this.depth = isFSNode ? parentDirectory.depth + 1 : 0; this.verbose = verbose || false; this.parent = isFSNode ? parentDirectory : null; this.parentPath = isFSNode ? parentDirectory.path : parentDirectory; this.path = path.join(this.parentPath, this.name); this.pathFromRoot = isFSNode ? path.join(parentDirectory.pathFromRoot, this.name) : '.'; if (verbose) { console.log('=============================================='); console.log('name', this.name); console.log('path', this.path); } } // eslint-disable-next-line @typescript-eslint/no-explicit-any isFileSystemNode(node) { return node instanceof FileSystemNode; } toObject() { return { name: this.name, type: this.type, path: this.path, }; } is(type) { return this.type === type; } get(name) { return this.children.find((tree) => tree.name === name); } addChild(value) { if (!this.isFileSystemNode(value)) { throw new TypeError(`Argument must be type FileNode or DirNode`); } const tree = value; this.children.push(tree); return tree; } getRelativePathFrom(parentDirNode, includeParentNode = true) { // eslint-disable-next-line @typescript-eslint/no-this-alias let child = this; const pathStack = new stack_1.Stack(); do { if (!child.parent) { throw new Error('The DirNode you passed in is not a parent of this Node'); } pathStack.push(child.name); child = child.parent; } while (parentDirNode !== child); // Add parent name top end of stack if (includeParentNode) { pathStack.push(parentDirNode.name); } let relativePath = ''; while (pathStack.size()) { relativePath = path.join(relativePath, pathStack.pop()); } return relativePath; } logTree(names = []) { this.depthFirstEach((tree) => { console.log(`${' '.repeat(tree.depth * 2)}${tree.name}: `); names.forEach((name) => { console.log(`${' '.repeat(tree.depth * 3)} -> ${name}: ${tree[name]} `); }, true); // console.log(`${' '.repeat(tree.depth * 2)}|`); // console.log(`${'\t'.repeat(this.depth * 2)}|`); }); } } exports.FileSystemNode = FileSystemNode; FileSystemNode.ignoreFiles = []; exports.default = FileSystemNode;