Skip to content

Commit ecb97f3

Browse files
Enhance content type schema reading and add Mocha root hooks. Updated readContentTypeSchemas to return an empty array for missing directories and added error handling. Introduced mocha-root-hooks.js for preloading Chalk in tests.
1 parent 1c5ae06 commit ecb97f3

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

packages/contentstack-utilities/.mocharc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"require": [
33
"test/helpers/init.js",
44
"ts-node/register",
5-
"source-map-support/register"
5+
"source-map-support/register",
6+
"test/helpers/mocha-root-hooks.js"
67
],
78
"watch-extensions": [
89
"ts"

packages/contentstack-utilities/src/content-type-utils.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
12
import { resolve as pResolve } from 'node:path';
2-
import { FsUtility } from './fs-utility';
33

44
/**
55
* Reads all content type schema files from a directory
66
* @param dirPath - Path to content types directory
77
* @param ignoredFiles - Files to ignore (defaults to schema.json, .DS_Store, __master.json, __priority.json)
8-
* @returns Array of content type schemas
8+
* @returns Array of content type schemas (empty if the path is missing or has no eligible files)
99
*/
1010
export function readContentTypeSchemas(
1111
dirPath: string,
1212
ignoredFiles: string[] = ['schema.json', '.DS_Store', '__master.json', '__priority.json', 'field_rules_uid.json'],
13-
): Record<string, unknown>[] | null {
14-
const fsUtil = new FsUtility();
15-
const files = fsUtil.readdir(dirPath);
13+
): Record<string, unknown>[] {
14+
if (!existsSync(dirPath)) {
15+
return [];
16+
}
17+
18+
const files = readdirSync(dirPath);
1619

1720
if (!files || files.length === 0) {
18-
return null;
21+
return [];
1922
}
2023

2124
const contentTypes: Record<string, unknown>[] = [];
@@ -33,7 +36,8 @@ export function readContentTypeSchemas(
3336

3437
try {
3538
const filePath = pResolve(dirPath, file);
36-
const contentType = fsUtil.readFile(filePath);
39+
const raw = readFileSync(filePath, 'utf8');
40+
const contentType = JSON.parse(raw) as Record<string, unknown>;
3741
if (contentType) {
3842
contentTypes.push(contentType as Record<string, unknown>);
3943
}

packages/contentstack-utilities/src/progress-summary/cli-progress-manager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export default class CLIProgressManager {
103103
return;
104104
}
105105

106+
if (configHandler.get('log')?.showConsoleLogs) {
107+
return;
108+
}
109+
106110
// Apply strategy-based corrections before printing
107111
CLIProgressManager.applyStrategyCorrections();
108112

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Chalk 5 is ESM-only and loaded asynchronously; production code calls loadChalk() at CLI init.
3+
* Tests must preload chalk before any getChalk() usage.
4+
*/
5+
const { loadChalk } = require('../../src/chalk');
6+
7+
exports.mochaHooks = {
8+
beforeAll() {
9+
this.timeout(30_000);
10+
return loadChalk();
11+
},
12+
};

0 commit comments

Comments
 (0)