/
Gruntfile.coffee
205 lines (168 loc) · 5.57 KB
/
Gruntfile.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
module.exports = (grunt) ->
fs = require 'fs'
glob = require 'glob'
_ = require 'underscore'
path = require 'path'
configuration = grunt.file.readJSON('build.json')
#each component requires a .coffe file in src and a coffe.md file in spec
components = configuration.build?.components ?[]
#non component source files
sourceFiles = configuration.build?.sourceFiles ?[]
for component in components
sourceFiles.push "src/#{component}.coffee"
if fs.existsSync('src/head.coffee')
sourceFiles.unshift('src/head.coffee')
if fs.existsSync('src/foot.coffee')
sourceFiles.push('src/foot.coffee')
tests = "#{configuration.path.tests}/**/*.js"
tasks = "lib/**/*.js"
links = false
grunt.initConfig
pkg: grunt.file.readJSON('package.json')
coffee:
options:
bare: true
compile:
files:
"lib/module.js": sourceFiles
specs:
expand: true
cwd: configuration.path.specs
src: './**/*.coffee.md'
dest: configuration.path.tests
ext: '.js'
simplemocha:
options:
globals: ['should']
timeout: 3000
ignoreLeaks: false
ui: 'bdd'
reporter: 'spec'
all:
src: tests
instrument:
files: tasks
options:
lazy: false
basePath: "#{configuration.path.coverage}/instrument/"
storeCoverage:
options:
dir: configuration.path.coverage
reloadTasks:
rootPath: "#{configuration.path.coverage}/instrument/lib"
makeReport:
src: "#{configuration.path.coverage}/**/*.json"
options:
type: "lcov"
dir: "#{configuration.path.docs}/coverage"
print: "detail"
coverage:
options:
thresholds:
'statements': configuration.coverage.statements
'branches': configuration.coverage.branches
'lines': configuration.coverage.lines
'functions': configuration.coverage.functions
dir: 'coverage/reports',
root: '.'
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.loadNpmTasks 'grunt-simple-mocha'
grunt.loadNpmTasks 'grunt-istanbul-coverage'
grunt.loadNpmTasks 'grunt-istanbul'
grunt.registerTask 'test', ['coffee','simplemocha']
grunt.registerTask 'default', [
'links'
'docs'
'readme'
'coverageReport'
'coverage'
]
grunt.registerTask 'coverageReport', [
'coffee'
'instrument'
'reloadTasks'
'simplemocha'
'storeCoverage'
'makeReport'
]
grunt.registerTask 'docs', ['links','write-documentation']
grunt.registerTask 'links', ->
base = configuration.path.specs
extension = configuration.extension.spec
files = glob.sync "#{base}/**/*#{extension}"
grunt.verbose.writeln "Link files:"
grunt.verbose.writeln files.join("\n")
links =
external: ''
perDirectory: {}
manual: ''
mdLink = (name,url) -> "[#{name}]: #{url}\n"
#get internal links
directories = _.uniq( _.map(files, path.dirname) )
directories.push '.'
for directory in directories
links.perDirectory[directory] = ''
for file in files
name = file
.substring(base.length+1,file.length - extension.length)
.replace('/','.')
links.perDirectory[directory] += mdLink(
name,
path.relative(directory, file)
)
#get external Links
for packageFile in glob.sync 'node_modules/*/package.json'
packageInfo = require "./#{packageFile}"
linkAdress = packageInfo.homepage ? "https://npmjs.org/package/#{[packageInfo.name]}"
links.external += mdLink(packageInfo.name, linkAdress)
#manualLinks
for name,url of configuration.see
links.manual += mdLink(name,url)
grunt.registerTask 'write-documentation', ->
target = configuration.path.docs
base = configuration.path.specs
extension = configuration.extension.spec
files = glob.sync "#{base}/**/*#{extension}"
for directory,specs of _.groupBy(files, path.dirname)
targetDirectory = '.'
for dir in (target + directory.substring(base.length)).split('/')
targetDirectory += "/#{dir}"
if ! fs.existsSync(targetDirectory)
fs.mkdirSync targetDirectory
for spec in specs
fs.writeFileSync(targetDirectory+'/'+path.basename(spec), [
links.external
links.perDirectory[directory]
links.manual
fs.readFileSync(spec).toString()
].join("\n")
)
grunt.registerTask 'readme', ->
packageInfo = require './package.json'
readme = ['']
if (!links)
grunt.task.run('links')
readme.push [links.external, links.manual, links.perDirectory['.']].join("\n")
readme.push fs.readFileSync 'spec/About.coffee.md'
readme.push """
##Setup
Install with npm:
```bash
npm install #{packageInfo.name}
```
Clone with GIT:
```bash
git clone #{packageInfo.repository.url}
```
##Documentation
Head here → [#{configuration.path.docs}](#{configuration.path.docs})
"""
for dependencyType in ['dependencies', 'devDependencies']
if (_.has packageInfo, dependencyType)
title = dependencyType.charAt(0).toUpperCase() + dependencyType.slice(1)
readme.push "###{title}"
for name,version of packageInfo[dependencyType]
readme.push " - [#{name}] #{version}"
readme.push('')
fs.writeFileSync('README.md', readme.join("\n"))