diff --git a/lib/initializes-app-entrypoint.coffee b/lib/initializes-app-entrypoint.coffee index 40c770d..8e1c5fd 100644 --- a/lib/initializes-app-entrypoint.coffee +++ b/lib/initializes-app-entrypoint.coffee @@ -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) -> @@ -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) diff --git a/package.json b/package.json index a73912d..4a9a01a 100644 --- a/package.json +++ b/package.json @@ -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"