Skip to content

Commit

Permalink
feat(core): created @botpress/util-sdk to help develop modules
Browse files Browse the repository at this point in the history
  • Loading branch information
slvnperron committed May 15, 2018
1 parent 9157849 commit 96d30a3
Show file tree
Hide file tree
Showing 8 changed files with 4,027 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/botpress-util-sdk/.babelrc
@@ -0,0 +1,10 @@
{
"presets": [
["env", {
"targets": {
"node": "6.10"
}
}]
],
"plugins": ["transform-object-rest-spread", "transform-export-extensions"]
}
2 changes: 2 additions & 0 deletions packages/core/botpress-util-sdk/.npmignore
@@ -0,0 +1,2 @@
.npmignore
src
Empty file.
32 changes: 32 additions & 0 deletions packages/core/botpress-util-sdk/package.json
@@ -0,0 +1,32 @@
{
"name": "@botpress/util-sdk",
"version": "10.8.0",
"description": "The built-in stuff such as Content Elements, Content Renderers and Actions for Botpress X+",
"main": "./dist/index.js",
"repository": "https://github.com/botpress/botpress",
"author": "Botpress, Inc.",
"license": "AGPL-3.0",
"private": false,
"scripts": {
"watch": "babel src --out-dir dist --source-maps --watch",
"compile": "babel src --out-dir dist --source-maps",
"test": "jest --watch"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-export-extensions": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"jest": "^22.4.3"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"bluebird": "^3.5.1",
"joi": "^13.3.0",
"lodash": "^4.17.10",
"mime": "^2.3.1",
"moment": "^2.22.1"
}
}
1 change: 1 addition & 0 deletions packages/core/botpress-util-sdk/src/index.js
@@ -0,0 +1 @@
export * as Skill from './skill'
3 changes: 3 additions & 0 deletions packages/core/botpress-util-sdk/src/index.test.js
@@ -0,0 +1,3 @@
test('Skills', () => {
// expect(obj).toBe(true)
})
76 changes: 76 additions & 0 deletions packages/core/botpress-util-sdk/src/skill.js
@@ -0,0 +1,76 @@
import _ from 'lodash'

const genId = () =>
Math.random()
.toString()
.substr(2, 6)

const _renderElement = (type, args) => {
if (_.isString(args)) {
return `say ${type} ${args}`
} else {
return `say ${type} ${JSON.stringify(args)}`
}
}

export function Flow(config) {
const obj = { ...config }

if (!obj.version) {
obj.version = '0.0'
}

if (!obj.name || !obj.location) {
obj.name = obj.location = genId()
}

if (!obj.startNode) {
if (!obj.nodes || obj.nodes.length <= 0) {
throw new Error('Expected at least one node in `nodes`')
}

if (!obj.nodes.filter(n => n.name === 'entry').length) {
obj.startNode = obj.nodes[0].name
}

obj.startNode = 'entry'
}

return obj
}

export function Node(config) {
const obj = { ...config }

if (!obj.id) {
obj.id = genId()
}

if (!obj.onEnter) {
obj.onEnter = []
}

if (!obj.onReceive) {
obj.onReceive = null
}

if (!obj.next) {
obj.next = []
}

return obj
}

export function renderElement(...args) {
if (args.length === 1) {
return _renderElement('#text', args[0])
} else if (args.length === 2) {
return _renderElement(args[0], args[1])
} else {
throw new Error('Can only call say with one or two args')
}
}

export function runAction(name, args) {
return args ? `${name} ${JSON.stringify(args)}` : name
}

0 comments on commit 96d30a3

Please sign in to comment.