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

add support for replacing with string #10

Merged
merged 4 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function(environment) {
includeInOutput: false,
},{
key: '2',
file: 'example2.txt',
string: 'My Text',
includeInIndexHtml: false,
includeInOutput: true,
}]
Expand Down
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,27 @@ module.exports = {
if (typeof appPath !== 'string') {
appPath = 'app';
}

if (new RegExp('^' + rootPath).test(appPath) || (appPath.indexOf(':\\') !== -1 && appPath.indexOf(appPath) === 0)){
contentFilePath = path.join(appPath, content.file);
} else {
contentFilePath = path.join(rootPath, appPath, content.file);
}

if(content.file) {
if (new RegExp('^' + rootPath).test(appPath) || (appPath.indexOf(':\\') !== -1 && appPath.indexOf(appPath) === 0)){
contentFilePath = path.join(appPath, content.file);
} else {
contentFilePath = path.join(rootPath, appPath, content.file);
}
}

this._handleIdDepracation(content);

if (fs.existsSync(contentFilePath)) {
if (content.file && fs.existsSync(contentFilePath)) {
strings[key] = startMarker + fs.readFileSync(contentFilePath) + endMarker;
} else if(content.string && typeof content.string === 'string') {
strings[key] = content.string;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing the markers here. change line to:

strings[key] = startMarker + content.string + endMarker;

This causes the following failure: https://travis-ci.org/ramybenaroya/ember-index/jobs/345734590#L986

} else {
console.error(('ember-index addon: Cannot find ' + contentFilePath).red);
if(content.file) {
console.error(('ember-index addon: Cannot find ' + contentFilePath).red);
} else {
console.error(('ember-index addon: No "file" or "string" property set for this item').red);
}
}
}.bind(this));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/dummy/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

{{content-for 'ember-index-4'}}

{{content-for 'ember-index-5'}}

{{content-for 'ember-index-coloring-script'}}
</body>
</html>
Expand Down
8 changes: 7 additions & 1 deletion tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ module.exports = function(environment) {
file: '_emberIndexContent/file3.txt',
includeInOutput: true,
includeInIndexHtml: false
},
{
key: '5',
string: 'My App Title',
includeInOutput: false,
includeInIndexHtml: true
}, {
key: 'coloring-script',
file: '_emberIndexContent/coloring-script.txt',
includeInOutput: false,
includeInIndexHtml: true
}],
},],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove comma here

output: 'index2.html'
}
};
Expand Down
119 changes: 76 additions & 43 deletions tests/unit/addon-tree-test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ function findEmberIndex(app) {
}
}

