Closed unphased closed 5 months ago
Since it seems like madge hasn't been converted to module...
I tried to load it this way:
// Import Madge using dynamic import syntax
const madgeModule = await import('madge');
const madge = madgeModule.default;
it doesn't look like it can work.
/Users/me/project/node_modules/madge/lib/api.js:130
const tree = this.obj();
^
TypeError: Cannot read properties of undefined (reading 'obj')
at depends (/Users/me/project/node_modules/madge/lib/api.js:130:21)
at Array.map (<anonymous>)
at file:///Users/me/project/build/project/deps.js:24:41
Node.js v21.5.0
Maybe i was going off of old documentation. But I was able to get it working via:
// Import Madge using dynamic import syntax
import * as path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isProgramLaunchContext = () => {
return fileURLToPath(import.meta.url) === process.argv[1];
}
if (isProgramLaunchContext()) {
console.log('ok');
(async () => {
const madgeModule = await import('madge');
const madge = madgeModule.default;
// Which other source files depend on these provided ones?
const computeDependents = async (srcFiles: string[]) => {
// need to grab actual typescript source location
let projectDir = path.resolve(__dirname)
if (projectDir.split('/').pop() === 'build') {
projectDir = path.resolve(projectDir, '..', 'src');
}
console.log('projectDir', projectDir);
const res = await madge(projectDir);
console.log('res', res);
};
await computeDependents(process.argv.slice(2));
})();
}
the insane thing i was trying before srcFiles.map(res.depends)
can't work.
Madge installed in my project as seen in package-lock:
Here is my code.
Running it leads to
I get this same thing if i compile it to js (targeting esm!) with babel first and launch it with
node
.