forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreator.coffee
95 lines (81 loc) · 2.6 KB
/
creator.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
Fs = require 'fs'
Path = require 'path'
# Simple generator class for deploying a version of hubot on heroku
class Creator
# Setup a ready to go version of hubot
#
# path - A String directory to create/upgrade scripts for
constructor: (path) ->
@path = path
@templateDir = "#{__dirname}/templates"
@scriptsDir = "#{__dirname}/scripts"
# Create a folder if it doesn't already exist.
#
# Returns nothing.
mkdir: (path) ->
Fs.exists path, (exists) ->
unless exists
Fs.mkdirSync path, 0o0755
# Copy the contents of a file from one place to another.
#
# from - A String source file to copy, must exist on disk.
# to - A String destination file to write to.
#
# Returns nothing.
copy: (from, to, callback) ->
Fs.readFile from, "utf8", (err, data) ->
console.log "Copying #{Path.resolve(from)} -> #{Path.resolve(to)}"
Fs.writeFileSync to, data, "utf8"
callback(err, to) if callback?
# Rename a file.
#
# from - A String source file to rename, must exist on disk.
# to - A String destination file to write to.
#
# Returns nothing.
rename: (from, to, callback) ->
Fs.rename from, to, (err, data) ->
console.log "Renaming #{Path.resolve(from)} -> #{Path.resolve(to)}"
callback(err, to) if callback?
# Copy the default scripts hubot ships with to the scripts folder
# This allows people to easily remove scripts hubot defaults to if
# they want. It also provides them with a few examples and a top
# level scripts folder.
#
# path - The destination.
#
# Returns nothing.
copyDefaultScripts: (path) ->
for file in Fs.readdirSync(@scriptsDir)
@copy "#{@scriptsDir}/#{file}", "#{path}/#{file}"
# Public: Run the creator process.
#
# Setup a ready to deploy folder that uses the hubot npm package
# Overwriting basic hubot files if they exist
#
# Returns nothing.
run: ->
console.log "Creating a hubot install at #{@path}"
@mkdir(@path)
@mkdir("#{@path}/bin")
@mkdir("#{@path}/scripts")
@copyDefaultScripts("#{@path}/scripts")
files = [
"Procfile",
"package.json",
"README.md",
"gitignore",
"hubot-scripts.json",
"external-scripts.json"
]
for file in files
@copy "#{@templateDir}/#{file}", "#{@path}/#{file}", (err, to)=>
@rename "#{@path}/gitignore", "#{@path}/.gitignore" if to == "#{@path}/gitignore"
bins = [
"bin/hubot",
"bin/hubot.cmd"
]
for bin in bins
@copy "#{@templateDir}/#{bin}", "#{@path}/#{bin}", (err, binPath) =>
Fs.chmodSync binPath, 0o755
module.exports = Creator