Skip to content
Open
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
42 changes: 28 additions & 14 deletions lib/initializes-app-entrypoint.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
fs = require('fs')
path = require('path')
chdir = require('chdir')
findsRoot = require('find-root-package')
grunt = require('lineman').grunt
minimatch = require('minimatch')
sh = require('execSync')

module.exports =
initialize: (dir = process.cwd()) ->
topDir = findsRoot.findTopPackageJson(dir)
return unless isInstalledAsDependency(dir, topDir)
new EntryPoint(topDir).ensureExists()

chdir topDir, ->
new EntryPoint(topDir).ensureExists()


isInstalledAsDependency = (dir, topDir) ->
Expand All @@ -15,20 +19,30 @@ isInstalledAsDependency = (dir, topDir) ->

class EntryPoint

constructor: (@projectDir, @name='entrypoint') ->
@js = "app/js/#{@name}.js"
@coffee = "app/js/#{@name}.coffee"

ensureExists: ->
unless @exists()
console.log("Writing a default '#{@coffee}' file into '#{@projectDir}'")
fs.writeFileSync path.join(@projectDir, @coffee), @contents

exists: ->
fs.existsSync(path.join(@projectDir, @js)) || fs.existsSync(path.join(@projectDir, @coffee))
default: "app/js/entrypoint.coffee"

contents: """
window._ = require("underscore")
require("./hello")

"""

constructor: (@projectDir) ->
@configuredPattern = sh.exec("lineman config --process files.browserify.entrypoint").stdout.replace(/\s*$/,'')

ensureExists: ->
if @exists(@configuredPattern)
grunt.log.writeln "Entry point file '#{@configuredPattern}' already exists; nothing to do..."

else if @isDefaultConfiguration()
grunt.log.ok "Writing a default entry point file '#{@default}' into '#{@projectDir}'"
grunt.file.write @default, @contents

else
grunt.log.error "Entry point configured as '#{@configuredPattern}' but no file exists"

exists: (pattern) ->
!! grunt.file.expand(pattern).length

isDefaultConfiguration: ->
minimatch(@default, @configuredPattern)
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"coffeeify": "~0.5.2",
"underscore": "~1.6.0",
"coffee-script": "~1.6.3",
"find-root-package": "0.0.1"
"find-root-package": "0.0.1",
"execSync": "~1.0.1-pre",
"minimatch": "~0.2.14",
"chdir": "0.0.0"
},
"peerDependencies": {
"coffeeify": "~0.5.2"
Expand Down