|
| 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 | +/** |
| 11 | + * Read file from templates folder. |
| 12 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 13 | + * Shortcut for this.fs.read(this.templatePath(filepath)) |
| 14 | + * |
| 15 | + * @param {String} filepath - absolute file path or relative to templates folder. |
| 16 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 17 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 18 | + */ |
| 19 | +fs.readTemplate = function(filepath, ...args) { |
| 20 | + return this.fs.read(this.templatePath(filepath), ...args); |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Copy file from templates folder to destination folder. |
| 25 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 26 | + * Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to)) |
| 27 | + * |
| 28 | + * @param {String} from - absolute file path or relative to templates folder. |
| 29 | + * @param {String} to - absolute file path or relative to destination folder. |
| 30 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 31 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 32 | + */ |
| 33 | +fs.copyTemplate = function(from, to, ...args) { |
| 34 | + return this.fs.copy(this.templatePath(from), this.destinationPath(to), ...args); |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Read file from destination folder |
| 39 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 40 | + * Shortcut for this.fs.read(this.destinationPath(filepath)). |
| 41 | + * |
| 42 | + * @param {String} filepath - absolute file path or relative to destination folder. |
| 43 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 44 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 45 | + */ |
| 46 | +fs.readDestination = function(filepath, ...args) { |
| 47 | + return this.fs.read(this.destinationPath(filepath), ...args); |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Write file to destination folder |
| 52 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 53 | + * Shortcut for this.fs.write(this.destinationPath(filepath)). |
| 54 | + * |
| 55 | + * @param {String} filepath - absolute file path or relative to destination folder. |
| 56 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 57 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 58 | + */ |
| 59 | +fs.writeDestination = function(filepath, ...args) { |
| 60 | + return this.fs.write(this.destinationPath(filepath), ...args); |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Write json file to destination folder |
| 65 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 66 | + * Shortcut for this.fs.writeJSON(this.destinationPath(filepath)). |
| 67 | + * |
| 68 | + * @param {String} filepath - absolute file path or relative to destination folder. |
| 69 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 70 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 71 | + */ |
| 72 | +fs.writeDestinationJSON = function(filepath, ...args) { |
| 73 | + return this.fs.writeJSON(this.destinationPath(filepath), ...args); |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Delete file from destination folder |
| 78 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 79 | + * Shortcut for this.fs.delete(this.destinationPath(filepath)). |
| 80 | + * |
| 81 | + * @param {String} filepath - absolute file path or relative to destination folder. |
| 82 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 83 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 84 | + */ |
| 85 | +fs.deleteDestination = function(filepath, ...args) { |
| 86 | + return this.fs.delete(this.destinationPath(filepath), ...args); |
| 87 | +}; |
| 88 | + |
| 89 | +/** |
| 90 | + * Copy file from destination folder to another destination folder. |
| 91 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 92 | + * Shortcut for this.fs.copy(this.destinationPath(from), this.destinationPath(to)). |
| 93 | + * |
| 94 | + * @param {String} from - absolute file path or relative to destination folder. |
| 95 | + * @param {String} to - absolute file path or relative to destination folder. |
| 96 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 97 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 98 | + */ |
| 99 | +fs.copyDestination = function(from, to, ...args) { |
| 100 | + return this.fs.copy(this.destinationPath(from), this.destinationPath(to), ...args); |
| 101 | +}; |
| 102 | + |
| 103 | +/** |
| 104 | + * Move file from destination folder to another destination folder. |
| 105 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 106 | + * Shortcut for this.fs.move(this.destinationPath(from), this.destinationPath(to)). |
| 107 | + * |
| 108 | + * @param {String} from - absolute file path or relative to destination folder. |
| 109 | + * @param {String} to - absolute file path or relative to destination folder. |
| 110 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 111 | + */ |
| 112 | +fs.moveDestination = function(from, to, ...args) { |
| 113 | + return this.fs.move(this.destinationPath(from), this.destinationPath(to), ...args); |
| 114 | +}; |
| 115 | + |
| 116 | +/** |
| 117 | + * Exists file on destination folder. |
| 118 | + * mem-fs-editor method's shortcut, for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor}. |
| 119 | + * Shortcut for this.fs.exists(this.destinationPath(filepath)). |
| 120 | + * |
| 121 | + * @param {String} filepath - absolute file path or relative to destination folder. |
| 122 | + * @param {...*} args - for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 123 | + * @returns {*} for more information see [mem-fs-editor]{@link https://github.com/SBoudrias/mem-fs-editor} |
| 124 | + */ |
| 125 | +fs.existsDestination = function(filepath, ...args) { |
| 126 | + return this.fs.exists(this.destinationPath(filepath), ...args); |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * Copy a template from templates folder to the destination. |
| 131 | + * |
| 132 | + * @param {String|Array} source - template file, absolute or relative to templatePath(). |
| 133 | + * @param {String|Array} [destination] - destination, absolute or relative to destinationPath(). |
| 134 | + * @param {Object} [templateData] - ejs data |
| 135 | + * @param {Object} [templateOptions] - ejs options |
| 136 | + * @param {Object} [copyOptions] - mem-fs-editor copy options |
| 137 | + */ |
| 138 | +fs.renderTemplate = function( |
| 139 | + source, |
| 140 | + destination = source, |
| 141 | + templateData = this.templateData(), |
| 142 | + templateOptions, |
| 143 | + copyOptions |
| 144 | +) { |
| 145 | + if (typeof templateData === 'string') { |
| 146 | + templateData = this.templateData(templateData); |
| 147 | + } |
| 148 | + |
| 149 | + source = Array.isArray(source) ? source : [source]; |
| 150 | + const templatePath = this.templatePath(...source); |
| 151 | + destination = Array.isArray(destination) ? destination : [destination]; |
| 152 | + const destinationPath = this.destinationPath(...destination); |
| 153 | + |
| 154 | + this.fs.copyTpl( |
| 155 | + templatePath, |
| 156 | + destinationPath, |
| 157 | + templateData, |
| 158 | + templateOptions, |
| 159 | + copyOptions |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +/** |
| 164 | + * Copy templates from templates folder to the destination. |
| 165 | + * |
| 166 | + * @param {Array} templates - template file, absolute or relative to templatePath(). |
| 167 | + * @param {function} [templates.when] - conditional if the template should be written. |
| 168 | + * First argument is the templateData, second is the generator. |
| 169 | + * @param {String|Array} templates.source - template file, absolute or relative to templatePath(). |
| 170 | + * @param {String|Array} [templates.destination] - destination, absolute or relative to destinationPath(). |
| 171 | + * @param {Object} [templates.templateOptions] - ejs options |
| 172 | + * @param {Object} [templates.copyOptions] - mem-fs-editor copy options |
| 173 | + * @param {Object} [templateData] - ejs data |
| 174 | + */ |
| 175 | +fs.renderTemplates = function(templates, templateData = this.templateData()) { |
| 176 | + assert(Array.isArray(templates), 'Templates must an array'); |
| 177 | + if (typeof templateData === 'string') { |
| 178 | + templateData = this.templateData(templateData); |
| 179 | + } |
| 180 | + |
| 181 | + const self = this; |
| 182 | + const renderEachTemplate = template => { |
| 183 | + if (template.when && !template.when(templateData, this)) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + const { source, destination, templateOptions, copyOptions } = template; |
| 188 | + self.renderTemplate(source, destination, templateData, templateOptions, copyOptions); |
| 189 | + }; |
| 190 | + |
| 191 | + templates.forEach(template => renderEachTemplate(template)); |
| 192 | +}; |
| 193 | + |
| 194 | +/** |
| 195 | + * Utility method to get a formatted data for templates. |
| 196 | + * |
| 197 | + * @param {String} path - path to the storage key. |
| 198 | + * @return {Object} data to be passed to the templates. |
| 199 | + */ |
| 200 | +fs.templateData = function(path) { |
| 201 | + if (path) { |
| 202 | + return this.config.getPath(path); |
| 203 | + } |
| 204 | + |
| 205 | + return this.config.getAll(); |
| 206 | +}; |
0 commit comments