UNPKG

3.08 kBtext/coffeescriptView Raw
1ninjaFactories = require './ninjaFactories'
2{findScript} = require './ninjaCommands'
3_ = require 'lodash'
4log = require('yadsil')('index')
5glob = require 'glob'
6
7# Collect all the coffee files across the project.
8#
9# This is used for linting purposes.
10#
11collectCoffeeFiles = ({extensions, paths}) ->
12 log.info 'looking for coffee script files..'
13 extensionMark = '$ext'
14 _.chain(paths)
15 .map((path) ->
16 extensions.map (ext) -> path.replace extensionMark, ext
17 )
18 .flatten()
19 .map((path) ->
20 glob.sync path
21 )
22 .flatten()
23 .value()
24
25# Default lint options to use on projects with the assumed directory
26# structure. They can be overridden via the `configureCoffeelint` below
27#
28defaultLintOptions = {
29 extensions: [
30 'coffee'
31 '_coffee'
32 ]
33 paths: [
34 'src/**/*.$ext'
35 'assets/js/**/*.$ext'
36 'bin/**/*.$ext'
37 'Gruntfile.$ext'
38 ]
39}
40
41setLintOptions = null
42
43# Expose the option to override default coffeelint paths.
44#
45exports.configureCoffeelint = (options) ->
46 setLintOptions = options
47
48# Collect all the coffee files to lint based on the currently set options
49#
50exports.getCoffeelintPaths = ->
51 collectCoffeeFiles setLintOptions ? defaultLintOptions
52
53# Register a factory. See ninjaFactories.coffee for details.
54exports.defineFactory = ninjaFactories.defineFactory
55
56#
57# * `sourceFile` is the entry file for browserify.
58# (e.g. "browserify/foo/foo.coffee")
59# * `targetFile` is the js file to write, relative to `build/assets/{release|debug}/js`. This will
60# automatically generate an i18n file and deps file. (e.g. "foo/foo.js")
61# * `options.extensions` is a list of extensions to automatically require (e.g. ['.coffee'].)
62# * `options.transforms` is a list of transform names to use (e.g. ['coffeeify'].)
63#
64exports.defineBrowserifyFactory = (name, sourceFile, targetFile, options) ->
65 exports.defineFactory "browserify-#{name}", {
66 makeRules: (ninja, config) ->
67 ['debug', 'release'].forEach (releaseType) ->
68 cli = "$node #{findScript 'browserify-bundle.js'}"
69 if options.extensions then cli += " --extensions '#{options.extensions.join ","}'"
70 if options.transforms then cli += " --transforms '#{options.transforms.join ","}'"
71 cli += " --out $out --i18n $out.i18n"
72 if releaseType is 'debug'
73 cli += " --deps $out.d --debug"
74 cli += " ./$in"
75
76 rule = ninja.rule("browserify-#{name}-#{releaseType}")
77 rule.run(cli)
78 if releaseType is 'debug' then rule.depfile('$out.d')
79 rule.description "(#{releaseType}) BROWSERIFY $in"
80
81 assetFiles: sourceFile
82
83 makeAssetEdge: (ninja, source, target, releaseType) ->
84 target = "build/assets/#{releaseType}/js/#{targetFile}"
85 ninja.edge(target)
86 .from(source)
87 .using("browserify-#{name}-#{releaseType}")
88 return [target]
89 }