UNPKG

iitcp

Version:

IITC Plugin creator and developer tools

69 lines (65 loc) 2.07 kB
const fs = require('fs'); const async = require('async'); const path = require('path'); module.exports = function(iitcp_config) { return new Promise((resolve, reject) => { iitcp_config = iitcp_config || JSON.parse(fs.readFileSync(process.cwd() + '/iitcp_config.json')); const source = iitcp_config.path + '/code/', destination = `${iitcp_config.path}/build/${iitcp_config.dir}.user.js`; fs.readFile(iitcp_config.path + '/header/header.js', (err, hData) => { if (err) return reject(err); const header = hData; fs.readdir(source, (err, files) => { if (err) return reject(err); files = files.map(file => path.join(source, file)); //Read all files in parallel async.map(files, fs.readFile, (err, results) => { if (err) return reject(err); //Write the joined results to destination const code = iitcp_config.GM_sandbox ? header + '\n\n' + results.join('\n') + ` (function iitc_is_loaded() { if (unsafeWindow.iitcLoaded) { setup(); } else { setTimeout(iitc_is_loaded, 100); } })(); ` : `${header} function wrapper(plugin_info) { ${results.join('\n')} setup.info = plugin_info; window.bootPlugins = window.bootPlugins || []; window.bootPlugins.push(setup); if (window.iitcLoaded && typeof setup === 'function') setup(); } var script = document.createElement('script'); var info = {}; if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) { info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description }; } var textContent = document.createTextNode( '(' + wrapper + ')(' + JSON.stringify(info) + ')' ); script.appendChild(textContent); (document.body || document.head || document.documentElement).appendChild(script); `; fs.writeFile(destination, code, err => { if (err) return reject(err); resolve(); }); }); }); }); }); };