-
Notifications
You must be signed in to change notification settings - Fork 45
/
gulpfile.js
194 lines (160 loc) · 7.24 KB
/
gulpfile.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
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
const gulp = require('gulp');
const fs = require('fs');
const cp = require('child_process');
const src = `${__dirname}/src/`;
function deleteFoldersRecursive(path, exceptions) {
if (fs.existsSync(path)) {
const files = fs.readdirSync(path);
for (const file of files) {
const curPath = `${path}/${file}`;
if (exceptions && exceptions.find(e => curPath.endsWith(e))) {
continue;
}
const stat = fs.statSync(curPath);
if (stat.isDirectory()) {
deleteFoldersRecursive(curPath);
fs.rmdirSync(curPath);
} else {
fs.unlinkSync(curPath);
}
}
}
}
function npmInstallRules() {
return new Promise((resolve, reject) => {
// Install node modules
const cwd = src.replace(/\\/g, '/');
const cmd = `npm install -f`;
console.log(`"${cmd} in ${cwd}`);
// System call used for update of js-controller itself,
// because during installation npm packet will be deleted too, but some files must be loaded even during the install process.
const exec = cp.exec;
const child = exec(cmd, {cwd});
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
reject(`Cannot install: ${code}`);
} else {
console.log(`"${cmd} in ${cwd} finished.`);
// command succeeded
resolve();
}
});
});
}
function buildRules() {
const version = JSON.parse(fs.readFileSync(__dirname + '/package.json').toString('utf8')).version;
const data = JSON.parse(fs.readFileSync(src + 'package.json').toString('utf8'));
data.version = version;
fs.writeFileSync(src + 'package.json', JSON.stringify(data, null, 4));
return new Promise((resolve, reject) => {
const options = {
stdio: 'pipe',
cwd: src
};
console.log(options.cwd);
let script = `${src}node_modules/@craco/craco/dist/bin/craco.js`;
if (!fs.existsSync(script)) {
script = `${__dirname}/node_modules/@craco/craco/dist/bin/craco.js`;
}
if (!fs.existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
const child = cp.fork(script, ['build'], options);
child.stdout.on('data', data => console.log(data.toString()));
child.stderr.on('data', data => console.log(data.toString()));
child.on('close', code => {
console.log(`child process exited with code ${code}`);
code ? reject(`Exit code: ${code}`) : resolve();
});
}
});
}
gulp.task('rules-0-clean', done => {
deleteFoldersRecursive(`${__dirname}/admin/rules`);
deleteFoldersRecursive(`${__dirname}/src/build`);
done();
});
gulp.task('rules-1-npm', async () => npmInstallRules());
gulp.task('rules-2-compile', async () => buildRules());
gulp.task('rules-3-copy', () => Promise.all([
gulp.src(['src/build/*.js']).pipe(gulp.dest('admin/rules')),
gulp.src(['src/build/*.map']).pipe(gulp.dest('admin/rules')),
gulp.src(['src/build/asset-manifest.json']).pipe(gulp.dest('admin/rules')),
gulp.src(['src/build/static/**/*', '!src/build/static/media/*.svg', '!src/build/static/media/*.txt', '!src/build/static/js/vendors*.js', '!src/build/static/js/vendors*.map']).pipe(gulp.dest('admin/rules/static')),
gulp.src(['src/src/i18n/*.json']).pipe(gulp.dest('admin/rules/i18n')),
]));
gulp.task('rules-build', gulp.series(['rules-0-clean', 'rules-1-npm', 'rules-2-compile', 'rules-3-copy']));
const srcAdmin = `${__dirname}/src-admin/`;
function npmInstallAdmin() {
return new Promise((resolve, reject) => {
// Install node modules
const cwd = srcAdmin.replace(/\\/g, '/');
const cmd = `npm install -f`;
console.log(`"${cmd} in ${cwd}`);
// System call used for update of js-controller itself,
// because during installation npm packet will be deleted too, but some files must be loaded even during the install process.
const exec = cp.exec;
const child = exec(cmd, {cwd});
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
reject(`Cannot install: ${code}`);
} else {
console.log(`"${cmd} in ${cwd} finished.`);
// command succeeded
resolve();
}
});
});
}
function buildAdmin() {
const version = JSON.parse(fs.readFileSync(`${__dirname}/package.json`).toString('utf8')).version;
const data = JSON.parse(fs.readFileSync(`${srcAdmin}package.json`).toString('utf8'));
data.version = version;
fs.writeFileSync(`${srcAdmin}package.json`, JSON.stringify(data, null, 4));
return new Promise((resolve, reject) => {
const options = {
stdio: 'pipe',
cwd: srcAdmin
};
console.log(options.cwd);
let script = `${srcAdmin}node_modules/@craco/craco/dist/bin/craco.js`;
if (!fs.existsSync(script)) {
script = `${__dirname}/node_modules/@craco/craco/dist/bin/craco.js`;
}
if (!fs.existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
const child = cp.fork(script, ['build'], options);
child.stdout.on('data', data => console.log(data.toString()));
child.stderr.on('data', data => console.log(data.toString()));
child.on('close', code => {
console.log(`child process exited with code ${code}`);
code ? reject(`Exit code: ${code}`) : resolve();
});
}
});
}
gulp.task('admin-0-clean', done => {
deleteFoldersRecursive(`${__dirname}/admin/custom`);
deleteFoldersRecursive(`${__dirname}/src-admin/build`);
done();
});
gulp.task('admin-1-npm', async () => npmInstallAdmin());
gulp.task('admin-2-compile', async () => buildAdmin());
gulp.task('admin-3-copy', () => Promise.all([
gulp.src(['src-admin/build/static/js/*.js', '!src-admin/build/static/js/vendors*.js']).pipe(gulp.dest('admin/custom/static/js')),
gulp.src(['src-admin/build/static/js/*.map', '!src-admin/build/static/js/vendors*.map']).pipe(gulp.dest('admin/custom/static/js')),
gulp.src(['src-admin/build/customComponents.js']).pipe(gulp.dest('admin/custom')),
gulp.src(['src-admin/build/customComponents.js.map']).pipe(gulp.dest('admin/custom')),
gulp.src(['src-admin/src/i18n/*.json']).pipe(gulp.dest('admin/custom/i18n')),
]));
gulp.task('admin-build', gulp.series(['admin-0-clean', 'admin-1-npm', 'admin-2-compile', 'admin-3-copy']));
gulp.task('default', gulp.series(['admin-build', 'rules-build']));