r3-v2/gulpfile.js

233 lines
4.6 KiB
JavaScript

const {series, parallel, src, dest, watch} = require('gulp');
const clean = require('gulp-clean');
const stringReplace = require('gulp-string-replace');
const webpack_stream = require('webpack-stream');
const webpack_config = require('./webpack.config.js');
const map = require('map-stream');
const execSync = require('child_process').execSync;
// gulp.task('buildEvents', buildEvents);
console.log('current working dir : ' + process.cwd());
const paths = {
source : './src/r3/**/*.js',
testSource : './test/*.js',
web : './dist',
node : './dist/r3-node'
};
function replace() {
return stringReplace('__DATE__', new Date().toString());
}
let update = false;
function clearBuild() {
return src(
paths.node,
{
allowEmpty: true
}
).pipe(clean());
}
function build() {
return src(paths.source)
.pipe(webpack_stream( webpack_config ))
.pipe(replace())
.pipe(dest(paths.web));
}
function buildNode() {
return src(paths.source)
.pipe(replace())
.pipe(dest(paths.node));
}
//
// function logMatches(regex) {
// return map(function(file, done) {
//
// //console.log(file);
//
// let contents = file.contents.toString();
//
// let m;
//
// do {
// m = regex.exec(contents);
// if (m) {
// console.log(m[0]);
// }
// } while (m);
// let matches = [...contents.matchAll(regex)];
// if (matches) {
// matches.map(
// function(match) {
// console.log(match[0]);
// }
// )
// }
// file.contents.toString().match(regex).forEach(function(match) {
// console.log(file.path + ': ' + match);
// });
// done(null, file);
// });
// }
// function buildEvents() {
//
// return gulp.src(
// [
// paths.source,
// paths.testSource
// ]
// ).pipe(logMatches(/Event\.([A-Z]+_*){2,}/g));
// $files = scandir('.', SCANDIR_SORT_DESCENDING);
//
// $events = [];
//
// foreach ($files as $file) {
//
//
// if (
// preg_match('/\.js$/', $file) &&
// !preg_match('/r3\-a\-2\-event/', $file)
// ) {
// echo "processing file " . $file . "\n";
//
// $fn = fopen($file, "r");
//
// while (!feof($fn)) {
// $line = fgets($fn);
//
//
// if (
// preg_match('/R3.Event\./', $line) &&
// !preg_match('/Emit|Subscribe|.call|GetEventName|Async|prototype/', $line)
// ) {
// $matches = [];
// preg_match_all('/(R3.Event\..*?)(\s+|,|;|$|\))/', $line, $matches);
//
// if ($matches[1] && $matches[1][0]) {
//
// $event = $matches[1][0];
//
// if (in_array($event, $events)) {
// // Do nothing
// } else {
// array_push($events, $event);
// }
//
// }
//
//
// }
//
//
// }
//
// fclose($fn);
// }
//
//
// }
//
// array_push($events, 'R3.Event.START');
// array_push($events, 'R3.Event.PAUSE');
// array_push($events, 'R3.Event.RESTART');
//
// sort($events);
//
// $i = 1;
//
// $eventList = '';
//
// $eventFunction = "/**\n * R3.Event.GetEventName\n * @param eventId\n * @returns {string}\n * @constructor\n */\nR3.Event.GetEventName = function(eventId) {\n\n\tswitch(eventId) {\n";
//
// foreach ($events as $event) {
// $eventList .= $event . " = " . "0x" . dechex($i) . ";\n";
//
// $eventFunction .= "\t\tcase 0x" . dechex($i). " : return '" . strtolower(str_replace('R3.Event.', '', $event)) . "';\n";
//
// $i++;
// }
//
// $eventList .= "R3.Event.MAX_EVENTS = " . "0x" . dechex($i) . ";\n\n";
//
// $eventFunction .= "\t\tdefault :\n\t\t\tthrow new Error('Event type not defined : ' + eventId);\n";
// $eventFunction .= "\t}\n\n";
// $eventFunction .= "};\n";
//
// echo $eventList;
// echo $eventFunction;
//
// file_put_contents('r3-a-2-event-1.js', $eventList . $eventFunction);
//
// }
const updateFile = function(path) {
return function(cb) {
if (!update) {
console.error('updating ' + path);
update=true
execSync('./update_options.php ' + path)
} else {
update = false;
console.error('not updating ' + path);
}
cb();
}
}
function monitor() {
const watcher = watch(
[paths.source],
series([
clearBuild,
parallel(build, buildNode)
])
);
watcher.on(
'add',
function (path, stats) {
series([
clearBuild,
parallel(build, buildNode)
])();
}
);
watcher.on(
'unlink',
function (path, stats) {
series([
clearBuild,
parallel(build, buildNode)
])();
}
);
watcher.on(
'change',
function (path, stats) {
series([
clearBuild,
parallel(build, buildNode)
])();
}
);
}
exports.build = series([clearBuild, parallel(build, buildNode)]);
exports.default = series([clearBuild, parallel(build, buildNode), monitor]);