-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·179 lines (159 loc) · 5.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
// Parsing app arguments
require("yargonaut")
.helpStyle('green')
global.VERSION = `LouveSystems' Parchment v1.1`;
const { existsSync, writeFileSync } = require("fs");
const path = require('path');
const yargs = require('yargs');
const argv = yargs
.command('adduser <login> <clear_password>', 'Adds an user to the wiki',{
})
.command('deluser <login>', 'Removes an user from the wiki',{
})
.command('run', 'Runs the wiki',{
})
.command('version', 'Prints the Parchment version number',{
})
.option('debuglvl', {
alias: 'd',
description: 'Sets the debug level for this instance',
type: 'string',
})
.option('port', {
alias: 'p',
description: 'Sets the HTTP port at which the wiki will be displayed',
type: 'number',
})
.option('logport', {
alias: 'l',
description: 'Sets the HTTP port at which the console will be displayed',
type: 'number',
})
.option('pemfile', {
description: 'Path to the permission file for git',
type: 'string',
})
.option('usersfile', {
description: 'Path to the users database TXT',
type: 'string',
})
.option('repo', {
description: 'URL for the work repository',
type: 'string',
})
.option('branch', {
description: 'Branch for the work repository - default is auto',
type: 'string',
})
.option('path', {
description: 'Path for the application - should stay default at all times',
type: 'string',
})
.option('color', {
description: 'Color for the wiki (#AABBCC format)',
type: 'string',
})
.option('name', {
description: 'Name for the wiki',
type: 'string',
})
.option('wikipath', {
description: 'Path for the wiki',
type: 'string',
})
.usage(`\n==========================\nWelcome to ${global.VERSION}!\n==========================\n\nUsage: $0 <command> [options]`)
.example("node app run")
.help()
.alias('help', 'h')
.wrap(yargs.terminalWidth())
.demandCommand()
.epilogue("louve.systems@2022")
.argv;
/////////////////////////////////
//
// Setting up ENV variables and main components
//
global.EXECUTION_ROOT = argv.path || process.cwd();
global.APPLICATION_ROOT = path.resolve(__dirname);
process.env = require("./app/env.js")(argv);
global.WIKI_PATH = process.env.WIKI_PATH
global.logger = require("./app/logger.js")
const meta = loadMetafile();
global.WIKI_COLOR = argv.color || meta.color;
global.WIKI_NAME = argv.name || meta.name || process.env.GIT_REPO_URL.split("/").pop().replace(".git", "").toUpperCase()
global.WIKI_CONTENTS_DIRECTORY_NAME = "_contents";
const gitStarter = require("./app/git.js"); // Will become global.git
global.markdown = require("./app/markdown.js")
global.permissions = require("./app/permissions.js")
global.wikiMap = require("./app/map.js")
global.wikiPage = require("./app/page.js")
global.wikiContents = require('./app/content.js')
global.utility = require("./app/utility.js")
global.searchEngine = require("./app/search.js");
logger.debug(`Running in directory ${EXECUTION_ROOT} with application root ${APPLICATION_ROOT} and wiki path: ${WIKI_PATH}`)
logger.debug("Parchment currently running as OS user: "+require('os').userInfo().username)
//
////////////////////////////////////
if (argv._.includes('run')){
// Setup git
gitStarter().then((git)=>{
global.git = git
// Scan wiki
return wikiMap.updateTree()
})
.then(()=>{
// Starts the web app
const app = require('./app/express.js');
return app(process.env.PORT);
})
.then(()=>{
try{console.log(require("fs").readFileSync(path.join(APPLICATION_ROOT, "res/ready.txt")).toString())}catch(e){
logger.warning(`There seem to be a problem with the APPLICATION_ROOT (${APPLICATION_ROOT})`);
}
logger.info("Parchment ready!")
});
}
else if (argv._.includes('adduser')){
permissions.addUser(argv.login, argv.clear_password)
permissions.writePermissions()
logger.info("User "+argv.login+" was succesfully added. Shutting down.")
process.exit(0)
}
else if (argv._.includes('deluser')){
permissions.destroyUser(argv.login)
permissions.writePermissions()
logger.info("User "+argv.login+" was successfully removed from the user list. Shutting down.")
process.exit(0)
}
else if (argv._.includes('version')){
logger.info(`${VERSION}`);
process.exit(0)
}
else {
logger.error("Nothing to do.")
process.exit(0)
}
function loadMetafile()
{
const defaultMeta = {
name: null,
color: "#AAAACC"
};
if (existsSync(WIKI_PATH))
{
const metaPath = path.join(WIKI_PATH, "meta.json");
if (existsSync(metaPath))
{
// good !
return require(metaPath);
}
else
{
// Sample meta.json file
writeFileSync(metaPath, JSON.stringify(defaultMeta));
logger.info(`Created new meta file at ${metaPath}`);
}
}
return defaultMeta;
}