imq-cli
Version:
Command Line Interface for IMQ
86 lines • 3.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
/*!
* IMQ-CLI library: license
*
* 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");
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
const LICENSES = require('./licenses.json');
// noinspection RegExpRedundantEscape
const RX_ESCAPE = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
/**
* Finds and returns license object by a given name pattern
*
* @param {string} name
* @return {any}
*/
function findLicense(name) {
for (let id of Object.keys(LICENSES)) {
if (name === id ||
name.toLowerCase() === id ||
new RegExp(`^${name.toLowerCase()}`, 'i')
.test(LICENSES[id].spdx_id)) {
return LICENSES[id];
}
}
for (let id of Object.keys(LICENSES)) {
if (new RegExp(`^${name.toLowerCase()}`, 'i')
.test(LICENSES[id].name)) {
return LICENSES[id];
}
}
return null;
}
exports.findLicense = findLicense;
// istanbul ignore next
/**
* Queries user for license selection
*
* @return {Promise<{id: string; name: string}>}
*/
async function licensingOptions() {
let answer = await inquirer.prompt([{
type: 'confirm',
name: 'addLicense',
message: 'Would you like to use specific license for your services?',
default: true
}]);
let licenseName = 'UNLICENSED';
if (!answer.addLicense) {
return { id: licenseName, name: licenseName };
}
const licenses = Object.keys(LICENSES)
.map((id) => LICENSES[id]);
answer = await inquirer.prompt([{
type: 'autocomplete',
name: 'licenseName',
message: 'Select license:',
source: async (answers, input) => {
return licenses.filter((license) => {
let rx = new RegExp(`^${(input || '').replace(RX_ESCAPE, "\\$&")}`, 'i');
return license.key.match(rx) || license.name.match(rx);
}).map((license) => license && license.name || '');
}
}]);
const license = licenses.find((license) => license.name === answer.licenseName);
if (license) {
licenseName = license.name;
}
return { id: license.spdx_id, name: licenseName };
}
exports.licensingOptions = licensingOptions;
//# sourceMappingURL=license.js.map
;