Skip to content

Commit 9f27059

Browse files
committed
Adds a postinstall script that initializes bowerrc
If .bowerrc file does not exist in the installing project, it'll instantiate one. This implementation has one goofy blind spot: in the event that the grandparent of your actual project directory happens to contain a "package.json" file, you're going to get a .bowerrc up there. Oops. This would be avoided if it were clear npm had a way for an installing module to know where the root was. Fixes #3
1 parent 3aa6729 commit 9f27059

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/initializes-bowerrc.coffee

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fs = require('fs')
2+
path = require('path')
3+
4+
module.exports =
5+
initialize: (dir = process.cwd()) ->
6+
return unless (topDir = findTopPackageJson(dir)) && (topDir != dir)
7+
return if fs.existsSync(dest = path.join(topDir, '.bowerrc'))
8+
console.log("Writing a default '.bowerrc' file into '#{topDir}'")
9+
fs.writeFileSync dest, """
10+
{
11+
"directory": "/vendor/bower"
12+
}
13+
14+
"""
15+
16+
findTopPackageJson = (dir) ->
17+
current = path.resolve(dir)
18+
grandparent = path.resolve(dir, "..", "..")
19+
if current == grandparent || !hasPackageJson(grandparent)
20+
if hasPackageJson(current)
21+
current
22+
else
23+
null
24+
else
25+
findTopPackageJson(grandparent)
26+
27+
hasPackageJson = (dir) ->
28+
fs.existsSync(path.join(dir, "package.json"))

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"name": "Justin Searls",
88
"company": "Test Double"
99
},
10+
"scripts": {
11+
"postinstall": "node script/postinstall.js"
12+
},
1013
"repository": {
1114
"type": "git",
1215
"url": "git://github.com/testdouble/lineman-bower.git"

script/postinstall.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('coffee-script')
2+
initializesBowerrc = require('./../lib/initializes-bowerrc')
3+
4+
initializesBowerrc.initialize()

0 commit comments

Comments
 (0)