Skip to content

Commit a6453ad

Browse files
committed
added coffeescript support, rewritten app/ and test/ to coffee
1 parent 07ffa8c commit a6453ad

23 files changed

+223
-313
lines changed

README.md

+14-131
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,26 @@
1-
# angular-seed — the seed for AngularJS apps
1+
# angular-coffee-seed
2+
## The seed for AngularJS apps in coffeescript
23

3-
This project is an application skeleton for a typical [AngularJS](http://angularjs.org/) web app.
4-
You can use it to quickly bootstrap your angular webapp projects and dev environment for these
5-
projects.
4+
This project is a [coffeescript](http://coffeescript.org/) port of the [angular-seed](https://github.com/angular/angular-seed) project.
65

7-
The seed contains AngularJS libraries, test libraries and a bunch of scripts all preconfigured for
8-
instant web development gratification. Just clone the repo (or download the zip/tarball), start up
9-
our (or yours) webserver and you are ready to develop and test your application.
6+
I have been inspired by the [angular-seed-coffeescript](https://github.com/OClement/angular-seed-coffeescript) project. I however disliked the browser coffee compilation, so I wrote my own.
107

11-
The seed app doesn't do much, just shows how to wire two controllers and views together. You can
12-
check it out by opening app/index.html in your browser (might not work file `file://` scheme in
13-
certain browsers, see note below).
8+
## How do I include a `.coffee` file in html?
149

15-
_Note: While angular is client-side-only technology and it's possible to create angular webapps that
16-
don't require a backend server at all, we recommend hosting the project files using a local
17-
webserver during development to avoid issues with security restrictions (sandbox) in browsers. The
18-
sandbox implementation varies between browsers, but quite often prevents things like cookies, xhr,
19-
etc to function properly when an html page is opened via `file://` scheme instead of `http://`._
10+
You don't. Just include a `.js` file. `./scripts/web-server.js` will search for a `.coffee` file and send it compiled.
2011

12+
## How do I include a `.js` file?
2113

22-
## How to use angular-seed
14+
Feel free to. If a `.coffee` file is not found and `.js` is, it will be sent untouched.
2315

24-
Clone the angular-seed repository and start hacking...
16+
## Tests
2517

18+
Both `.coffee` and `.js` tests work.
2619

27-
### Running the app during development
20+
## Why another coffee seed?
2821

29-
You can pick one of these options:
22+
This seed uses server-side compilation. While it may take some time (miliseconds?), there is no need for any workarounds that arise from compiling in the browser.
3023

31-
* serve this repository with your webserver
32-
* install node.js and run `scripts/web-server.js`
24+
## Compilation takes too long
3325

34-
Then navigate your browser to `http://localhost:<port>/app/index.html` to see the app running in
35-
your browser.
36-
37-
38-
### Running the app in production
39-
40-
This really depends on how complex is your app and the overall infrastructure of your system, but
41-
the general rule is that all you need in production are all the files under the `app/` directory.
42-
Everything else should be omitted.
43-
44-
Angular apps are really just a bunch of static html, css and js files that just need to be hosted
45-
somewhere, where they can be accessed by browsers.
46-
47-
If your Angular app is talking to the backend server via xhr or other means, you need to figure
48-
out what is the best way to host the static files to comply with the same origin policy if
49-
applicable. Usually this is done by hosting the files by the backend server or through
50-
reverse-proxying the backend server(s) and a webserver(s).
51-
52-
53-
### Running unit tests
54-
55-
We recommend using [jasmine](http://pivotal.github.com/jasmine/) and
56-
[Testacular](http://vojtajina.github.com/testacular/) for your unit tests/specs, but you are free
57-
to use whatever works for you.
58-
59-
Requires [node.js](http://nodejs.org/), Testacular (`sudo npm install -g testacular`) and a local
60-
or remote browser.
61-
62-
* start `scripts/test.sh` (on windows: `scripts\test.bat`)
63-
* a browser will start and connect to the Testacular server (Chrome is default browser, others can be captured by loading the same url as the one in Chrome or by changing the `config/testacular.conf.js` file)
64-
* to run or re-run tests just change any of your source or test javascript files
65-
66-
67-
### End to end testing
68-
69-
Angular ships with a baked-in end-to-end test runner that understands angular, your app and allows
70-
you to write your tests with jasmine-like BDD syntax.
71-
72-
Requires a webserver, node.js + `./scripts/web-server.js` or your backend server that hosts the angular static files.
73-
74-
Check out the
75-
[end-to-end runner's documentation](http://docs.angularjs.org/guide/dev_guide.e2e-testing) for more
76-
info.
77-
78-
* create your end-to-end tests in `test/e2e/scenarios.js`
79-
* serve your project directory with your http/backend server or node.js + `scripts/web-server.js`
80-
* to run do one of:
81-
* open `http://localhost:port/test/e2e/runner.html` in your browser
82-
* run the tests from console with [Testacular](vojtajina.github.com/testacular) via
83-
`scripts/e2e-test.sh` or `script/e2e-test.bat`
84-
85-
86-
### Receiving updates from upstream
87-
88-
When we upgrade angular-seed's repo with newer angular or testing library code, you can just
89-
fetch the changes and merge them into your project with git.
90-
91-
92-
## Directory Layout
93-
94-
app/ --> all of the files to be used in production
95-
css/ --> css files
96-
app.css --> default stylesheet
97-
img/ --> image files
98-
index.html --> app layout file (the main html template file of the app)
99-
index-async.html --> just like index.html, but loads js files asynchronously
100-
js/ --> javascript files
101-
app.js --> application
102-
controllers.js --> application controllers
103-
directives.js --> application directives
104-
filters.js --> custom angular filters
105-
services.js --> custom angular services
106-
lib/ --> angular and 3rd party javascript libraries
107-
angular/
108-
angular.js --> the latest angular js
109-
angular.min.js --> the latest minified angular js
110-
angular-*.js --> angular add-on modules
111-
version.txt --> version number
112-
partials/ --> angular view partials (partial html templates)
113-
partial1.html
114-
partial2.html
115-
116-
config/testacular.conf.js --> config file for running unit tests with Testacular
117-
config/testacular-e2e.conf.js --> config file for running e2e tests with Testacular
118-
119-
scripts/ --> handy shell/js/ruby scripts
120-
e2e-test.sh --> runs end-to-end tests with Testacular (*nix)
121-
e2e-test.bat --> runs end-to-end tests with Testacular (windows)
122-
test.bat --> autotests unit tests with Testacular (windows)
123-
test.sh --> autotests unit tests with Testacular (*nix)
124-
web-server.js --> simple development webserver based on node.js
125-
126-
test/ --> test source files and libraries
127-
e2e/ -->
128-
runner.html --> end-to-end test runner (open in your browser to run)
129-
scenarios.js --> end-to-end specs
130-
lib/
131-
angular/ --> angular testing libraries
132-
angular-mocks.js --> mocks that replace certain angular services in tests
133-
angular-scenario.js --> angular's scenario (end-to-end) test runner library
134-
version.txt --> version file
135-
unit/ --> unit level specs/tests
136-
controllersSpec.js --> specs for controllers
137-
directivessSpec.js --> specs for directives
138-
filtersSpec.js --> specs for filters
139-
servicesSpec.js --> specs for services
140-
141-
## Contact
142-
143-
For more information on AngularJS please check out http://angularjs.org/
26+
Get a SSD drive.

app/js/app.coffee

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
# Declare app level module which depends on filters, and services
4+
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
5+
config ['$routeProvider', ($routeProvider) ->
6+
7+
$routeProvider.when '/view1',
8+
templateUrl: 'partials/partial1.html'
9+
controller: MyCtrl1
10+
11+
$routeProvider.when '/view2',
12+
templateUrl: 'partials/partial2.html'
13+
controller: MyCtrl2
14+
15+
$routeProvider.otherwise redirectTo: '/view1'
16+
]

app/js/app.js

-10
This file was deleted.

app/js/controllers.coffee

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
# Controllers
4+
5+
window.MyCtrl1 = class MyCtrl1
6+
constructor: ->
7+
@$inject = []
8+
9+
class MyCtrl2
10+
constructor: ->
11+
@$inject = []

app/js/controllers.js

-12
This file was deleted.

app/js/directives.coffee

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
# Directives
4+
5+
angular.module('myApp.directives', []).
6+
directive 'appVersion', ['version', (version) ->
7+
(scope, elm, attrs) ->
8+
elm.text version
9+
]

app/js/directives.js

-11
This file was deleted.

app/js/filters.coffee

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
# Filters
4+
5+
angular.module('myApp.filters', []).
6+
filter 'interpolate', ['version', (version) ->
7+
(text) ->
8+
String(text).replace /\%VERSION\%/mg, version
9+
]

app/js/filters.js

-10
This file was deleted.

app/js/services.coffee

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
# Services
4+
5+
# Demonstrate how to register services
6+
# In this case it is a simple value service.
7+
angular.module('myApp.services', []).
8+
value('version', '0.1')

app/js/services.js

-9
This file was deleted.

config/testacular.conf.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ files = [
77
'app/lib/angular/angular-*.js',
88
'test/lib/angular/angular-mocks.js',
99
'app/js/**/*.js',
10-
'test/unit/**/*.js'
10+
'app/js/**/*.coffee',
11+
'test/unit/**/*.js',
12+
'test/unit/**/*.coffee'
1113
];
1214

1315
autoWatch = true;
1416

1517
browsers = ['Chrome'];
1618

19+
preprocessors = {
20+
'**/*.coffee': 'coffee',
21+
};
22+
1723
junitReporter = {
1824
outputFile: 'test_out/unit.xml',
1925
suite: 'unit'

scripts/web-server.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/env node
22

3-
var util = require('util'),
3+
// note: coffeescript required
4+
5+
// sudo npm install -g coffee-script
6+
// export NODE_PATH=$NODE_PATH:/usr/local/lib/node:/usr/local/lib/node_modules
7+
8+
var coffee = require('coffee-script'),
9+
util = require('util'),
410
http = require('http'),
511
fs = require('fs'),
612
url = require('url'),
@@ -93,6 +99,14 @@ StaticServlet.prototype.handleRequest = function(req, res) {
9399
var parts = path.split('/');
94100
if (parts[parts.length-1].charAt(0) === '.')
95101
return self.sendForbidden_(req, res, path);
102+
103+
// is a .js file is requested,
104+
// check for .coffee file first
105+
var jsMatch = path.match(/(.+)\.js$/);
106+
if (jsMatch && jsMatch.length >= 2) {
107+
return self.sendCoffeeFile_(req, res, jsMatch);
108+
}
109+
96110
fs.stat(path, function(err, stat) {
97111
if (err)
98112
return self.sendMissing_(req, res, path);
@@ -184,6 +198,41 @@ StaticServlet.prototype.sendFile_ = function(req, res, path) {
184198
}
185199
};
186200

201+
/**
202+
* Tries to find and compile a .coffee file first
203+
* If not found, will try to send the original .js
204+
* @param {Array.String} match is a result from path.match function
205+
*/
206+
StaticServlet.prototype.sendCoffeeFile_ = function(req, res, match) {
207+
var self = this;
208+
var path = match[0];
209+
var coffeeFile = match[1] + '.coffee';
210+
fs.exists(coffeeFile, function (exists) {
211+
if (exists) {
212+
fs.readFile(coffeeFile, 'utf8', function(err, data) {
213+
// compiles the coffee file without top-level wrapper function
214+
// to enable the option to pollute global scope
215+
// and let this unhealthy feature of angular work
216+
var compilerOptions = {
217+
'bare': true
218+
}
219+
var compiled = coffee.compile(data, compilerOptions);
220+
res.write(compiled);
221+
res.end();
222+
});
223+
} else {
224+
// .coffee not found, try to find and return original request
225+
fs.exists(path, function(exists) {
226+
if (exists) {
227+
self.sendFile_(req, res, path);
228+
} else {
229+
self.sendMissing_(req, res, path);
230+
}
231+
});
232+
}
233+
});
234+
}
235+
187236
StaticServlet.prototype.sendDirectory_ = function(req, res, path) {
188237
var self = this;
189238
if (path.match(/[^\/]$/)) {

test/e2e/scenarios.coffee

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
# http://docs.angularjs.org/guide/dev_guide.e2e-testing
4+
5+
describe 'my app', ->
6+
7+
beforeEach ->
8+
browser().navigateTo '../../app/index.html'
9+
10+
11+
it 'should automatically redirect to /view1 when location hash/fragment is empty', ->
12+
expect(browser().location().url()).toBe("/view1")
13+
14+
15+
describe 'view1', ->
16+
17+
beforeEach ->
18+
browser().navigateTo('#/view1')
19+
20+
21+
it 'should render view1 when user navigates to /view1', ->
22+
expect(element('[ng-view] p:first').text()).
23+
toMatch(/partial for view 1/)
24+
25+
26+
describe 'view2', ->
27+
28+
beforeEach ->
29+
browser().navigateTo('#/view2')
30+
31+
32+
it 'should render view2 when user navigates to /view2', ->
33+
expect(element('[ng-view] p:first').text()).
34+
toMatch(/partial for view 2/)

0 commit comments

Comments
 (0)