UNPKG

3.14 kBPlain TextView Raw
1#!/usr/bin/env node
2
3/*
4 * Jingo, wiki engine
5 * http://github.com/claudioc/jingo
6 *
7 * Copyright 2015 Claudio Cicali <claudio.cicali@gmail.com>
8 * Released under the MIT license
9 */
10var program = require('commander'),
11 tools = require('./lib/tools'),
12 config = require('./lib/config'),
13 http = require('http'),
14 fs = require('fs'),
15 os = require('os'),
16 semver = require('semver');
17
18global.Git = require('./lib/gitmech');
19
20program.version('1.5.0')
21 .option('-c, --config <path>', 'Specify the config file')
22 .option('-#, --hash-string <string>', 'Create an hash for a string')
23 .option('-l, --local', 'Listen on localhost only')
24 .option('-s, --sample-config', 'Dumps a config file template and exits')
25 .parse(process.argv);
26
27if (program.sampleConfig) {
28 console.log(config.sample());
29 process.exit(0);
30}
31
32if (program.hashString) {
33 console.log(tools.hashify(program.hashString));
34 process.exit(0);
35}
36
37if (!program.config || !fs.existsSync(program.config)) {
38 program.help();
39 process.exit(-1);
40}
41
42if (!config.load(program.config)) {
43 console.log("Error: " + config.getError());
44 process.exit(-1);
45}
46
47if (!config.validate()) {
48 console.log("Error: " + config.getError());
49 process.exit(-1);
50}
51
52var refspec = config.get("application").remote.split(/\s+/);
53
54Git.setup(config.get("application").git,
55 config.get("application").repository,
56 config.get("application").docSubdir,
57 refspec, function(err, version) {
58
59 if (err) {
60 console.log(err);
61 process.exit(-1);
62 }
63
64 if (os.platform() == "darwin" &&
65 !config.get("application").skipGitCheck &&
66 config.get("pages").title.fromFilename &&
67 !semver.satisfies(version, ">=1.8.5")) {
68 console.log("Your current setup uses the filename of the wiki page as the page title.");
69 console.log("Unfortunately this version of git (" + version + ".x) on OSX doesn't handle");
70 console.log("very well non ASCII characters used in filenames, therefore I rather not start.");
71 console.log("You can continue anyway, setting `application.skipGitCheck` to true in the");
72 console.log("config file but you should better upgrade your git. Thank you.");
73 process.exit(-1);
74 }
75
76 start();
77});
78
79function start() {
80
81 var app = require("./lib/app").initialize(config);
82
83 var listenAddr = process.env.NW_ADDR || "";
84 if (config.get("server").localOnly) {
85 listenAddr = "localhost";
86 }
87
88 http.createServer(app).listen(config.get("server").port, listenAddr, function() {
89 console.log((new Date()) + " - Jingo%sserver v%s listening on port %s", config.get("server").localOnly ? " (local) " : " ", program.version(), config.get("server").port);
90 });
91
92 if (config.get("application").pushInterval && refspec.length > 0) {
93 setInterval(function() {
94 Git.pull(function(err) {
95 if (err) {
96 console.log("Error: " + err);
97 } else {
98 Git.push(function(err) {
99 if (err) {
100 console.log("Error: " + err);
101 }
102 });
103 }
104 });
105 }, config.get("application").pushInterval * 1000);
106 }
107}