mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 09:36:44 +00:00
refactor: Format root-level files (#3858)
* ✨ Create `format.mjs` script * 👕 Add lint exception * ⚡ Add root formatting to `format` command * 🎨 Fix glob in `.prettierignore` * 🚚 Improve naming * 🎨 Format root-level files * ⚡ Simplify check * ⚡ Add period to extension * ⚡ Locate config * ⚡ Add `ignore` arg * 🚚 Move `config` and `ignore` after check
This commit is contained in:
46
scripts/format.mjs
Normal file
46
scripts/format.mjs
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { execSync } from 'child_process';
|
||||
|
||||
const prettier = path.resolve('node_modules', 'prettier', 'bin-prettier.js');
|
||||
|
||||
if (!fs.existsSync(prettier)) {
|
||||
throw new Error(
|
||||
[`Prettier not found at path: ${prettier}`, 'Please run `npm i` first'].join('\n'),
|
||||
);
|
||||
}
|
||||
|
||||
const config = path.resolve('.prettierrc.js');
|
||||
const ignore = path.resolve('.prettierignore');
|
||||
|
||||
const ROOT_DIRS_TO_SKIP = ['.git', 'node_modules', 'packages'];
|
||||
const EXTENSIONS_TO_FORMAT = ['.md', '.yml', '.js', '.json'];
|
||||
|
||||
const isDir = (path) => fs.lstatSync(path).isDirectory();
|
||||
|
||||
const isTarget = (path) => EXTENSIONS_TO_FORMAT.some((ext) => path.endsWith(ext));
|
||||
|
||||
const walk = (dir, test, found = []) => {
|
||||
fs.readdirSync(dir).forEach((entry) => {
|
||||
const entryPath = path.resolve(dir, entry);
|
||||
if (isDir(entryPath)) walk(entryPath, test, found);
|
||||
if (test(entryPath)) found.push(entryPath);
|
||||
});
|
||||
|
||||
return found;
|
||||
};
|
||||
|
||||
const targets = fs
|
||||
.readdirSync('.')
|
||||
.reduce((acc, cur) => {
|
||||
if (ROOT_DIRS_TO_SKIP.includes(cur)) return acc;
|
||||
if (isDir(cur)) return [...acc, ...walk(cur, isTarget)];
|
||||
if (isTarget(cur)) return [...acc, cur];
|
||||
|
||||
return acc;
|
||||
}, [])
|
||||
.join(' ');
|
||||
|
||||
execSync([prettier, '--config', config, '--ignore-path', ignore, '--write', targets].join(' '));
|
||||
Reference in New Issue
Block a user