Skip to content

Commit

Permalink
support js module import
Browse files Browse the repository at this point in the history
  • Loading branch information
unwriter committed Aug 27, 2019
1 parent da21766 commit 1147b53
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs');
const path = require('path');
const stream = require('stream')
const minimist = require('minimist')
const requireStr = require('require-from-string');

// Implement log stream - must define _read() callback or errs
const log = new stream.Readable()
Expand Down Expand Up @@ -38,9 +39,15 @@ const validate = function(config, vmode) {

const loadAndValidateConfig = async function(options) {
const configs = await new Promise(function(resolve, reject) {
if (options.jsonconfig) {
console.log("EVENTCHAIN", "Using JSON config")
let config = JSON.parse(options.jsonconfig)
if (options.strconfig) {
let config;
if (options.strconfig.startsWith("module.exports")) {
console.log("EVENTCHAIN", "Using JavaScript config")
config = requireStr(options.strconfig)
} else {
console.log("EVENTCHAIN", "Using JSON config")
config = JSON.parse(options.strconfig)
}
resolve([config])
} else if (options.config) {
console.log("EVENTCHAIN", "Loading config:", options.config)
Expand Down Expand Up @@ -104,7 +111,7 @@ const start = async function(options) {
log.push("ONSTART " + Date.now() + " " + JSON.stringify(e) + "\n")
},
};
if (options && options.tape) gene.tape = options.tape;
if (options && options.dest) gene.tape = options.dest;
if (gene.tape) {
chaindir = path.resolve(process.cwd(), gene.tape, "eventchain")
} else {
Expand All @@ -117,10 +124,10 @@ if (require.main === module && process.argv.length > 2) {
const cmd = process.argv[2].toLowerCase();
const opts = minimist(process.argv.slice(3), {
alias: {
config: 'c',
jsonconfig: 'j',
fileconfig: 'f',
strconfig: 's',
pipe: 'p',
tape: 't',
dest: 'd',
},
boolean: ['pipe']
});
Expand Down
50 changes: 18 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "echain",
"version": "0.0.39",
"version": "0.0.42",
"description": "",
"main": "index.js",
"author": "[email protected]",
Expand All @@ -11,6 +11,7 @@
"dependencies": {
"glob": "^7.1.4",
"minimist": "^1.2.0",
"neonplanaria": "0.0.39"
"neonplanaria": "0.0.39",
"require-from-string": "^2.0.2"
}
}

0 comments on commit 1147b53

Please sign in to comment.