Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(cli): doctor --built's file-limit case reads a 50-file limit (#9897)

Christopher Chedeaub31575d73ff5
diff --git a/.changeset/cli-built-read-file-limit-option.md b/.changeset/cli-built-read-file-limit-option.md
new file mode 100644
index 0000000000..1842ffd211
--- /dev/null
+++ b/.changeset/cli-built-read-file-limit-option.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[chore] Internal: the file read behind `astryx doctor --built` takes its file limit as an option, 20,000 unless a caller passes one, and the checks' messages name the limit the read used. The command's checks and output are unchanged. Not exported from the package entry point.
+
+@vjeux
diff --git a/packages/cli/api/doctor/_adapter.mjs b/packages/cli/api/doctor/_adapter.mjs
index 4fe6bc11ea..a676a4a1b9 100644
--- a/packages/cli/api/doctor/_adapter.mjs
+++ b/packages/cli/api/doctor/_adapter.mjs
@@ -468,11 +468,13 @@ export function resolveBuiltPath(cwd, target) {
* path relative to the folder (a file's own name when the path is one file),
* whether it sits in build output, and a reader that returns its text, or
* null when it cannot be read or is over 5 MB. Installs and version control
- * are skipped; at most BUILT_FILE_LIMIT files are listed.
+ * are skipped; at most `fileLimit` files are listed.
* @param {string} target an existing absolute path
+ * @param {{fileLimit?: number}} [options] `fileLimit`: BUILT_FILE_LIMIT
+ * unless the caller reads fewer
* @returns {{files: {rel: string, built: boolean, read: () => string | null}[], truncated: boolean, missing?: number}}
*/
-export function readBuiltPath(target) {
+export function readBuiltPath(target, {fileLimit = BUILT_FILE_LIMIT} = {}) {
/** @param {string} file */
const reader = file => () => {
try {
@@ -568,7 +570,7 @@ export function readBuiltPath(target) {
built: built || BUILD_OUTPUT_DIRS.has(entry.name),
});
} else if (entry.isFile()) {
- if (files.length >= BUILT_FILE_LIMIT) return {files, truncated: true};
+ if (files.length >= fileLimit) return {files, truncated: true};
files.push({
rel: path.relative(target, full).split(path.sep).join('/'),
built,
diff --git a/packages/cli/api/doctor/built-checks.mjs b/packages/cli/api/doctor/built-checks.mjs
index 6b1a5007ad..a94b8c4289 100644
--- a/packages/cli/api/doctor/built-checks.mjs
+++ b/packages/cli/api/doctor/built-checks.mjs
@@ -560,10 +560,11 @@ function jsxFindings(text, rel, iconNames) {
* @param {string} target an existing file or folder
* @param {readonly string[]} packages package names whose components count as
* Astryx, beside every `@astryxdesign/*` package
+ * @param {{fileLimit?: number}} [options] how many files to read at most
* @returns {BuiltScan}
*/
-export function scanBuilt(target, packages) {
- const {files, truncated, missing = 0} = readBuiltPath(target);
+export function scanBuilt(target, packages, {fileLimit} = {}) {
+ const {files, truncated, missing = 0} = readBuiltPath(target, {fileLimit});
/** @type {BuiltScan} */
const scan = {
sourceFiles: 0,
@@ -705,11 +706,15 @@ function plural(count, word) {
/**
* The two checks for a path.
* @param {string} target as the caller gave it
- * @param {{cwd: string, packages: readonly string[]}} options `packages`: the
- * integration packages whose components count as Astryx
+ * @param {{cwd: string, packages: readonly string[], fileLimit?: number}} options
+ * `packages`: the integration packages whose components count as Astryx.
+ * `fileLimit`: how many files to read at most (BUILT_FILE_LIMIT).
* @returns {import('./doctor.type.mjs').DoctorCheck[]}
*/
-export function checkBuilt(target, {cwd, packages}) {
+export function checkBuilt(
+ target,
+ {cwd, packages, fileLimit = BUILT_FILE_LIMIT},
+) {
const resolved = resolveBuiltPath(cwd, target);
if (resolved == null) {
return [
@@ -722,10 +727,10 @@ export function checkBuilt(target, {cwd, packages}) {
},
];
}
- const scan = scanBuilt(resolved, packages);
+ const scan = scanBuilt(resolved, packages, {fileLimit});
const used =
scan.imports > 0 || scan.componentUses > 0 || scan.stylesheets.length > 0;
- const read = `${plural(scan.sourceFiles, 'source file')} and ${plural(scan.builtFiles, 'built file')}${scan.truncated ? ` (the first ${BUILT_FILE_LIMIT} files)` : ''}`;
+ const read = `${plural(scan.sourceFiles, 'source file')} and ${plural(scan.builtFiles, 'built file')}${scan.truncated ? ` (the first ${fileLimit} files)` : ''}`;
const evidence = [
scan.imports > 0
? `${plural(scan.imports, 'import')} from ${scan.packages.join(', ')} in ${plural(scan.importFiles, 'file')}`
@@ -761,7 +766,7 @@ export function checkBuilt(target, {cwd, packages}) {
id: 'built-astryx-usage',
label: 'Built output uses Astryx',
status: 'warn',
- message: `Read ${read} and found no Astryx in them, but ${[scan.truncated ? `the rest past the first ${BUILT_FILE_LIMIT} files` : null, scan.unread > 0 ? `${plural(scan.unread, 'source file')} over ${BUILT_FILE_MAX_MB} MB, unreadable or unparsed` : null, scan.missingLinks > 0 ? `${plural(scan.missingLinks, 'file')} the page links but that are not there` : null].filter(Boolean).join(' and ')} went unread, so this does not show whether the output uses Astryx.`,
+ message: `Read ${read} and found no Astryx in them, but ${[scan.truncated ? `the rest past the first ${fileLimit} files` : null, scan.unread > 0 ? `${plural(scan.unread, 'source file')} over ${BUILT_FILE_MAX_MB} MB, unreadable or unparsed` : null, scan.missingLinks > 0 ? `${plural(scan.missingLinks, 'file')} the page links but that are not there` : null].filter(Boolean).join(' and ')} went unread, so this does not show whether the output uses Astryx.`,
fix: `Run \`${formatCliCommand('doctor --built <folder>', cwd)}\` on the folder that holds the page itself, or on its build output.`,
}
: {
@@ -798,7 +803,7 @@ export function checkBuilt(target, {cwd, packages}) {
id: 'built-astryx-purity',
label: 'Source uses Astryx well',
status: 'info',
- message: `Not fully inspected: ${plural(scan.jsxFiles, 'source file')} with JSX had no finding${scan.truncated ? `, but only the first ${BUILT_FILE_LIMIT} files were read` : ''}.${unreadNote}`,
+ message: `Not fully inspected: ${plural(scan.jsxFiles, 'source file')} with JSX had no finding${scan.truncated ? `, but only the first ${fileLimit} files were read` : ''}.${unreadNote}`,
fix: `Run \`${formatCliCommand('doctor --built <folder>', cwd)}\` on the folder that holds the page's own source.`,
};
} else if (scan.findings.length === 0) {
@@ -829,7 +834,7 @@ export function checkBuilt(target, {cwd, packages}) {
id: 'built-astryx-purity',
label: 'Source uses Astryx well',
status: 'warn',
- message: `${plural(scan.findings.length, 'finding')} (${counts}): ${shown}${more}.${unreadNote}${scan.truncated ? ` Only the first ${BUILT_FILE_LIMIT} files were read.` : ''}`,
+ message: `${plural(scan.findings.length, 'finding')} (${counts}): ${shown}${more}.${unreadNote}${scan.truncated ? ` Only the first ${fileLimit} files were read.` : ''}`,
fix: `Replace each with the Astryx component, icon surface, prop or token it names; \`${formatCliCommand('docs is-my-code-good', cwd)}\` has the full rubric. Keep a raw element only where its native meaning is needed.`,
};
}
diff --git a/packages/cli/api/doctor/built-checks.test.mjs b/packages/cli/api/doctor/built-checks.test.mjs
index 7f87144ae0..061c37ad19 100644
--- a/packages/cli/api/doctor/built-checks.test.mjs
+++ b/packages/cli/api/doctor/built-checks.test.mjs
@@ -24,9 +24,13 @@ function write(files) {
}
}
-/** @param {string} target @param {string[]} [packages] */
-function check(target, packages = []) {
- const [usage, purity] = checkBuilt(target, {cwd: dir, packages});
+/**
+ * @param {string} target
+ * @param {string[]} [packages]
+ * @param {{fileLimit?: number}} [options]
+ */
+function check(target, packages = [], options = {}) {
+ const [usage, purity] = checkBuilt(target, {cwd: dir, packages, ...options});
return {usage, purity};
}
@@ -290,20 +294,15 @@ describe('--built edge cases', () => {
});
it('warns, not fails, when the file limit ends the read before any Astryx', () => {
- // One more file than the read takes, spread over 50 folders. Each folder
- // is made once, and each file is a link to one empty file: a file is
- // one directory entry, not a recursive mkdir and a write each.
- const empty = path.join(dir, 'empty.txt');
- fs.writeFileSync(empty, '');
- for (let folder = 0; folder < 50; folder++) {
- fs.mkdirSync(path.join(dir, 'a', String(folder)), {recursive: true});
+ // One more file than a read of 50 takes, spread over 5 folders. The
+ // default limit reads the same way, so a small one keeps the folder small.
+ for (let i = 0; i < 51; i++) {
+ write({[`a/${i % 5}/${i}.txt`]: ''});
}
- for (let i = 0; i < 20_000; i++) {
- fs.linkSync(empty, path.join(dir, 'a', String(i % 50), `${i}.txt`));
- }
- const {usage} = check('.');
+ const {usage} = check('.', [], {fileLimit: 50});
expect(usage.status).toBe('warn');
expect(usage.message).toContain('went unread');
+ expect(usage.message).toContain('the first 50 files');
});
it('finds a root-relative asset from the build root for a page in a subfolder', () => {