/
generator.js
295 lines (233 loc) · 8.4 KB
/
generator.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict'
const {promisify} = require('util')
const fs = require('fs')
const readFilePromised = promisify(fs.readFile)
const writeFilePromised = promisify(fs.writeFile)
const path = require('upath')
const rimraf = require('rimraf')
const chalk = require('chalk')
const console = require('../helpers/color-console')
const sfs = require('../helpers/sfs')
const {installWatcher, installDevelopmentWatcher} = require('../helpers/watcher')
const {build, createAndGetBuildFolder, getAbsolutePageUrl} = require('./builder')
const loader = require('./loader')
const {assetsToCopy} = require('../assets')
const config = require('../config')
// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------
async function generate (entryPoint) {
console.log(`Starting directory: ${chalk.green(entryPoint)}`)
cleanBuildFolder()
const pages = findData(entryPoint)
if (countDataFiles(pages) === 0) {
console.log('')
console.warn('No data files found!')
return
}
if (!pages.company) {
console.log('')
console.error('No company data file found!')
return
}
await generateHTML(pages)
await exportAdditionalAssets()
console.log('')
console.log('Done! 👌')
if (config.commands.build.watch) {
const port = config.commands.build.port
const address = chalk.green(`http://localhost:${port}/`)
showHeader(`Watching and serving files from ${address}`)
startWatcher(entryPoint, () => generateHTML(findData(entryPoint)))
}
}
function cleanBuildFolder () {
if (!config.commands.build.cleanBuildFolder) return
showHeader('Cleaning build folder')
rimraf.sync(createAndGetBuildFolder())
}
function findData (entryPoint) {
showHeader('Finding data')
const pages = {products: []}
// Inspect given path for data files and get all correct ones.
let files = sfs.findAllFiles(entryPoint, { maxDepth: 1, ignoredFolders: ['build', 'node_modules'] })
files.filter(isDataFile).forEach(file => {
try {
let presskit = loader.loadDataFile(file)
if (!presskit) return
console.log(`- ${presskit.type}: "${presskit.title}" ${chalk.dim(file)}`)
// presskit() compatibility support:
// If we find a "game" file at the top-level, it's probably a company.
const isTopLevel = path.relative(path.dirname(file), entryPoint) === ''
if (isTopLevel && presskit.type !== 'company') {
console.warn('This top-level `data.xml` root tag should be `<company>`, not `<game>`! ')
console.warn('If this is a legacy presskit(), please check the migration guide:')
console.log(chalk.blue('https://github.com/pixelnest/presskit.html#migration-guide'))
console.log('')
presskit.type = 'company'
}
if (presskit.type === 'company') {
if (pages.company) {
console.warn('Multiple companies detected. This is not supported yet, only the last will be used!')
}
pages.company = { path: entryPoint, presskit: presskit }
} else {
pages.products.push({ path: file, presskit: presskit })
}
} catch (err) {
console.error(`There was an error during the parsing of \`${file}\`. Check that your XML document is valid.`)
console.error('You can use a validator like this: ' + chalk.blue('http://codebeautify.org/xmlvalidator'))
process.exit()
}
})
return pages
}
async function generateHTML (pages) {
showHeader('Generating HTML')
const productsList = []
let analytics = ''
if (pages.company && pages.company.presskit.analytics) {
analytics = pages.company.presskit.analytics
}
// Create dependency relations between products.
// ie., if a product is a DLC of another, it will add
// this link between the two products.
addProductsRelations(pages.products)
for (const product of pages.products) {
const outputPath = await build(
product.path,
product.presskit,
{
company: pages.company.presskit,
analytics
}
)
productsList.push({path: outputPath, title: product.presskit.title})
}
// Company should be the last one, listing all the products
if (pages.company) {
// Notice that we provide a ref to all the products here.
await build(
pages.company.path,
pages.company.presskit,
{
products: productsList,
analytics
}
)
}
}
function addProductsRelations (products) {
// Find a <relations> tag in each product.
for (const currentProduct of products) {
const relations = currentProduct.presskit.relations
if (!relations) continue
// Erase the old relations object and make it an array for future use.
currentProduct.presskit.relations = []
// Then go through each relation and add the links between the
// current product and its target.
relations.forEach(relation => {
if (!relation.type || typeof relation.type !== 'string') return
const type = relation.type.trim()
const productId = cleanId(relation.product)
// Find the target of the relation (ONE product per relation).
const relatedProduct = products
.filter(x => x !== currentProduct)
.find(x => productId === cleanId(x.presskit.title))
// Stop if nothing.
if (!relatedProduct) return
// Then add the relations.
// On the current product:
currentProduct.presskit.relations.push(
createRelation(type, relatedProduct)
)
// And the target.
if (!relatedProduct.presskit.relationOf) {
relatedProduct.presskit.relationOf = []
}
relatedProduct.presskit.relationOf.push(createRelation(type, currentProduct))
})
}
function cleanId (x) {
if (!x || typeof x !== 'string') return ''
return x.trim().toLowerCase().replace(/\s/g, '')
}
// Helper to create a relation object.
function createRelation (type, product) {
return {
type,
text: product.presskit.title,
path: getAbsolutePageUrl(product.path, product.presskit.type)
}
}
}
async function exportAdditionalAssets () {
showHeader('Exporting assets')
// Copy assets (CSS and stuff)
await copyMandatoryFiles()
}
// Print a console UI header text.
function showHeader (text) {
console.log('')
console.log(chalk.magenta(text + '…'))
}
// Add css and any mandatory files specified to the build directory
async function copyMandatoryFiles () {
const buildDir = createAndGetBuildFolder()
for (const f of assetsToCopy) {
const filepath = path.resolve(f)
// Get filename and dirname from the provided path.
const filename = path.basename(filepath)
const dirname = path.basename(path.dirname(filepath))
// Create the directory for this file if needed.
// ie. css/master.css needs a `css` directory.
const targetDir = path.join(buildDir, dirname)
sfs.createDir(targetDir)
// And copy the file.
const targetPath = path.join(targetDir, filename)
console.log('- ' + targetPath)
try {
await writeFilePromised(targetPath, await readFilePromised(filepath))
} catch (e) {
const msg = chalk.dim(`(${e.message})`)
console.error(`There was an error while copying ${chalk.bold(filename)}. ${msg}`)
}
}
}
// Count the number of products/company in the presskit.
function countDataFiles (pages) {
let count = 0
count += (pages.company ? 1 : 0)
count += pages.products.length
return count
}
// Is a file qualifying to be a data file? ie. `data.xml`.
// We don't check the content here, just the name.
function isDataFile (filename) {
const ext = path.extname(filename)
const filenameWithoutExt = path.basename(filename, ext)
return filenameWithoutExt === 'data' && ext === '.xml'
}
// Start watching files and assets.
function startWatcher (entryPoint, callback) {
if (config.commands.build.dev) {
// Delete the assets (CSS/etc.) before.
// They might not be there, but we must ensure that they are properly
// deleted.
//
// This is necessary, otherwise, remaining copied CSS files will have
// an higher priority than the ones provided by the server (from
// the `assets/` folder).
rimraf(path.join(createAndGetBuildFolder(), 'css'), () => {
installDevelopmentWatcher(entryPoint, callback)
})
} else {
installWatcher(entryPoint, callback)
}
}
// -------------------------------------------------------------
// Exports.
// -------------------------------------------------------------
module.exports = {
generate
}