|
1 | | - |
2 | | -var argv = require('minimist')(process.argv.slice(2)); |
3 | | -var dirTree = require('dir-tree'); |
4 | | -var fs = require('fs-extra'); |
5 | | -var hotBuilder = require('hot-builder'); |
6 | | -var httpServer = require('http-server'); |
7 | | -var merge = require('merge'); |
8 | | -var path = require('path'); |
9 | | -var Promise = require('promise'); |
10 | | - |
11 | | -var TESTS_DIR = 'test'; |
12 | | -var RESULTS_DIR = 'benchmark/results'; |
13 | | -var SAMPLE_SIZE = 100; |
14 | | -var SERVER_HOST = 'localhost'; |
15 | | -var SERVER_PORT = '8080'; |
16 | | - |
17 | | -var BASE_URL = 'http://' + SERVER_HOST + ':' + SERVER_PORT + '/'; |
18 | | -var hotVersion = null; |
19 | | -var hotBranch = argv['hot-branch']; |
20 | | -var runServer = argv['server']; |
21 | | -var genMapFile = argv['gen-map-file']; |
22 | | - |
23 | | -if (runServer) { |
24 | | - console.log('Running HTTP server (http://' + SERVER_HOST + ':' + 8080 + ')...'); |
25 | | - runHttpServer(); |
26 | | - |
27 | | - return; |
| 1 | +const program = require('caporal'); |
| 2 | +const semver = require('semver'); |
| 3 | +const config = require('./config'); |
| 4 | +const { version: packageVersion, engines } = require('./../package'); |
| 5 | + |
| 6 | +const hotVersionRegExp = /^\d{1,3}\.\d{1,3}\.\d{1,3}$/; |
| 7 | + |
| 8 | +function parseArgs() { |
| 9 | + program |
| 10 | + .version(packageVersion) |
| 11 | + .description('JavaScript performance tests for Handsontable') |
| 12 | + .command('local-server', 'Run a local server ("test-runner" or "benchmark-viewer").') |
| 13 | + .argument('<app_to_serve>', 'Type of the application to serve ("test-runner" or "benchmark-viewer")', /^(test\-runner|benchmark\-viewer)$/, 'test-runner') |
| 14 | + .option('--hot-version <version>', 'The Handsontable <version> which will be used for running a benchmark.', hotVersionRegExp) |
| 15 | + .option('--hot-server <url>', 'The server <url> which will be used to serve Handsontable assets from.') |
| 16 | + .action((args, options) => { |
| 17 | + require('./commands/local-server')(args.appToServe, parseInt(config.SERVER_PORT, 10) + 1, options); |
| 18 | + }) |
| 19 | + .command('run', 'Run a benchmark.') |
| 20 | + .option('--hot-version <version>', 'The Handsontable <version> which will be used for running a benchmark.', hotVersionRegExp) |
| 21 | + .option('--hot-server <url>', 'The server <url> which will be used to serve Handsontable assets from.') |
| 22 | + .action(async (args, options) => { |
| 23 | + await require('./commands/local-server')('test-runner', config.SERVER_PORT, options); |
| 24 | + |
| 25 | + const statsGenerator = require('./commands/protractor')(options); |
| 26 | + |
| 27 | + await require('./storage').saveByReplace(statsGenerator); |
| 28 | + |
| 29 | + process.exit(0); |
| 30 | + }) |
| 31 | + ; |
| 32 | + |
| 33 | + program.parse(process.argv); |
28 | 34 | } |
29 | | -if (genMapFile) { |
30 | | - console.log('Generating map file...'); |
31 | | - |
32 | | - new Promise(function(resolve, reject) { |
33 | | - fs.mkdirs(RESULTS_DIR, function(err) { |
34 | | - if (err) { |
35 | | - return reject(err); |
36 | | - } |
37 | | - resolve(); |
38 | | - }); |
39 | | - }).then(function() { |
40 | | - return dirTree(RESULTS_DIR); |
41 | 35 |
|
42 | | - }).then(function(tree) { |
43 | | - return new Promise(function(resolve, reject) { |
44 | | - fs.writeFile(path.resolve(RESULTS_DIR + '/map.json'), JSON.stringify(tree), function(err) { |
45 | | - if (err) { |
46 | | - return reject(err); |
47 | | - } |
48 | | - resolve(); |
49 | | - }); |
50 | | - }); |
51 | | - }).then(function() { |
52 | | - console.log('Generated map file in ' + RESULTS_DIR + '/map.json'); |
53 | | - }).catch(function(error) { |
54 | | - console.error('Error generating map ->', error); |
55 | | - }); |
56 | | - |
57 | | - return; |
58 | | -} |
59 | | - |
60 | | -buildHandsontable({ |
61 | | - hotBranch: hotBranch, |
62 | | - outputDir: TESTS_DIR + '/public/lib/handsontable/disabled_plugins', |
63 | | - include: ['SheetClip', 'autoResize', 'copyPaste', 'jsonpatch'], |
64 | | - includeTypes: [] // without plugins |
65 | | -}).then(function() { |
66 | | - return buildHandsontable({ |
67 | | - outputDir: TESTS_DIR + '/public/lib/handsontable/enabled_all_modules', |
68 | | - includeTypes: ['all'] // all modules |
69 | | - }); |
70 | | -}).then(function(builder) { |
71 | | - hotVersion = builder.entryFile.getFile('handsontable').package.version; |
72 | | - |
73 | | - if (hotBranch && ['latest', 'master', 'link'].indexOf(hotBranch) === -1) { |
74 | | - hotVersion = hotBranch.replace(/\//g, '-'); |
| 36 | +(function main() { |
| 37 | + try { |
| 38 | + if (!semver.satisfies(process.versions.node, engines.node)) { |
| 39 | + throw Error(`The project requires Node.js${engines.node} for running. You've currently installed version ${process.versions.node}.`); |
| 40 | + } |
| 41 | + |
| 42 | + parseArgs(); |
| 43 | + } catch (ex) { |
| 44 | + /* eslint-disable no-console */ |
| 45 | + console.log(ex.message); |
| 46 | + console.log(''); |
| 47 | + process.exit(2); |
75 | 48 | } |
76 | | - fs.ensureDirSync('./' + RESULTS_DIR); |
77 | | - |
78 | | - runHttpServer(); |
79 | | - runCmd('./node_modules/.bin/protractor', ['protractor.conf.js']); |
80 | | -}).catch(function(error) { |
81 | | - console.error(error.message); |
82 | | -}); |
83 | | - |
84 | | -/** |
85 | | - * Build handsontable |
86 | | - * |
87 | | - * @returns {Promise} |
88 | | - */ |
89 | | -function buildHandsontable(options) { |
90 | | - var defaultOptions = { |
91 | | - disableUI: true, |
92 | | - includeTypes: [], |
93 | | - minify: false |
94 | | - }; |
95 | | - |
96 | | - options = merge(defaultOptions, options); |
97 | | - |
98 | | - return new Promise(function(resolve, reject) { |
99 | | - var builder = new hotBuilder(null, options); |
100 | | - |
101 | | - builder.on('complete', function() { |
102 | | - resolve(builder); |
103 | | - }); |
104 | | - builder.on('error', reject); |
105 | | - }); |
106 | | -} |
107 | | - |
108 | | -/** |
109 | | - * Run HTTP Server |
110 | | - */ |
111 | | -function runHttpServer() { |
112 | | - httpServer.createServer({ |
113 | | - showDir: false, |
114 | | - root: './' + TESTS_DIR + '/public' |
115 | | - }).listen(SERVER_PORT, SERVER_HOST); |
116 | | -} |
117 | | - |
118 | | -/** |
119 | | - * Run shell command |
120 | | - * |
121 | | - * @param {String} cmd |
122 | | - * @param {Array} args |
123 | | - */ |
124 | | -function runCmd(cmd, args) { |
125 | | - var |
126 | | - fork = require('child_process').fork, |
127 | | - toExport = {}, |
128 | | - child; |
129 | | - |
130 | | - toExport.BASE_URL = BASE_URL; |
131 | | - toExport.TESTS_DIR = TESTS_DIR; |
132 | | - toExport.RESULTS_DIR = RESULTS_DIR; |
133 | | - toExport.SAMPLE_SIZE = SAMPLE_SIZE; |
134 | | - toExport.SERVER_HOST = SERVER_HOST; |
135 | | - toExport.SERVER_PORT = SERVER_PORT; |
136 | | - toExport.hotVersion = hotVersion; |
137 | | - |
138 | | - child = fork(cmd, args); |
139 | | - child.send({ |
140 | | - toExport: toExport |
141 | | - }); |
142 | | - child.on('exit', function() { |
143 | | - process.exit(0); |
144 | | - }); |
145 | | -} |
| 49 | +}()); |
0 commit comments