-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
142 lines (129 loc) · 4.01 KB
/
build.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
const version = require('./package.json').version;
const exec = require('child_process').execSync;
const util = require('util');
const rimraf = util.promisify(require('rimraf'));
const copyFile = require('fs').copyFileSync;
const unlink = require('fs').unlinkSync;
const mv = util.promisify(require('mv'));
const { readdirSync } = require('fs');
const usedBlocks = [
'hero',
'gallery-masonry',
'dynamic-separator',
'gallery-stacked',
'pricing-table',
'click-to-tweet',
];
const filesToRemove = [
'includes/class-coblocks-accordion-ie-support.php',
'includes/class-coblocks-form.php',
'includes/class-coblocks-google-map-block.php',
'includes/class-coblocks-block-settings.php',
'includes/get-dynamic-blocks.php',
'dist/js/coblocks-accordion-polyfill.min.js',
'dist/js/coblocks-google-maps.min.js',
'dist/js/coblocks-google-recaptcha.min.js',
'dist/js/vendors/flickity.min.js',
'dist/js/vendors/lity.min.js',
'dist/css/coblocks-getting-started.css',
'dist/css/coblocks-getting-started.min.css',
'dist/images/social/email.svg',
'dist/images/social/facebook.svg',
'dist/images/social/google.svg',
'dist/images/social/linkedin.svg',
'dist/images/social/pinterest.svg',
'dist/images/social/reddit.svg',
'dist/images/social/tumblr.svg',
];
const directoriesToRemove = [
'includes/admin',
'src',
'dist/images/admin',
'dist/images/map',
'dist/images/markers',
];
build();
async function build() {
await removeDirectory('./coblocks');
await removeDirectory('./build');
cloneRelease(version);
removedUnusedBlocks(usedBlocks);
copy('./blocks.js', './coblocks/src/blocks.js');
copy('./class-coblocks.php', './coblocks/class-coblocks.php');
runCoBlocksBuild();
await moveDirectory('./coblocks/build/coblocks', `./build/${version}`);
await removeDirectory('./coblocks');
cleanBuild();
}
function removedUnusedBlocks(usedBlocks) {
console.log(`Removing unused block src`);
const path = './coblocks/src/blocks';
readdirSync(path, { withFileTypes: true })
.filter(directoryItem => directoryItem.isDirectory())
.map(directoryItem => directoryItem.name)
.forEach(dir => {
if (!usedBlocks.includes(dir)) {
removeDirectory(`${path}/${dir}`);
}
});
removeDirectory('./coblocks/src/sidebars/block-manager');
removeDirectory('./coblocks/src/extensions/typography');
}
function removeDirectory(directory) {
console.log(`Removing directory ${directory}`);
try {
return rimraf(directory);
} catch (error) {
console.log(`There was an error removing ${directory}: ${error}`);
}
}
function cloneRelease(version) {
console.log(`Cloning the CoBlocks branch for release ${version}`);
try {
exec(`git clone --branch ${version} https://github.com/godaddy/coblocks.git`);
} catch (error) {
console.log(`There was an error cloning the CoBlocks repo: ${error}`);
}
}
function copy(source, destination) {
console.log(`Copying ${source} to ${destination}`);
try {
copyFile(source, destination, () => console.log('success'));
} catch (error) {
console.log(`There was an error copying ${source} to ${destination}`);
}
}
function removeFile(file) {
console.log(`Removing ${file}`);
try {
unlink(file, () => console.log('success'));
} catch (error) {
console.log(`There was an error removing ${file}`);
}
}
function moveDirectory(source, destination) {
console.log(`Moving ${source} to ${destination}`);
try {
return mv(source, destination, { mkdirp: true });
} catch (error) {
console.log(`There was an error moving ${source} to ${destination}`);
}
}
function runCoBlocksBuild() {
console.log('Running CoBlocks build ...');
try {
exec('cd coblocks && npm install && grunt build', {
maxBuffer: 1024 * 2000,
});
} catch (error) {
console.log(`There was an error running the CoBlocks build: ${error}`);
}
}
function cleanBuild() {
directoriesToRemove.forEach(directory => removeDirectory(`./build/${version}/${directory}`));
filesToRemove.forEach(file => removeFile(`./build/${version}/${file}`));
copy(
'./class-coblocks-register-blocks.php',
`./build/${version}/includes/class-coblocks-register-blocks.php`
);
}