-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.ts
More file actions
195 lines (157 loc) · 5.72 KB
/
integration.ts
File metadata and controls
195 lines (157 loc) · 5.72 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
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
/* eslint-disable no-console */
import fs from 'fs';
import path, { resolve } from 'path';
import crypto from 'node:crypto';
import nodeFs from 'node:fs';
import slash from 'slash';
import { glob } from 'glob';
import { loadEnv } from 'vite';
import chalk from 'chalk';
interface CopyItem {
from: string;
to?: string;
}
interface FileExistCheck {
folder?: string;
fileName: string | RegExp;
}
export interface IntegrationOptions {
mode?: string;
}
export const runIntegration = (options: IntegrationOptions = {}) => {
const mode = options.mode ?? 'production';
const projectRoot = process.cwd();
const xpackEnv = loadEnv(mode, projectRoot);
const toAbsolute = (p: string) => slash(path.resolve(projectRoot, p));
const log = console.log.bind(console);
const hashes: Map<string, string> = new Map();
const staticBasePath = toAbsolute('dist/static');
const srcBasePath = toAbsolute('dist/static/assets');
const destBasePath = toAbsolute(xpackEnv.VITE_INTE_ASSET_DIR);
const patternPath = xpackEnv.VITE_INTE_PATTERN_DIR ? toAbsolute(xpackEnv.VITE_INTE_PATTERN_DIR) : undefined;
if (patternPath && fs.existsSync(patternPath)) {
fs.rmSync(patternPath, { recursive: true, force: true });
fs.mkdirSync(patternPath, { recursive: true });
}
const copyItems: CopyItem[] = [
{ from: 'css' },
{ from: 'fonts' },
{ from: 'images' },
{ from: 'js' },
{ from: 'vendors' },
{ from: 'hashes.json' },
{ from: 'pages', to: patternPath },
];
const hashItems: string[] = ['css', 'images', 'js'];
copyItems.forEach((item) => {
const srcPath = slash(path.join(srcBasePath, item.from));
const destPath = slash(item.to ?? path.join(destBasePath, item.from));
if (!fs.existsSync(srcPath)) {
return;
}
log(`Copy file ${srcPath} to ${destPath}`);
if (fs.statSync(srcPath).isDirectory()) {
if (fs.existsSync(destPath)) {
fs.rmSync(destPath, { recursive: true, force: true });
}
fs.mkdirSync(destPath, { recursive: true });
fs.cpSync(srcPath, destPath, { recursive: true, force: true });
} else {
const destDirPath = path.dirname(destPath);
if (!fs.existsSync(destDirPath)) {
fs.mkdirSync(destDirPath);
}
fs.copyFileSync(srcPath, destPath);
}
});
hashItems.forEach((item) => {
const srcPath = slash(path.join(srcBasePath, item));
const files = glob.sync(srcPath + '/**/*.{css,js,svg}');
files.forEach((file) => {
const relativePath = slash(file.substring(staticBasePath.length));
if (!/\.0x[a-z0-9_-]{8,12}\.\w+$/gi.test(file)) {
const content = fs.readFileSync(file);
const sha1Hash = crypto.createHash('sha1');
sha1Hash.update(content);
const hash = sha1Hash.digest('base64url').substring(0, 10);
hashes.set(relativePath, hash);
} else {
hashes.set(relativePath, '');
}
});
});
const sortedHashes = Array.from(hashes)
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce(
(obj, [key, value]) => {
obj[key] = value;
return obj;
},
{} as { [key: string]: string }
);
fs.writeFileSync(path.join(destBasePath, 'hashes.json'), JSON.stringify(sortedHashes, null, ' '));
if (patternPath) {
fs.mkdirSync(patternPath, { recursive: true });
glob.sync('./dist/static/{atoms,molecules,organisms,templates,pages}/**/*.*').forEach((p) => {
let basename = '';
const segments = slash(p).split('/');
if (segments.length < 4) return;
switch (segments.length) {
case 4:
basename = path.basename(slash(p).replaceAll(/(atoms|molecules|organisms|templates|pages)\/([\w._-]+)$/gi, '$1-$2'));
fs.copyFileSync(p, resolve(patternPath, basename));
break;
default:
segments.splice(0, 2);
nodeFs.cpSync(p, resolve(patternPath, segments.join('-')), { recursive: true });
break;
}
});
glob.sync(slash(path.resolve(patternPath + '/**/*.{htm,html}'))).forEach((p) => {
const text = fs.readFileSync(p, 'utf-8');
const newText = text
.replaceAll(/react-loader\.0x[a-z0-9_-]{8,12}\.js/gi, 'react-loader.0x00000000.js')
.replaceAll(/\.svg\?v=[a-z0-9_-]+/gi, '.svg');
if (text !== newText) {
fs.writeFileSync(p, newText);
}
});
}
const checkExistFileList: FileExistCheck[] = [
{ fileName: 'hashes.json' },
{ fileName: /react-loader\.0x[a-z0-9_-]{8,12}\.js/gi, folder: 'js' },
{ fileName: 'main.js', folder: 'js' },
];
let isAllExist = true;
checkExistFileList.forEach((file) => {
if (typeof file.fileName === 'string') {
const destPath = slash(path.join(destBasePath, file.folder ?? '', file.fileName.toString()));
if (!fs.existsSync(destPath)) {
log(chalk.yellow(`Cannot find: ${destPath}`));
isAllExist = false;
}
} else {
const fileName = file.fileName;
const folderFiles = slash(path.join(destBasePath, file.folder ?? ''));
const files = fs.readdirSync(folderFiles);
const found = files.find((f) => fileName.test(f));
if (!found) {
log(chalk.yellow(`Cannot find: ${slash(path.join(folderFiles, fileName.toString()))}`));
isAllExist = false;
}
}
});
if (!isAllExist) {
process.exit(1);
}
}; // end runIntegration
// Direct execution support
const isDirectRun = process.argv[1]?.endsWith('integration.js') || process.argv[1]?.endsWith('integration.ts');
if (isDirectRun) {
const argvModeIndex = process.argv.indexOf('--mode');
const mode =
argvModeIndex >= 0 && argvModeIndex < process.argv.length - 1 && !process.argv[argvModeIndex + 1].startsWith('-')
? process.argv[argvModeIndex + 1]
: 'production';
runIntegration({ mode });
}