broccoli-plugin-adapter
Advanced tools
Comparing version
@@ -0,1 +1,21 @@ | ||
<a name="1.2.0"></a> | ||
# [1.2.0](https://github.com/stfsy/broccoli-plugin-adapter/compare/v1.1.0...v1.2.0) (2020-04-13) | ||
### Features | ||
* let subclasses return promises ([f5aa592](https://github.com/stfsy/broccoli-plugin-adapter/commit/f5aa592)) | ||
<a name="1.1.0"></a> | ||
# [1.1.0](https://github.com/stfsy/broccoli-plugin-adapter/compare/v1.0.0...v1.1.0) (2020-04-12) | ||
### Features | ||
* allow passing single input node ([f5f236a](https://github.com/stfsy/broccoli-plugin-adapter/commit/f5f236a)) | ||
<a name="1.0.0"></a> | ||
@@ -2,0 +22,0 @@ # 1.0.0 (2020-04-05) |
'use strict' | ||
const BroccoliPlugin = require('broccoli-plugin') | ||
const resolve = require('path').resolve | ||
const toBuffer = require('./to-buffer') | ||
@@ -11,24 +9,42 @@ | ||
constructor(inputNodes, options) { | ||
super(inputNodes, Object.assign(options, {trackInputChanges: true})) | ||
super(Array.isArray(inputNodes) ? inputNodes : [inputNodes], Object.assign(options, { trackInputChanges: true })) | ||
} | ||
build(nodes) { | ||
Object.keys(nodes.changedNodes).forEach((changed, index) => { | ||
return Promise.all(Object.keys(nodes.changedNodes).map((changed, index) => { | ||
if (changed) { | ||
this._walkDirectoriesAndProcessFiles(index, '.', this.input.at(index).readdirSync('.')) | ||
return this._walkDirectoriesAndProcessFiles(this.input.at(index), '.') | ||
} | ||
}) | ||
})) | ||
} | ||
_walkDirectoriesAndProcessFiles(inputAt, folder) { | ||
const entries = this.input.at(inputAt).entries(folder) | ||
entries.forEach(entry => { | ||
const fullRelativePath = folder + '/' + entry.relativePath | ||
if (entry.isDirectory()) { | ||
if (!this.output.existsSync(entry.relativePath)) { | ||
this.output.mkdirSync(entry.relativePath) | ||
} | ||
this._walkDirectoriesAndProcessFiles(inputAt, fullRelativePath) | ||
} else { | ||
this._processSingleFile(inputAt, fullRelativePath) | ||
/** | ||
* @param {FSMerger} fs representing the current folder | ||
* @param {String} folder current folder name relative to build base | ||
* @returns {Promise} | ||
*/ | ||
_walkDirectoriesAndProcessFiles(fs, folder) { | ||
const entries = fs.entries(folder) | ||
return this._processNextDirectoryEntry(fs, entries, 0, folder) | ||
} | ||
/** | ||
* @param {FSMerger} fs representing the current folder | ||
* @param {walkSync.entry[]} entries | ||
* @param {Number} index index of the entries to be processed | ||
* @param {String} folder current folder name relative to build base | ||
* @returns {Promise} | ||
*/ | ||
_processNextDirectoryEntry(fs, entries, index, folder) { | ||
let promise = null | ||
const entry = entries[index] | ||
if (entry.isDirectory()) { | ||
promise = this._processDirectoryEntry(fs, folder, entry) | ||
} else { | ||
promise = this._processFileEntry(fs, folder, entry) | ||
} | ||
return promise.then(() => { | ||
if (index + 1 < entries.length) { | ||
return this._processNextDirectoryEntry(fs, entries, ++index, folder) | ||
} | ||
@@ -40,11 +56,39 @@ }) | ||
* | ||
* @param {String} path to file | ||
* @param {FSMerger} fs representing the current folder | ||
* @param {String} folder current folder name relative to build base | ||
* @param {walkSync.entry} entries the directory entry to be processed | ||
* @returns {Promise} resolved after folder was processed | ||
*/ | ||
_processDirectoryEntry(fs, folder, entry) { | ||
const path = folder + '/' + entry.relativePath | ||
if (!this.output.existsSync(entry.relativePath)) { | ||
this.output.mkdirSync(entry.relativePath) | ||
} | ||
return this._walkDirectoriesAndProcessFiles(fs, path) | ||
} | ||
/** | ||
* | ||
* @param {FSMerger} fs representing the current folder | ||
* @param {String} folder current folder name relative to build base | ||
* @param {walkSync.entry} entries the file entry to be processed | ||
* @returns {Promise} resolved after files was processed | ||
*/ | ||
_processSingleFile(inputAt, path) { | ||
const content = this.input.at(inputAt).readFileSync(path, { encoding: null }) | ||
const processedContent = this.handleContent(path, content) | ||
this.output.writeFileSync(path, toBuffer(processedContent), { encoding: null }) | ||
_processFileEntry(fs, folder, entry) { | ||
const path = folder + '/' + entry.relativePath | ||
return Promise.resolve().then(() => { | ||
return fs.readFileSync(path, { encoding: null }) | ||
}).then(data => { | ||
return this.handleContent(path, data) | ||
}).then(data => { | ||
this.output.writeFileSync(path, toBuffer(data), { encoding: null }) | ||
}) | ||
} | ||
/** | ||
* | ||
* @param {String} path path to the current file | ||
* @param {Buffer} content content of the current file | ||
* @returns {Promise<Buffer>|String} return the processed file content | ||
*/ | ||
handleContent(path, content) { | ||
@@ -51,0 +95,0 @@ return content |
{ | ||
"name": "broccoli-plugin-adapter", | ||
"version": "1.0.0", | ||
"version": "1.2.0", | ||
"description": "Adapter for custom broccoli plugins", | ||
@@ -5,0 +5,0 @@ "main": "lib/index", |
463780
0.46%166
33.87%