Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
/spec/testOutputFile
23 changes: 17 additions & 6 deletions bin/ng-html2js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

var fs = require('fs');
var optimist = require('optimist');
var path = require('path');

var html2js = require('../src/html2js');


var path = require('path');
var html2js = require('../src/html2js');

//
// Setup usage and parse arguments
Expand All @@ -24,7 +21,11 @@ var argv = optimist.usage('Usage: $0 <input file> <output file>')
.alias('b', 'basedir')
.string('b')
.describe('b', 'If basedir is provided, basedir will be removed from the templates\' path.')
// -h, --help option
// -s, --strip-prefix option
.alias('s', 'strip-prefix')
.string('s')
.describe('s', 'If strip-prefix is provided, strip-prefix will remove the given prefix from the filename')
// -h, --help option
.alias('h', 'help')
.boolean('h')
.describe('h', 'Shows usage message; what you are looking at.')
Expand All @@ -40,17 +41,27 @@ var outputFile = argv._[1];
var moduleName = argv.m;
var moduleVar = argv['module-var'];
var baseDir = argv.b;
var prefixToStrip = argv.s;

//
// Main script
//

var wordsMatchLeftJustified = function(word1, word2) {
return word1.slice(0, word2.length) === word2;
};

var content = fs.readFileSync(inputFile, 'utf8');
var inputAlias = inputFile;
if(baseDir){
baseDir = path.resolve(baseDir);
inputAlias = inputAlias.replace(baseDir, '');
}
if (prefixToStrip) {
if(wordsMatchLeftJustified(inputAlias, prefixToStrip) && (prefixToStrip !== inputAlias)) {
inputAlias = inputAlias.replace(prefixToStrip, '');
}
}
inputAlias = inputAlias.replace(/\\/g, '/');
var output = html2js(inputAlias, content, moduleName, moduleVar);

Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"name": "Brian Park",
"email": "[email protected]"
},

"name": "ng-html2js",
"description": "Standalone script to turn Angular template into js and put it in a module.",
"version": "2.0.0",
Expand All @@ -12,26 +11,23 @@
"type": "git",
"url": "[email protected]:yaru22/ng-html2js.git"
},

"keywords": [
"angular",
"template",
"html2js"
],

"main": "./src/html2js.js",
"dependencies": {
"optimist": "*"
},
"devDependencies": {
"should": "~1.3.0",
"mocha": "~1.13.0"
"jasmine": "^2.3.2",
"shelljs": "0.5.3"
},

"bin": {
"ng-html2js": "./bin/ng-html2js"
},
"scripts": {
"test": "mocha --require should --reporter spec"
"test": "`npm bin`/jasmine"
}
}
146 changes: 146 additions & 0 deletions spec/ng-html2js_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
require('shelljs/global');

