UNPKG

@sap/cds-dk

Version:

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

83 lines (72 loc) 2.66 kB
const fs = require('node:fs') const path = require('node:path') const io = require('../../../lint/io') const cds = require('../../../cds') const { merge } = require('../../merge') const { dim, bold } = require('../../../util/term') const VSCODE_FILE_ASSOCIATIONS = { csv: ['csv', 'csv (semicolon)', 'tsv', 'tab'] } /** * Searches for ESLint config file types (in order or precedence) * and returns corresponding directory (usually project's root dir) * https://eslint.org/docs/user-guide/configuring#configuration-file-formats * @param {string} currentDir start here and search until root dir * @returns {string} dir containing ESLint config file (empty if not exists) */ function getConfigPath(currentDir = '.') { let configFiles = [ 'eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs' ] let configDir = path.resolve(currentDir) while (configDir !== path.resolve(configDir, '..')) { for (const configFile of configFiles) { const configPath = path.join(configDir, configFile) if (fs.existsSync(configPath) && fs.statSync(configPath).isFile()) { return configPath } } configDir = path.join(configDir, '..') } return '' } module.exports = class LintTemplate extends require('../../plugin') { static help() { return 'configure cds lint' } constructor() { super() const customPath = path.join(cds.root, '.eslint') this.docsPath = path.join(customPath, 'docs') this.rulesPath = path.join(customPath, 'rules') this.testsPath = path.join(customPath, 'tests') } async run() { // Require local ESLint plugin installation and corresponding config await this.#addEslintPluginConfig() // Generate VS Code extension settings const ALLOWED_FILE_EXTENSIONS = ['*.cds', '*.csn', '*.csv'] const extensions = new Set(ALLOWED_FILE_EXTENSIONS.map((ext) => path.extname(ext).replace('.', ''))) for (const ext of extensions) { if (ext in VSCODE_FILE_ASSOCIATIONS) { for (const assoc of VSCODE_FILE_ASSOCIATIONS[ext]) { extensions.add(assoc) } } } await io.sanitizeVscodeSettings( path.join(cds.root, '.vscode/settings.json'), Array.from(extensions), this.customRuleExample ) await merge(path.join(__dirname, 'files', 'package.json')).into('package.json') console.log('\n', dim('Make sure to also run ' + bold('npm i') + dim(' to install the newly added dependencies.'))) } async #addEslintPluginConfig() { const configPath = getConfigPath(cds.root) ?? path.join(cds.root, 'eslint.config.mjs') await io.sanitizeEslintConfig(configPath) } }