ensure elm is installed before building

This commit is contained in:
Ryan Haskell-Glatz 2021-03-10 23:28:44 -06:00
parent b5544c9478
commit 3390e60e38
4 changed files with 7506 additions and 82 deletions

7531
src/cli/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "elm-spa",
"version": "6.0.11--beta",
"version": "6.0.13--beta",
"description": "single page apps made easy",
"bin": "dist/src/index.js",
"scripts": {

View File

@ -2,15 +2,17 @@ import path from "path"
import { exists } from "../file"
import config from '../config'
import * as File from '../file'
import { createInterface } from 'readline'
import RouteTemplate from '../templates/routes'
import PagesTemplate from '../templates/pages'
import PageTemplate from '../templates/page'
import RequestTemplate from '../templates/request'
import ModelTemplate from '../templates/model'
import MsgTemplate from '../templates/msg'
import ChildProcess from 'child_process'
import ParamsTemplate from '../templates/params'
import terser from 'terser'
import { bold, underline, colors, reset, check, dim, dot } from "../terminal"
import { bold, underline, colors, reset, check, dim, dot, warn, error } from "../terminal"
import { isStandardPage, isStaticPage, isStaticView, options, PageKind } from "../templates/utils"
import { createMissingAddTemplates } from "./_common"
const elm = require('node-elm-compiler')
@ -157,6 +159,8 @@ type Environment = 'production' | 'development'
const outputFilepath = path.join(config.folders.dist, 'elm.js')
const compileMainElm = (env: Environment) => async () => {
await ensureElmIsInstalled(env)
const start = Date.now()
const elmMake = async () => {
@ -257,6 +261,51 @@ const compileMainElm = (env: Environment) => async () => {
.then(_ => [success() + '\n'])
}
const ensureElmIsInstalled = async (environment : Environment) => {
await new Promise((resolve, reject) => {
ChildProcess.exec('elm', (err) => {
if (err) {
if (environment === 'production') {
attemptToInstallViaNpm(resolve, reject)
} else {
offerToInstallForDeveloper(resolve, reject)
}
} else {
resolve(undefined)
}
})
})
}
const attemptToInstallViaNpm = (resolve: (value: unknown) => void, reject: (reason: unknown) => void) => {
process.stdout.write(`\n ${bold}Awesome!${reset} Installing Elm via NPM... `)
ChildProcess.exec(`npm install --global elm@latest`, (err) => {
if (err) {
console.info(error)
reject(` The automatic install didn't work...\n Please visit ${colors.green}https://guide.elm-lang.org/install/elm${reset} to install Elm.\n`)
} else {
console.info(check)
console.info(` Elm is now installed!`)
resolve(undefined)
}
})
}
const offerToInstallForDeveloper = (resolve: (value: unknown) => void, reject: (reason: unknown) => void) => {
const rl = createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(`\n${warn} Elm hasn't been installed yet.\n\n May I ${colors.cyan}install${reset} it for you? ${dim}[y/n]${reset} `, answer => {
if (answer.toLowerCase() === 'n') {
reject(` ${bold}No changes made!${reset}\n Please visit ${colors.green}https://guide.elm-lang.org/install/elm${reset} to install Elm.`)
} else {
attemptToInstallViaNpm(resolve, reject)
}
})
}
export default {
run: build('production')
}

View File

@ -31,10 +31,6 @@ const config = {
public: path.join(cwd, 'public'),
dist: path.join(cwd, 'public', 'dist'),
},
binaries: {
elm: `npx elm`,
terser: `npx terser`
},
defaults: [
[ 'Auth.elm' ],
[ 'Effect.elm' ],