|
| 1 | +var shell = require('shelljs'), |
| 2 | + path = require('path'), |
| 3 | + async = require('async'), |
| 4 | + fs = require('fs'), |
| 5 | + exists, |
| 6 | + codegen, |
| 7 | + codegens, |
| 8 | + codegen_path, |
| 9 | + getSubfolders, |
| 10 | + individual_test, |
| 11 | + commandOut; |
| 12 | + |
| 13 | +const args = process.argv[2], |
| 14 | + PATH_TO_CODEGENS_FOLDER = path.resolve(__dirname, '../codegens'); |
| 15 | + |
| 16 | +// throw JS error when any shell.js command encounters an error |
| 17 | +shell.config.fatal = true; |
| 18 | + |
| 19 | +// ensure that the working tree is clean before packaging |
| 20 | +commandOut = shell.exec('source ./npm/package/require_clean_work_tree.sh'); |
| 21 | +if (commandOut.code !== 0) { |
| 22 | + shell.exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +getSubfolders = (folder) => { |
| 26 | + return fs.readdirSync(folder) |
| 27 | + .map((subfolder) => { return { path: path.join(folder, subfolder), name: subfolder}; }) |
| 28 | + .filter((obj) => { return fs.statSync(obj.path).isDirectory(); }); |
| 29 | +}; |
| 30 | + |
| 31 | +individual_test = (codegen) => { |
| 32 | + |
| 33 | + console.log(`Creating package for ${codegen}`); |
| 34 | + async.series([ |
| 35 | + function (next) { |
| 36 | + console.log(`Running codegen test for codegen ${codegen}`); |
| 37 | + commandOut = shell.exec(`npm run test ${codegen} --color always`); |
| 38 | + if (commandOut.code !== 0) { |
| 39 | + console.error(`Failed to run codegen test on codegen ${codegen}, here's the error:`); |
| 40 | + return next(commandOut.stderr); |
| 41 | + } |
| 42 | + console.log(commandOut.stdout); |
| 43 | + return next(); |
| 44 | + }, |
| 45 | + function (next) { |
| 46 | + console.log(`Generating zip for codegen ${codegen}`); |
| 47 | + commandOut = shell.exec(`npm run zip ${codegen} --color always`); |
| 48 | + if (commandOut.code !== 0) { |
| 49 | + console.error(`Failed to zip codegen ${codegen}, here's the error:`); |
| 50 | + return next(commandOut.stderr); |
| 51 | + } |
| 52 | + console.log(commandOut.stdout); |
| 53 | + } |
| 54 | + ], (err) => { |
| 55 | + console.error(err); |
| 56 | + shell.exit(1); |
| 57 | + }); |
| 58 | +}; |
| 59 | + |
| 60 | +if (args) { |
| 61 | + codegen = args; |
| 62 | + codegen_path = path.join(PATH_TO_CODEGENS_FOLDER, codegen); |
| 63 | + try { |
| 64 | + exists = fs.statSync(codegen_path).isDirectory(); |
| 65 | + } |
| 66 | + catch (err) { |
| 67 | + console.log(`Codegen ${codegen} doesn't exist, please enter a valid name`); |
| 68 | + console.log(err); |
| 69 | + shell.exit(1); |
| 70 | + } |
| 71 | + if (exists) { |
| 72 | + individual_test(codegen); |
| 73 | + } |
| 74 | +} |
| 75 | +else { |
| 76 | + console.log('Packaging all the codegens'); |
| 77 | + codegens = getSubfolders(PATH_TO_CODEGENS_FOLDER); |
| 78 | + codegens.forEach((codegen) => { |
| 79 | + individual_test(codegen.name); |
| 80 | + }); |
| 81 | +} |
0 commit comments