From 3bbe3badfd01b3d981946dad919a4e47caca5de0 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Thu, 23 Mar 2023 10:06:24 -0700 Subject: [PATCH] Use elm as a fallback executable when running elm-pages scripts. --- generator/src/build.js | 3 ++- generator/src/cli.js | 39 +++++++++++++++++++++++-------- generator/src/render.js | 2 -- generator/src/rewrite-elm-json.js | 14 +++++++---- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/generator/src/build.js b/generator/src/build.js index 9eb52330..577eddd2 100755 --- a/generator/src/build.js +++ b/generator/src/build.js @@ -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, diff --git a/generator/src/cli.js b/generator/src/cli.js index 14872a09..cd9d6cfa 100755 --- a/generator/src/cli.js +++ b/generator/src/cli.js @@ -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) { diff --git a/generator/src/render.js b/generator/src/render.js index 32169cbc..47345b60 100755 --- a/generator/src/render.js +++ b/generator/src/render.js @@ -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(); diff --git a/generator/src/rewrite-elm-json.js b/generator/src/rewrite-elm-json.js index f8202fd7..ee194a9e 100644 --- a/generator/src/rewrite-elm-json.js +++ b/generator/src/rewrite-elm-json.js @@ -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;