Skip to content
Open
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
18 changes: 16 additions & 2 deletions packages/contentstack-utilities/src/chalk.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
/**
* Chalk 5 is ESM-only. We load it via dynamic import and cache for use in CommonJS.
*
* More than one physical copy of this package can load in one process (e.g. pnpm).
* Cache on globalThis via Symbol.for so loadChalk() from any copy warms getChalk() for all.
*/
let chalkInstance: typeof import('chalk').default | null = null;

export type ChalkInstance = typeof import('chalk').default;

const chalkGlobal = Symbol.for('@contentstack/cli-utilities/chalk');

function readCached(): ChalkInstance | undefined {
return (globalThis as unknown as Record<symbol, ChalkInstance | undefined>)[chalkGlobal];
}

function writeCached(chalkInstance: ChalkInstance): void {
(globalThis as unknown as Record<symbol, ChalkInstance>)[chalkGlobal] = chalkInstance;
}

/**
* Load chalk (ESM) and cache it. Call this once during CLI init before any chalk usage.
*/
export async function loadChalk(): Promise<ChalkInstance> {
let chalkInstance = readCached();
if (!chalkInstance) {
const chalkModule = await import('chalk');
chalkInstance = chalkModule.default;
writeCached(chalkInstance);
}
return chalkInstance;
}
Expand All @@ -20,6 +33,7 @@ export async function loadChalk(): Promise<ChalkInstance> {
* Get the cached chalk instance. Must call loadChalk() first (e.g. in init hook).
*/
export function getChalk(): ChalkInstance {
const chalkInstance = readCached();
if (!chalkInstance) {
throw new Error('Chalk not loaded. Ensure loadChalk() is called during init (e.g. in utils-init hook).');
}
Expand Down
26 changes: 26 additions & 0 deletions packages/contentstack/bin/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
#!/usr/bin/env node

const path = require('path');
const fs = require('fs');
const Module = require('module');

// In this monorepo, pnpm can place a second physical copy of @contentstack/cli-utilities
// (older chalk cache) while plugins resolve the main package's symlinked copy. Force all
// requires to the same package root so loadChalk/getChalk share one implementation.
const utilitiesPkgJson = path.resolve(__dirname, '..', 'node_modules', '@contentstack', 'cli-utilities', 'package.json');
if (fs.existsSync(utilitiesPkgJson)) {
const resolveFromUtilities = Module.createRequire(utilitiesPkgJson);
const origResolveFilename = Module._resolveFilename.bind(Module);
Module._resolveFilename = (request, parent, isMain, options) => {
if (request === '@contentstack/cli-utilities' || request.startsWith('@contentstack/cli-utilities/')) {
try {
const relative =
request === '@contentstack/cli-utilities' ? '.' : `.${request.slice('@contentstack/cli-utilities'.length)}`;
return resolveFromUtilities.resolve(relative);
} catch {
/* fall through */
}
}
return origResolveFilename(request, parent, isMain, options);
};
}

// eslint-disable-next-line unicorn/prefer-top-level-await
(async () => {
try {
Expand Down
Loading