- 1 :
/* eslint max-params: [1, 5] */
- 2 :
const assert = require('assert');
- 3 :
- 4 :
/**
- 5 :
* @mixin
- 6 :
* @alias actions/fs
- 7 :
*/
- 8 :
const fs = module.exports;
- 9 :
- 10 :
const renderEachTemplate = (template, templateData, context, callback) => {
- 11 :
if (template.when && !template.when(templateData, context)) {
- 12 :
return;
- 13 :
}
- 14 :
- 15 :
const {source, destination, templateOptions, copyOptions} = template;
- 16 :
return callback(
- 17 :
source,
- 18 :
destination,
- 19 :
templateData,
- 20 :
templateOptions,
- 21 :
copyOptions
- 22 :
);
- 23 :
};
- 24 :
- 25 :
/**
- 26 :
* Read file from templates folder.
- 27 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 28 :
* Shortcut for this.fs.read(this.templatePath(filepath))
- 29 :
*
- 30 :
* @param {String} filepath - absolute file path or relative to templates folder.
- 31 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 32 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 33 :
*/
- 34 :
fs.readTemplate = function (filepath, ...args) {
- 35 :
return this.fs.read(this.templatePath(filepath), ...args);
- 36 :
};
- 37 :
- 38 :
/**
- 39 :
* Copy file from templates folder to destination folder.
- 40 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 41 :
* Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to))
- 42 :
*
- 43 :
* @param {String} from - absolute file path or relative to templates folder.
- 44 :
* @param {String} to - absolute file path or relative to destination folder.
- 45 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 46 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 47 :
*/
- 48 :
fs.copyTemplate = function (from, to, ...args) {
- 49 :
return this.fs.copy(
- 50 :
this.templatePath(from),
- 51 :
this.destinationPath(to),
- 52 :
...args
- 53 :
);
- 54 :
};
- 55 :
- 56 :
/**
- 57 :
* Copy file from templates folder to destination folder.
- 58 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 59 :
* Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to))
- 60 :
*
- 61 :
* @param {String} from - absolute file path or relative to templates folder.
- 62 :
* @param {String} to - absolute file path or relative to destination folder.
- 63 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 64 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 65 :
*/
- 66 :
fs.copyTemplateAsync = function (from, to, ...args) {
- 67 :
this.checkEnvironmentVersion('mem-fs-editor', '9.0.0');
- 68 :
return this.fs.copyAsync(
- 69 :
this.templatePath(from),
- 70 :
this.destinationPath(to),
- 71 :
...args
- 72 :
);
- 73 :
};
- 74 :
- 75 :
/**
- 76 :
* Read file from destination folder
- 77 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 78 :
* Shortcut for this.fs.read(this.destinationPath(filepath)).
- 79 :
*
- 80 :
* @param {String} filepath - absolute file path or relative to destination folder.
- 81 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 82 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 83 :
*/
- 84 :
fs.readDestination = function (filepath, ...args) {
- 85 :
return this.fs.read(this.destinationPath(filepath), ...args);
- 86 :
};
- 87 :
- 88 :
/**
- 89 :
* Read JSON file from destination folder
- 90 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 91 :
* Shortcut for this.fs.readJSON(this.destinationPath(filepath)).
- 92 :
*
- 93 :
* @param {String} filepath - absolute file path or relative to destination folder.
- 94 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 95 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 96 :
*/
- 97 :
fs.readDestinationJSON = function (filepath, ...args) {
- 98 :
return this.fs.readJSON(this.destinationPath(filepath), ...args);
- 99 :
};
- 100 :
- 101 :
/**
- 102 :
* Write file to destination folder
- 103 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 104 :
* Shortcut for this.fs.write(this.destinationPath(filepath)).
- 105 :
*
- 106 :
* @param {String} filepath - absolute file path or relative to destination folder.
- 107 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 108 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 109 :
*/
- 110 :
fs.writeDestination = function (filepath, ...args) {
- 111 :
return this.fs.write(this.destinationPath(filepath), ...args);
- 112 :
};
- 113 :
- 114 :
/**
- 115 :
* Write json file to destination folder
- 116 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 117 :
* Shortcut for this.fs.writeJSON(this.destinationPath(filepath)).
- 118 :
*
- 119 :
* @param {String} filepath - absolute file path or relative to destination folder.
- 120 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 121 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 122 :
*/
- 123 :
fs.writeDestinationJSON = function (filepath, ...args) {
- 124 :
return this.fs.writeJSON(this.destinationPath(filepath), ...args);
- 125 :
};
- 126 :
- 127 :
/**
- 128 :
* Delete file from destination folder
- 129 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 130 :
* Shortcut for this.fs.delete(this.destinationPath(filepath)).
- 131 :
*
- 132 :
* @param {String} filepath - absolute file path or relative to destination folder.
- 133 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 134 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 135 :
*/
- 136 :
fs.deleteDestination = function (filepath, ...args) {
- 137 :
return this.fs.delete(this.destinationPath(filepath), ...args);
- 138 :
};
- 139 :
- 140 :
/**
- 141 :
* Copy file from destination folder to another destination folder.
- 142 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 143 :
* Shortcut for this.fs.copy(this.destinationPath(from), this.destinationPath(to)).
- 144 :
*
- 145 :
* @param {String} from - absolute file path or relative to destination folder.
- 146 :
* @param {String} to - absolute file path or relative to destination folder.
- 147 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 148 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 149 :
*/
- 150 :
fs.copyDestination = function (from, to, ...args) {
- 151 :
return this.fs.copy(
- 152 :
this.destinationPath(from),
- 153 :
this.destinationPath(to),
- 154 :
...args
- 155 :
);
- 156 :
};
- 157 :
- 158 :
/**
- 159 :
* Move file from destination folder to another destination folder.
- 160 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 161 :
* Shortcut for this.fs.move(this.destinationPath(from), this.destinationPath(to)).
- 162 :
*
- 163 :
* @param {String} from - absolute file path or relative to destination folder.
- 164 :
* @param {String} to - absolute file path or relative to destination folder.
- 165 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 166 :
*/
- 167 :
fs.moveDestination = function (from, to, ...args) {
- 168 :
return this.fs.move(
- 169 :
this.destinationPath(from),
- 170 :
this.destinationPath(to),
- 171 :
...args
- 172 :
);
- 173 :
};
- 174 :
- 175 :
/**
- 176 :
* Exists file on destination folder.
- 177 :
* mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}.
- 178 :
* Shortcut for this.fs.exists(this.destinationPath(filepath)).
- 179 :
*
- 180 :
* @param {String} filepath - absolute file path or relative to destination folder.
- 181 :
* @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 182 :
* @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}
- 183 :
*/
- 184 :
fs.existsDestination = function (filepath, ...args) {
- 185 :
return this.fs.exists(this.destinationPath(filepath), ...args);
- 186 :
};
- 187 :
- 188 :
/**
- 189 :
* Copy a template from templates folder to the destination.
- 190 :
*
- 191 :
* @param {String|Array} source - template file, absolute or relative to templatePath().
- 192 :
* @param {String|Array} [destination] - destination, absolute or relative to destinationPath().
- 193 :
* @param {Object} [templateData] - ejs data
- 194 :
* @param {Object} [templateOptions] - ejs options
- 195 :
* @param {Object} [copyOptions] - mem-fs-editor copy options
- 196 :
*/
- 197 :
fs.renderTemplate = function (
- 198 :
source = '',
- 199 :
destination = source,
- 200 :
templateData = this._templateData(),
- 201 :
templateOptions,
- 202 :
copyOptions
- 203 :
) {
- 204 :
if (typeof templateData === 'string') {
- 205 :
templateData = this._templateData(templateData);
- 206 :
}
- 207 :
- 208 :
templateOptions = {context: this, ...templateOptions};
- 209 :
- 210 :
source = Array.isArray(source) ? source : [source];
- 211 :
const templatePath = this.templatePath(...source);
- 212 :
destination = Array.isArray(destination) ? destination : [destination];
- 213 :
const destinationPath = this.destinationPath(...destination);
- 214 :
- 215 :
this.fs.copyTpl(
- 216 :
templatePath,
- 217 :
destinationPath,
- 218 :
templateData,
- 219 :
templateOptions,
- 220 :
copyOptions
- 221 :
);
- 222 :
};
- 223 :
- 224 :
/**
- 225 :
* Copy a template from templates folder to the destination.
- 226 :
*
- 227 :
* @param {String|Array} source - template file, absolute or relative to templatePath().
- 228 :
* @param {String|Array} [destination] - destination, absolute or relative to destinationPath().
- 229 :
* @param {Object} [templateData] - ejs data
- 230 :
* @param {Object} [templateOptions] - ejs options
- 231 :
* @param {Object} [copyOptions] - mem-fs-editor copy options
- 232 :
*/
- 233 :
fs.renderTemplateAsync = function (
- 234 :
source = '',
- 235 :
destination = source,
- 236 :
templateData = this._templateData(),
- 237 :
templateOptions,
- 238 :
copyOptions
- 239 :
) {
- 240 :
this.checkEnvironmentVersion('mem-fs-editor', '9.0.0');
- 241 :
if (typeof templateData === 'string') {
- 242 :
templateData = this._templateData(templateData);
- 243 :
}
- 244 :
- 245 :
templateOptions = {context: this, ...templateOptions};
- 246 :
- 247 :
source = Array.isArray(source) ? source : [source];
- 248 :
const templatePath = this.templatePath(...source);
- 249 :
destination = Array.isArray(destination) ? destination : [destination];
- 250 :
const destinationPath = this.destinationPath(...destination);
- 251 :
- 252 :
return this.fs.copyTplAsync(
- 253 :
templatePath,
- 254 :
destinationPath,
- 255 :
templateData,
- 256 :
templateOptions,
- 257 :
copyOptions
- 258 :
);
- 259 :
};
- 260 :
- 261 :
/**
- 262 :
* Copy templates from templates folder to the destination.
- 263 :
*
- 264 :
* @param {Array} templates - template file, absolute or relative to templatePath().
- 265 :
* @param {function} [templates.when] - conditional if the template should be written.
- 266 :
* First argument is the templateData, second is the generator.
- 267 :
* @param {String|Array} templates.source - template file, absolute or relative to templatePath().
- 268 :
* @param {String|Array} [templates.destination] - destination, absolute or relative to destinationPath().
- 269 :
* @param {Object} [templates.templateOptions] - ejs options
- 270 :
* @param {Object} [templates.copyOptions] - mem-fs-editor copy options
- 271 :
* @param {Object} [templateData] - ejs data
- 272 :
*/
- 273 :
fs.renderTemplates = function (templates, templateData = this._templateData()) {
- 274 :
assert(Array.isArray(templates), 'Templates must an array');
- 275 :
if (typeof templateData === 'string') {
- 276 :
templateData = this._templateData(templateData);
- 277 :
}
- 278 :
- 279 :
for (const template of templates)
- 280 :
renderEachTemplate(template, templateData, this, (...args) =>
- 281 :
this.renderTemplate(...args)
- 282 :
);
- 283 :
};
- 284 :
- 285 :
/**
- 286 :
* Copy templates from templates folder to the destination.
- 287 :
*
- 288 :
* @param {Array} templates - template file, absolute or relative to templatePath().
- 289 :
* @param {function} [templates.when] - conditional if the template should be written.
- 290 :
* First argument is the templateData, second is the generator.
- 291 :
* @param {String|Array} templates.source - template file, absolute or relative to templatePath().
- 292 :
* @param {String|Array} [templates.destination] - destination, absolute or relative to destinationPath().
- 293 :
* @param {Object} [templates.templateOptions] - ejs options
- 294 :
* @param {Object} [templates.copyOptions] - mem-fs-editor copy options
- 295 :
* @param {Object} [templateData] - ejs data
- 296 :
*/
- 297 :
fs.renderTemplatesAsync = function (
- 298 :
templates,
- 299 :
templateData = this._templateData()
- 300 :
) {
- 301 :
assert(Array.isArray(templates), 'Templates must an array');
- 302 :
this.checkEnvironmentVersion('mem-fs-editor', '9.0.0');
- 303 :
if (typeof templateData === 'string') {
- 304 :
templateData = this._templateData(templateData);
- 305 :
}
- 306 :
- 307 :
return Promise.all(
- 308 :
templates.map((template) =>
- 309 :
renderEachTemplate(template, templateData, this, (...args) =>
- 310 :
this.renderTemplateAsync(...args)
- 311 :
)
- 312 :
)
- 313 :
);
- 314 :
};
- 315 :
- 316 :
/**
- 317 :
* Utility method to get a formatted data for templates.
- 318 :
*
- 319 :
* @param {String} path - path to the storage key.
- 320 :
* @return {Object} data to be passed to the templates.
- 321 :
*/
- 322 :
fs._templateData = function (path) {
- 323 :
if (path) {
- 324 :
return this.config.getPath(path);
- 325 :
}
- 326 :
- 327 :
const allConfig = this.config.getAll();
- 328 :
if (this.generatorConfig) {
- 329 :
Object.assign(allConfig, this.generatorConfig.getAll());
- 330 :
}
- 331 :
- 332 :
if (this.instanceConfig) {
- 333 :
Object.assign(allConfig, this.instanceConfig.getAll());
- 334 :
}
- 335 :
- 336 :
return allConfig;
- 337 :
};