Skip to content

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

TypeScript

[CLI] fix: exclude node_modules from integration codemod discovery (#7145)

Joey4cdaedc67eab
diff --git a/.changeset/fix-codemod-discovery-node-modules.md b/.changeset/fix-codemod-discovery-node-modules.md
new file mode 100644
index 0000000000..683daf7cde
--- /dev/null
+++ b/.changeset/fix-codemod-discovery-node-modules.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] upgrade: exclude node_modules from integration codemod discovery
+@josephfarina
+
+When an integration declared its codemods root as the package root (`codemods: "./"`), the version-folder scan treated `node_modules`, `.git`, `__tests__`, and `__fixtures__` as version folders and loaded their files as codemods. The same `SKIP_DIRS` filter that the recursive file walk already used is now also applied to the top-level version-folder enumeration.
diff --git a/packages/cli/assets/codemods/integration-discovery.mjs b/packages/cli/assets/codemods/integration-discovery.mjs
index c4534f8963..7ea378da4e 100644
--- a/packages/cli/assets/codemods/integration-discovery.mjs
+++ b/packages/cli/assets/codemods/integration-discovery.mjs
@@ -129,7 +129,7 @@ export async function discoverIntegrationCodemods(loadedIntegrations = []) {
const versionFolders = fs
.readdirSync(root, {withFileTypes: true})
- .filter(entry => entry.isDirectory())
+ .filter(entry => entry.isDirectory() && !SKIP_DIRS.has(entry.name))
.map(entry => entry.name);
// Track ids seen ACROSS versions within this package — duplicate id across
diff --git a/packages/cli/assets/codemods/integration-discovery.test.mjs b/packages/cli/assets/codemods/integration-discovery.test.mjs
index 34f8db8a6f..078586ea85 100644
--- a/packages/cli/assets/codemods/integration-discovery.test.mjs
+++ b/packages/cli/assets/codemods/integration-discovery.test.mjs
@@ -294,3 +294,67 @@ describe('test files beside a codemod', () => {
]);
});
});
+
+describe('non-source directories at codemods root', () => {
+ const MARKER_CODEMOD = `
+ import * as fs from 'node:fs';
+ export default {
+ type: 'code',
+ title: 'Evil marker',
+ transform: (file) => {
+ fs.writeFileSync('/tmp/evil-marker', 'pwned');
+ return file.source;
+ },
+ };
+ `;
+ const REAL_CODEMOD = `
+ export default {
+ type: 'code',
+ title: 'Real transform',
+ transform: (file) => file.source.replace(/old/g, 'new'),
+ };
+ `;
+
+ it('excludes node_modules from version folder discovery', async () => {
+ // When codemods root is the package root (codemods: "./"), node_modules
+ // must not be treated as a version folder.
+ const pkgDir = scaffold({
+ '0.2.0/real.mjs': REAL_CODEMOD,
+ });
+ // Plant a file inside node_modules at the codemods root level
+ const nmDir = path.join(pkgDir, 'codemods', 'node_modules', 'evil');
+ fs.mkdirSync(nmDir, {recursive: true});
+ fs.writeFileSync(path.join(nmDir, 'index.mjs'), MARKER_CODEMOD);
+
+ const project = await Project.load(tmpDir);
+ const byVersion = await discoverIntegrationCodemods(
+ project.loadedIntegrations,
+ );
+
+ // Only the real version folder is discovered
+ expect([...byVersion.keys()]).toEqual(['0.2.0']);
+ expect(byVersion.get('0.2.0').map(e => e.id)).toEqual(['real']);
+ });
+
+ it.each([
+ ['node_modules'],
+ ['.git'],
+ ['__tests__'],
+ ['__fixtures__'],
+ ])('excludes %s from version folder enumeration', async (dirName) => {
+ const pkgDir = scaffold({
+ '0.3.0/legit.mjs': REAL_CODEMOD,
+ });
+ // Create a non-source dir at the codemods root with a valid-looking codemod
+ const badDir = path.join(pkgDir, 'codemods', dirName, 'sneaky.mjs');
+ fs.mkdirSync(path.dirname(badDir), {recursive: true});
+ fs.writeFileSync(badDir, REAL_CODEMOD);
+
+ const project = await Project.load(tmpDir);
+ const byVersion = await discoverIntegrationCodemods(
+ project.loadedIntegrations,
+ );
+
+ expect([...byVersion.keys()]).toEqual(['0.3.0']);
+ });
+});