-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
234 lines (212 loc) · 7.31 KB
/
page.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2024 Joseph P Medley
'use strict';
const fs = require('fs');
const { help } = require('./help/help.js');
const { Questions } = require('./questions.js');
const utils = require('./utils.js');
const path = require('path');
const TOKEN_RE = /\[\[(?:shared:)?([\w\-]+)\]\]/;
function pageFactory(name, type, sharedQuestions, options) {
if (type === 'includes') { type = 'interface'; }
if (!utils.haveTemplate(type)) {
const apiName = `${sharedQuestions.questions.interface.answer}.${name}`;
const msg = `Cannot find a template for page named ${apiName} with type ${type}.`;
throw new Error(msg, __filename, 26);
}
switch (type) {
case 'interface':
return new _InterfacePage(name, type, sharedQuestions, options);
default:
return new _Page(name, type, sharedQuestions, options);
}
}
class _PageBase {
constructor(name, type, sharedQuestions, options) {
this.name = name;
this._type = type;
this.sharedQuestions = sharedQuestions;
this._interfaceData = options.interfaceData
this._outPath = this._resolveOutPath(options.root);
// The type and name if the interface are also a question.
this.sharedQuestions.add(type, name);
let introMessage = `\nQuestions for the ${this.name} ${this._type} page\n` + (`-`.repeat(80)) + help[this._type] + '\n';
this.questions = new Questions(introMessage);
this.questions.add(type, name);
this.contents = utils.getTemplate(type);
this._processQuestions(this.contents);
}
_processQuestions(inText) {
const reg = RegExp(TOKEN_RE, 'g');
let matches;
while ((matches = reg.exec(inText)) != null) {
if (matches[0].startsWith('[[shared:')) {
this.sharedQuestions.add(matches[1]);
} else {
this.questions.add(matches[1]);
}
}
}
_resolveOutPath(root) {
if (!root) {
// Backward compatibility
root = utils.makeOutputFolder(this.sharedQuestions.name);
}
return root;
}
get type() {
return this._type;
}
async askQuestions(extraMessage) {
if (this.sharedQuestions.needsAnswers()) {
// extraMessage is proxy for whether this is a first call or
// one generated by an action. Not ideal, but it's the best
// currently available.
if (extraMessage) {
this.sharedQuestions.introMessage = "More shared questions found.\n" + (`-`.repeat(28))
}
await this._askQuestions(this.sharedQuestions);
}
if (this.questions.needsAnswers()) {
if (extraMessage) {
let len = extraMessage.length;
extraMessage = extraMessage + '\n' + (`-`.repeat(len));
this.questions.introMessage = extraMessage;
}
await this._askQuestions(this.questions);
}
}
async _askQuestions(questionObject) {
const questions = questionObject.questions;
if (questionObject.needsAnswers()) {
questionObject.printIntro();
} else {
return;
}
for (let q in questions) {
if (questions[q].answer) { continue; }
await questions[q].ask(this);
}
}
render() {
const reg = RegExp(TOKEN_RE);
let matches;
let answer;
while ((matches = reg.exec(this.contents)) != null) {
if (matches[0].startsWith('[[shared:')) {
answer = this.sharedQuestions.questions[matches[1]].answer;
} else {
answer = this.questions.questions[matches[1]].answer
}
if (answer === null) { answer = ''; }
this.contents = this.contents.replace(matches[0], answer);
}
}
async write(overwrite = 'prompt') {
this.render();
let outDir;
let outPath;
let msg;
switch (this._type) {
case 'landing':
outDir = this.sharedQuestions.name.toLowerCase();
outDir = `${outDir}_${this._type}`;
break;
case 'interface':
outDir = `${this.name}`.toLowerCase();
break;
default:
outDir = this.sharedQuestions.interface.toLowerCase();
outDir = path.join(outDir, `${this.name.toLowerCase()}`);
break;
}
outDir = path.join(this._outPath, outDir);
switch (overwrite) {
case 'never':
if (fs.existsSync(outDir)) { return; }
outDir = utils.makeFolder(outDir);
outPath = path.join(`${outDir}`, 'index.md');
fs.writeFileSync(outPath, this.contents);
msg = `\nA page has been written to\n\t${outPath}\n`;
utils.sendUserOutput(msg);
break;
case 'always':
outDir = utils.makeFolder(outDir);
outPath = path.join(`${outDir}`, 'index.md');
fs.writeFileSync(outPath, this.contents);
msg = `\nA page has been written to\n\t${outPath}\n`;
utils.sendUserOutput(msg);
break;
case 'prompt':
outPath = path.join(`${outDir}`, 'index.md');
if (fs.existsSync(outPath)) {
msg = `\nA file already exits at:\n\t${outPath}\n\n`;
msg += 'Do you want to overwrite it?'
const answer = await utils.confirm(msg);
if (!answer) {
// Attractive message spacing.
utils.sendUserOutput();
return;
}
}
outDir = utils.makeFolder(outDir);
outPath = path.join(`${outDir}`, 'index.md');
fs.writeFileSync(outPath, this.contents);
msg = `\nA page has been written to\n\t${outPath}\n`;
utils.sendUserOutput(msg);
break;
}
}
}
class _InterfacePage extends _PageBase {
constructor(name, type, sharedQuestions, options) {
super(name, type, sharedQuestions, options);
this._addMembers();
}
_addMembers() {
let template = utils.getFile(`templates/_frag_events.md`);
this._processQuestions(template);
const eventHandlers = this._interfaceData.eventHandlers;
if (eventHandlers.length) {
let handlersString = '### Event handlers\n\n';
eventHandlers.forEach(eh => {
let templateCopy = (' ' + template).slice(1);
const eventPage = eh.name.slice(2) + '_event';
templateCopy = templateCopy.replace('[[event]]', eventPage);
handlersString += `${templateCopy}\n`;
});
this.questions.answer('events', handlersString.trim());
}
template = utils.getFile(`templates/_frag_methods.md`);
this._processQuestions(template);
const methods = this._interfaceData.methods;
if (methods.length) {
let methodString = '## Methods\n\n';
methods.forEach(m => {
let templateCopy = (' ' + template).slice(1);
templateCopy = templateCopy.replace('[[method]]', m.name);
methodString += `${templateCopy}\n`;
});
this.questions.answer('methods', methodString.trim());
}
template = utils.getFile(`templates/_frag_properties.md`);
this._processQuestions(template);
const properties = this._interfaceData.properties;
if (properties.length) {
let propertyString = '## Properties\n\n';
properties.forEach(p => {
let templateCopy = (' ' + template).slice(1);
templateCopy = templateCopy.replace('[[property]]', p.name);
propertyString += `${templateCopy}\n`;
});
this.questions.answer('properties', propertyString.trim());
}
}
}
class _Page extends _PageBase {
constructor(name, type, sharedQuestions, options) {
super(name, type, sharedQuestions, options);
}
}
module.exports.pageFactory = pageFactory;
module.exports.InterfacePage = _InterfacePage;
module.exports.Page = _Page;