UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

136 lines (130 loc) 4.05 kB
const fs = require("fs"); const os = require("os"); const path = require("path"); const term = require("../util/term"); const IS_WIN = os.platform() === "win32"; module.exports = { /** * Checks whether ESLint v>=8 is installed by trying to resolve * 'eslint' module from (1) <inputPath> or (2) global npm path * @param {*} inputPath where to resolve 'eslint' module from * @param {*} globalPath resolves 'eslint' module from cds-dk * @param {*} logger if logger is passed, log err messages * @returns undefined or ESLint cmd string */ resolveEslint(inputPath, globalPath, logger) { let cmd; let version; let errMessage; try { const eslintPath = require.resolve("eslint/package.json", { paths: [inputPath, globalPath], }); const packageJson = require(eslintPath); if (packageJson.version >= "8") { version = packageJson.version; } else { errMessage = `ESLint v>=8.0.0 required!\n\n` + ` Make sure to update and check for any breaking changes:\n` + ` ${term.warn( "https://eslint.org/docs/user-guide/migrating-to-8.0.0" )}\n`; } const bin = packageJson.bin.eslint; let script = path.resolve(eslintPath, "..", bin); if (IS_WIN) { script = `"${script}"`; } cmd = `node ${script}`; } catch (err) { errMessage = `ESLint executable 'eslint' not found!\n\n` + ` See instructions on how to install ESLint at:\n` + ` ${term.warn( "https://eslint.org/docs/user-guide/getting-started" )}\n`; } if (cmd && version) { return cmd; } if (logger) { logger.error(errMessage); } }, /** * Checks whether ESLint CDS plugin `@sap/eslint-plugin-cds` * is installed in inputPath * @param {*} inputPath CDS project root * @returns boolean whether plugin is installed in inputPath */ resolvePlugin(plugin, inputPath) { try { require.resolve(plugin, { paths: [inputPath] }); } catch (error) { return false; } return true; }, /** * Checks ESLint config file for proper setup * in context of a local ESLint plugin installation * (1) Rules (recommended, model or environment) must be loaded * (2) Collects rules to overwrite severities for * @param {*} configPath ESLint config file * @param {*} options options to check * @returns boolean whether option checks were successful */ hasEslintConfigContent(configContents = {}, option = "") { if (typeof configContents === "object") { switch (option) { case "extends": if ( !configContents.extends || (!configContents.extends.includes(`plugin:@sap/cds/recommended`) && !configContents.extends.includes(`plugin:@sap/cds/model`) && !configContents.extends.includes(`plugin:@sap/cds/environment`)) ) { return false; } break; case "rules": if (configContents.rules) { Object.keys(configContents.rules).forEach((rule) => { const plugin = require("@sap/eslint-plugin-cds"); const pluginRules = Object.keys(plugin.rules); if (pluginRules.includes(rule)) { return true; } }); } break; default: break; } } else { return false; } return true; }, /** * Checks whether any custom CDS rules are "on" * @param {*} rulesDir CDS custom rules directory * @returns boolean whether any CDS custom rules are "on" */ hasCustomCDSRules(rulesDir, configContents) { try { const rules = fs.readdirSync(rulesDir); if (rules.length > 0) { for (let rule in configContents.rules) { if (rules.includes(`${rule}.js`)) { return true; } } } } catch (err) { // Do nothing } return false; } };