{"version":3,"file":"reorder-json.cjs","sources":["../../src/sort/sort-json-file.function.ts","../../src/bin/reorder-json.ts"],"sourcesContent":["import { sortObject, type ObjectKeyOrder } from '@alexaegis/common';\nimport { readJson, writeJson } from '@alexaegis/fs';\nimport type { Logger } from '@alexaegis/logging';\nimport { normalizeSortingPreferenceForPackageJson } from '@alexaegis/workspace-tools';\nimport { basename } from 'node:path';\n\n/**\n * Reads a json file, sorts it based on a sorting config then writes it back,\n * if available also formats it with prettier. If the file is named\n * `package.json` it may override some rules to always produce a functional\n * result.\n *\n * @param path\n *\n * @return was sorted or not\n */\nexport const sortJsonFile = async (\n\tpath: string,\n\toptions: SortJsonFileOptions\n): Promise<boolean> => {\n\tconst content = await readJson<Record<string, unknown>>(path);\n\tif (content) {\n\t\tlet sortPreferences = options.sortingPreference;\n\n\t\tif (basename(path) === 'package.json') {\n\t\t\tsortPreferences = normalizeSortingPreferenceForPackageJson(sortPreferences);\n\t\t}\n\n\t\tconst sortedContent = sortObject(content, sortPreferences);\n\n\t\tconst alreadySorted = JSON.stringify(content) === JSON.stringify(sortedContent);\n\n\t\tif (!options.dry && !options.check) {\n\t\t\tawait writeJson(sortedContent, path, options);\n\t\t} else if (options.dry && !options.check) {\n\t\t\toptions.logger?.info(`sorting ${path}`);\n\t\t} else if (options.check && !alreadySorted) {\n\t\t\toptions.logger?.info(`not sorted: ${path}`);\n\t\t}\n\n\t\treturn alreadySorted;\n\t} else {\n\t\treturn false;\n\t}\n};\n\nexport interface SortJsonFileOptions {\n\tsortingPreference: ObjectKeyOrder;\n\t/**\n\t * @defaultValue true\n\t */\n\tautoPrettier?: boolean;\n\t/**\n\t * @defaultValue false\n\t */\n\tdry?: boolean;\n\t/**\n\t * If using check mode, it won't write, but won't log either\n\t * @defaultValue false\n\t */\n\tcheck?: boolean;\n\n\tlogger?: Logger<unknown>;\n}\n","import { createLogger } from '@alexaegis/logging';\nimport { DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE } from '@alexaegis/workspace-tools';\nimport { basename } from 'node:path';\nimport yargs, { type Argv } from 'yargs';\nimport packageJson from '../../package.json';\nimport type { AutoReorderOptions } from '../helpers/auto-reorder.class.options.js';\n\nimport { sortJsonFile } from '../sort/sort-json-file.function.js';\n\nconst yarguments: Argv<AutoReorderOptions & { dry: boolean; check: boolean }> = yargs(\n\tprocess.argv.splice(2)\n)\n\t.version(packageJson.version)\n\t.epilogue(`${packageJson.name}@${packageJson.version} see project at ${packageJson.homepage}`)\n\t.help()\n\t.completion()\n\t.option('check', {\n\t\tboolean: true,\n\t\tdescription:\n\t\t\t'Checks if every packageJson in a workspace conforms to the ordering, fails if not',\n\t\tdefault: false,\n\t})\n\n\t.option('dry', {\n\t\tboolean: true,\n\t\tdescription: \"Don't actually modify anything, just print out what would be deleted\",\n\t\tdefault: false,\n\t});\n\nvoid (async () => {\n\tconst options = await yarguments.parseAsync();\n\tconst logger = createLogger({ name: 'reorder' });\n\n\tconst wasSorted = await Promise.all(\n\t\toptions._.map((positional) => {\n\t\t\tconst defaultSort =\n\t\t\t\tbasename(positional.toString()) === 'package.json'\n\t\t\t\t\t? DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE\n\t\t\t\t\t: [];\n\n\t\t\treturn sortJsonFile(positional.toString(), {\n\t\t\t\tsortingPreference: options.sortingPreference ?? defaultSort,\n\t\t\t\tcheck: options.check,\n\t\t\t\tdry: options.dry,\n\t\t\t\tlogger,\n\t\t\t});\n\t\t})\n\t);\n\n\tif (options.check && wasSorted.some((s) => !s)) {\n\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\tprocess.exit(1);\n\t}\n})();\n"],"names":["readJson","basename","normalizeSortingPreferenceForPackageJson","sortObject","writeJson","createLogger","DEFAULT_PACKAGE_JSON_SORTING_PREFERENCE"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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