Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if duplicate slugs are detected #62

Merged
merged 1 commit into from
May 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions freestyle-usage-snippet-finder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* jshint node: true */
/* globals require, module */

var Writer = require('broccoli-writer');
var glob = require('glob');
Expand Down Expand Up @@ -26,15 +26,16 @@ function findFiles(srcDir) {
});
}

function extractHbsComponentSnippets(fileContent, componentName) {
function extractHbsComponentSnippets(fileContent, componentName, ui) {
var matched = false;
var inside = false;
var content = [];
var output = {};
var name;
fileContent.split("\n").forEach(function(line){
fileContent.split("\n").forEach(function(line) {
if (matched) {
if (inside) {
// Test for start of closing curlies {{/
if (new RegExp('\\{\\{\\/' + componentName).test(line)) {
matched = false;
inside = false;
Expand All @@ -47,14 +48,22 @@ function extractHbsComponentSnippets(fileContent, componentName) {
inside = line.indexOf('}}') >= 0; // curlies closed }}
}
} else {
// Test for start of opening curlies {{#freestyle-usage 'name'
var m = new RegExp('\\{\\{#' + componentName + '\\s+[\'|"](\\S+)[\'|"].*').exec(line);
if (m) {
matched = true;
inside = m[0].indexOf('}}') >= 0; // curlies closed }}
name = m[1];
// TODO: Cleanup freestyle-notes vs freestyle-usage disambiguation here
if (name.indexOf(':notes') < 0) {
name += ':usage';
if (name.indexOf(':notes') >= 0) {
if (output[name]) {
ui.writeLine('ember-freestyle detected multiple instances of the freestyle-note slug "' + name +'"');
}
} else {
if (output[name + ':usage']) {
ui.writeLine('ember-freestyle detected multiple instances of the freestyle-usage slug "' + name +'"');
}
name += ':usage';
}
}
}
Expand Down Expand Up @@ -87,21 +96,23 @@ function extractCommentSnippets(fileContent) {
return output;
}

function SnippetFinder(inputTree){
function SnippetFinder(inputTree, ui) {
if (!(this instanceof SnippetFinder)){
return new SnippetFinder(inputTree);
return new SnippetFinder(inputTree, ui);
}
this.inputTree = inputTree;
this.ui = ui;
}

SnippetFinder.prototype = Object.create(Writer.prototype);
SnippetFinder.prototype.constructor = SnippetFinder;

SnippetFinder.prototype.write = function (readTree, destDir) {
var self = this;
return readTree(this.inputTree).then(findFiles).then(function(files) {
files.forEach(function(filename) {
var freestyleUsageSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-usage');
var freestyleNoteSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-note');
var freestyleUsageSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-usage', self.ui);
var freestyleNoteSnippets = extractHbsComponentSnippets(fs.readFileSync(filename, 'utf-8'), 'freestyle-note', self.ui);
var componentSnippets = naiveMerge(freestyleUsageSnippets, freestyleNoteSnippets);
var commentSnippets = extractCommentSnippets(fs.readFileSync(filename, 'utf-8'));
var snippets = naiveMerge(componentSnippets, commentSnippets);
Expand Down
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* jshint node: true */
/* globals require, module */
'use strict';
var mergeTrees = require('broccoli-merge-trees');
var stew = require('broccoli-stew');

var path = require('path');
var fs = require('fs');
var mergeTrees = require('broccoli-merge-trees');
var flatiron = require('broccoli-flatiron');
var freestyleUsageSnippetFinder = require('./freestyle-usage-snippet-finder');

Expand All @@ -17,13 +15,14 @@ module.exports = {

treeForApp: function(tree) {
var treesToMerge = [tree];
var self = this;

var snippets = mergeTrees(this.snippetPaths().filter(function(path){
var snippets = mergeTrees(this.snippetPaths().filter(function(path) {
return fs.existsSync(path);
}));

snippets = mergeTrees(this.snippetSearchPaths().map(function(path){
return freestyleUsageSnippetFinder(path);
snippets = mergeTrees(this.snippetSearchPaths().map(function(path) {
return freestyleUsageSnippetFinder(path, self.ui);
}).concat(snippets));

snippets = flatiron(snippets, {
Expand Down