imq-cli
Version:
Command Line Interface for IMQ
121 lines • 3.93 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
/*!
* IMQ-CLI library: template
*
* Copyright (c) 2018, Mykhailo Stadnyk <mikhus@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
const inquirer = require("inquirer");
const _1 = require(".");
const fs = require("fs");
const child_process_1 = require("child_process");
const commandExists = require('command-exists').sync;
const wordWrap = require('word-wrap');
/**
* Wraps words of given text to match given char width, using given indentation
*
* @param {string} text
* @param {number} width
* @param {string} indent
* @return {string}
*/
function wrap(text, width = 80, indent = '') {
return wordWrap(text, { width, indent });
}
exports.wrap = wrap;
/**
* Checks if a git command available. If no - throws an error
*
* @throws Error
*/
function checkGit() {
if (!commandExists('git')) {
throw new Error('Git required but is not installed!');
}
}
exports.checkGit = checkGit;
// due to problematic testing of user-interaction
// istanbul ignore next
/**
* Load IMQ templates from templates git repository
*
* @return {Promise<any>}
*/
async function loadTemplates() {
if (fs.existsSync(_1.TPL_HOME)) {
await updateTemplates();
}
else {
checkGit();
console.log('Loading IMQ templates, please, wait...');
child_process_1.execSync(`git clone ${_1.TPL_REPO} ${_1.TPL_HOME}`);
}
return fs.readdirSync(_1.TPL_HOME).reduce((res, next) => {
const path = _1.resolve(_1.TPL_HOME, next);
if (/^\./.test(next))
return res;
if (fs.statSync(path).isDirectory()) {
res[next] = path;
}
return res;
}, {});
}
exports.loadTemplates = loadTemplates;
// due to problematic testing of user-interaction
// istanbul ignore next
/**
* Updates local copy of templates repo from remote source
*
* @return {Promise<void>}
*/
async function updateTemplates() {
const cwd = process.cwd();
process.chdir(_1.TPL_HOME);
checkGit();
console.log('Updating IMQ templates, please, wait...');
child_process_1.execSync('git pull');
process.chdir(cwd);
}
exports.updateTemplates = updateTemplates;
// due to problematic testing of user-interaction
// istanbul ignore next
/**
* Loads custom template from a given git repository
*
* @param {string} url
* @return {Promise<string>}
*/
async function loadTemplate(url) {
const name = (url.split(/[\/]/).pop() || '').replace(/\.git$/, '');
const path = _1.resolve(_1.CUSTOM_TPL_HOME, name);
if (fs.existsSync(path)) {
let answer = await inquirer.prompt([{
type: 'confirm',
name: 'overwrite',
message: 'Seems such template was already loaded, would you like ' +
'to fetch it again and overwrite?',
default: false
}]);
if (!answer.overwrite) {
return path;
}
_1.rmdir(path);
}
console.log(`Loading template from repository ${url}, please, wait...`);
child_process_1.execSync(`git clone ${url} ${path}`);
return path;
}
exports.loadTemplate = loadTemplate;
//# sourceMappingURL=template.js.map
;