-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.utf-fix.js
66 lines (53 loc) · 1.71 KB
/
script.utf-fix.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
const fs = require('node:fs');
const path = require('node:path');
const weblog = require('webpack-log');
const logger = weblog({ name: 'utf-fix' });
const ENV = require('./app.env');
const UTILS = require('./webpack.utils');
const VERBOSE = ENV.ARGV.verbose;
const statMessages = { fixed: 0, skipped: 0 };
const stripWhitespaces = (string) => {
let result = string;
if (result.charCodeAt(0) === 0xfeff) {
result = result.slice(1);
}
result = result
.replace(/\r\n/g, '\n')
.replace(/\t/g, ' ')
.replace(/[ \t]+\n/g, '\n');
return result;
};
const files = UTILS.globArraySync(
[
path.join(__dirname, '**/*.htaccess'),
path.join(__dirname, '.fonts-subsets'),
path.join(__dirname, '.*ignore'),
path.join(__dirname, '*.{js,json}'),
`${ENV.SOURCE_PATH}/**/*.{html,svg,js,json,css,scss,njk}`,
],
{
ignore: [`${ENV.OUTPUT_PATH}/**/*`],
dot: true,
nodir: true,
}
);
logger.info(`${files.length} files\n`);
files.forEach((resourcePath) => {
const resourceStat = fs.lstatSync(resourcePath);
if (!resourceStat.isFile()) return;
const relativePath = UTILS.slash(path.relative(__dirname, resourcePath));
const source = fs.readFileSync(resourcePath, 'utf8').toString();
const fixedSource = stripWhitespaces(source.normalize('NFC'));
if (fixedSource === source) {
statMessages.skipped += 1;
if (VERBOSE) {
logger.info(`skiped ${relativePath}`);
}
} else {
statMessages.fixed += 1;
fs.writeFileSync(resourcePath, fixedSource);
logger.info(`fixed ${relativePath}`);
}
});
console.log('');
logger.info('stats:', statMessages);