UNPKG

3.3 kBPlain TextView Raw
1#!/usr/bin/env node
2
3/*
4 * Jingo, wiki engine
5 * http://github.com/claudioc/jingo
6 *
7 * Copyright 2013-2017 Claudio Cicali <claudio.cicali@gmail.com>
8 * Released under the MIT license
9 */
10var program = require('commander')
11var tools = require('./lib/tools')
12var config = require('./lib/config')
13var http = require('http')
14var fs = require('fs')
15var os = require('os')
16var semver = require('semver')
17var pkg = require('./package')
18
19global.Git = require('./lib/gitmech')
20
21program.version(pkg.version)
22 .option('-c, --config <path>', 'Specify the config file')
23 .option('-#, --hash-string <string>', 'Create an hash for a string')
24 .option('-l, --local', 'Listen on localhost only')
25 .option('-s, --sample-config', 'Dumps a config file template and exits')
26 .parse(process.argv)
27
28if (program.sampleConfig) {
29 console.log(config.sample())
30 process.exit(0)
31}
32
33if (program.hashString) {
34 console.log(tools.hashify(program.hashString))
35 process.exit(0)
36}
37
38if (!program.config || !fs.existsSync(program.config)) {
39 program.help()
40 process.exit(-1)
41}
42
43if (!config.load(program.config)) {
44 console.log('Error: ' + config.getError())
45 process.exit(-1)
46}
47
48if (!config.validate()) {
49 console.log('Error: ' + config.getError())
50 process.exit(-1)
51}
52
53var refspec = config.get('application').remote.split(/\s+/)
54
55Git.setup(config.get('application').git,
56 config.get('application').repository,
57 config.get('application').docSubdir,
58 refspec, function (err, version) {
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 var app = require('./lib/app').initialize(config)
81
82 var listenAddr = process.env.NW_ADDR || ''
83 if (config.get('server').localOnly) {
84 listenAddr = 'localhost'
85 }
86
87 http.createServer(app).listen(config.get('server').port, listenAddr, function () {
88 console.log((new Date()) + ' - Jingo%sserver v%s listening on port %s', config.get('server').localOnly ? ' (local) ' : ' ', program.version(), config.get('server').port)
89 })
90
91 if (config.get('application').pushInterval && refspec.length > 0) {
92 setInterval(function () {
93 Git.pull(function (err) {
94 if (err) {
95 console.log('Error: ' + err)
96 } else {
97 Git.push(function (err) {
98 if (err) {
99 console.log('Error: ' + err)
100 }
101 })
102 }
103 })
104 }, config.get('application').pushInterval * 1000)
105 }
106}