-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (99 loc) · 4.4 KB
/
index.js
File metadata and controls
129 lines (99 loc) · 4.4 KB
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
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
// built-in modules
const path = require('path');
// declared dependencies
const fse = require('fs-extra');
// local modules
const engine = require('./tools/main'),
{ asyncForEach } = require('./tools/helpers'),
initFolders = require('./tools/initFolders'),
// Define src folders - should move to .env folder
srcHTLFolder = './test/templates/',
srcSpecFolder = './test/specs/',
jsOutputFolder = './jsoutput/';
const { addCarriageReturn } = require('./tools/helpers');
const jsdom = require('jsdom'),
{ JSDOM } = jsdom,
// Full HTL section
executeHtlParser = async () => {
initFolders();
fse.readdir(srcHTLFolder, async (err, files) => {
if (err) {
console.error('Could not list the directory.', err);
process.exit(1);
}
await asyncForEach(files, async (file) => {
// The reason to delete is to allow hot reloading to also rebuild components
let filename = path.join(__dirname, srcHTLFolder, file),
fileshorthtml = file.replace('.htl', '.html'),
resourceFile = file.replace('.htl', '.data.js'),
resourceFileFullPath = srcSpecFolder + resourceFile;
delete require.cache[require.resolve(resourceFileFullPath)];
const resource = await require(resourceFileFullPath),
template = await fse.readFile(filename, 'utf-8');
fse.stat(filename, async (error, stat) => {
if (error) {
console.error('Error stating file.', error);
process.exit(1);
}
if (stat.isFile()) {
await parseEachHTl(filename, fileshorthtml, resource, template, resourceFile);
}
else if (stat.isDirectory())
{console.log("'%s' is a directory to be skipped.", filename);}
});
});
});
},
parseEachHTl = async (filename, fileshorthtml, resource, template, resourceFile) => {
console.log("'%s' is a template to be processed.", filename);
let ret = await engine(resource, template, resourceFile),
dom = new JSDOM(ret.body);
const filenameOut = path.resolve(process.cwd(), './generated_html/' + fileshorthtml);
// Need to also load in clientlib JS and CSS as described in clientlib.config.js
for (let componentName in global.fullObj) {
// Only load the component if it has been called in the HTL
const checkElement = dom.window.document.getElementById('load-component-' + componentName);
if (checkElement === null) {
continue;
}
dom = addCssToHead (dom, componentName);
dom = addJsToBody (dom, componentName);
}
const modifiedBody = dom.serialize();
fse.writeFile(filenameOut, modifiedBody, 'utf-8');
// Remove generated javascript files
fse.unlinkSync(jsOutputFolder + resourceFile);
// Optional output html to console
// console.log(ret.body);
},
addCssToHead = (dom, componentName) => {
let style = dom.window.document.createElement('link');
style.rel = 'stylesheet';
style.href = '/test/components/' + componentName + '/dev/' + componentName + '.css';
dom.window.document.head.appendChild(style);
// Add line feed for easier reading
dom.window.document = addCarriageReturn(dom.window.document, 'head');
return dom;
},
addJsToBody = (dom, componentName) => {
let script = dom.window.document.createElement('script');
script.type = 'text/javascript';
script.src = '/test/components/' + componentName + '/dev/' + componentName + '.js';
dom.window.document.body.appendChild(script);
// Add line feed for easier reading
dom.window.document = addCarriageReturn(dom.window.document, 'body');
return dom;
};
module.exports = executeHtlParser;
executeHtlParser();