Skip to content

Commit

Permalink
Add shortcuts to mem-fs-editor methods (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored May 1, 2020
1 parent 0104fe5 commit f5b6327
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 0 deletions.
206 changes: 206 additions & 0 deletions lib/actions/fs.js
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();
};
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class Generator extends EventEmitter {
* @mixes actions/install
* @mixes actions/spawn-command
* @mixes actions/user
* @mixes actions/fs
* @mixes nodejs/EventEmitter
*
* @param {string[]} args - Provide arguments at initialization
Expand Down Expand Up @@ -1364,6 +1365,7 @@ class Generator extends EventEmitter {
_.extend(Generator.prototype, require('./actions/install'));
_.extend(Generator.prototype, require('./actions/help'));
_.extend(Generator.prototype, require('./actions/spawn-command'));
_.extend(Generator.prototype, require('./actions/fs'));
Generator.prototype.user = require('./actions/user');

module.exports = Generator;
Loading

0 comments on commit f5b6327

Please sign in to comment.