@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
98 lines (88 loc) • 3.16 kB
JavaScript
const os = require("os");
const cp = require("child_process");
const term = require("../util/term");
const IS_WIN = os.platform() === "win32";
const REGEX_FLAGS = /--[a-z,a-z-]+[,\s+]\s+/gm;
const REGEX_OPTIONS = /--[a-z,a-z-]+\s[^\s]/gm;
const REGEX_SHORTCUT_MAPPINGS = /\s-[a-zA-Z]+, --[a-zA-Z-]+/gm;
module.exports = {
/**
* Generates `cds lint` help string to print when calling `cds lint [-h, --help]`
* by getting available options/shortcuts from installed version of ESLint
* @returns `cds lint` help string
*/
genEslintHelp: function (eslintCmd) {
let eslintHelp;
try {
eslintHelp = cp
.execSync(`${eslintCmd} -h`, {
cwd: process.cwd(),
shell: IS_WIN,
stdio: "pipe",
})
.toString();
} catch (err) {
console.log(
`${term.error("Cannot call 'eslint -h', install and try again.")}\n`
);
}
let eslintOpts = eslintHelp.split("\n");
let cdslintCmd = eslintOpts[0].replace("eslint", "*cds lint*");
eslintOpts[0] = `
# SYNOPSIS
${cdslintCmd}
Runs environment checks and/or checks the specified models
based on the ESLint framework
# OPTIONS`;
return `${eslintOpts.join("\n ")}\n`;
},
/**
* Generates `cds lint` options array by extracting
* all [--options] and [-shortcuts] strings from help string
* Shortcut order must match order of [options, flags]
* Any options without an shortcut must act as filler (undefined)
* until the shortcuts for the flags start
* @param help help string and shortcuts array for `cds lint`
* @param flags help string and shortcuts array for `cds lint`
* @returns `cds lint` allowed options
*/
genEslintShortcutsAndOpts: function (help, flags) {
const allOptions = help
.match(REGEX_OPTIONS)
.map((str) => str.split(' ')[0].trim().replace(",", ""));
const shortcutMappings = help
.match(REGEX_SHORTCUT_MAPPINGS)
.map((str) => str.trim().split(/,\s?/));
let optionsWithShortcuts = [];
let shortcuts = [];
let shortcutsForFlags = [];
shortcutMappings.forEach((m) => {
if (!flags.includes(m[1])) {
optionsWithShortcuts.push(m[1]);
shortcuts.push(m[0]);
} else {
const index = flags.indexOf(m[1]);
flags.splice(index, 1);
flags.unshift(m[1]);
shortcutsForFlags.unshift(m[0]);
}
});
const optionsWithoutShortcuts = allOptions.filter((o) => !optionsWithShortcuts.includes(o));
optionsWithoutShortcuts.forEach(() => {
shortcuts.push(undefined)
});
let options = optionsWithShortcuts.concat(optionsWithoutShortcuts);
shortcuts = shortcuts.concat(shortcutsForFlags);
return [help, options, shortcuts, flags];
},
/**
* Generates a list of ESLint flags by extracting
* all [--options] strings from help string which
* do not require an extra argument
* @param help string from `cds lint [-h, --help]`
* @returns ESLint flags
*/
genEslintFlags: function (help) {
return help.match(REGEX_FLAGS).map((str) => str.trim().replace(",", ""));
}
};