mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 17:19:37 +02:00
[web] change esm codemod to use vitest and general refactor GitOrigin-RevId: 7f8c699b160ee0b7ff991d6284cb126165694c4f
34 lines
870 B
JavaScript
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',
|
|
})
|
|
}
|