run-instrument.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. const path = require('path');
  6. const fs = require('fs');
  7. const mkdirp = require('make-dir');
  8. const once = require('once');
  9. const async = require('async');
  10. const libInstrument = require('istanbul-lib-instrument');
  11. const libCoverage = require('istanbul-lib-coverage');
  12. const filesFor = require('./file-matcher').filesFor;
  13. const inputError = require('./input-error');
  14. /*
  15. * Chunk file size to use when reading non JavaScript files in memory
  16. * and copying them over when using complete-copy flag.
  17. */
  18. const READ_FILE_CHUNK_SIZE = 64 * 1024;
  19. function BaselineCollector(instrumenter) {
  20. this.instrumenter = instrumenter;
  21. this.map = libCoverage.createCoverageMap();
  22. this.instrument = instrumenter.instrument.bind(this.instrumenter);
  23. const origInstrumentSync = instrumenter.instrumentSync;
  24. this.instrumentSync = function(...args) {
  25. const ret = origInstrumentSync.apply(this.instrumenter, args);
  26. const baseline = this.instrumenter.lastFileCoverage();
  27. this.map.addFileCoverage(baseline);
  28. return ret;
  29. };
  30. //monkey patch the instrumenter to call our version instead
  31. instrumenter.instrumentSync = this.instrumentSync.bind(this);
  32. }
  33. BaselineCollector.prototype.getCoverage = function() {
  34. return this.map.toJSON();
  35. };
  36. function processFiles(instrumenter, opts, callback) {
  37. const inputDir = opts.inputDir;
  38. const outputDir = opts.outputDir;
  39. const relativeNames = opts.names;
  40. const extensions = opts.extensions;
  41. const verbose = opts.verbose;
  42. const processor = function(name, callback) {
  43. const inputFile = path.resolve(inputDir, name);
  44. const outputFile = path.resolve(outputDir, name);
  45. const inputFileExtension = path.extname(inputFile);
  46. const isJavaScriptFile = extensions.indexOf(inputFileExtension) > -1;
  47. const oDir = path.dirname(outputFile);
  48. let readStream;
  49. let writeStream;
  50. callback = once(callback);
  51. mkdirp.sync(oDir);
  52. /* istanbul ignore if */
  53. if (fs.statSync(inputFile).isDirectory()) {
  54. return callback(null, name);
  55. }
  56. if (isJavaScriptFile) {
  57. fs.readFile(inputFile, 'utf8', (err, data) => {
  58. /* istanbul ignore if */ if (err) {
  59. return callback(err, name);
  60. }
  61. instrumenter.instrument(
  62. data,
  63. inputFile,
  64. (iErr, instrumented) => {
  65. if (iErr) {
  66. return callback(iErr, name);
  67. }
  68. fs.writeFile(outputFile, instrumented, 'utf8', err =>
  69. callback(err, name)
  70. );
  71. }
  72. );
  73. });
  74. } else {
  75. // non JavaScript file, copy it as is
  76. readStream = fs.createReadStream(inputFile, {
  77. bufferSize: READ_FILE_CHUNK_SIZE
  78. });
  79. writeStream = fs.createWriteStream(outputFile);
  80. readStream.on('error', callback);
  81. writeStream.on('error', callback);
  82. readStream.pipe(writeStream);
  83. readStream.on('end', () => {
  84. callback(null, name);
  85. });
  86. }
  87. };
  88. const q = async.queue(processor, 10);
  89. const errors = [];
  90. let count = 0;
  91. const startTime = new Date().getTime();
  92. q.push(relativeNames, (err, name) => {
  93. let inputFile;
  94. let outputFile;
  95. if (err) {
  96. errors.push({
  97. file: name,
  98. error: err.message || /* istanbul ignore next */ err.toString()
  99. });
  100. inputFile = path.resolve(inputDir, name);
  101. outputFile = path.resolve(outputDir, name);
  102. fs.writeFileSync(outputFile, fs.readFileSync(inputFile));
  103. }
  104. if (verbose) {
  105. console.error('Processed: ' + name);
  106. } else {
  107. if (count % 100 === 0) {
  108. process.stdout.write('.');
  109. }
  110. }
  111. count += 1;
  112. });
  113. q.drain = function() {
  114. const endTime = new Date().getTime();
  115. console.error(
  116. '\nProcessed [' +
  117. count +
  118. '] files in ' +
  119. Math.floor((endTime - startTime) / 1000) +
  120. ' secs'
  121. );
  122. if (errors.length > 0) {
  123. console.error(
  124. 'The following ' +
  125. errors.length +
  126. ' file(s) had errors and were copied as-is'
  127. );
  128. console.error(errors);
  129. }
  130. return callback();
  131. };
  132. }
  133. function run(config, opts, callback) {
  134. opts = opts || {};
  135. const iOpts = config.instrumentation;
  136. const input = opts.input;
  137. const output = opts.output;
  138. const excludes = opts.excludes;
  139. let stream;
  140. let includes;
  141. let instrumenter;
  142. const origCallback = callback;
  143. const needBaseline = iOpts.saveBaseline();
  144. const baselineFile = path.resolve(iOpts.baselineFile());
  145. if (iOpts.completeCopy()) {
  146. includes = ['**/*'];
  147. } else {
  148. includes = iOpts.extensions().map(ext => '**/*' + ext);
  149. }
  150. if (!input) {
  151. return callback(new Error('No input specified'));
  152. }
  153. instrumenter = libInstrument.createInstrumenter(
  154. iOpts.getInstrumenterOpts()
  155. );
  156. if (needBaseline) {
  157. mkdirp.sync(path.dirname(baselineFile));
  158. instrumenter = new BaselineCollector(instrumenter);
  159. callback = function(err) {
  160. /* istanbul ignore else */
  161. if (!err) {
  162. console.error('Saving baseline coverage at ' + baselineFile);
  163. fs.writeFileSync(
  164. baselineFile,
  165. JSON.stringify(instrumenter.getCoverage()),
  166. 'utf8'
  167. );
  168. }
  169. return origCallback(err);
  170. };
  171. }
  172. const file = path.resolve(input);
  173. const stats = fs.statSync(file);
  174. if (stats.isDirectory()) {
  175. if (!output) {
  176. return callback(
  177. inputError.create(
  178. 'Need an output directory when input is a directory!'
  179. )
  180. );
  181. }
  182. if (output === file) {
  183. return callback(
  184. inputError.create(
  185. 'Cannot instrument into the same directory/ file as input!'
  186. )
  187. );
  188. }
  189. mkdirp.sync(output);
  190. filesFor(
  191. {
  192. root: file,
  193. includes,
  194. excludes: excludes || iOpts.excludes(false),
  195. relative: true
  196. },
  197. (err, files) => {
  198. /* istanbul ignore if */
  199. if (err) {
  200. return callback(err);
  201. }
  202. processFiles(
  203. instrumenter,
  204. {
  205. inputDir: file,
  206. outputDir: output,
  207. names: files,
  208. extensions: iOpts.extensions(),
  209. verbose: config.verbose
  210. },
  211. callback
  212. );
  213. }
  214. );
  215. } else {
  216. if (output) {
  217. stream = fs.createWriteStream(output);
  218. } else {
  219. stream = process.stdout;
  220. }
  221. stream.write(
  222. instrumenter.instrumentSync(fs.readFileSync(file, 'utf8'), file)
  223. );
  224. if (stream !== process.stdout) {
  225. stream.end();
  226. }
  227. return callback();
  228. }
  229. }
  230. module.exports = {
  231. run
  232. };