-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbuild-static-resources.js
162 lines (146 loc) · 4.69 KB
/
build-static-resources.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
const {promisify} = require('util');
const ncp = promisify(require('ncp'));
const fs = require('fs').promises;
const path = require('path');
const STATIC_RESOURCES_PATH = './force-app/main/default/staticresources';
const TEMP_DIR = '.tmp/quantic-compiled';
function resolveLibraryPath(packageName, relativePath) {
return path.resolve(path.dirname(require.resolve(packageName)), relativePath);
}
const LIBRARY_CONFIG = {
dompurify: {
directories: [`${STATIC_RESOURCES_PATH}/dompurify`],
files: [
{
src: resolveLibraryPath('dompurify', '../dist/purify.min.js'),
dest: `${STATIC_RESOURCES_PATH}/dompurify/purify.min.js`,
},
],
},
marked: {
directories: [`${STATIC_RESOURCES_PATH}/marked`],
files: [
{
src: resolveLibraryPath('marked', '../marked.min.js'),
dest: `${STATIC_RESOURCES_PATH}/marked/marked.min.js`,
},
],
},
bueno: {
directories: [
`${STATIC_RESOURCES_PATH}/coveobueno/browser`,
`${STATIC_RESOURCES_PATH}/coveobueno/definitions`,
],
files: [
{
src: resolveLibraryPath('@coveo/bueno', '../cdn/bueno.js'),
dest: `${STATIC_RESOURCES_PATH}/coveobueno/browser/bueno.js`,
},
{
src: resolveLibraryPath('@coveo/bueno', '../dist/definitions'),
dest: `${STATIC_RESOURCES_PATH}/coveobueno/definitions`,
},
],
},
headless: {
directories: [
`${STATIC_RESOURCES_PATH}/coveoheadless/case-assist`,
`${STATIC_RESOURCES_PATH}/coveoheadless/insight`,
`${STATIC_RESOURCES_PATH}/coveoheadless/recommendation`,
`${STATIC_RESOURCES_PATH}/coveoheadless/definitions`,
],
files: [
{
src: `${TEMP_DIR}/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/headless.js`,
},
{
src: `${TEMP_DIR}/case-assist/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/case-assist/headless.js`,
},
{
src: `${TEMP_DIR}/insight/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/insight/headless.js`,
},
{
src: `${TEMP_DIR}/recommendation/headless.js`,
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/recommendation/headless.js`,
},
{
src: resolveLibraryPath('@coveo/headless', '../dist/definitions'),
dest: `${STATIC_RESOURCES_PATH}/coveoheadless/definitions`,
},
],
},
};
async function getPackageVersion() {
const packageJsonPath = path.join(__dirname, 'package.json');
const packageData = await fs.readFile(packageJsonPath, 'utf8');
return JSON.parse(packageData).version;
}
async function copy(src, dest) {
try {
await ncp(src, dest);
} catch (error) {
console.error(`Failed to copy: ${src}\nError: ${error.message}`);
process.exit(1);
}
}
async function createDirectories(directories) {
try {
for (const dir of directories) {
await fs.mkdir(dir, {recursive: true});
}
} catch (error) {
console.error(`Failed to create directories\nError: ${error.message}`);
process.exit(1);
}
}
async function writeQuanticVersion(filePath) {
try {
const existingContent = await fs.readFile(filePath, 'utf8');
const version = await getPackageVersion();
const contentToAppend = `\nwindow.coveoQuanticVersion = '${version}';`;
const endsWithNewline = existingContent.endsWith('\n');
const newContent = endsWithNewline
? existingContent + contentToAppend
: existingContent + '\n' + contentToAppend;
await fs.writeFile(filePath, newContent, 'utf8');
console.info(`Quantic version written to ${filePath}`);
} catch (error) {
console.error(
`Failed to write Quantic version in: ${filePath}\nError: ${error.message}`
);
process.exit(1);
}
}
async function copyLibrary(config) {
if (config.directories) {
await createDirectories(config.directories);
}
for (const {src, dest} of config.files) {
await copy(src, dest);
}
}
async function main() {
console.info('Begin building static resources');
await copyLibrary(LIBRARY_CONFIG.dompurify);
console.info('DOMPurify copied.');
await copyLibrary(LIBRARY_CONFIG.marked);
console.info('Marked copied.');
await copyLibrary(LIBRARY_CONFIG.bueno);
console.info('Bueno copied.');
await copyLibrary(LIBRARY_CONFIG.headless);
await fs.rm(TEMP_DIR, {recursive: true});
console.info('Headless copied.');
LIBRARY_CONFIG.headless.files.forEach(async ({dest}) => {
if (dest.includes('headless.js')) {
await writeQuanticVersion(dest);
}
});
console.info('Headless augmented with Quantic version.');
console.info('All resources built successfully!');
}
main().catch((error) => {
console.error('An error occurred during the build process:', error);
});