describe('Addon', function() {
describe('Addon', function () {
var builder;

this.timeout(15000);

afterEach(function() {
afterEach(function () {
if (builder) {
return builder.cleanup();
}
});

describe('#contentFor', function() {
describe('#contentFor', function () {
var contentFor;
var addon;

it('returns proper content for a single "content-for" tag', function() {
it('returns proper content for a single "content-for" tag, replacing a file', function () {
addon = getEmberIndexAddon({
'ember-index': {
content: {
Expand All @@ -51,7 +51,22 @@ describe('Addon', function() {
expect(contentFor).to.be.equal(addon._startMarkerPrefix + '-default' + '<!-- content from file1 -->' + addon._endMarkerPrefix + '-default');
});

it('returns proper content for multiple "content-for" tags', function() {
it('returns proper content for a single "content-for" tag, replacing a string', function () {
addon = getEmberIndexAddon({
'ember-index': {
content: {
string: '<!-- content from string -->',
includeInOutput: false,
includeInIndexHtml: true
}
}
});
contentFor = addon.contentFor('ember-index');

expect(contentFor).to.be.equal(addon._startMarkerPrefix + '-default' + '<!-- content from string -->' + addon._endMarkerPrefix + '-default');
});

it('returns proper content for multiple "content-for" tags', function () {
addon = getEmberIndexAddon({
'ember-index': {
content: [{
Expand All @@ -69,6 +84,12 @@ describe('Addon', function() {
file: '_emberIndexContent/file3.txt',
includeInOutput: false,
includeInIndexHtml: true
},
{
key: '4',
string: '<!-- content from string -->',
includeInOutput: false,
includeInIndexHtml: true
}]
}
});
Expand All @@ -84,15 +105,19 @@ describe('Addon', function() {
contentFor = addon.contentFor('ember-index-3');

expect(contentFor).to.be.equal(addon._startMarkerPrefix + '-3' + '<!-- content from file3 -->' + addon._endMarkerPrefix + '-3');

contentFor = addon.contentFor('ember-index-4');

expect(contentFor).to.be.equal(addon._startMarkerPrefix + '-3' + '<!-- content from string -->' + addon._endMarkerPrefix + '-3');
Copy link
Owner

@ramybenaroya ramybenaroya Feb 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a typo( -3 instead of -4). change the line to:

expect(contentFor).to.be.equal(addon._startMarkerPrefix + '-4' + '<!-- content from string -->' + addon._endMarkerPrefix + '-4');

});
});


describe('#appTree', function() {
describe('#appTree', function () {
var appTree;
var dummyVar;

it('creates a simple clone of index.html', function() {
it('creates a simple clone of index.html', function () {
appTree = new EmberAddon({
'ember-index': {
output: 'index.jsp'
Expand All @@ -101,7 +126,7 @@ describe('Addon', function() {

builder = new broccoli.Builder(appTree);
return builder.build()
.then(function(results) {
.then(function (results) {
var indexHtml, indexJsp,
outputPath = results.directory,
indexJspPath = path.join(outputPath, 'index.jsp'),
Expand All @@ -118,7 +143,7 @@ describe('Addon', function() {
});
});

it('ember-index is disabled when enabled=false', function() {
it('ember-index is disabled when enabled=false', function () {
appTree = new EmberAddon({
'ember-index': {
output: 'index.jsp',
Expand All @@ -128,15 +153,15 @@ describe('Addon', function() {

builder = new broccoli.Builder(appTree);
return builder.build()
.then(function(results) {
.then(function (results) {
var outputPath = results.directory,
indexJspPath = path.join(outputPath, 'index.jsp');

expect(fs.existsSync(indexJspPath)).to.be.equal(false);
});
});

it('Insert content according to "includeInOutput" & "includeInIndexHtml"', function() {
it('Insert content according to "includeInOutput" & "includeInIndexHtml"', function () {
appTree = new EmberAddon({
'ember-index': {
output: 'index.jsp',
Expand All @@ -155,28 +180,35 @@ describe('Addon', function() {
file: '_emberIndexContent/file3.txt',
includeInOutput: false,
includeInIndexHtml: true
},{
}, {
key: '4',
file: '_emberIndexContent/file4.txt',
includeInOutput: true,
includeInIndexHtml: true
},
{
key: '5',
string: '<!-- content from string -->',
includeInOutput: true,
includeInIndexHtml: true
}]
}
}).toTree();

builder = new broccoli.Builder(appTree);
return builder.build()
.then(function(results) {
.then(function (results) {
var indexHtml, indexJsp,
outputPath = results.directory,
indexJspPath = path.join(outputPath, 'index.jsp'),
indexHtmlPath = path.join(outputPath, 'index.html'),
file1RegExp = /<!-- content from file1 -->/,
file2RegExp = /<!-- content from file2 -->/,
file3RegExp = /<!-- content from file3 -->/,
file4RegExp = /<!-- content from file4 -->/;
file4RegExp = /<!-- content from file4 -->/,
stringRegExp = /<!-- content from string -->/;

expect(fs.existsSync(indexJspPath)).to.be.equal(true);
expect(fs.existsSync(indexJspPath)).to.be.equal(true);

indexJsp = fs.readFileSync(indexJspPath).toString();
indexHtml = fs.readFileSync(indexHtmlPath).toString();
Expand All @@ -197,38 +229,39 @@ describe('Addon', function() {

expect(file4RegExp.test(indexHtml)).to.be.equal(true);

expect(stringRegExp.test(indexJsp)).to.be.equal(true);

expect(stringRegExp.test(indexHtml)).to.be.equal(true);
});
});

it('Use the destDir option correctly.', function() {
appTree = new EmberAddon({
'ember-index': {
destDir: 'export',
output: 'index.jsp',
content: [{
key: '1',
file: '_emberIndexContent/file1.txt',
includeInOutput: false,
includeInIndexHtml: false
}, {
key: '2',
file: '_emberIndexContent/file2.txt',
includeInOutput: true,
includeInIndexHtml: false
}]
}
}).toTree();

builder = new broccoli.Builder(appTree);
return builder.build()
.then(function(results) {
var outputPath = results.directory;
var indexJspPath = path.join(outputPath, 'export/index.jsp');

expect(fs.existsSync(indexJspPath)).to.be.equal(true);
it('Use the destDir option correctly.', function () {
appTree = new EmberAddon({
'ember-index': {
destDir: 'export',
output: 'index.jsp',
content: [{
key: '1',
file: '_emberIndexContent/file1.txt',
includeInOutput: false,
includeInIndexHtml: false
}, {
key: '2',
file: '_emberIndexContent/file2.txt',
includeInOutput: true,
includeInIndexHtml: false
}]
}
}).toTree();

builder = new broccoli.Builder(appTree);
return builder.build()
.then(function (results) {
var outputPath = results.directory;
var indexJspPath = path.join(outputPath, 'export/index.jsp');

});
expect(fs.existsSync(indexJspPath)).to.be.equal(true);
});
});
});
});
});
Loading