-
Notifications
You must be signed in to change notification settings - Fork 1
/
launchShopify.js
executable file
·150 lines (135 loc) · 4.96 KB
/
launchShopify.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
#!/usr/bin/env node
var requirejs = require('requirejs')
, cradle = require('cradle');
// hardcode these for this app
var client = 'bluetrail'
, server = 'shopify';
// local testing
config = {
// main database instance
host: '127.0.0.1',
port: 5984,
user: '',
pass: '',
dbName: 'mapsherpa'
};
var config = {
// main database instance
host: 'couch.mapsherpa.com',
port: 5984,
user: 'PaddingtonBear',
pass: '2W1tiUsZRhUbMG',
dbName: 'mapsherpa'
};
// build configuration for requirejs when we are running in production mode
var buildConfig = {
baseUrl: 'public/javascripts',
name: server,
out: 'public/javascripts/'+server+'.built.js',
paths: {
'hbs': 'libs/require/hbs-1.0.7',
'jquery': 'libs/jquery/jquery-1.7.1',
'backbone': 'libs/backbone/backbone',
'underscore': 'libs/underscore/underscore',
'handlebars': 'libs/handlebars/handlebars',
'amd-loader': 'libs/amd-loader',
'bootstrap': 'libs/bootstrap/bootstrap',
'client': 'empty:',
'user': 'empty:'
}
};
var clientConfig = {
name: client,
googleAnalyticsId: '',
db: {
host: config.host,
port: config.port,
user: config.user,
pass: config.pass,
name: client + '-clientdb',
session: client + '-clientdb-sessions'
},
shopify: {
webhookDomain: 'http://localhost:8003',
ports: {
production: 9003,
development: 8003
}
}
};
// we need to be able to reach the central database and get the overall configuration document for our client
var mapsherpa = new (cradle.Connection)({host: config.host, port: config.port, auth: {username: config.user, password: config.pass}}).database(config.dbName);
mapsherpa.exists(function(err, exists) {
if (err) {
console.log('cradle exists error', err);
process.exit();
}
if (!exists) {
// create db if it doesn't exist, a minor convenience as we can't run until a configuration is set.
mapsherpa.create(function() {
mapsherpa.save(client, clientConfig, function(err, res) {
if (err) {
console.log('error saving default client config', err);
} else {
console.log('Default client configuration created for ' + client + ', please visit http://' + config.host+':'+config.port+'/_utils/document.html?mapsherpa/'+client +' to configure this client');
}
process.exit();
});
});
return;
}
mapsherpa.get(client, function(err, doc) {
if (err) {
console.log('Error fetching client configuration for ' + client, err);
mapsherpa.save(client, clientConfig, function(err, res) {
if (err) {
console.log('error saving default client config', err);
} else {
console.log('Default client configuration created for ' + client + ', please visit http://' + config.host+':'+config.port+'/_utils/document.html?mapsherpa/'+client +' to configure this client');
}
process.exit();
});
return;
}
if (!doc[server]) {
console.log('Client configuration for ' + server + ' is missing, please visit http://' + config.host+':'+config.port+'/_utils/document.html?mapsherpa/'+client +' to configure this client');
process.exit();
}
var mode = process.env.NODE_ENV || 'development';
// validate that there are ports defined
if (!doc[server].ports || !doc[server].ports[mode]) {
console.log('Error in client configuration, missing '+server+'.ports configuration for '+mode+' mode, please visit http://' + config.host+':'+config.port+'/_utils/document.html?mapsherpa/'+client +' to configure this client');
process.exit();
}
// make sure they have been changed from the default
if (doc[server].ports[mode] < 8000) {
console.log('Client port configuration is invalid for the '+server+' server in ' + mode + ' mode (values must be >= 8000), please visit http://' + config.host+':'+config.port+'/_utils/document.html?mapsherpa/'+client +' to configure this client.');
console.log('config', doc)
process.exit();
}
// make sure name is right
if (doc.name !== client) {
console.log('Error in client configuration, name is missing or incorrect. Please set "name":"'+client+'"');
process.exit();
}
// now we can try launching
var port = doc[server].ports[mode];
console.log('config', doc)
if (mode == "production") {
console.log('launching '+server+' server in production mode, building assets');
requirejs.optimize(buildConfig, function(buildResponse) {
launch(server, doc, port);
});
} else {
console.log('launching '+server+' server in development mode');
launch(server, doc, port);
}
});
});
function launch(server, config, port) {
console.log('require:'+server+'.js');
require('./'+server+'.js')(config).on('init', function(app) {
app.listen(port);
console.log(server + ' server started on port ' + port);
});
}