UNPKG

5.42 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var nodePath = require("path")
4var program = require("commander")
5var fse = require("fs-extra")
6var downloadRepo = require("download-github-repo")
7var pkg = require("../package.json")
8var helpers = require("../lib/helpers")
9var harp = require("../")
10
11var output = function(msg){
12 var v = pkg.version
13 console.log("------------")
14 console.log("Harp v" + v + " – Chloi Inc. 2012–2015")
15 if(msg){
16 console.log(msg)
17 console.log("Press Ctl+C to stop the server")
18 }
19 console.log("------------")
20}
21
22program
23 .version(pkg.version)
24
25program
26 .command("init [path]")
27 .usage("initializes a new Harp project in the current directory.\n See available boilerplates at https://github.com/harp-boilerplates")
28 .option("-b, --boilerplate <github-username/repo>", "use a github repo as a boilerplate", "harp-boilerplates/default")
29 .description("Initialize a new Harp project in current directory")
30 .action(function(path, program){
31 var projectPath = nodePath.resolve(process.cwd(), path || "")
32 var boilerplatePath = nodePath.resolve(__dirname, "..", "lib", "default_boilerplate")
33 var repo = program.boilerplate
34
35 // Assume `harp-boilerplates` github org if boilerplate doesn't contain a slash
36 repo.match(/\//) || (repo = "harp-boilerplates/"+repo)
37
38 var done = function() {
39 console.log("Initialized project at", projectPath)
40 }
41
42 fse.mkdirp(projectPath, function(err){
43 if(err) return err
44
45 fse.readdir(projectPath, function(err, contents){
46
47 if(err) return err
48
49 if(contents.length !== 0){
50 console.log("Sorry,", projectPath, "must be empty.")
51 return
52 }
53
54 console.log("Downloading boilerplate: https://github.com/"+repo)
55
56 //fse.writeFileSync("/Desktop/harp-test-output.txt", repo + "::" + projectPath)
57
58 downloadRepo(repo, projectPath, function(err) {
59 if (!err) return done()
60
61 if (require('util').isError(err) && err['code'] === 'ENOTFOUND') {
62 console.error("You're not connected to the Internet, so we'll use the default boilerplate.")
63 fse.copy(boilerplatePath, projectPath, function(err){
64 if (err) return err
65 return done()
66 })
67 } else {
68 return console.error("Template not found:", "https://github.com/"+repo)
69 }
70
71 })
72
73 })
74 })
75 })
76
77program
78 .command("server [path]")
79 .option("-i, --ip <ip>", "Specify IP to bind to")
80 .option("-p, --port <port>", "Specify a port to listen on")
81 .usage("starts a Harp server in current directory, or in the specified directory.")
82 .description("Start a Harp server in current directory")
83 .action(function(path, program){
84 var projectPath = nodePath.resolve(process.cwd(), path || "")
85 var ip = program.ip || '0.0.0.0'
86 var port = program.port || 9000
87 harp.server(projectPath, { ip: ip, port: port }, function(){
88 var address = ''
89 if(ip == '0.0.0.0' || ip == '127.0.0.1') {
90 address = 'localhost'
91 } else {
92 address = ip
93 }
94 var hostUrl = "http://" + address + ":" + port + "/"
95 output("Your server is listening at " + hostUrl)
96 })
97 })
98
99program
100 .command("multihost [path]")
101 .option("-p, --port <port>", "Specify a port to listen on")
102 .usage("starts a Harp server to host a directory of Harp projects.")
103 .description("Start a Harp server to host a directory of Harp projects")
104 .action(function(path, program){
105 var projectPath = nodePath.resolve(process.cwd(), path || "")
106 var port = program.port || 9000
107 harp.multihost(projectPath, { port: port }, function(){
108 if(port == "80"){
109 var loc = "http://lvh.me"
110 }else{
111 var loc = "http://lvh.me:" + port
112 }
113 output("Your server is hosting multiple projects at " + loc)
114 })
115 })
116
117program
118 .command("compile [projectPath] [outputPath]")
119 .option("-o, --output <path>", "Specify the output directory for compiled assets (relative to project path)")
120 .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.")
121 .description("Compile project to static assets (HTML, JS and CSS)")
122 .action(function(projectPath, outputPath, program){
123
124 if(!program){
125 program = outputPath
126 outputPath = null
127 }
128
129 projectPath = nodePath.resolve(process.cwd(), projectPath || "")
130
131 /**
132 * We deal with output path 3 different ways
133 *
134 * 1. second argument (relative to directory called in)
135 * 2. `--output` argument (relative to project root)
136 * 3. implicitly projectPath + "/www"
137 *
138 */
139
140 if(outputPath){
141 outputPath = nodePath.resolve(process.cwd(), (outputPath || program.output || ''))
142 }else{
143 outputPath = nodePath.resolve(projectPath, (program.output || "www"))
144 }
145
146 harp.compile(projectPath, outputPath, function(errors, output){
147 if(errors) {
148 console.log(JSON.stringify(errors, null, 2))
149 process.exit(1)
150 }
151 })
152 })
153
154program.on("--help", function(){
155 console.log(" Use 'harp <command> --help' to get more information or visit http://harpjs.com/ to learn more.")
156 console.log('')
157})
158
159program.parse(process.argv)
160
161if(program.args.length < 1){
162 program.help()
163}