UNPKG

php-htx-cli

Version:

A cli to convert .htx (different components) files to .php

59 lines (58 loc) 3.04 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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 config_1 = require("../config/config"); const fileScanner_1 = __importDefault(require("../fileScanner")); const convert_1 = require("../convert"); function generateOutputFilePath(file, config) { const rootPath = process.cwd(); const srcRoot = path_1.default.join(rootPath, config.directory.src); const relativeFilePath = path_1.default.relative(srcRoot, file); const parentFolder = path_1.default.dirname(relativeFilePath); const filenName = path_1.default.basename(relativeFilePath, "." + config.extension.src); const outputPath = path_1.default.join(rootPath, config.directory.out, parentFolder, filenName + "." + config.extension.out); return outputPath; } function ensureDirectoryExistence(filePath) { var dirname = path_1.default.dirname(filePath); if (fs_1.default.existsSync(dirname)) { return true; } ensureDirectoryExistence(dirname); fs_1.default.mkdirSync(dirname); } function writeFile(filePath, contents) { ensureDirectoryExistence(filePath); fs_1.default.writeFileSync(filePath, contents); } function Run() { return __awaiter(this, void 0, void 0, function* () { console.log("Reading configuration"); const config = (0, config_1.readConfig)(); console.log(`Scanning for '.${config.extension.src}' files in '${config.directory.src}'`); const filesToConvert = yield (0, fileScanner_1.default)(config); for (let fileFoundIndex = 0; fileFoundIndex < filesToConvert.length; fileFoundIndex++) { const file = filesToConvert[fileFoundIndex]; console.log(`Processing file ${fileFoundIndex + 1}/${filesToConvert.length}: ${file}`); const fileContents = fs_1.default.readFileSync(file, 'utf8'); const outputFile = generateOutputFilePath(file, config); const convertedContents = (0, convert_1.convertHtx)(fileContents, file, config); writeFile(outputFile, convertedContents); } console.log("All converted"); }); } exports.default = Run;