const { join } = require('path'); const t = require('@babel/types'); const { assert } = require('console'); module.exports = (babel) => { return { visitor: { FunctionDeclaration(path, state) { const { cwd } = state; const { node } = path; if (t.isIdentifier(node.id) && node.id.name === 'oakGetPackageJsonVersion') { const { body } = node; assert(t.isBlockStatement(body)); assert(body.body.length === 1); const pkgJson = join(cwd, 'package.json'); const { version } = require(pkgJson); body.body = [ t.returnStatement(t.stringLiteral(version)) ]; } }, }, }; };