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 | */
|
10 | var program = require('commander')
|
11 | var tools = require('./lib/tools')
|
12 | var config = require('./lib/config')
|
13 | var http = require('http')
|
14 | var fs = require('fs')
|
15 | var os = require('os')
|
16 | var semver = require('semver')
|
17 | var pkg = require('./package')
|
18 |
|
19 | global.Git = require('./lib/gitmech')
|
20 |
|
21 | program.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 |
|
28 | if (program.sampleConfig) {
|
29 | console.log(config.sample())
|
30 | process.exit(0)
|
31 | }
|
32 |
|
33 | if (program.hashString) {
|
34 | console.log(tools.hashify(program.hashString))
|
35 | process.exit(0)
|
36 | }
|
37 |
|
38 | if (!program.config || !fs.existsSync(program.config)) {
|
39 | program.help()
|
40 | process.exit(-1)
|
41 | }
|
42 |
|
43 | if (!config.load(program.config)) {
|
44 | console.log('Error: ' + config.getError())
|
45 | process.exit(-1)
|
46 | }
|
47 |
|
48 | if (!config.validate()) {
|
49 | console.log('Error: ' + config.getError())
|
50 | process.exit(-1)
|
51 | }
|
52 |
|
53 | var refspec = config.get('application').remote.split(/\s+/)
|
54 |
|
55 | Git.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 |
|
79 | function 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 | }
|