Skip to content

Commit

Permalink
chore: verifies the module system before import config file
Browse files Browse the repository at this point in the history
It imports the package.json from projectDirectory to check the module system,
ECMAScript Modules or CommonJS. If the  module system is ESM then just import
the eventcatalog.config.js. Otherwise, make a copy of eventcatalog.config.js
to the tmpdir/eventcatalog.config.mjs and import it.
It removes the convertEsmToCjs used before.
  • Loading branch information
carlosallexandre committed Sep 29, 2024
1 parent 3b11141 commit 8d71b61
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 52 deletions.
7 changes: 4 additions & 3 deletions scripts/__tests__/eventcatalog-config-file-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
verifyRequiredFieldsAreInCatalogConfigFile,
writeEventCatalogConfigFile,
} from 'scripts/eventcatalog-config-file-utils';
import { tmpdir } from 'os';

describe('catalog-to-astro-content-directory', () => {
afterEach(async () => {
Expand Down Expand Up @@ -101,11 +102,11 @@ describe('catalog-to-astro-content-directory', () => {
})
);
});
it('eventcatalog.config.cjs (temp commonjs file) is removed when fetching the catalog file', async () => {
it('removes eventcatalog config from the tmpdir', async () => {
await getEventCatalogConfigFile(CATALOG_DIR);

const commonJsFile = path.join(CATALOG_DIR, 'eventcatalog.config.cjs');
expect(existsSync(commonJsFile)).toBe(false);
const configFile = path.join(tmpdir(), 'eventcatalog.config.mjs');
expect(existsSync(configFile)).toBe(false);
});
});

Expand Down
3 changes: 3 additions & 0 deletions scripts/__tests__/example-catalog/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"description": "Only for testing purposes"
}
66 changes: 17 additions & 49 deletions scripts/eventcatalog-config-file-utils.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,34 @@
import { readFile, writeFile, rm } from 'node:fs/promises';
import { existsSync } from 'fs';
import { existsSync } from 'node:fs';
import { copyFile } from 'node:fs/promises';
import path from 'node:path';
import { v4 as uuidV4 } from 'uuid';
import { pathToFileURL } from 'url';
import { tmpdir } from 'node:os';

// * Very strange behavior when importing ESM files from catalogs into core.
// * Core (node) does not know how to handle ESM files, so we have to try and convert them.
// *
// * This needs sorting out! Sorry if you are reading this, but it unblocked me for now!
// * @param {*} content
// * @returns
// */
function convertESMtoCJS(content) {
// Replace import statements with require
content = content.replace(/import\s+([a-zA-Z0-9{},\s*]+)\s+from\s+['"]([^'"]+)['"];/g, (match, imports, modulePath) => {
return `const ${imports.trim()} = require('${modulePath}');`;
});

// Replace export default with module.exports
content = content.replace(/export\s+default\s+/g, 'module.exports = ');

// Replace named exports with module.exports
content = content.replace(/export\s+{([^}]+)}/g, (match, exports) => {
return `module.exports = {${exports.trim()}};`;
});

// Remove declarations of __filename and __dirname
content = content.replace(/^\s*(const|let|var)\s+__(filename|dirname)\s+=\s+.*;?\s*$/gm, '');

return content;
}

export async function cleanup(projectDirectory) {
const filePath = path.join(projectDirectory, 'eventcatalog.config.cjs');
export async function cleanup() {
const filePath = path.join(tmpdir(), 'eventcatalog.config.mjs');
if (existsSync(filePath)) {
await rm(filePath);
}
}

export const getEventCatalogConfigFile = async (projectDirectory) => {
try {
const rawFile = await readFile(path.join(projectDirectory, 'eventcatalog.config.js'), 'utf8');

// Have to conver the ESM to CJS...
const configAsCommonJS = convertESMtoCJS(rawFile);
let configFilePath = path.join(projectDirectory, 'eventcatalog.config.js');

await writeFile(path.join(projectDirectory, 'eventcatalog.config.cjs'), configAsCommonJS);
const packageJson = await import(/* @vite-ignore */ path.join(projectDirectory, 'package.json'));
if (packageJson.default?.type !== 'module') {
await copyFile(configFilePath, path.join(tmpdir(), 'eventcatalog.config.mjs'));
configFilePath = path.join(tmpdir(), 'eventcatalog.config.mjs');
}

const configFilePath = path.join(projectDirectory, 'eventcatalog.config.cjs');
const configFileURL = pathToFileURL(configFilePath).href;
const configAsCJS = await import(/* @vite-ignore */ configFileURL);

// Clean up?
await writeFile(path.join(projectDirectory, 'eventcatalog.config.js'), rawFile);

await cleanup(projectDirectory);
const config = await import(/* @vite-ignore */ configFileURL);

return configAsCJS.default;
} catch (error) {
await cleanup(projectDirectory);
return config.default;
} finally {
await cleanup();
}
};

Expand Down Expand Up @@ -92,10 +62,8 @@ export const writeEventCatalogConfigFile = async (projectDirectory, newConfig) =

// Write the updated content back to the file
await writeFile(configFilePath, content);

await cleanup(projectDirectory);
} catch (error) {
await cleanup(projectDirectory);
} finally {
await cleanup();
}
};

Expand Down

0 comments on commit 8d71b61

Please sign in to comment.