UNPKG

btnexus-node

Version:
155 lines (131 loc) 5.14 kB
/** * Blackout Technologies Nexus, Hook data abstract * @author Marc Fiedler, Kejsi Sali * @copyright 2019 Blackout Technologies */ // Use Strict mode ECMA Script 5+ "use_strict"; // Load local libraries const fs = require('fs'); const archiver = require('archiver'); const findRoot = require('find-root'); const mkdirp = require('mkdirp'); const chalk = require('chalk'); const sizeOf = require('image-size'); // get current root directory var root = findRoot(process.cwd()); // utility function function isDirectory(item) { try { var stat = fs.lstatSync(item); return stat.isDirectory(); } catch (e) { console.dir(e); return e; } } // create destination folder mkdirp(process.env.HOME+'/.btnexus'); // read current config const pck = JSON.parse(fs.readFileSync(root+"/package.json", 'utf-8')); if( pck.type != undefined ){ var flavor = ""; if( pck.type == "web-app" ){ root = root+"/dist"; flavor = "-"+fs.readFileSync(root+"/flavor", 'utf-8'); } // check icon size, if one is set if( pck.icon != undefined ){ // check image dimensions try{ var dimensions = sizeOf(pck.icon); if( dimensions.width != 256 || dimensions.height != 256 ){ console.log(chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+ ": Icon dimensions must be 256x256 puxels." ); // end here if the image has the wrong dimensions return; } }catch(err){ console.log(chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+ ": Icon "+pck.icon+" not found or invalid file format." ); // end here if the image does not exist return; } } // enforce replacement of default values if( pck.author == undefined || pck.author.name == undefined || pck.author.url == undefined || pck.author.email == undefined || pck.author.name == "<your name>" || pck.author.url == "<your URL>" || pck.author.email == "<your email>" ){ console.log(chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+ ": Please fill out all author fields (name, url, email)" ); // end here if defaults are not changed return; } // create a file to stream archive data to. const outFileName = pck.name+flavor+"-"+pck.version+".pck"; // check if file does already exist // also check if the btNexus type field was set const outFile = process.env.HOME+"/.btnexus/"+outFileName; try { // console.log("Checking if file exists"); if (fs.existsSync(outFile)) { console.log(chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+": Package "+chalk.yellow(outFileName)+" already exists."); process.exit(-1); }else{ // file does not exist, continue } } catch(err) { // file does not exist, continue } var output = fs.createWriteStream(outFile); var archive = archiver('zip', { zlib: { level: 9 } // Sets the compression level. }); // list of files to be excluded const exclude = ["package-lock.json", ".btnexusrc", ".git", "node_modules"] // listen for all archive data to be written // 'close' event is fired only when a file descriptor is involved output.on('close', () => { console.log( chalk.green("[")+chalk.blue("btNexus")+chalk.green("]")+ ": Packed "+chalk.yellow(outFileName)+" with "+chalk.blue(archive.pointer()) + ' total bytes'); }); // This event is fired when the data source is drained no matter what was the data source. // It is not part of this library but rather from the NodeJS Stream API. // @see: https://nodejs.org/api/stream.html#stream_event_end output.on('end', () => { console.log('Data has been drained'); }); // good practice to catch this error explicitly archive.on('error', (err) => { console.dir(err); }); // pipe archive data to the file archive.pipe(output); // read cwd (current working directory) var files = fs.readdirSync(root); for( var i in files ){ var item = files[i]; var isDir = isDirectory(root+'/'+item); // only select not excluded files if( exclude.indexOf(item) == -1 ){ if(isDir){ // append files from a sub-directory and naming it `new-subdir` within the archive archive.directory(root+'/'+item, item); }else{ archive.file(root+'/'+item, { name: item }); } } } // finalize the archive (ie we are done appending files but streams have to finish yet) // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand archive.finalize(); }else{ console.log(chalk.green("[")+chalk.red("btNexus")+chalk.green("]")+ ": Package "+ " has no btBundle type. Please add a type field with a valid btBundle string." ); }