This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
174 lines (139 loc) · 3.97 KB
/
index.js
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
var debug = require('debug')('steelmesh-cli');
var events = require('events');
var util = require('util');
var async = require('async');
var path = require('path');
var fs = require('fs');
/**
# steelmesh-cli
This module installs the command-line tools for working with a steelmesh server.
## Commands
```
mesh run
```
Run a local development server that tests the application.
```
mesh pack
```
Package the application resources into a `.tar.gz` file that can be uploaded
into steelmesh using the admin dashboard.
```
mesh publish
```
Publish the application to a steelmesh CouchDB server.
**/
function AppBuilder() {
if (! (this instanceof AppBuilder)) {
return new AppBuilder();
}
// initialise members
this.pkgInfo = {
name: path.basename(process.cwd)
};
// add the version to the pkgInfo (done this way to prevent gendocs being helpful)
this.pkgInfo.version = '0.0.0';
this.workingDir = process.cwd();
}
module.exports = AppBuilder;
util.inherits(AppBuilder, events.EventEmitter);
AppBuilder.prototype._findVersion = function(targetFolder, callback) {
// if we have package data, then use that version
// otherwise, look for an app.js file
try {
require(path.join(targetFolder, 'app.js'));
this.version = '1.0.0';
}
catch (e) {
this.version = '2.0.0';
}
callback();
};
AppBuilder.prototype._loadPackageData = function(targetFolder, callback) {
var builder = this;
// load the package.json file from the specified directory
fs.readFile(path.join(targetFolder, 'package.json'), 'utf8', function(err, data) {
// if we read the file successfully, then parse it
if (! err) {
try {
builder.pkgInfo = JSON.parse(data);
}
catch(e) {
err = e;
}
}
callback(err);
});
};
AppBuilder.prototype.exec = function(action, args, callback) {
var handler = this.handler(action);
if (typeof handler == 'function') {
handler.call(this, args, function(err) {
if (callback) {
callback(err);
}
});
}
else {
callback(new Error('No correct version (' + this.version + ') handler for "' + action + '" action'));
}
};
AppBuilder.prototype.getSourcePath = function() {
var pathDist = path.join(this.workingDir, 'dist');
if (fs.existsSync(pathDist)) {
return pathDist;
}
return this.workingDir;
};
AppBuilder.prototype.handler = function(action) {
var handler;
debug('attempting to open handler for action: ' + action);
try {
// first attempt to get the handler for the current version
handler = require('./handlers/' + action + '-' + this.version);
}
catch (e) {
debug('unable to include action handler (version specific): ', e);
try {
// couldn't find a version specific handler, let's try the default handler
handler = require('./handlers/' + action);
}
catch (e2) {
debug('unable to include action handler (generic version): ', e2);
}
}
return handler;
};
AppBuilder.prototype.init = function(opts, callback) {
var builder = opts.builder;
debug('initializing appbuilder, working directory: ' + builder.workingDir);
async.series([
builder._loadPackageData.bind(builder, builder.workingDir),
builder._findVersion.bind(builder, builder.workingDir)
], function(err) {
debug('Current project detected as steelmesh project version: ', builder.version);
callback(err);
});
};
['pack', 'publish'].forEach(function(action) {
AppBuilder.prototype[action] = function() {
var builder = this,
args = Array.prototype.slice.call(arguments),
callback = args[args.length - 1] || function() {};
this.init(function(err) {
if (err) {
callback(err);
}
else {
builder.exec.apply(builder, [action].concat(args));
}
});
};
});
AppBuilder.prototype.log = function(callback) {
return function(message) {
// display the error to the console
if (message instanceof Error) {
callback(message);
}
};
};