This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstage-library-ts.js
102 lines (84 loc) · 2.8 KB
/
stage-library-ts.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
/*jshint node: true*/
'use strict';
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const sass = require('node-sass');
const tildeImporter = require('node-sass-tilde-importer');
const skyPagesConfigUtil = require('../../config/sky-pages/sky-pages.config');
const spaPathTempSrc = skyPagesConfigUtil.spaPathTempSrc();
function copySource() {
fs.copySync(
skyPagesConfigUtil.spaPath('src', 'app', 'public'),
skyPagesConfigUtil.spaPathTemp()
);
}
function deleteNonDistFiles() {
let files = glob.sync(`${spaPathTempSrc}/**/*.spec.ts`);
files.forEach(file => fs.removeSync(file));
}
function inlineHtmlCss() {
const templateUrlRegEx = /templateUrl\:\s*'(.+?\.html)'/gi;
const styleUrlsRegEx = /styleUrls\:\s*\[\s*'(.+?\.scss)']/gi;
let files = glob.sync(`${spaPathTempSrc}/**/*.ts`);
files.forEach((file) => {
let fileContents = fs.readFileSync(file, { encoding: 'utf8' });
let dirname = path.dirname(file);
let matches;
// templateUrl
matches = templateUrlRegEx.exec(fileContents);
while (matches) {
let requireFile = path.join(dirname, matches[1]);
let requireContents = getFileContents(requireFile);
requireContents = `template: ${requireContents}`;
fileContents = fileContents.replace(matches[0], requireContents);
matches = templateUrlRegEx.exec(fileContents);
// Since we're changing the file contents in each iteration and since the regex is stateful
// we need to reset the regex; otherwise it might not be able to locate subsequent matches
// after the first replacement.
templateUrlRegEx.lastIndex = 0;
}
// styleUrls
matches = styleUrlsRegEx.exec(fileContents);
while (matches) {
let requireFile = path.join(dirname, matches[1]);
let requireContents = getFileContents(requireFile);
requireContents = `styles: [${requireContents}]`;
fileContents = fileContents.replace(matches[0], requireContents);
styleUrlsRegEx.lastIndex = 0;
matches = styleUrlsRegEx.exec(fileContents);
}
fs.writeFileSync(file, fileContents, { encoding: 'utf8' });
});
}
function getFileContents(filePath) {
let contents = '';
switch (path.extname(filePath)) {
case '.scss':
contents = compileSass(filePath);
break;
case '.html':
contents = getHtmlContents(filePath);
break;
}
contents = contents
.toString()
.replace(/\\f/g, '\\\\f')
.replace(/`/g, '\\`');
return '`' + contents + '`';
}
function getHtmlContents(filePath) {
return fs.readFileSync(filePath).toString();
}
function compileSass(filePath) {
return sass.renderSync({
file: filePath,
importer: tildeImporter,
outputStyle: 'compressed'
}).css;
}
module.exports = () => {
copySource();
deleteNonDistFiles();
inlineHtmlCss();
};