UNPKG

eslint-config-vuetify

Version:
130 lines (123 loc) 4.85 kB
'use strict'; const promises = require('node:fs/promises'); const node_path = require('node:path'); const prompts = require('@clack/prompts'); const kolorist = require('kolorist'); const nypm = require('nypm'); const commands = require('package-manager-detector/commands'); const tinyexec = require('tinyexec'); const index = require('./shared/eslint-config-vuetify.DvbgKdIP.cjs'); require('node:fs'); require('exsolve'); require('local-pkg'); require('package-manager-detector'); require('@eslint/eslintrc'); const ESLINT = "eslint"; const ESLINT_CONFIG = "eslint-config-vuetify"; const VUETIFY = "vuetify"; const LINKS = { [ESLINT]: kolorist.link(ESLINT, "https://npmjs.org/package/eslint"), [ESLINT_CONFIG]: kolorist.link(ESLINT_CONFIG, "https://npmjs.org/package/eslint-config-vuetify"), [VUETIFY]: kolorist.link("Vuetify", "https://vuetifyjs.com/") }; const configData = `import vuetify from 'eslint-config-vuetify' export default vuetify() `; const blue = kolorist.ansi256(33); const description = `Welcome to ${blue(ESLINT_CONFIG)} \u2014 an opinionated ESLint config by the ${blue(LINKS[VUETIFY])} team.`; const eslintVersion = index.getPackageVersion(ESLINT) ?? "0.0.0"; const configVersion = index.getPackageVersion(ESLINT_CONFIG) ?? "0.0.0"; const hasEslint = index.hasPackage(ESLINT); const hasEslintConfig = index.hasPackage(ESLINT_CONFIG); const isEslintVersionValid = index.isVersionAtLeast(eslintVersion, "9.5.0"); const isConfigVersionValid = index.isVersionAtLeast(configVersion, "4.0.0"); const packagesToInstall = [ ...hasEslint ? [] : [ESLINT], ...hasEslintConfig ? [] : [ESLINT_CONFIG] ]; const packagesToUpgrade = [ ...isEslintVersionValid ? [] : [ESLINT], ...isConfigVersionValid ? [] : [ESLINT_CONFIG] ]; function getActionMessage() { const actions = []; if (packagesToInstall.length > 0) { actions.push(`install ${packagesToInstall.map((pkg) => LINKS[pkg]).join(", ")}`); } if (packagesToUpgrade.length > 0) { const upgradeAction = `upgrade ${packagesToUpgrade.map((pkg) => LINKS[pkg]).join(", ")}`; if (packagesToInstall.length > 0) { actions.push(`and ${upgradeAction}`); } else { actions.push(upgradeAction); } } if (hasEslint || hasEslintConfig) { actions.push("(Currently:"); if (hasEslint) { actions.push(` ${ESLINT}: ${eslintVersion}`); } if (hasEslintConfig) { actions.push(` ${ESLINT_CONFIG}: ${configVersion}`); } actions.push(")"); } return `We need to ${actions.join(" ")}.`; } async function main() { prompts.intro(description); if (packagesToInstall.length > 0 || packagesToUpgrade.length > 0) { prompts.log.info(getActionMessage()); const shouldInstall = await prompts.confirm({ message: "Do you want to proceed?" }); if (shouldInstall === true) { const s = prompts.spinner(); s.start("Installing dependencies..."); if (packagesToInstall.length > 0) { await nypm.addDevDependency(packagesToInstall); } if (packagesToUpgrade.length > 0) { const packageManager = await index.getPackageManager(); const upgradeCommand = commands.resolveCommand(packageManager.agent, "upgrade", packagesToUpgrade); await tinyexec.x(upgradeCommand.command, upgradeCommand.args); } s.stop("Dependencies installed!"); } } else { prompts.log.info("All required dependencies are already installed."); } let overwriteConfig = false; overwriteConfig = await (index.configUrl ? prompts.confirm({ message: `Found ${kolorist.underline(node_path.relative(process.cwd(), index.configUrl))}. Do you want to overwrite it?` }) : prompts.confirm({ message: "No ESLint config found. Do you want to create one?" })); if (overwriteConfig === true) { const s = prompts.spinner(); s.start("Setting up ESLint config..."); await promises.writeFile(index.configUrl ?? "eslint.config.mjs", configData); s.stop("ESLint config setup complete!"); } if (index.hasFile("package.json")) { const packageJson = JSON.parse(await promises.readFile("package.json", "utf8")); if (!packageJson.scripts) { packageJson.scripts = {}; } const hasLintAndFixScripts = packageJson.scripts.lint && packageJson.scripts["lint:fix"]; const shouldAddScripts = hasLintAndFixScripts ? false : await prompts.confirm({ message: "Do you want to add lint scripts to package.json?" }); if (shouldAddScripts === true) { if (!packageJson.scripts.lint) { packageJson.scripts.lint = "eslint"; } if (!packageJson.scripts["lint:fix"]) { packageJson.scripts["lint:fix"] = "eslint --fix"; } await promises.writeFile("package.json", JSON.stringify(packageJson, null, 2)); } } prompts.outro("All done! Happy hacking!"); } exports.main = main;