1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-09-11 13:15:28 +03:00
n8n/scripts/format.mjs
Iván Ovejero 7ce5d8fd90
refactor: Upgrade to Prettier 3 (no-changelog) (#6947)
Supersedes https://github.com/n8n-io/n8n/pull/6937

Excluding fixtures and test workflow JSONs to avoid having to update
tests.
2023-08-16 17:13:57 +02:00

47 lines
1.3 KiB
JavaScript

#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
const prettier = path.resolve('node_modules', '.bin', 'prettier');
if (!fs.existsSync(prettier)) {
throw new Error(
[`Prettier not found at path: ${prettier}`, 'Please run `pnpm 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', '.ts'];
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(' '));