Skip to content

Commit

Permalink
v0.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamPuzio committed Jun 9, 2022
1 parent b93e54e commit b7813a4
Show file tree
Hide file tree
Showing 37 changed files with 831 additions and 596 deletions.
6 changes: 3 additions & 3 deletions base/apps/api.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ApiService = require('moleculer-web')

const cfg = Hub.getAppConfig('api')
const app = cfg.app
if (!app.config) throw new Error(`API requires a config to be set`)
if (!app.config) throw new Error('API requires a config to be set')

let routes = app.config
if (!Array.isArray(routes)) routes = [routes]
Expand All @@ -15,6 +15,6 @@ module.exports = {

settings: {
port: process.env.API_PORT || app.port || 4000,
routes: routes
routes
}
}
}
57 changes: 14 additions & 43 deletions base/apps/web.app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict'

const Panda = require('../../')
const ctx = Panda.ctx
const Core = require('panda-core')
const Hub = require('../../src/hub')
const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const cors = require('@koa/cors')
const serve = require('koa-static')
const render = require('../lib/koa-render')
const session = require('koa-session')
const path = require('path')
const mount = require('koa-mount')

const appCfg = Hub.getAppConfig('web')
Expand All @@ -19,7 +16,7 @@ module.exports = {
name: 'web',

settings: {
port: process.env.API_PORT || appBaseCfg.port || 5000
port: process.env.PORT || appBaseCfg.port || 5000
},

methods: {
Expand All @@ -33,26 +30,20 @@ module.exports = {
}
},


async created () {
const app = this.app = new Koa()
app.broker = app.context.broker = this.broker
const nodeEnv = process.env.NODE_ENV || 'development'
const logger = this.logger
// const nodeEnv = process.env.NODE_ENV || 'development'
const logger = Core.Logger.getLogger()

const projectCfg = Hub.getAppConfig('web')
// const projectCfg = Hub.getAppConfig('web')

app.use(cors())
app.use(bodyParser())

app.use(this.establishSession(app))

/*app.context.cmp = function(cmp, cfg={}) {
console.log('app.context.cmp')
app.broker.call('component.render', { cmp, cfg })
.then(result => { console.log(result); return result })
}*/
app.context.cmp = async function(cmp, cfg={}) {
app.context.cmp = async function (cmp, cfg = {}) {
return await app.broker.call('component.render', { cmp, cfg })
}

Expand All @@ -62,17 +53,20 @@ module.exports = {
ctx.state._url = ctx.originalUrl
ctx.state._env = ctx.app.env
const start = Date.now()

if (!ctx.session.session_id) ctx.session.session_id = Core.Utility.uuid()

await next()

const ms = Date.now() - start
ctx.set('X-Response-Time', `${ms}ms`)
logger.debug(`${ctx.method} ${ctx.url} (${ctx.status}) - ${ms}ms`)
logger.http(`${ctx.method} ${ctx.url} (${ctx.status}) - ${ms}ms [session:${ctx.session.session_id}]`)
})

if (appCfg.static) {
this.logger.debug(`setting up static directories...`)
this.logger.debug('setting up static directories...')
appCfg.static.forEach((staticCfg) => {
this.logger.debug(` ${staticCfg.path}`)
//app.use(serve(path.join(ctx.PROJECT_PATH, staticCfg.path)))
if (staticCfg.mount) {
app.use(mount(staticCfg.mount, serve(staticCfg.path)))
} else {
Expand All @@ -82,39 +76,16 @@ module.exports = {
}

if (appCfg.routes) {
this.logger.debug(`setting up routes...`)
this.logger.debug('setting up routes...')
appCfg.routes.forEach((route) => {
const files = route.files || []
files.forEach((file) => {
this.logger.debug(` ${file}`)
//const routeFile = require(path.join(ctx.PROJECT_PATH, file))
const routeFile = require(file)
app.use(routeFile.routes())
})
})
}

if (appCfg.views) {
this.logger.debug(`setting up views...`)
render (app, appCfg)
appCfg.views.forEach((views) => {
//const viewBasePath = path.join(ctx.PROJECT_PATH, views.path)
const viewBasePath = path.join(views.path)
/*render(app, {
root: viewBasePath,
includer: function (originalPath, parsedPath) {
//const filename = path.join(localApp.viewsDir, originalPath + '.html')
const filename = path.join(viewBasePath, originalPath + '.html')
const fileExists = fs.fileExistsSync(filename)
if (!fileExists) {
logger.error(`Trying to access a view that does not exist: ${originalPath}`)
const tpl = (nodeEnv === 'production') ? '-' : 'missing view: ' + originalPath
return { template: tpl }
}
}
})*/
})
}
},

started () {
Expand All @@ -134,4 +105,4 @@ module.exports = {
})
}
}
}
}
24 changes: 11 additions & 13 deletions base/lib/koa-render.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const Panda = require('../../')
const fs = require('fs')
const path = require('path')
const ejs = require('ejs')

const defaultSettings = {
Expand All @@ -16,7 +14,7 @@ const defaultSettings = {
async: true
}

const settings = {...{}, ...defaultSettings}
const settings = { ...{}, ...defaultSettings }

const viewCache = {}

Expand Down Expand Up @@ -50,12 +48,12 @@ exports = module.exports = function (app, appCfg) {
// override `ejs` node_module `resolveInclude` function
ejs.resolveInclude = function (name, filename, isDir) {
return viewCache[name].path
/*if (!path.extname(name)) {
/* if (!path.extname(name)) {
name += '.html'
}
const viewsDir = path.join(Panda.APP_PATH, 'app', 'views')
return path.join(viewsDir, name)*/
return path.join(viewsDir, name) */
}

/**
Expand All @@ -65,20 +63,20 @@ exports = module.exports = function (app, appCfg) {
* @return {String} html
*/
async function render (view, options) {
//view += settings.viewExt
//const viewPath = path.join(settings.root, view)
// view += settings.viewExt
// const viewPath = path.join(settings.root, view)
const viewInfo = viewCache[view]
// debug(`render: ${viewPath}`);
// get from cache
if (settings.cache && cache[view]) {
return cache[view].call(options.scope, options)
if (settings.cache && settings.cache[view]) {
return settings.cache[view].call(options.scope, options)
}

const tpl = fs.readFileSync(viewInfo.path, 'utf8')

const fn = ejs.compile(tpl, {
//filename: viewInfo.path,
//_with: settings._with,
// filename: viewInfo.path,
// _with: settings._with,
compileDebug: settings.debug && settings.compileDebug,
debug: settings.debug,
delimiter: settings.delimiter,
Expand All @@ -87,13 +85,13 @@ exports = module.exports = function (app, appCfg) {
outputFunctionName: settings.outputFunctionName
})
if (settings.cache) {
cache[viewInfo] = fn
settings.cache[viewInfo] = fn
}

return fn.call(options.scope, options)
}

app.context.cmp = async function(cmp, cfg={}) {
app.context.cmp = async function (cmp, cfg = {}) {
return await app.broker.call('component.render', { cmp, cfg })
}

Expand Down
8 changes: 4 additions & 4 deletions base/services/component.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path')
const cache = new CacheBase()

module.exports = {
name: "component",
name: 'component',
actions: {
get: {
params: {
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = {
const cmp = cache.get(ns)
if (!cmp) throw new Error(`${ctx.params.cmp} is not a valid component`)
if (!cmp._webView) {
//const cmpPath = path.join(ctx.PROJECT_PATH, path.dirname(cmp.cmp.path))
// const cmpPath = path.join(ctx.PROJECT_PATH, path.dirname(cmp.cmp.path))
const cmpPath = path.dirname(cmp.cmp.path)
const webViewPath = path.resolve(cmpPath, cmp.cmp.config.webView)
cmp._webView = fs.readFileSync(webViewPath, { encoding: 'utf8' })
Expand All @@ -51,10 +51,10 @@ module.exports = {
},

async created () {
//const shrinkwrap = await Project.shrinkwrap()
// const shrinkwrap = await Project.shrinkwrap()
const shrinkwrap = await Project.live()
shrinkwrap.components.forEach((cmp) => {
cache.set(cmp.namespace, { cmp })
})
}
}
}
4 changes: 2 additions & 2 deletions base/services/project.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Project = require('../../src/project')

module.exports = {
name: "project",
name: 'project',
actions: {

shrinkwrap: {
Expand All @@ -14,4 +14,4 @@ module.exports = {
}
}
}
}
}
24 changes: 24 additions & 0 deletions bin/panda-app:run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

'use strict'

const Core = require('panda-core')
const Wasp = Core.Wasp
const program = new Wasp.Command()
const Hub = require('../src/hub')

program
.description('Run an Application and all Services')
.argument('<app>', 'The App to run')
.action(async function (app, opts, cmd) {
this.debug('command: run')

this.heading(`Running the ${app} app and all Services`)

// check to make sure we are in a Project directory
await this.confirmInProject()

// run it...
Hub.start(app)
})
.parse(process.argv)
27 changes: 27 additions & 0 deletions bin/panda-command:create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const Core = require('panda-core')
const Wasp = Core.Wasp
const program = new Wasp.ScaffoldCommand()

program
.description('Create a new Command')
.argument('[command]')
.option('--command', 'The command that will get run')
.action(async function (command, opts, cmd) {
this.debug('command: command:create')

this.heading('Creating a new Command', { subhead: 'Press Ctrl+C to cancel' })

// check to make sure we are in a Project, Panda, PandaCore, PandaDev or PrivateLabel directory
await this.locationTest(['inProject', 'inPanda', 'inPandaCore', 'inPandaDev', 'inPrivateLabel'], { operator: 'OR' })

await this.parseScaffold('command', { interactiveMode: !command, mapping: { command } })
.then(() => { this.success('Panda command successfully created') })
.catch((err) => {
this.exitError(err, 'Panda command creation failed')
this.error('Command creation failed')
this.debug(err)
})
})
.parse(process.argv)
26 changes: 26 additions & 0 deletions bin/panda-component:create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const Core = require('panda-core')
const Wasp = Core.Wasp
const program = new Wasp.ScaffoldCommand()

program
.description('Create a new Component')
.argument('[name]')
.option('--name', 'The name of the component')
.option('--slug', 'The slug and filename of the component being built')
.action(async function (name, opts, cmd) {
this.debug('command: component:create')

this.heading('Creating a new Component')

// check to make sure we are in a Project directory
await this.confirmInProject()

await this.parseScaffold('component', { interactiveMode: !name, mapping: { name } })
.then(() => { this.success('Component successfully created') })
.catch((err) => {
this.exitError(err, 'Component creation failed')
})
})
.parse(process.argv)
32 changes: 0 additions & 32 deletions bin/panda-create-component.js

This file was deleted.

Loading

0 comments on commit b7813a4

Please sign in to comment.