Use elm as a fallback executable when running elm-pages scripts.

This commit is contained in:
Dillon Kearns 2023-03-23 10:06:24 -07:00
parent aec36b13c3
commit 3bbe3badfd
4 changed files with 41 additions and 17 deletions

View File

@ -446,8 +446,9 @@ function modeToOptions(mode) {
*/
function runElmMake(mode, options, elmEntrypointPath, outputPath, cwd) {
return new Promise(async (resolve, reject) => {
const executableName = options.executableName || "lamdera";
const subprocess = spawnCallback(
`lamdera`,
executableName,
[
"make",
elmEntrypointPath,

View File

@ -14,7 +14,7 @@ import * as esbuild from "esbuild";
import { rewriteElmJson } from "./rewrite-elm-json.js";
import { ensureDirSync } from "./file-helpers.js";
import * as url from "url";
import { default as which } from "which";
import * as commander from "commander";
import { runElmCodegenInstall } from "./elm-codegen.js";
import { packageVersion } from "./compatibility-key.js";
@ -121,9 +121,19 @@ async function main() {
path.join("./script/elm-stuff/elm-pages/.elm-pages/Main.elm"),
generatorWrapperFile(moduleName)
);
let executableName = "lamdera";
try {
await which("lamdera");
executableName = "elm";
} catch (error) {
await which("elm");
console.log("Falling back to Elm");
executableName = "elm";
}
await rewriteElmJson(
"./script/elm.json",
"./script/elm-stuff/elm-pages/elm.json"
"./script/elm-stuff/elm-pages/elm.json",
{ executableName }
);
const portBackendTaskCompiled = esbuild
@ -162,7 +172,7 @@ async function main() {
process.chdir("./script");
// TODO have option for compiling with --debug or not (maybe allow running with elm-optimize-level-2 as well?)
await build.compileCliApp({ debug: "debug" });
await build.compileCliApp({ debug: "debug", executableName });
process.chdir("../");
fs.renameSync(
"./script/elm-stuff/elm-pages/elm.js",
@ -224,15 +234,28 @@ async function main() {
path.join("./script/elm-stuff/elm-pages/.elm-pages/Main.elm"),
generatorWrapperFile(moduleName)
);
let executableName = "lamdera";
try {
await which("lamdera");
executableName = "elm";
} catch (error) {
await which("elm");
console.log("Falling back to Elm");
executableName = "elm";
}
await rewriteElmJson(
"./script/elm.json",
"./script/elm-stuff/elm-pages/elm.json"
"./script/elm-stuff/elm-pages/elm.json",
{ executableName }
);
process.chdir("./script");
// TODO have option for compiling with --debug or not (maybe allow running with elm-optimize-level-2 as well?)
console.log("Compiling...");
await build.compileCliApp({ debug: options.debug });
await build.compileCliApp({
debug: options.debug,
executableName,
});
process.chdir("../");
if (!options.debug) {
console.log("Running elm-optimize-level-2...");
@ -361,7 +384,6 @@ async function requireElm(compiledElmPath) {
function generatorWrapperFile(moduleName) {
return `port module Main exposing (main)
import Bytes
import BackendTask exposing (BackendTask)
import FatalError
import Cli.Program as Program
@ -378,7 +400,7 @@ main =
, toJsPort = toJsPort
, fromJsPort = fromJsPort identity
, gotBatchSub = gotBatchSub identity
, sendPageData = sendPageData
, sendPageData = \\_ -> Cmd.none
}
@ -389,9 +411,6 @@ port fromJsPort : (Decode.Value -> msg) -> Sub msg
port gotBatchSub : (Decode.Value -> msg) -> Sub msg
port sendPageData : { oldThing : Encode.Value, binaryPageData : Bytes.Bytes } -> Cmd msg
`;
}
function collect(value, previous) {

View File

@ -133,7 +133,6 @@ function runGeneratorAppHelp(
killApp = () => {
app.ports.toJsPort.unsubscribe(portHandler);
app.ports.sendPageData.unsubscribe(portHandler);
app.die();
app = null;
// delete require.cache[require.resolve(compiledElmPath)];
@ -209,7 +208,6 @@ function runGeneratorAppHelp(
}
}
app.ports.toJsPort.subscribe(portHandler);
app.ports.sendPageData.subscribe(portHandler);
}).finally(() => {
try {
killApp();

View File

@ -1,6 +1,10 @@
import * as fs from "node:fs";
export async function rewriteElmJson(sourceElmJsonPath, targetElmJsonPath) {
export async function rewriteElmJson(
sourceElmJsonPath,
targetElmJsonPath,
options
) {
var elmJson = JSON.parse(
(await fs.promises.readFile(sourceElmJsonPath)).toString()
);
@ -9,11 +13,11 @@ export async function rewriteElmJson(sourceElmJsonPath, targetElmJsonPath) {
await writeFileIfChanged(
targetElmJsonPath,
JSON.stringify(rewriteElmJsonHelp(elmJson))
JSON.stringify(rewriteElmJsonHelp(elmJson, options))
);
}
function rewriteElmJsonHelp(elmJson) {
function rewriteElmJsonHelp(elmJson, options) {
// The internal generated file will be at:
// ./elm-stuff/elm-pages/
// So, we need to take the existing elmJson and
@ -27,7 +31,9 @@ function rewriteElmJsonHelp(elmJson) {
elmJson["source-directories"] = elmJson["source-directories"].map((item) => {
return "../../" + item;
});
elmJson["dependencies"]["direct"]["lamdera/codecs"] = "1.0.0";
if (options && options.executableName !== "elm") {
elmJson["dependencies"]["direct"]["lamdera/codecs"] = "1.0.0";
}
// 3. add our own secret My.elm module 😈
elmJson["source-directories"].push(".elm-pages");
return elmJson;