Skip to content

Commit

Permalink
Initial version of dev server. finos#33
Browse files Browse the repository at this point in the history
  • Loading branch information
AttilaMihaly committed Dec 1, 2020
1 parent 4f60a79 commit 1ec34c4
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules
elm-stuff
.vscode
.idea
dist/
dist/
morphir-ir.json
tests/
3 changes: 2 additions & 1 deletion cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Morphir.*.js
Morphir.*.js
web/main.js
17 changes: 8 additions & 9 deletions cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function make(projectDir) {

async function packageDefinitionFromSource(morphirJson, sourceFiles) {
return new Promise((resolve, reject) => {
worker.ports.decodeError.subscribe(err => {
worker.ports.jsonDecodeError.subscribe(err => {
reject(err)
})

Expand Down Expand Up @@ -79,11 +79,11 @@ async function gen(input, outputPath, options) {
fileMap.map(async ([[dirPath, fileName], content]) => {
const fileDir = dirPath.reduce((accum, next) => path.join(accum, next), outputPath)
const filePath = path.join(fileDir, fileName)
if (await fileExist(filePath)) {
if (await fileExist(filePath)) {
console.log(`UPDATE - ${filePath}`)
} else {
await mkdir(fileDir, {recursive: true})
console.log(`INSERT - ${filePath}`)
await mkdir(fileDir, { recursive: true })
console.log(`INSERT - ${filePath}`)
}
return fsWriteFile(filePath, content)
})
Expand All @@ -99,14 +99,13 @@ async function gen(input, outputPath, options) {

async function copyRecursiveSync(src, dest) {
var exists = fs.existsSync(src);
if(exists)
{
if (exists) {
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
if(!fs.existsSync(dest))
if (!fs.existsSync(dest))
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function(childItemName) {
fs.readdirSync(src).forEach(function (childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
Expand All @@ -118,7 +117,7 @@ async function copyRecursiveSync(src, dest) {

async function generate(options, ir) {
return new Promise((resolve, reject) => {
worker.ports.decodeError.subscribe(err => {
worker.ports.jsonDecodeError.subscribe(err => {
reject(err)
})

Expand Down
40 changes: 40 additions & 0 deletions cli/morphir-elm-develop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node
'use strict'

// NPM imports
const path = require('path')
const commander = require('commander')
const express = require('express')
const cli = require('./cli')

// Set up Commander
const program = new commander.Command()
program
.name('morphir-elm develop')
.description('Start up a web server and expose developer tools through a web UI')
.option('-p, --project-dir <path>', 'Root directory of the project where morphir.json is located.', '.')
.parse(process.argv)

const app = express()
const port = 3000

app.get('/', (req, res) => {
res.redirect('index.html')
})

app.use(express.static(path.join(__dirname, 'web')))

app.get('/server/make', (req, res) => {
cli.make(program.projectDir)
.then((packageDef) => {
res.send(['ok', packageDef])
})
.catch((err) => {
res.send(['err', err])
})
})

app.listen(port, () => {
console.log(`Developer server listening at http://localhost:${port}`)
})

2 changes: 1 addition & 1 deletion cli/morphir-elm-make.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cli.make(program.projectDir)
if (err.code == 'ENOENT') {
console.error(`Could not find file at '${err.path}'`)
} else {
console.error(JSON.stringify(err))
console.error(`Error: ${JSON.stringify(err)}`)
}
process.exit(1)
})
1 change: 1 addition & 0 deletions cli/morphir-elm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ program
.version(packageJson.version, '-v, --version')
.command('make', 'Translate Elm sources to Morphir IR')
.command('gen', 'Generate code from Morphir IR')
.command('develop', 'Start up a web server and expose developer tools through a web UI')
.parse(process.argv)
37 changes: 31 additions & 6 deletions cli/src/Morphir/Elm/CLI.elm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Morphir.IR.Distribution.Codec as DistributionCodec
import Morphir.IR.Package as Package
import Morphir.IR.Type exposing (Type)
import Morphir.Type.Infer as Infer
import Morphir.Type.Infer.Codec exposing (encodeValueTypeError)
import Morphir.Type.Infer.Codec exposing (decodeValueTypeError, encodeValueTypeError)
import Morphir.Type.MetaTypeMapping as MetaTypeMapping


Expand All @@ -39,7 +39,7 @@ port packageDefinitionFromSource : (( Decode.Value, List SourceFile ) -> msg) ->
port packageDefinitionFromSourceResult : Encode.Value -> Cmd msg


port decodeError : String -> Cmd msg
port jsonDecodeError : String -> Cmd msg


port generate : (( Decode.Value, Decode.Value ) -> msg) -> Sub msg
Expand Down Expand Up @@ -114,7 +114,7 @@ update msg model =
( model
, errorMessage
|> Decode.errorToString
|> decodeError
|> jsonDecodeError
)

Generate ( optionsJson, packageDistJson ) ->
Expand Down Expand Up @@ -142,7 +142,7 @@ update msg model =
( model, fileMap |> Ok |> encodeResult Encode.string encodeFileMap |> generateResult )

Err errorMessage ->
( model, errorMessage |> Decode.errorToString |> decodeError )
( model, errorMessage |> Decode.errorToString |> jsonDecodeError )


subscriptions : () -> Sub Msg
Expand Down Expand Up @@ -173,7 +173,32 @@ encodeError : Error -> Encode.Value
encodeError error =
case error of
FrontendError frontendErrors ->
Encode.list FrontendCodec.encodeError frontendErrors
Encode.list identity
[ Encode.string "frontend_error"
, Encode.list FrontendCodec.encodeError frontendErrors
]

TypeError typeError ->
Encode.list encodeValueTypeError typeError
Encode.list identity
[ Encode.string "type_error"
, Encode.list encodeValueTypeError typeError
]


decodeError : Decode.Decoder Error
decodeError =
Decode.index 0 Decode.string
|> Decode.andThen
(\tag ->
case tag of
"frontend_error" ->
Decode.index 1 (Decode.succeed [])
|> Decode.map FrontendError

"type_error" ->
Decode.index 1 (Decode.succeed [])
|> Decode.map TypeError

other ->
Decode.fail ("Unknown tag: " ++ other)
)
Loading

0 comments on commit 1ec34c4

Please sign in to comment.