-
-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shortcuts to mem-fs-editor methods (#1234)
- Loading branch information
Showing
3 changed files
with
535 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/* eslint max-params: [1, 5] */ | ||
const assert = require('assert'); | ||
|
||
/** | ||
* @mixin | ||
* @alias actions/fs | ||
*/ | ||
const fs = module.exports; | ||
|
||
/** | ||
* Read file from templates folder. | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.read(this.templatePath(filepath)) | ||
* | ||
* @param {String} filepath - absolute file path or relative to templates folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.readTemplate = function(filepath, ...args) { | ||
return this.fs.read(this.templatePath(filepath), ...args); | ||
}; | ||
|
||
/** | ||
* Copy file from templates folder to destination folder. | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to)) | ||
* | ||
* @param {String} from - absolute file path or relative to templates folder. | ||
* @param {String} to - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.copyTemplate = function(from, to, ...args) { | ||
return this.fs.copy(this.templatePath(from), this.destinationPath(to), ...args); | ||
}; | ||
|
||
/** | ||
* Read file from destination folder | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.read(this.destinationPath(filepath)). | ||
* | ||
* @param {String} filepath - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.readDestination = function(filepath, ...args) { | ||
return this.fs.read(this.destinationPath(filepath), ...args); | ||
}; | ||
|
||
/** | ||
* Write file to destination folder | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.write(this.destinationPath(filepath)). | ||
* | ||
* @param {String} filepath - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.writeDestination = function(filepath, ...args) { | ||
return this.fs.write(this.destinationPath(filepath), ...args); | ||
}; | ||
|
||
/** | ||
* Write json file to destination folder | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.writeJSON(this.destinationPath(filepath)). | ||
* | ||
* @param {String} filepath - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.writeDestinationJSON = function(filepath, ...args) { | ||
return this.fs.writeJSON(this.destinationPath(filepath), ...args); | ||
}; | ||
|
||
/** | ||
* Delete file from destination folder | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.delete(this.destinationPath(filepath)). | ||
* | ||
* @param {String} filepath - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.deleteDestination = function(filepath, ...args) { | ||
return this.fs.delete(this.destinationPath(filepath), ...args); | ||
}; | ||
|
||
/** | ||
* Copy file from destination folder to another destination folder. | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.copy(this.destinationPath(from), this.destinationPath(to)). | ||
* | ||
* @param {String} from - absolute file path or relative to destination folder. | ||
* @param {String} to - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.copyDestination = function(from, to, ...args) { | ||
return this.fs.copy(this.destinationPath(from), this.destinationPath(to), ...args); | ||
}; | ||
|
||
/** | ||
* Move file from destination folder to another destination folder. | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.move(this.destinationPath(from), this.destinationPath(to)). | ||
* | ||
* @param {String} from - absolute file path or relative to destination folder. | ||
* @param {String} to - absolute file path or relative to destination folder. | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.moveDestination = function(from, to, ...args) { | ||
return this.fs.move(this.destinationPath(from), this.destinationPath(to), ...args); | ||
}; | ||
|
||
/** | ||
* Exists file on destination folder. | ||
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. | ||
* Shortcut for this.fs.exists(this.destinationPath(filepath)). | ||
* | ||
* @param {String} filepath - absolute file path or relative to destination folder. | ||
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} | ||
*/ | ||
fs.existsDestination = function(filepath, ...args) { | ||
return this.fs.exists(this.destinationPath(filepath), ...args); | ||
}; | ||
|
||
/** | ||
* Copy a template from templates folder to the destination. | ||
* | ||
* @param {String|Array} source - template file, absolute or relative to templatePath(). | ||
* @param {String|Array} [destination] - destination, absolute or relative to destinationPath(). | ||
* @param {Object} [templateData] - ejs data | ||
* @param {Object} [templateOptions] - ejs options | ||
* @param {Object} [copyOptions] - mem-fs-editor copy options | ||
*/ | ||
fs.renderTemplate = function( | ||
source, | ||
destination = source, | ||
templateData = this.templateData(), | ||
templateOptions, | ||
copyOptions | ||
) { | ||
if (typeof templateData === 'string') { | ||
templateData = this.templateData(templateData); | ||
} | ||
|
||
source = Array.isArray(source) ? source : [source]; | ||
const templatePath = this.templatePath(...source); | ||
destination = Array.isArray(destination) ? destination : [destination]; | ||
const destinationPath = this.destinationPath(...destination); | ||
|
||
this.fs.copyTpl( | ||
templatePath, | ||
destinationPath, | ||
templateData, | ||
templateOptions, | ||
copyOptions | ||
); | ||
}; | ||
|
||
/** | ||
* Copy templates from templates folder to the destination. | ||
* | ||
* @param {Array} templates - template file, absolute or relative to templatePath(). | ||
* @param {function} [templates.when] - conditional if the template should be written. | ||
* First argument is the templateData, second is the generator. | ||
* @param {String|Array} templates.source - template file, absolute or relative to templatePath(). | ||
* @param {String|Array} [templates.destination] - destination, absolute or relative to destinationPath(). | ||
* @param {Object} [templates.templateOptions] - ejs options | ||
* @param {Object} [templates.copyOptions] - mem-fs-editor copy options | ||
* @param {Object} [templateData] - ejs data | ||
*/ | ||
fs.renderTemplates = function(templates, templateData = this.templateData()) { | ||
assert(Array.isArray(templates), 'Templates must an array'); | ||
if (typeof templateData === 'string') { | ||
templateData = this.templateData(templateData); | ||
} | ||
|
||
const self = this; | ||
const renderEachTemplate = template => { | ||
if (template.when && !template.when(templateData, this)) { | ||
return; | ||
} | ||
|
||
const { source, destination, templateOptions, copyOptions } = template; | ||
self.renderTemplate(source, destination, templateData, templateOptions, copyOptions); | ||
}; | ||
|
||
templates.forEach(template => renderEachTemplate(template)); | ||
}; | ||
|
||
/** | ||
* Utility method to get a formatted data for templates. | ||
* | ||
* @param {String} path - path to the storage key. | ||
* @return {Object} data to be passed to the templates. | ||
*/ | ||
fs.templateData = function(path) { | ||
if (path) { | ||
return this.config.getPath(path); | ||
} | ||
|
||
return this.config.getAll(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.