Files
overleaf-cep/services/web/transform/cjs-to-esm/codemods/addExtensions.js
Andrew Rumble eda1bd697f Merge pull request #27958 from overleaf/ar-change-esm-codemod-to-use-vitest-and-general-refactor
[web] change esm codemod to use vitest and general refactor

GitOrigin-RevId: 7f8c699b160ee0b7ff991d6284cb126165694c4f
2025-09-17 08:04:57 +00:00

34 lines
870 B
JavaScript

const fs = require('node:fs')
const Path = require('node:path')
module.exports = function (fileInfo, api) {
const j = api.jscodeshift
const root = j(fileInfo.source)
// Add extension to relative path imports
root
.find(j.ImportDeclaration)
.filter(path => path.node.source.value.startsWith('.'))
.forEach(path => {
const importPath = path.node.source.value
const fullPathJs = Path.resolve(
Path.dirname(fileInfo.path),
`${importPath}.js`
)
const fullPathMjs = Path.resolve(
Path.dirname(fileInfo.path),
`${importPath}.mjs`
)
if (fs.existsSync(fullPathJs)) {
path.node.source.value = `${importPath}.js`
} else if (fs.existsSync(fullPathMjs)) {
path.node.source.value = `${importPath}.mjs`
}
})
return root.toSource({
quote: 'single',
})
}