UNPKG

@soleil-se/stylelint-config

Version:

Stylelint config based on stylelint-config-standard-scss, stylelint-config-recess-order and @stylistic/stylelint-plugin.

106 lines (90 loc) 3.15 kB
import fs from 'node:fs'; import { execSync } from 'node:child_process'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const { name, version } = require('./package.json'); const dependencies = { [name]: `^${version}`, stylelint: '^17', }; const configFileContent = `export default { extends: '@soleil-se/stylelint-config', }; `; const dir = dirname(fileURLToPath(import.meta.url)); function writeJsonFileSync(file, content) { try { return fs.writeFileSync(file, JSON.stringify(content, null, 2)); } catch (error) { console.error(`Error writing to JSON file: ${file}`, error); return {}; } } function readJsonFileSync(file) { try { return JSON.parse(fs.readFileSync(file, 'utf-8')); } catch (error) { console.error(`Error reading JSON file: ${file}`); return {}; } } function notEligible() { const packageFile = readJsonFileSync('./package.json'); return !![ ...Object.keys(packageFile.dependencies || {}), ...Object.keys(packageFile.devDependencies || {}), ].find((dependency) => dependency === '@soleil/stylelint-config'); } async function printStartup() { const packageFile = readJsonFileSync(`${dir}/package.json`, 'utf-8'); console.log(`\nSetting up Stylelint dependencies and configuration for ${packageFile.name}@${packageFile.version}.`); } function removeOldConfigFiles() { ['.stylelintrc.js', '.stylelintrc.cjs', '.stylelintrc', '.stylelintrc.json'].forEach((file) => { if (fs.existsSync(file)) fs.unlinkSync(file); }); } function renameToCjsIfNeeded() { ['.eslintrc.js', '.prettierrc.js', 'svelte.config.js'].forEach((file) => { if (fs.existsSync(file)) { const content = fs.readFileSync(file, 'utf-8'); if (content.includes('module.exports')) { fs.renameSync(file, file.replace('.js', '.cjs')); } } }); } function installDependencies() { const packageManager = fs.existsSync('pnpm-lock.yaml') ? 'pnpm' : 'npm'; const dependencyVersions = Object .entries(dependencies) .map((dependency) => `${dependency[0]}@${dependency[1]}`) .join(' '); const command = `${packageManager} i ${dependencyVersions} --force --save-dev`; console.log(`\nInstalling dependecies...\n> ${command}`); execSync(command, { cwd: '.', env: process.env, stdio: 'inherit' }); } function setupFiles() { console.log('\nSetting up configuration files...'); removeOldConfigFiles(); renameToCjsIfNeeded(); fs.writeFileSync('./stylelint.config.js', configFileContent); } function setup() { if (notEligible()) { console.error('The Stylelint config needs to be migrated to the @soleil-se scope, run the npm migration script first with "npx @soleil-se/run migrate-npm".'); process.exit(1); } printStartup(); const packageFile = readJsonFileSync('./package.json'); if (packageFile.type !== 'module') { packageFile.type = 'module'; } writeJsonFileSync('./package.json', packageFile); installDependencies(); setupFiles(); console.log('\nStylelint setup complete!'); } setup();