All files / src/util writers.js

100% Statements 4/4
100% Branches 4/4
100% Functions 0/0
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107          1x                                     6x   6x                                                                                                                                             2x                  
/**
 * This file defines mixins for writing files to disk
 */
 
// Import dependencies
import ejs from 'ejs';
 
/**
 * Writes out a JSON object to the destination file.
 *
 * If the file already exists, the existing object will be run through
 * `Object.assign` with the content object passed. This allows for some
 * mutation of an existing object if desired while ensuring we don't
 * overwrite and destroy the existing JSON object in the file.
 *
 * The contents and file names are run through templating so ejs template
 * tags can be used in both.
 *
 * @param  {Object} contents   The JS object to use as the cntents.
 * @param  {String} location   The file path where the json file will live.
 * @param  {String} pad        Optional. The whitespace to use in output.
 *                             Will default to the defined default.
 * @return {void}
 */
export function writeJSON ( content, location, pad = this.defaultPad) {
	location = this.destinationPath( ejs.render( location, this.data ) );
	const existing = this.fs.readJSON( location, { defaults: '{}' } );
 
	// Write the file as a template passing the generator data.
	this.fs.write(
		location,
		ejs.render(
			JSON.stringify(
				Object.assign( {}, content, existing ),
				null,
				pad
			),
			this.data
		)
	);
}
/**
 * Writes out a JS module to the file system.
 *
 * The contents and file names are run through templating so ejs template
 * tags can be used in both.
 *
 * @param  {Object} module   The ASTConfig object for this module.
 * @param  {String} location The file path where the module will live.
 * @return {void}
 */
export function writeModule ( module, location ) {
	// Write the module file.
	this.fs.write(
		this.destinationPath( ejs.render( location, this.data ) ),
		ejs.render( module.toString(), this.data )
	);
}
/**
 * Copies files from the source to the desitination.
 *
 * While the file is copied, the file name is run through an ejs template so
 * that dynamic filenames can be defined in the copy templates if needed.
 *
 * @param  {String} source The template path where the file is located.
 * @param  {String} dest   The destination path where the file is written.
 * @return {void}
 */
export function writeCopy ( source, dest ) {
	this.copy( source, ejs.render( dest, this.data ) );
}
/**
  * Copies files from the source to the desitination, running it through ejs.
  *
  * Both the file contents and the file name are run through ejs templating
  * so dynamic filenames and content can be specified based off of the
  * collected data stored in `this.data`.
  *
  * @param  {String} source The template path where the file is located.
  * @param  {String} dest   The destination path where the file is written.
  * @return {void}
  */
export function writeTemplate ( source, dest ) {
	this.template( source, ejs.render( dest, this.data ), this.data );
}
/**
 * If needed, writes the Gruntfile out to the file root as Gruntfile.js.
 *
 * @param  {Function} done The function to continue generation.
 * @return {void}
 */
export function writeGruntfile ( done ) {
	this.fs.write(
		this.destinationPath( 'Gruntfile.js' ),
		this.grunt.toString()
	);
	done();
}
 
export default {
	writeJSON,
	writeModule,
	writeCopy,
	writeTemplate,
	writeGruntfile
};