describe('ng-html2js', function() {
it('converts an html file to a javascript object', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJs");
expect(returnValue.output).toEqual(fileContents);
});

it('can save the output to a file', function() {
exec("./bin/ng-html2js spec/test.tmpl spec/testOutputFile", {silent: true});
var testFileContents = cat("spec/testOutputs/expectedTestTmplOutputFileJs");
var outputFileContents = cat("spec/testOutputFile");
expect(testFileContents).toEqual(outputFileContents);
});

describe('choosing a module name', function() {
it('can be performed by providing the flag "-m <module-name>"', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -m foo", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModule");
expect(returnValue.output).toEqual(fileContents);
});

it('can be performed by providing the flag "--module <module-name>"', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl --module foo", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModule");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('choosing a module variable', function() {
it('can be performed by providing the flag "--module-var <module-variable>"', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl --module-var ngModule", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModuleVar");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('choosing a module variable and a module name', function() {
it('can be performed by providing the -m and --module-var flags', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -m foo --module-var ngModule", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModuleAndModuleVar");
expect(returnValue.output).toEqual(fileContents);
});

it('can be performed by providing the --module and --module-var flags', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl --module foo --module-var ngModule", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModuleAndModuleVar");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('stripping part of the path from the template name using the -b or --basedir flag', function() {
it('can be performed by providing the flag "-b <subpath>"', function() {
var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b `pwd`/spec", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir");
expect(returnValue.output).toEqual(fileContents);
});

it('can be performed by providing the flag "--basedir <subpath>"', function() {
var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl --basedir `pwd`/spec", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir");
expect(returnValue.output).toEqual(fileContents);
});

describe('when a user provides an absolute path to the input file and an absolute subpath to be stripped from the file name', function() {
describe('when the user excludes the trailing slash from the subpath to be stripped', function() {
it('strips the provided subpath completely from the file name', function() {
var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b `pwd`/spec", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('when the user includes the trailing slash in the subpath to be stripped', function() {
it('strips the subpath but leaves the trailing slash in the file name', function() {
var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b `pwd`/spec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir");
expect(returnValue.output).toEqual(fileContents);
});
});
});

describe('when the user provides a relative path to the input file and an absolute subpath to be stripped from the file name', function() {
it('does not strip anything from the file name', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -b `pwd`/spec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJs");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('when the user provides an absolute path to the input file and a relative subpath to be stripped from the file name', function() {
it('strips the provided relative subpath from the file name but keeps the trailing slash', function() {
var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b spec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('when the user provides a relative subpath to the input file and a relative subpath to be stripped from the file name', function() {
it('does not strip the subpath from the file name', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -b spec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJs");
expect(returnValue.output).toEqual(fileContents);
});
});
});

describe('stripping an arbitrary subpath from the name of the template file', function() {
it('can be performed by providing the flag "-s <prefix>"', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s spec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsStrippedPrefix");
expect(returnValue.output).toEqual(fileContents);
});

it('can be performed by providing the flag "--strip-prefix <prefix>"', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s spec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJsStrippedPrefix");
expect(returnValue.output).toEqual(fileContents);
});

describe('when the arbitrary prefix to strip does not match characters starting from the beginning of the input file name', function() {
it('does not strip the prefix', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s pec/", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJs");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('when the prefix does not match at all', function() {
it('does not strip the prefix', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s bananagrams", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJs");
expect(returnValue.output).toEqual(fileContents);
});
});

describe('when the prefix to strip is a complete match of the input file name', function() {
it('does not strip the prefix', function() {
var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s spec/test.tmpl", {silent: true});
var fileContents = cat("spec/testOutputs/expectedTestTmplJs");
expect(returnValue.output).toEqual(fileContents);
});
});
});
});
9 changes: 9 additions & 0 deletions spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
File renamed without changes.
12 changes: 12 additions & 0 deletions spec/testOutputs/expectedTestTmplJs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var module = angular.module('spec/test.tmpl', []);
module.run(['$templateCache', function($templateCache) {
$templateCache.put('spec/test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
' {{ item }} it\'s value is great\n' +
' </div>\n' +
'</div>\n' +
'');
}]);

12 changes: 12 additions & 0 deletions spec/testOutputs/expectedTestTmplJsStrippedPrefix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var module = angular.module('test.tmpl', []);
module.run(['$templateCache', function($templateCache) {
$templateCache.put('test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
' {{ item }} it\'s value is great\n' +
' </div>\n' +
'</div>\n' +
'');
}]);

18 changes: 18 additions & 0 deletions spec/testOutputs/expectedTestTmplJsWithModule
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var module;
try {
module = angular.module('foo');
} catch (e) {
module = angular.module('foo', []);
}

module.run(['$templateCache', function ($templateCache) {
$templateCache.put('spec/test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
' {{ item }} it\'s value is great\n' +
' </div>\n' +
'</div>\n' +
'');
}]);

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ try {
}

ngModule.run(['$templateCache', function ($templateCache) {
$templateCache.put('test/test.tmpl',
$templateCache.put('spec/test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
Expand All @@ -15,3 +15,4 @@ ngModule.run(['$templateCache', function ($templateCache) {
'</div>\n' +
'');
}]);

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var ngModule = angular.module('test/test.tmpl', []);
var ngModule = angular.module('spec/test.tmpl', []);
ngModule.run(['$templateCache', function($templateCache) {
$templateCache.put('test/test.tmpl',
$templateCache.put('spec/test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
Expand All @@ -9,3 +9,4 @@ ngModule.run(['$templateCache', function($templateCache) {
'</div>\n' +
'');
}]);

12 changes: 12 additions & 0 deletions spec/testOutputs/expectedTestTmplJsWithoutBaseDir
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var module = angular.module('/test.tmpl', []);
module.run(['$templateCache', function($templateCache) {
$templateCache.put('/test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
' {{ item }} it\'s value is great\n' +
' </div>\n' +
'</div>\n' +
'');
}]);

11 changes: 11 additions & 0 deletions spec/testOutputs/expectedTestTmplOutputFileJs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var module = angular.module('spec/test.tmpl', []);
module.run(['$templateCache', function($templateCache) {
$templateCache.put('spec/test.tmpl',
'<div>\n' +
' hello world\n' +
' <div ng-repeat="item in items">\n' +
' {{ item }} it\'s value is great\n' +
' </div>\n' +
'</div>\n' +
'');
}]);
30 changes: 0 additions & 30 deletions test/html2jsSpec.js

This file was deleted.