automerge/javascript/scripts/denoify-replacer.mjs
alexjg 5629a7bec4
Various CI script fixes (#501)
Some of the scripts in scripts/ci were not reliable detecting the path
they were operating in. Additionally the deno_tests script was not
correctly picking up the ROOT_MODULE environment variable. Add more
robust path handling and fix the deno_tests script.
2023-01-19 15:38:27 +00:00

42 lines
1.5 KiB
JavaScript

// @denoify-ignore
import { makeThisModuleAnExecutableReplacer } from "denoify"
// import { assert } from "tsafe";
// import * as path from "path";
makeThisModuleAnExecutableReplacer(
async ({ parsedImportExportStatement, destDirPath, version }) => {
version = process.env.VERSION || version
switch (parsedImportExportStatement.parsedArgument.nodeModuleName) {
case "@automerge/automerge-wasm":
{
const moduleRoot =
process.env.ROOT_MODULE ||
`https://deno.land/x/automerge_wasm@${version}`
/*
*We expect not to run against statements like
*import(..).then(...)
*or
*export * from "..."
*in our code.
*/
if (
!parsedImportExportStatement.isAsyncImport &&
(parsedImportExportStatement.statementType === "import" ||
parsedImportExportStatement.statementType === "export")
) {
if (parsedImportExportStatement.isTypeOnly) {
return `${parsedImportExportStatement.statementType} type ${parsedImportExportStatement.target} from "${moduleRoot}/index.d.ts";`
} else {
return `${parsedImportExportStatement.statementType} ${parsedImportExportStatement.target} from "${moduleRoot}/automerge_wasm.js";`
}
}
}
break
}
//The replacer should return undefined when we want to let denoify replace the statement
return undefined
}
)