Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fileignoreconfig:
- filename: pnpm-lock.yaml
checksum: 64ca5ee7000d70e76ba367e3cacc2158e5cd33e41c7844fb6af79837a9469bb6
checksum: 4bad5f9428f5bc7ed837c91567b28afc97acefa7086eb03d2ffd90b4a2820233
version: '1.0'
9 changes: 9 additions & 0 deletions packages/contentstack-utilities/src/content-type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { resolve as pResolve } from 'node:path';
* @param ignoredFiles - Files to ignore (defaults to schema.json, .DS_Store, __master.json, __priority.json)
* @returns Array of content type schemas (empty if the path is missing or has no eligible files)
*/
const DEFAULT_GF_IGNORED_FILES = ['schema.json', '.DS_Store', '__master.json', '__priority.json', 'field_rules_uid.json', 'globalfields.json'];

export function readGlobalFieldSchemas(
dirPath: string,
ignoredFiles: string[] = DEFAULT_GF_IGNORED_FILES,
): Record<string, unknown>[] {
return readContentTypeSchemas(dirPath, ignoredFiles);
}

export function readContentTypeSchemas(
dirPath: string,
ignoredFiles: string[] = ['schema.json', '.DS_Store', '__master.json', '__priority.json', 'field_rules_uid.json'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { readContentTypeSchemas } from '../../src/content-type-utils';
import { readContentTypeSchemas, readGlobalFieldSchemas } from '../../src/content-type-utils';

describe('readContentTypeSchemas', () => {
afterEach(() => {
Expand Down Expand Up @@ -144,4 +144,110 @@ describe('readContentTypeSchemas', () => {

sinon.restore();
});

it('should NOT ignore globalfields.json (that is readGlobalFieldSchemas responsibility)', () => {
const mockBulk = [{ uid: 'gf_1', title: 'GF 1' }];
const mockPerUid = { uid: 'gf_2', title: 'GF 2', schema: [] };

sinon.stub(require('fs'), 'existsSync').returns(true);
sinon.stub(require('fs'), 'readdirSync').returns(['globalfields.json', 'gf_2.json']);
const readFileStub = sinon.stub(require('fs'), 'readFileSync');
readFileStub.withArgs(sinon.match(/globalfields\.json/), 'utf8').returns(JSON.stringify(mockBulk));
readFileStub.withArgs(sinon.match(/gf_2\.json/), 'utf8').returns(JSON.stringify(mockPerUid));

const result = readContentTypeSchemas('/test/path');

expect(result).to.have.lengthOf(2);

sinon.restore();
});
});

describe('readGlobalFieldSchemas', () => {
afterEach(() => {
sinon.restore();
});

it('should return empty array when directory does not exist', () => {
sinon.stub(require('fs'), 'existsSync').returns(false);

const result = readGlobalFieldSchemas('/nonexistent/path');

expect(result).to.be.an('array');
expect(result).to.have.lengthOf(0);
});

it('should read per-uid JSON files and ignore globalfields.json by default', () => {
const mockGF = { uid: 'gf_1', title: 'GF 1', schema: [] };

sinon.stub(require('fs'), 'existsSync').returns(true);
sinon.stub(require('fs'), 'readdirSync').returns(['gf_1.json', 'globalfields.json', '.DS_Store', 'schema.json']);
const readFileStub = sinon.stub(require('fs'), 'readFileSync');
readFileStub.withArgs(sinon.match(/gf_1\.json/), 'utf8').returns(JSON.stringify(mockGF));

const result = readGlobalFieldSchemas('/test/path');

expect(result).to.be.an('array');
expect(result).to.have.lengthOf(1);
expect(result[0].uid).to.equal('gf_1');
});

it('should not include globalfields.json — prevents bulk-array corruption on import', () => {
const mockBulkArray = [{ uid: 'gf_1' }, { uid: 'gf_2' }];
const mockPerUid = { uid: 'gf_3', title: 'GF 3', schema: [] };

sinon.stub(require('fs'), 'existsSync').returns(true);
sinon.stub(require('fs'), 'readdirSync').returns(['globalfields.json', 'gf_3.json']);
const readFileStub = sinon.stub(require('fs'), 'readFileSync');
readFileStub.withArgs(sinon.match(/globalfields\.json/), 'utf8').returns(JSON.stringify(mockBulkArray));
readFileStub.withArgs(sinon.match(/gf_3\.json/), 'utf8').returns(JSON.stringify(mockPerUid));

const result = readGlobalFieldSchemas('/test/path');

// Only the per-uid file is returned; globalfields.json array not parsed as a schema entry
expect(result).to.have.lengthOf(1);
expect(Array.isArray(result[0])).to.be.false;
expect((result[0] as any).uid).to.equal('gf_3');
});

it('should read multiple per-uid files', () => {
const mockGF1 = { uid: 'gf_1', title: 'GF 1', schema: [] };
const mockGF2 = { uid: 'gf_2', title: 'GF 2', schema: [] };

sinon.stub(require('fs'), 'existsSync').returns(true);
sinon.stub(require('fs'), 'readdirSync').returns(['gf_1.json', 'gf_2.json', 'globalfields.json']);
const readFileStub = sinon.stub(require('fs'), 'readFileSync');
readFileStub.withArgs(sinon.match(/gf_1\.json/), 'utf8').returns(JSON.stringify(mockGF1));
readFileStub.withArgs(sinon.match(/gf_2\.json/), 'utf8').returns(JSON.stringify(mockGF2));

const result = readGlobalFieldSchemas('/test/path');

expect(result).to.have.lengthOf(2);
expect(result.map((r: any) => r.uid)).to.include.members(['gf_1', 'gf_2']);
});

it('should return empty array when directory contains only globalfields.json', () => {
sinon.stub(require('fs'), 'existsSync').returns(true);
sinon.stub(require('fs'), 'readdirSync').returns(['globalfields.json', '.DS_Store']);

const result = readGlobalFieldSchemas('/test/path');

expect(result).to.be.an('array');
expect(result).to.have.lengthOf(0);
});

it('readContentTypeSchemas default ignore list is unchanged — no globalfields.json exclusion', () => {
sinon.stub(require('fs'), 'existsSync').returns(true);
sinon.stub(require('fs'), 'readdirSync').returns(['globalfields.json']);
sinon.stub(require('fs'), 'readFileSync')
.withArgs(sinon.match(/globalfields\.json/), 'utf8')
.returns(JSON.stringify([{ uid: 'gf_1' }]));

// readContentTypeSchemas includes globalfields.json; readGlobalFieldSchemas excludes it
const ctResult = readContentTypeSchemas('/test/path');
const gfResult = readGlobalFieldSchemas('/test/path');

expect(ctResult).to.have.lengthOf(1);
expect(gfResult).to.have.lengthOf(0);
});
});
Loading
Loading