-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(null): make yarn null:autoadd sort and collapse added cases by …
…directory (#3329) #### Description of changes This PR improves the readability of `yarn null:autoadd` output by making it automatically collapse cases where an entire directory is detected as "completed" into a directory-wise include pattern, rather than listing out individual files. #### Pull request checklist <!-- If a checklist item is not applicable to this change, write "n/a" in the checkbox --> - [x] Addresses an existing issue: part of #2869 - [x] Ran `yarn fastpass` - [n/a] Added/updated relevant unit test(s) (and ran `yarn test`) - [n/a] Verified code coverage for the changes made. Check coverage report at: `<rootDir>/test-results/unit/coverage` - [x] PR title *AND* final merge commit title both start with a semantic tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See `CONTRIBUTING.md`. - [n/a] (UI changes only) Added screenshots/GIFs to description above - [n/a] (UI changes only) Verified usability with NVDA/JAWS
- Loading branch information
Showing
4 changed files
with
133 additions
and
41 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
85 changes: 85 additions & 0 deletions
85
tools/strict-null-checks/collapse-completed-directories.js
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,85 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
// @ts-check | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const config = require('./config'); | ||
const { writeTsconfigSync } = require('./write-tsconfig'); | ||
|
||
const repoRoot = config.repoRoot; | ||
|
||
function collapseCompletedDirectories(tsconfigPath) { | ||
const tsconfigContent = JSON.parse(fs.readFileSync(tsconfigPath).toString()); | ||
|
||
const listedFiles = Array.from(new Set(tsconfigContent.files.sort())); | ||
const listedIncludes = Array.from(new Set(tsconfigContent.include.sort())); | ||
|
||
const listedDirectories = listedIncludes.map(includeToDirectory); | ||
|
||
const completedSet = new Set([...listedFiles, ...listedDirectories]); | ||
reduceCompletedSet(completedSet, './src'); | ||
|
||
const completedPaths = Array.from(completedSet).sort(); | ||
tsconfigContent.files = completedPaths.filter(isTsFile); | ||
tsconfigContent.include = completedPaths.filter(isSourceDirectory).map(directoryToInclude); | ||
|
||
writeTsconfigSync(tsconfigPath, tsconfigContent); | ||
} | ||
|
||
// convert from src/common/styles/**/* to ./src/common/styles | ||
function includeToDirectory(include) { | ||
return './' + include.replace('/**/*', ''); | ||
} | ||
|
||
// convert from ./src/common/styles to src/common/styles/**/* | ||
function directoryToInclude(directory) { | ||
return directory.substring(2) + '/**/*'; | ||
} | ||
|
||
function reduceCompletedSet(completedSet, root) { | ||
if (completedSet.has(root)) { | ||
return true; | ||
} | ||
if (!isSourceDirectory(root)) { | ||
return false; | ||
} | ||
|
||
const children = listRelevantChildren(root); | ||
let allChildrenReduced = true; | ||
for (const child of children) { | ||
const childReduced = reduceCompletedSet(completedSet, child); | ||
allChildrenReduced = allChildrenReduced && childReduced; | ||
} | ||
|
||
if (allChildrenReduced) { | ||
for (const child of children) { | ||
completedSet.delete(child); | ||
} | ||
completedSet.add(root); | ||
} | ||
return allChildrenReduced; | ||
} | ||
|
||
function isSourceDirectory(relativePath) { | ||
// this assumes directories don't have .s in their names, which isn't robust generally | ||
// but happens to be true in our repo | ||
const isDirectory = -1 === relativePath.indexOf('.', 1); | ||
return isDirectory && !relativePath.includes('__snapshots__'); | ||
} | ||
|
||
const isTsFileRegex = /\.(ts|tsx)$/; | ||
function isTsFile(relativePath) { | ||
return isTsFileRegex.test(relativePath); | ||
} | ||
|
||
function listRelevantChildren(relativePath) { | ||
const rawReaddir = fs.readdirSync(path.join(repoRoot, relativePath)); | ||
const directories = rawReaddir.filter(isSourceDirectory); | ||
const tsFiles = rawReaddir.filter(isTsFile); | ||
return [...directories, ...tsFiles].map(name => relativePath + '/' + name); | ||
} | ||
|
||
module.exports = { | ||
collapseCompletedDirectories, | ||
}; |
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,12 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
const fs = require('fs'); | ||
|
||
module.exports = { | ||
writeTsconfigSync: (tsconfigPath, content) => { | ||
let serializedContent = JSON.stringify(content, null, ' '); | ||
serializedContent += '\n'; | ||
|
||
fs.writeFileSync(tsconfigPath, serializedContent); | ||
}, | ||
}; |
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