UNPKG

tyr-cli

Version:

A command line interface for hammer-io.

115 lines (32 loc) 27.2 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports. createDirectory = createDirectory;exports. readFile = readFile;exports. loadTemplate = loadTemplate;exports. writeFile = writeFile;exports. deleteFile = deleteFile;exports. exists = exists;var _fsExtra = require('fs-extra');var _fsExtra2 = _interopRequireDefault(_fsExtra);var _path = require('path');var _path2 = _interopRequireDefault(_path);var _winston = require('./winston');function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}const log = (0, _winston.getActiveLogger)(); /** * Creates the given directory * @param directoryPath the path to the directory to create */function createDirectory(directoryPath) {log.verbose(`creating directory: ${directoryPath}`);try {_fsExtra2.default.mkdirsSync(directoryPath);} catch (error) {log.debug(error.message);throw new Error(`Failed to create directory ${directoryPath}`);}} /** * Reads the file at the path given. * @param filePath * @returns {*} */function readFile(filePath) {log.verbose(`reading file: ${filePath}`);try {return _fsExtra2.default.readFileSync(filePath, 'utf-8');} catch (error) {log.debug(error.message);throw new Error(`Failed to read file ${filePath}!`);}} /** * Load template file. IF an error is thrown, it will be caught, logged, then thrown again. * The given errMsg should be a constant. * * @param filePath * @returns {*} */function loadTemplate(filePath) {log.verbose(`loading template: ${_path2.default.join(__dirname, '/', filePath)}`);try {return _fsExtra2.default.readFileSync(_path2.default.join(__dirname, '/', filePath), 'utf-8');} catch (error) {log.debug(error.message);throw new Error(`Failed to read template ${filePath}!`);}} /** * Write to the given filePath with the contents of a file. The given errMsg should be a constant. * * @param filePath * @param fileContents * @returns {*} */function writeFile(filePath, fileContents) {log.verbose(`writing file: ${filePath}`);try {return _fsExtra2.default.writeFileSync(filePath, fileContents);} catch (error) {log.debug(error.message);throw new Error(`Failed to write ${filePath}!`);}} /** * Deletes a file at the given path * @param filePath the path to the file to be deleted */function deleteFile(filePath) {log.verbose(`deleting file: ${filePath}`);try {_fsExtra2.default.unlinkSync(filePath);} catch (error) {log.debug(error.message);throw new Error(`Failed to delete ${filePath}!`);}} /** * Checks if teh given file exists * @param filePath path to the file */function exists(filePath) {log.verbose(`checking if ${filePath} exists`);try {return _fsExtra2.default.existsSync(filePath);} catch (error) {log.debug(error.message);throw new Error(`Failed to check if ${filePath} exists!`);}}