Skip to content

Commit

Permalink
move installing and rendering responsibilities into jsreport package
Browse files Browse the repository at this point in the history
  • Loading branch information
pofider committed Nov 13, 2015
1 parent 94f8a5b commit 54fa201
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 283 deletions.
119 changes: 7 additions & 112 deletions index.js
@@ -1,114 +1,9 @@
/*!
* Copyright(c) 2014 Jan Blaha
*/

var path = require("path"),
extend = require("node.extend"),
fs = require("fs"),
path = require("path"),
Reporter = require("./lib/reporter.js");


function initializeApp(force) {


if (!fs.existsSync("server.js") || force) {
console.log("Creating server.js");
fs.writeFileSync("server.js", "require('jsreport').bootstrapper().start();");
}

if (!fs.existsSync("package.json") || force) {
console.log("Creating package.json");
var packageJson = {
"name": "jsreport-server",
"dependencies": {
"jsreport": "*"
},
"main": "server.js"
};
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2));
}

if (!fs.existsSync("prod.config.json") || force) {
console.log("Creating prod.config.json");
fs.writeFileSync("prod.config.json", fs.readFileSync(path.join(__dirname, "example.config.json")));
}

console.log("Initialized");
}


function init() {
initializeApp(false);
}

function repair() {
initializeApp(true);
}

var renderDefaults = {
connectionString: { name: "InMemory"},
dataDirectory: path.join(__dirname, "data"),
blobStorage: "inMemory",
cacheAvailableExtensions: true,
logger: { providerName: "dummy"},
rootDirectory: __dirname,
extensions: ["html", "templates", "data", "phantom-pdf", "jsrender", "handlebars"]
};

function ensureTempFolder() {
if (renderDefaults.tempDirectory) {
return;
}

renderDefaults.tempDirectory = path.join(require("os").tmpdir(), "jsreport");

try {
fs.mkdirSync(renderDefaults.tempDirectory);
} catch(e) {
if ( e.code !== 'EEXIST' ) throw e;
}
}

function start() {
return require("./lib/bootstrapper.js")(renderDefaults).start().then(function (b) {
return Reporter.instance;
});
var bootstrapper = require("./lib/bootstrapper.js");
module.exports = function(options) {
return bootstrapper(options).start().then(function(b) {
return b.reporter;
});
}

function render(req) {
if (!Reporter.instance) {
ensureTempFolder();

return start().then(function () {
return Reporter.instance.render(req);
});
}

return Reporter.instance.render(req);
}

function extendDefaults(config) {
return extend(true, renderDefaults, config);
}

if (require.main === module) {
//jsreport commandline support can precreate app...

require('commander')
.version(require("./package.json").version)
.usage('[options]')
.option('-i, --init', 'Initialize server.js, config.json and package.json of application and starts it. For windows also installs service.', init)
.option('-r, --repair', 'Recreate server.js, config.json and package.json of application to default.', repair)
.parse(process.argv);


} else {
module.exports.Reporter = require("./lib/reporter.js");
module.exports.bootstrapper = require("./lib/bootstrapper.js");
module.exports.renderDefaults = renderDefaults;
module.exports.render = render;
module.exports.start = start;
module.exports.extendDefaults = extendDefaults;
module.exports.reporter = Reporter.instance;
}
module.exports.Reporter = require("./lib/reporter.js");
module.exports.Bootstrapper = require("./lib/bootstrapper.js");
20 changes: 0 additions & 20 deletions lib/bootstrapper.js
Expand Up @@ -14,7 +14,6 @@ var path = require("path"),
fs = require("fs"),
q = require("q"),
Reporter = require("./reporter.js"),
commander = require("./reportingCommander.js"),
nconf = require('nconf'),
numCPUs = require('os').cpus().length,
Reaper = require("reap"),
Expand All @@ -35,9 +34,6 @@ Bootstrapper.prototype.start = function () {
var self = this;

return q().then(function () {
//apply command line arguments
commander(self.config);

self.config.dataDirectory = self.config.dataDirectory || path.join(self.config.rootDirectory, "data");
self.config.tempDirectory = self.config.tempDirectory || path.join(self.config.dataDirectory, "temp");
self.config.connectionString = self.config.connectionString || { name: "neDB"};
Expand All @@ -53,22 +49,6 @@ Bootstrapper.prototype.start = function () {

return self._startServer();
}).then(function() {
if (self.config.render) {

return self.reporter.render(extend(true, self.config.render, self.config.template || {})).then(function(resp) {
var wstream = fs.createWriteStream(self.config.output);
resp.stream.pipe(wstream);
wstream.on("close", function() {
process.nextTick(function() {
process.exit();
});
});
}).catch(function(e) {
console.log(e);
process.exit();
});
}

return self;
}).fail(function (e) {
console.log("error when bootstrapping jsreport");
Expand Down
151 changes: 0 additions & 151 deletions lib/reportingCommander.js

This file was deleted.

0 comments on commit 54fa201

Please sign in to comment.