cocos-enhance-kit/engine/gulp/util/polyfill-node-promisify.js
2022-06-25 00:23:03 +08:00

45 lines
1.1 KiB
JavaScript

// Replace node promisify for testing editor-extends.
//
// Replace:
// require('util')
// As:
// { promisify }
const es = require('event-stream');
const stream = require('stream');
const promisify = (function (handle) {
return function (...args) {
return new Promise(function (resolve, reject) {
handle(...args, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
}).toString();
const REPLACE = /require\(['"]util['"]\)/g;
const AS = `{promisify:${promisify}}`;
module.exports = function (filename) {
if (filename.endsWith('.js') || filename.endsWith('.ts')) {
return es.map(function (data, callback) {
var content = data.toString();
if (REPLACE.test(content)) { // fast test
content = content.replace(REPLACE, AS);
data = new Buffer(content);
}
callback(null, data);
});
}
else {
return stream.PassThrough();
}
};