All files / tools format.ts

41.66% Statements 5/12
57.14% Branches 4/7
33.33% Functions 1/3
45.45% Lines 5/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                1x       1x                       12x   12x                           1x      
/**
 * Formats the framework's own CSS, JSON, and YAML with Prettier, the way paf format does a unit's.
 *
 * It takes the file rule, the settings, and the run itself from the code-style plugin, so the framework
 * and a unit that turns Prettier on are formatted alike. On top of what the plugin leaves alone, this
 * leaves a spec's fixtures, which are read byte for byte, build output, which tools write, and the three
 * docs folders the docs package formats with a Prettier of its own, so no file is formatted by two runs.
 *
 * npm run format rewrites the files, and npm run format:check says which would change and writes none.
 */
import path from 'node:path';
import { formatTree, formats } from '../src/plugins/code-style/format.ts';
import { ToolError, reportFailure } from '../src/tools/shared/tool-error.ts';
import { isMainScript } from '../src/tools/shared/entry.ts';
 
/** The framework repo's root, one folder up from here. */
const ROOT = path.resolve(import.meta.dirname, '..');
 
// the docs site, the TypeDoc theme, and the Doxygen theme. the docs package formats its own files in
// these with npm --prefix docs run format, and the rest is built output or copied in unchanged
const DOCS_OWN = ['docs/site', 'docs/typedoc', 'docs/doxygen'];
 
/**
 * Whether the format run leaves a folder unwalked, by its path from the repo root.
 *
 * @param rel The folder's path from the repo root, with forward slashes.
 * @return True for a folder of fixtures or of build output, at any depth, and for a folder the docs
 *   package formats.
 */
export function leavesHere(rel: string): boolean {
  // build/ is gitignored output, such as the page check's sandboxes, whose tsconfig the build writes
  // with JSON.stringify rather than Prettier
  const parts = rel.split('/');
 
  return parts.includes('fixtures') || parts.includes('build') || DOCS_OWN.includes(rel);
}
 
async function main(): Promise<void> {
  const args = process.argv.slice(2);
  const unknown = args.find((arg) => arg !== '--check');
 
  if (unknown !== undefined) {
    throw new ToolError(`unknown argument: ${unknown}. It takes --check`);
  }
 
  await formatTree(ROOT, { takes: formats, leaves: leavesHere }, args.includes('--check'), 'npm run format');
}
 
Iif (isMainScript(import.meta)) {
  main().catch(reportFailure);
}