Created
January 12, 2017 17:49
-
-
Save fivetanley/5c84253d4857e72a1b90fca197002587 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
const Writer = require('broccoli-caching-writer'); | |
function strip(str) { | |
return str.toString().replace(/\s/g, ''); | |
} | |
function Base(inputNodes, opts) { | |
const options = opts || {}; | |
// options.inputFiles === array of globs, to consider for the cache key | |
Writer.call(this, inputNodes, { | |
annotation: options.annotation | |
}); | |
} | |
Base.extend = function(klassDef) { | |
function plugin() { | |
Base.apply(this, [].slice.call(arguments)); | |
} | |
klassDef = klassDef || {}; | |
const newProto = Object.create(Base.prototype); | |
newProto.constructor = Base; | |
Object.assign(newProto, klassDef); | |
plugin.prototype = newProto; | |
return plugin; | |
} | |
function extend(newProto) { | |
const newKlass = Base.extend(newProto); | |
return function(inputNode, opts) { | |
return new newKlass(inputNode, opts); | |
}; | |
} | |
Base.prototype = Object.create(Writer.prototype); | |
Base.prototype.constructor = Base; | |
Base.prototype.build = function() { | |
// cache has been busted | |
// do anything, for example: | |
// 1. read from this.inputPaths | |
// 2. do something based on the result | |
// 3. and then, write to this.outputPath | |
const fs = require('fs'); | |
const path = require('path'); | |
const mkdirp = require('mkdirp'); | |
this.listEntries().forEach((node) => { | |
const fullPath = node.relativePath; | |
const dirname = path.dirname(fullPath); | |
const newOutputDir = path.join(this.outputPath, dirname); | |
const content = fs.readFileSync(node.fullPath, 'utf8').toString(); | |
//console.log(fullPath); | |
if (!this.filter(content, fullPath)) { | |
mkdirp.sync(newOutputDir); | |
fs.writeFileSync(path.join(this.outputPath, fullPath), content); | |
} else { | |
console.log('shaking', fullPath); | |
} | |
}); | |
}; | |
function contains(string, match) { | |
return string.indexOf(match) > -1; | |
} | |
module.exports = extend({ | |
filter: function(string, fileName) { | |
string = strip(string || ''); | |
if (contains(fileName, 'controllers')) { | |
return filterController(string); | |
} else if (contains(fileName, 'components')) { | |
return filterComponent(string); | |
} else if (contains(fileName, 'routes')) { | |
return filterRoute(string); | |
} | |
return false; | |
} | |
}); | |
function filterComponent(string) { | |
let componentTemplate = strip` | |
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
});`; | |
return string === componentTemplate; | |
} | |
function filterController(string) { | |
let controllerTemplate = strip` | |
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
});`; | |
return string === controllerTemplate; | |
} | |
function filterRoute(string) { | |
let routeTemplate = strip` | |
import Ember from 'ember'; | |
export default Ember.Route.extend({ | |
});`; | |
return string === routeTemplate; | |
} | |
module.exports.extend = extend; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment