-
Notifications
You must be signed in to change notification settings - Fork 349
/
harp
executable file
·235 lines (198 loc) · 7.19 KB
/
harp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env node
var nodePath = require("path")
var program = require("commander")
var fse = require("fs-extra")
var downloadRepo = require("download-github-repo")
var tmp = require('tmp')
var surge = require('surge')({ platform: 'harpjs.com' })
var pkg = require("../package.json")
var helpers = require("../lib/helpers")
var harp = require("../")
var hooks = {}
var output = function(msg){
var v = pkg.version
console.log("------------")
console.log("Harp v" + v + " – Chloi Inc. 2012–2015")
if(msg){
console.log(msg)
console.log("Press Ctl+C to stop the server")
}
console.log("------------")
}
program
.version(pkg.version)
program
.command("init [path]")
.usage("initializes a new Harp project in the current directory.\n See available boilerplates at https://github.com/harp-boilerplates")
.option("-b, --boilerplate <github-username/repo>", "use a github repo as a boilerplate", "harp-boilerplates/default")
.description("Initialize a new Harp project in current directory")
.action(function(path, program){
var projectPath = nodePath.resolve(process.cwd(), path || "")
var boilerplatePath = nodePath.resolve(__dirname, "..", "lib", "default_boilerplate")
var repo = program.boilerplate
// Assume `harp-boilerplates` github org if boilerplate doesn't contain a slash
repo.match(/\//) || (repo = "harp-boilerplates/"+repo)
var done = function() {
console.log("Initialized project at", projectPath)
}
fse.mkdirp(projectPath, function(err){
if(err) return err
fse.readdir(projectPath, function(err, contents){
if(err) return err
if(contents.length !== 0){
console.log("Sorry,", projectPath, "must be empty.")
return
}
console.log("Downloading boilerplate: https://github.com/"+repo)
//fse.writeFileSync("/Desktop/harp-test-output.txt", repo + "::" + projectPath)
downloadRepo(repo, projectPath, function(err) {
if (!err) return done()
if (require('util').isError(err) && err['code'] === 'ENOTFOUND') {
console.error("You're not connected to the Internet, so we'll use the default boilerplate.")
fse.copy(boilerplatePath, projectPath, function(err){
if (err) return err
return done()
})
} else {
return console.error("Template not found:", "https://github.com/"+repo)
}
})
})
})
})
program
.command("server [path]")
.option("-i, --ip <ip>", "Specify IP to bind to")
.option("-p, --port <port>", "Specify a port to listen on")
.usage("starts a Harp server in current directory, or in the specified directory.")
.description("Start a Harp server in current directory")
.action(function(path, program){
var projectPath = nodePath.resolve(process.cwd(), path || "")
var ip = program.ip || '0.0.0.0'
var port = program.port || 9000
harp.server(projectPath, { ip: ip, port: port }, function(){
var address = ''
if(ip == '0.0.0.0' || ip == '127.0.0.1') {
address = 'localhost'
} else {
address = ip
}
var hostUrl = "http://" + address + ":" + port + "/"
output("Your server is listening at " + hostUrl)
})
})
program
.command("multihost [path]")
.option("-p, --port <port>", "Specify a port to listen on")
.usage("starts a Harp server to host a directory of Harp projects.")
.description("Start a Harp server to host a directory of Harp projects")
.action(function(path, program){
var projectPath = nodePath.resolve(process.cwd(), path || "")
var port = program.port || 9000
harp.multihost(projectPath, { port: port }, function(){
if(port == "80"){
var loc = "http://harp.nu"
}else{
var loc = "http://harp.nu:" + port
}
output("Your server is hosting multiple projects at " + loc)
})
})
program
.command("compile [projectPath] [outputPath]")
.option("-o, --output <path>", "Specify the output directory for compiled assets (relative to project path)")
.usage("compile your project files to static assets (HTML, JS and CSS). \n Use this command if you want to host your application without using the Harp web server.")
.description("Compile project to static assets (HTML, JS and CSS)")
.action(function(projectPath, outputPath, program){
if(!program){
program = outputPath
outputPath = null
}
projectPath = nodePath.resolve(process.cwd(), projectPath || "")
/**
* We deal with output path 3 different ways
*
* 1. second argument (relative to directory called in)
* 2. `--output` argument (relative to project root)
* 3. implicitly projectPath + "/www"
*
*/
if(outputPath){
outputPath = nodePath.resolve(process.cwd(), (outputPath || program.output || ''))
}else{
outputPath = nodePath.resolve(projectPath, (program.output || "www"))
}
harp.compile(projectPath, outputPath, function(errors, output){
if(errors) {
console.log(JSON.stringify(errors, null, 2))
process.exit(1)
}
})
})
program
.command("login")
.usage("Login to publish Harp projects to the web.")
.description("Login to publish Harp projects to the web.")
.action(surge.login(hooks))
program
.command("logout")
.usage("Log out of your Harp account.")
.description("Log out of your Harp account.")
.action(surge.logout(hooks))
program
.command("publish [projectPath] [domain]")
.usage("Publishes a Harp project to the web.")
.description("Publishes a Harp project to the web.")
.action(surge.publish(hooks))
hooks.postProject = function (req, next) {
tmp.dir(function(err, outputPath) {
harp.compile(req.project, outputPath, function (err, output, cleanup) {
if (err) {
console.error(err)
process.exit(1)
} else {
req.src = req.project
req.project = outputPath // Set the compile directory as the directory to publish
next()
}
})
})
}
hooks.postPublish = function (req, next) {
fse.remove(req.project, function (err) { // Remove the compile directory
if (err) {
console.log(err)
process.exit(1)
} else {
next()
}
})
}
program
.command("whoami")
.usage("Check who you are logged in as.")
.description("Check who you are logged in as.")
.action(surge.whoami(hooks))
program
.command("list")
.usage("List out the Harp projects you’ve published to the web.")
.description("List out the Harp projects you’ve published to the web.")
.action(surge.list(hooks))
program
.command("teardown")
.usage("Tear down a live project.")
.description("Tear down a live project.")
.action(surge.teardown(hooks))
program
.command("plus")
.usage("Upgrade and deploy a project with Surge Plus")
.description("Upgrade and deploy a project with Surge Plus")
.action(surge.plus(hooks))
program.on("--help", function(){
console.log(" Use 'harp <command> --help' to get more information or visit http://harpjs.com/ to learn more.")
console.log('')
})
program.parse(process.argv)
if(program.args.length < 1){
program.help()
}