botbuilder-dialog-loader
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -0,8 +1,25 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
module.exports = function (bot, basePath, config) { | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
config = "object" == typeof config ? config : {}; | ||
let loadJsFiles = function ( basePath, config ) { | ||
const getSubMapper = function ( baseMapper, subItem) { | ||
let isFunction = "function" === typeof baseMapper; | ||
if ( isFunction) { | ||
return baseMapper; | ||
} else { | ||
return "undefined" != typeof baseMapper[subItem] ? baseMapper[subItem] : []; | ||
} | ||
} | ||
const loadFile = function (path, mapper,bot,relativePath ) { | ||
let contents = require( path ); | ||
if ( "object" == typeof mapper) { | ||
if ( "function" == typeof contents ) { | ||
let arguments = [bot].concat(mapper); | ||
contents.apply( null, arguments); | ||
} | ||
} else { | ||
mapper.call(this,bot, relativePath, contents); | ||
} | ||
} | ||
const loadJsFiles = function ( basePath, mapper, relativePath ) { | ||
let files = fs.readdirSync( basePath ); | ||
@@ -12,16 +29,10 @@ files.forEach( function ( src ) { | ||
let parse = path.parse( basePath + src); | ||
let subConfig = "undefined" != typeof config[parse.name] ? config[parse.name] : []; | ||
let newMapper = getSubMapper( mapper, parse.name); | ||
if ( stat.isDirectory() ){ | ||
loadJsFiles( basePath + src + '/', subConfig ); | ||
loadJsFiles( basePath + src + '/', newMapper, relativePath + src + '/'); | ||
} else { | ||
let pathInfo = path.parse( basePath + src, subConfig ); | ||
if ( '.js' == pathInfo.ext ) { | ||
let callback = require( basePath + src ); | ||
let arguments = [bot].concat(subConfig); | ||
if ( "function" == typeof callback ) { | ||
callback.apply( null, arguments); | ||
} | ||
let pathInfo = path.parse( basePath + src ); | ||
let isExtensionSupported = ['.js','.json'].indexOf( pathInfo.ext) > -1; | ||
if ( isExtensionSupported ) { | ||
loadFile(basePath + src, newMapper,bot,relativePath + pathInfo.name); | ||
} | ||
@@ -31,3 +42,10 @@ } | ||
} | ||
loadJsFiles( basePath, config ); | ||
let isValid = ( "object" == typeof config ) || ( "function" == typeof config ); | ||
if (!isValid ) { | ||
config = {}; | ||
} | ||
loadJsFiles( basePath, config,'/' ); | ||
} |
@@ -7,3 +7,4 @@ const botbuilder = require('botbuilder'); | ||
let bot = new botbuilder.UniversalBot(connector); | ||
let basePath = path.dirname(__filename) + '/base-dialogs/'; | ||
const FACTORIES_PATH = path.dirname(__filename) + '/dialog-factories/'; | ||
const CONFIGURATIONS_PATH = path.dirname(__filename) + '/dialog-configs/'; | ||
@@ -13,6 +14,13 @@ bot.dialog('/', function ( session ) { | ||
}) | ||
loader( bot, basePath, { | ||
loader( bot, FACTORIES_PATH, { | ||
"welcome" : ["DialogLoader"] | ||
}); | ||
loader( bot, CONFIGURATIONS_PATH,function ( bot, path, response ) { | ||
console.log(path); | ||
bot.dialog(path,function (session ) { | ||
response.forEach(item => session.send(item)); | ||
session.endDialog(); | ||
}); | ||
}) | ||
console.log('Press any key...'); |
@@ -6,3 +6,3 @@ { | ||
"name": "botbuilder-dialog-loader", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "A Library that helps to autload Microsoft Bot Framework dialogs", | ||
@@ -9,0 +9,0 @@ "main": "botbuilder-dialog-loader.js", |
@@ -13,71 +13,20 @@ # botbuilder-dialog-loader | ||
. **basePath** - a path to dialogs folder; | ||
. **config** - (optional) a configuration object. Every object's key reflects to | ||
a folder or to a concrete javascript file (without extension). Key's value | ||
should contain or an object (in case of folder) or an array of arguments, | ||
that will be passed to thefile. | ||
## Configuration object Example: | ||
```javascript | ||
{ | ||
"folderA" : { | ||
"folderB" : { | ||
"some_dialogs" : [arg1, ..., argN] // A | ||
} | ||
}, | ||
"welcome" : ["Hello", "Wolrd!"] | ||
} | ||
``` | ||
. **config** - (optional) could be a _function_ or an object. | ||
. If function passed, than the Library will call it with every loaded javascript module. Callback arguments: relative filename, _exports_ section of the loaded module; | ||
. If an object passed, than the Library will interprete keys in the object as names of folders and files. Attribute values will be passed as an arguments to loaded files, in case if filename (with directory name) matches to a key i _config_ object. Example of such object: | ||
```javascript | ||
{ | ||
"folderA" : { | ||
"folderB" : { | ||
"some_dialogs" : [arg1, ..., argN] // A | ||
} | ||
}, | ||
"welcome" : ["Hello", "Wolrd!"] | ||
} | ||
``` | ||
The Library **will ignore** any loaded file if it will not return a callback | ||
# Usage Example | ||
1. Create a folder - `/src/dialogs`; | ||
2. Create a file - `/src/dialogs/welcome.js`; | ||
3. Place next code into the `welcome.js`: | ||
```javascript | ||
module.exports = function (bot,name) { | ||
bot.dialog('/welcome', [function (session) { | ||
session.send(`My name is - ${name}`) | ||
session.beginDialog('/askForName') | ||
}, | ||
function (session, results) { | ||
session.endDialog(`Hi, ${results.response}!`); | ||
setTimeout( function () { | ||
process.exit() | ||
},1000); | ||
} | ||
]); | ||
} | ||
``` | ||
4. Create a file - `/src/dialogs/askForName.js`; | ||
5. Place next code into the `askForName.js`: | ||
```javascript | ||
const botbuilder = require('botbuilder'); | ||
Please take a look on **example** folder in repository. | ||
module.exports = function (bot) { | ||
bot.dialog('/askForName', [function (session) { | ||
botbuilder.Prompts.text(session, 'What is your name?'); | ||
}]); | ||
} | ||
``` | ||
6. Create `index.js`, a Code: | ||
```javascript | ||
const botbuilder = require('botbuilder'); | ||
const path = require('path'); | ||
const loader = require('botbuilder-dialog-loader'); | ||
let connector = new botbuilder.ConsoleConnector().listen(); | ||
let bot = new botbuilder.UniversalBot(connector); | ||
let basePath = path.dirname(__filename) + '/src/dialogs/'; | ||
bot.dialog('/', function ( session ) { | ||
session.beginDialog( '/welcome'); | ||
}) | ||
loader( bot, basePath, { | ||
"welcome" : ["DialogLoader"] | ||
}); | ||
console.log('Press any key...'); | ||
``` | ||
# Installation | ||
@@ -84,0 +33,0 @@ |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
41095
10
100
36