UNPKG

@tririga/tri-template

Version:

A simple tool for generating IBM TRIRIGA UX view skeletons from available templates.

132 lines (116 loc) 3.73 kB
"use strict"; const path = require('path'); const fs = require("fs"); const colors = require("colors"); class TriTemplate { constructor(template, elementName, dir, force, cleanDir) { this.template = template; this.elementName = elementName; this.dir = this._stripTrailingSlash(dir); this.force = force; this.cleanDir = cleanDir; } generateViewFiles() { var templateDir = __dirname + path.sep + "templates" + path.sep + this.template; if (this.isDirectory(templateDir)) { if (this.cleanDir) { this._rmDir(this.dir); } this._copyFiles(templateDir, this.dir, this.elementName); } } isDirectory(path) { return fs.statSync(path).isDirectory(); } _copyFiles(sourceDir, targetDir, elementName) { var templateFiles = fs.readdirSync(sourceDir); this._createDirIfNecessary(targetDir); for (var index = 0; index < templateFiles.length; index++) { var filePath = sourceDir + path.sep + templateFiles[index]; if (this.isDirectory(filePath)) { var dirPath = targetDir + path.sep + path.basename(filePath); this._copyFiles(filePath, dirPath, elementName); } else if (this._isHTML(filePath) || this._isJS(filePath)) { this._copyHTMLJS(filePath, targetDir, elementName); } else { this._copyFile(filePath, targetDir, elementName); } } } _createDirIfNecessary(dir) { if(!fs.existsSync(dir)) { fs.mkdirSync(dir); } } _rmDir(dir, removeDir) { if(fs.existsSync(dir)) { var files = fs.readdirSync(dir); for (var index = 0; index < files.length; index++) { var filePath = dir + path.sep + files[index]; if (this.isDirectory(filePath)) { this._rmDir(filePath, true); } else { fs.unlinkSync(filePath); } } if (removeDir) { fs.rmdirSync(dir); } } } _copyHTMLJS(filePath, targetDir, elementName) { var data = fs.readFileSync(filePath, 'utf8'); var elementCamelCase = this.toCamelCase(elementName); if (this._isJS(filePath)) { elementCamelCase = this.capitalize(elementCamelCase); } data = data.replace(/@ElementCamelCase/g, elementCamelCase); data = data.replace(/@element/g, elementName); var targetFile = this._computeTargetFilePath(filePath, targetDir, elementName); if (!this.force && fs.existsSync(targetFile)) { throw "A file with the name " + targetFile + " already exists."; } fs.writeFileSync(targetFile, data); console.log("Generated File: " + targetFile.magenta); } _copyFile(filePath, targetDir, elementName) { var data = fs.readFileSync(filePath); var targetFile = this._computeTargetFilePath(filePath, targetDir, elementName); if (!this.force && fs.existsSync(targetFile)) { throw "A file with the name " + targetFile + " already exists."; } fs.writeFileSync(targetFile, data); console.log("Generated File: " + targetFile.magenta); } _computeTargetFilePath(filePath, targetDir, elementName) { var fileName = path.basename(filePath); return targetDir + path.sep + fileName.replace(/@element/, elementName); } _stripTrailingSlash(dir) { if (dir == null || dir.length == 0) { return ""; } return path.resolve(dir).replace(/\/+$/, ""); } _isHTML(filePath) { var fileExtension = path.extname(filePath); return fileExtension != null && (fileExtension == ".htm" || fileExtension == ".html"); } _isJS(filePath) { var fileExtension = path.extname(filePath); return fileExtension != null && fileExtension == ".js"; } toCamelCase(name) { return name.replace( /^([A-Z])|[\s-_](\w)/g, function(match, p1, p2, offset) { if (p2) return p2.toUpperCase(); return p1.toLowerCase(); } ); } capitalize(name) { return name.charAt(0).toUpperCase() + name.slice(1); } }; module.exports = TriTemplate;