Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Templates are now outputted in alphabetical order. Tests have been up…

…dated to follow this as well.
  • Loading branch information...
commit f0f5b6133d3c72de1fb66db6598f8a1a790a3dd7 1 parent 24aeee4
@krunkosaurus authored
View
39 index.js
@@ -61,7 +61,7 @@ var fs = require('fs'),
return this;
},
- // Recursively return an array of all files in a directory.
+ // Recursively return an array of all files (paths) in a directory.
getFilesFrom: function(dir){
var filesAr = [];
var recurse;
@@ -114,17 +114,41 @@ var fs = require('fs'),
// Update target page with new templates.
updateIndex: function(){
var date = new Date();
- var filesAr = this.getFilesFrom(this.watchFolder);
- var content = this.stringFromFiles(filesAr);
+ // Ar of sorted ids from each template.
+ var idsAr = [];
+ // Lookup hash of ids to template content.
+ var fileHash = {};
+ // Final concated text content.
+ var content = '';
var template;
- var i;
+ var f;
+
+ // For each file get its template id so we can sort alphabetically.
+ _.each(this.getFilesFrom(this.watchFolder), function(file, i, list){
+ var contents = this.getFileContents(file);
+ var id = contents.match(/id=['"](.*)['"]/);
+ if (id){
+ id = id[1];
+ // Add to filehash
+ fileHash[id] = contents;
+ idsAr.push(id);
+ }else{
+ throw new Error('Template missing id: ' + file);
+ }
+ }, this);
+
+ idsAr.sort();
+
+ _.each(idsAr, function(id){
+ content += fileHash[id];
+ });
if (this.outputFile instanceof Array){
- for (i = 0; i < this.outputFile.length; i++){
- template = this.getFileContents(this.inputFile[i]);
+ for (f = 0; f < this.outputFile.length; f++){
+ template = this.getFileContents(this.inputFile[f]);
template = template.replace(this.replacementString, content);
- this.writeToFile(this.outputFile[i], template);
+ this.writeToFile(this.outputFile[f], template);
}
}else{
template = this.getFileContents(this.inputFile);
@@ -133,7 +157,6 @@ var fs = require('fs'),
this.writeToFile(this.outputFile, template);
}
-
log.info('Completed in ' + ((new Date() - date) / 1000) + ' seconds.');
}
});
View
26 tests/assets/index.html
@@ -6,19 +6,7 @@
<body>
<h1>Test Test page</h1>
<p>My templates below</p>
- <script type="text/template" id="template-09">
- This is my template 09.
-</script>
-
-<script type="text/template" id="template-07">
- This is my template 07.
-</script>
-
-<script type="text/template" id="template-08">
- This is my template 08.
-</script>
-
-<script type="text/template" id="template-01">
+ <script type="text/template" id="template-01">
This is my template 01.
</script><script type="text/template" id="template-02">
This is my template 02.
@@ -40,6 +28,18 @@
This is my template 06.
</script>
+<script type="text/template" id="template-07">
+ This is my template 07.
+</script>
+
+<script type="text/template" id="template-08">
+ This is my template 08.
+</script>
+
+<script type="text/template" id="template-09">
+ This is my template 09.
+</script>
+
<p>My templates above</p>
</body>
View
121 tests/node_modules/nodeinterval/index.js
@@ -1,121 +0,0 @@
-var fs = require('fs'),
- _ = require('underscore'),
- watch = require('nodewatch'),
- log = require('simple-logger');
-
-(function(NodeInterval){
- NodeInterval.Watcher = function(options){
- var that = this;
- this.options = _.extend(this.defaults, options);
- _(this).extend(this.options);
-
- this.watchFiles = [];
-
- // TODO: We should store the current set of files in an array for future
- // comparing.
- _.each(this.getFilesFrom(this.watchFolder), function(file){
- that.watchFiles.push(file);
- });
- // Also watch the input file for changes.
- // TODO: If inputFile changes, unwatch removed file, or watch new file.
- watch.add(options.inputFile);
-
- // Start the watch change listener.
- watch.onChange(function(file, prevTime, currTime){
- log.info('>>> Change detected to:', file);
- that.updateIndex();
- });
-
- // Render files on start.
- log.log_level = 'info';
- log.info('NodeInterval is watching for changes. Press Ctrl-C to stop.');
- this.updateIndex();
- return this;
- }
-
- _.extend(NodeInterval.Watcher.prototype, {
- defaults: {
- watchFolder: '../src/templates/',
- inputFile: '../src/html/index.html',
- replacementString: '@templates@',
- outputFile: '../assets/index.html'
- },
-
- startWatch: function(){
- _.each(this.watchFiles, function(file){
- watch.add(file);
- });
- return this;
- },
-
- stopWatch: function(){
- _.each(this.watchFiles, function(file){
- watch.remove(file);
- });
- return this;
- },
-
- // Recursively return an array of all files in a directory.
- getFilesFrom: function(dir){
- var filesAr = [];
- var recurse;
-
- // Normalize path.
- if (dir[dir.length-1] !== '/'){
- dir = dir + '/';
- }
-
- recurse = function(dir){
- var arr = fs.readdirSync(dir);
- if (arr.length){
- _.each(arr, function(fileOrDir){
- if (fileOrDir[0] !== '.'){
-
- if (fs.lstatSync(dir + fileOrDir).isDirectory()){
- recurse(dir + fileOrDir + '/');
- }else{
- filesAr.push(dir + fileOrDir);
- }
- }
- });
- }
- }
- recurse(dir);
- return filesAr;
- },
-
- // Get the text contents of a file.
- getFileContents: function(file){
- return fs.readFileSync(file, 'utf8');
- },
-
- // Concat an array of files together into a single string.
- stringFromFiles: function(filesAr){
- var that = this;
- var str = '';
- _.each(filesAr, function(file){
- str += that.getFileContents(file);
- });
- return str;
- },
-
- // Write a string to a file.
- writeToFile: function(file, str){
- log.info('overwrite', file);
- fs.writeFileSync(file, str, 'utf8');
- },
-
- // Update target page with new templates.
- updateIndex: function(){
- var date = new Date();
- var filesAr = this.getFilesFrom(this.watchFolder);
- var content = this.stringFromFiles(filesAr);
- var template = this.getFileContents(this.inputFile);
- template = template.replace(this.replacementString, content);
- this.writeToFile(this.outputFile, template);
- log.info('Completed in ' + ((new Date() - date) / 1000) + ' seconds.');
- }
- });
-
- module.exports = NodeInterval;
-})({});
View
1  tests/node_modules/nodeinterval/index.js
Please sign in to comment.
Something went wrong with that request. Please try again.