forked from adrianvlupu/C4-Builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
156 lines (133 loc) · 5.61 KB
/
cli.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
const figlet = require('figlet');
const program = require('commander');
const package = require('./package.json');
const chalk = require('chalk');
const path = require('path');
const Configstore = require('configstore');
const cmdHelp = require('./cli.help');
const cmdNewProject = require('./cli.new');
const cmdList = require('./cli.list');
const cmdSite = require('./cli.site');
const cmdCollect = require('./cli.collect');
const { build } = require('./build');
const watch = require('node-watch');
const {
clearConsole
} = require('./utils.js');
const updateNotifier = require('update-notifier');
const intro = () => {
console.log(chalk.blue(figlet.textSync('c4builder')));
console.log(chalk.gray('Blow up your software documentation writing skills'));
};
const getOptions = conf => {
return {
PLANTUML_VERSION: conf.get('plantumlVersion'),
GENERATE_MD: conf.get('generateMD'),
GENERATE_PDF: conf.get('generatePDF'),
GENERATE_WEBSITE: conf.get('generateWEB'),
GENERATE_COMPLETE_MD_FILE: conf.get('generateCompleteMD'),
GENERATE_COMPLETE_PDF_FILE: conf.get('generateCompletePDF'),
GENERATE_LOCAL_IMAGES: conf.get('generateLocalImages'),
ROOT_FOLDER: conf.get('rootFolder'),
DIST_FOLDER: conf.get('distFolder'),
PROJECT_NAME: conf.get('projectName'),
REPO_NAME: conf.get('repoUrl'),
HOMEPAGE_NAME: conf.get('homepageName'),
WEB_THEME: conf.get('webTheme'),
INCLUDE_NAVIGATION: conf.get('includeNavigation'),
INCLUDE_BREADCRUMBS: conf.get('includeBreadcrumbs'),
INCLUDE_TABLE_OF_CONTENTS: conf.get('includeTableOfContents'),
INCLUDE_LINK_TO_DIAGRAM: conf.get('includeLinkToDiagram'),
PDF_CSS: conf.get('pdfCss') || path.join(__dirname, 'pdf.css'),
DIAGRAMS_ON_TOP: conf.get('diagramsOnTop'),
CHARSET: conf.get('charset'),
WEB_PORT: conf.get('webPort'),
HAS_RUN: conf.get('hasRun'),
MD_FILE_NAME: 'README',
WEB_FILE_NAME: 'HOME',
DIAGRAM_FORMAT: 'svg'
}
};
module.exports = async () => {
updateNotifier({ pkg: package }).notify();
program
.version(package.version)
.option('new', 'create a new project from template')
.option('config', 'change configuration for the current directory')
.option('list', 'display the current configuration')
.option('reset', 'clear all configuration')
.option('site', 'serve the generated site')
.option('-w, --watch', 'watch for changes and rebuild')
.option('docs', 'a brief explanation for the available configuration options')
.option('-p, --port <n>', 'port used for serving the generated site', parseInt)
.parse(process.argv);
let conf = { get: () => { } };
if (!program.new)
conf = new Configstore(process.cwd().split(path.sep).splice(1).join('_'), {}, { configPath: path.join(process.cwd(), '.c4builder') });
if (program.docs)
return cmdHelp();
//initial options
let options = getOptions(conf);
if (program.new || program.config || !options.HAS_RUN)
clearConsole();
intro();
if (!options.HAS_RUN && !program.new) {
console.log(`\nif you created the project using the 'c4model new' command you can just press enter and go with the default options to get a basic idea of how it works.\n`);
console.log(`you can always change the configuration by running > c4builder config\n`);
}
if (program.new)
return cmdNewProject();
if (program.list)
return cmdList(options);
if (program.reset) {
conf.clear();
console.log(`configuration was reset`);
return;
}
await cmdCollect(options, conf, program);
if (!program.config) {
conf.set('hasRun', true);
let isBuilding = false;
let attemptedWatchBuild = false;
//get options after wizard
options = getOptions(conf);
if (program.watch) {
//watch warning
if (options.GENERATE_PDF ||
options.GENERATE_COMPLETE_PDF_FILE ||
options.GENERATE_LOCAL_IMAGES) {
console.log(chalk.bold(chalk.yellow('\nWARNING:')));
console.log(chalk.bold(chalk.yellow('Rebuilding with pdf or local image generation enabled will take a long time')));
}
watch(options.ROOT_FOLDER, { recursive: true }, async (evt, name) => {
// clearConsole();
// intro();
console.log(chalk.gray(`\n${name} changed. Rebuilding...`));
if (isBuilding) {
attemptedWatchBuild = true;
if (options.GENERATE_PDF ||
options.GENERATE_COMPLETE_PDF_FILE ||
options.GENERATE_LOCAL_IMAGES)
console.log(chalk.bold(chalk.yellow('Build already in progress, consider disabling pdf or local image generation ')));
return;
}
isBuilding = true;
await build(options);
while (attemptedWatchBuild) {
attemptedWatchBuild = false;
await build(options);
}
isBuilding = false;
});
}
isBuilding = true;
await build(options);
isBuilding = false;
if (program.site)
return await cmdSite(options, program);
if (options.GENERATE_WEBSITE && !program.watch) {
console.log(chalk.gray('\nto view the generated website run'));
console.log(`> c4builder site`);
}
}
};