UNPKG

replacedoc

Version:

Replace all JsTestDriver DOC comments to use a simple javascript function to inject HTML in pages.

71 lines (66 loc) 2.34 kB
/** * This file must be executed using Node.js in the root of your project! "node replaceDOC.js" * replaceDOC file must be needed to replace all the DOC comments to use a function to inject HTML * it will parse your karma.conf.js file to get all the test files * then it will find all the DOC comments (used in JSTD to inject HTML) and replace them with injectHTML function. * injectHTML exist in jstd-adapter **/ var fs = require( 'fs' ), walk = require( 'walk' ), replacements = 0, files = 0, walker; // Helper functions to modify the content in files. function fixQuotesAndRemoveLineBreaks( match, p1 ) { replacements++; var data1 = p1.replace( /\"/g, '\\\"' ).replace( /\'/g, "\\'" ); return "injectHTML('" + removeAsterisk( trim( data1.replace( /\r?\n|\r/gim, ' ' ) ) ) + "');"; } function trim( html ) { return html.replace( /(^\s*)|(\s{2,})|(\s*$)/g, '' ); } function removeAsterisk( html ) { return html.replace( /(\*)/g, '' ); } process.argv.forEach( function ( startpath, index ) { if ( index > 1 ) { walker = walk.walk( startpath ); walker.on('file', function(path, stat, next){ var filePath = [path, '\\', stat.name].join(''); fs.readFile( filePath, 'utf8', function ( err, data ) { if ( err ) { throw err; } var reDOC = /(?:\/\*\:DOC\s*?\+(?:\s*?)=)([\s\S]*?)(?:\*\/)/gim; // Replace the DOC comments by injectHTML function. (Added some special checks for extra or less spaces than really needed to work even in mismatching typing) var matches = data.match(reDOC); if(matches === null){ next(); return; } files++; var data1 = data.replace( reDOC, "injectHTML('$1');" ) .replace( /(?:injectHTML\(\')([\s\S]*?)(?:\'\);)/gim, fixQuotesAndRemoveLineBreaks ); // After reading the content of the file we do a simple copy in versions directory. fs.writeFile( filePath, data1, 'utf8', function ( err ) { if ( err ) { throw err; } next(); } ); } ); }); walker.on('end', function(){ console.log('\r\nThere were ' + replacements + ' DOC comments replaced in ' + startpath + '.\r\n ' + files +' files processed.\r\n Thanks for your time.'); }); } } );