-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.js
79 lines (66 loc) · 2.01 KB
/
main.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
const fs = require('fs');
const path = require('path');
const minify = require('minify');
const wpPot = require('wp-pot');
/**
* Minify JS and CSS files
*
* @param extension
*/
function minifyFiles (extension) {
const assetsFiles = findFilesInDir(`./assets/${extension}`, `.${extension}`, '/blocks');
const isNotMinifiedAndHasSelectedExtension = (filePath) => filePath.includes(`.${extension}`) && !filePath.includes('.min');
const filteredFiles = assetsFiles.filter((filePath) => isNotMinifiedAndHasSelectedExtension(filePath));
filteredFiles.forEach((file) => {
const filePath = path.resolve(`${file}`);
minify(filePath, { js: { ecma: 6 }, css: { compatibility: '*' } })
.then((minifiedContent) => {
const newFilePathName = filePath
.split(`.${extension}`)[0]
.concat(`.min.${extension}`);
fs.writeFileSync(newFilePathName, minifiedContent);
})
.catch(console.error);
});
}
/**
* Generate .pot files
*/
function generatePotFiles () {
wpPot({
domain: 'woocommerce-mercadopago',
destFile: './i18n/languages/woocommerce-mercadopago.pot',
lastTranslator: 'Mercado Pago Developers <[email protected]>',
src: ['src/**/*.php', 'templates/**/*.php']
});
}
/**
* Find a file by extension
*
* @param startPath
* @param filter
*
* @returns {*[]}
*/
function findFilesInDir (startPath, filter, excludes = '') {
let results = [];
if (!fs.existsSync(startPath)) {
console.error('no dir ', startPath);
return [];
}
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
if (filename.includes(excludes)) {
continue;
}
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
results = results.concat(findFilesInDir(filename, filter, excludes));
} else if (filename.indexOf(filter) >= 0) {
results.push(filename);
}
}
return results;
}
module.exports = { minifyFiles, generatePotFiles };