@imqueue/cli
Version:
Command Line Interface for IMQ
93 lines • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findLicense = findLicense;
exports.licensingOptions = licensingOptions;
/*!
* IMQ-CLI library: license
*
* I'm Queue Software Project
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* If you want to use this code in a closed source (commercial) project, you can
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
const inquirer = require("inquirer");
const inquirer_autocomplete_prompt_1 = require("inquirer-autocomplete-prompt");
inquirer.registerPrompt('autocomplete', inquirer_autocomplete_prompt_1.default);
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;
}
// 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 };
}
//# sourceMappingURL=license.js.map