-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(custom-checks): fix broken package.json sort verification
- Remove not working check-package-json-sort.ts script. - Add ci action to check for unsorted package.json files. Peter's changes: 1. Changed the commit message in a way that it won't show up in the release notes among the bug fixes (the section that is meant for production bug-fixes not tooling bug-fixes) 2. Fixed typos in the .ts scripts 3. Applied the prettier/eslint formatting, deleted unused variables 4. Added a convenience script to the root package.json that runs the package.json sorter 5. Fixed the sorter script so that it appends a newline character after the sorted JSON string when writing back to the file. This is needed because when you run `yarn install` or `npm install` they both like to close the package.json file's with an empty line last so we have to mimic that in order to avoid spamming all future PRs with these line changes. One problem that this might still have is that on different operating systems the newline character might be different and it also depends on the git configuration in effect (you can configure Windows' git to use Unix line endings for example). So it's not trivial how to fix it but we'll cross that bridge when we get to it. Closes: #2356 Co-authored-by: Peter Somogyvari <peter.somogyvari@accenture.com> Signed-off-by: Tomasz Awramski <tomasz.awramski@fujitsu.com Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
3 changed files
with
66 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from "fs-extra"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import { globby, Options as GlobbyOptions } from "globby"; | ||
import { sortPackageJson } from "sort-package-json"; | ||
import lernaCfg from "../lerna.json" assert { type: "json" }; | ||
|
||
export interface ICheckPackageJsonSort { | ||
readonly argv: string[]; | ||
readonly env: NodeJS.ProcessEnv; | ||
} | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const SCRIPT_DIR = __dirname; | ||
const PROJECT_DIR = path.join(SCRIPT_DIR, "../"); | ||
const globbyOpts: GlobbyOptions = { | ||
cwd: PROJECT_DIR, | ||
ignore: ["**/node_modules"], | ||
}; | ||
|
||
export async function sortPackages(): Promise<void> { | ||
const TAG = "[tools/sort-package-json.ts]"; | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
const SCRIPT_DIR = __dirname; | ||
const PROJECT_DIR = path.join(SCRIPT_DIR, "../../"); | ||
console.log(`${TAG} SCRIPT_DIR=${SCRIPT_DIR}`); | ||
console.log(`${TAG} PROJECT_DIR=${PROJECT_DIR}`); | ||
|
||
const includeGlobs = lernaCfg.packages.map((x) => x.concat("/package.json")); | ||
|
||
const pkgJsonPaths = await globby(includeGlobs, globbyOpts); | ||
for (const pkg of pkgJsonPaths) { | ||
const json = await fs.readJSON(pkg); | ||
const sortedJson = await sortPackageJson(json); | ||
|
||
const sortedJsonString = JSON.stringify(sortedJson, null, 2).concat("\n"); | ||
fs.writeFile(pkg, sortedJsonString); | ||
} | ||
} | ||
|
||
sortPackages(); |