Skip to content

Commit 45eedc5

Browse files
committed
Fixed the Ngrok script to use Sails' internal configuration loader. Updated deps.
1 parent 4bf39c7 commit 45eedc5

File tree

5 files changed

+446
-443
lines changed

5 files changed

+446
-443
lines changed

.idea/dictionaries/neonexus.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ services: mysql
88
before_install:
99
- mysql -e 'CREATE DATABASE IF NOT EXISTS testing;'
1010
- mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'mypass'"
11-
after_success: npm run codecov
12-
deploy:
13-
skip_cleanup: true
11+
script: npm run codecov

ngrok.js

+116-139
Original file line numberDiff line numberDiff line change
@@ -23,164 +23,141 @@ try {
2323
const sails = require('sails');
2424
const rc = require('sails/accessible/rc');
2525
const {spawn} = require('child_process');
26-
const {existsSync} = require('fs');
2726
const path = require('path');
28-
29-
let config;
30-
31-
// Find / use the proper config file.
32-
try {
33-
let configPath = path.resolve(__dirname, 'config/local.js');
34-
35-
// If local.js exists, use it...
36-
if (existsSync(configPath)) {
37-
config = require(configPath);
38-
} else {
39-
// No local.js, find our environmental configuration.
40-
const environment = process.env.NODE_ENV || 'development';
41-
42-
switch (environment.toLowerCase()) {
43-
case 'dev':
44-
case 'development':
45-
configPath = path.resolve(__dirname, 'config/env/development.js');
46-
break;
47-
case 'prod':
48-
case 'production':
49-
configPath = path.resolve(__dirname, 'config/env/production.js');
50-
break;
51-
default:
52-
configPath = path.resolve(__dirname, 'config/env/' + environment + '.js');
53-
break;
54-
}
55-
56-
if (!existsSync(configPath)) {
57-
return console.error(
58-
'ERROR! Trying to load the environment configuration from "../config/env/'
59-
+ environment + '.js", but no such file exists. NODE_ENV="' + process.env.NODE_ENV + '".'
60-
);
27+
const loader = require('sails/lib/hooks/moduleloader');
28+
const merge = require('lodash.merge');
29+
30+
// Load configuration the way Sails would.
31+
loader({
32+
config: {
33+
environment: process.env.NODE_ENV || 'development',
34+
paths: {
35+
config: path.join(__dirname, 'config')
6136
}
37+
}
38+
}).loadUserConfig((err, config) => {
39+
if (err) {
40+
console.error(err);
6241

63-
// Now we can read our configuration.
64-
config = require(configPath);
42+
return process.exit(1);
6543
}
66-
} catch (err) {
67-
console.error(err);
6844

69-
return process.exit(1);
70-
}
45+
// Set Ngrok defaults.
46+
config = merge({
47+
ngrok: {
48+
auth: process.env.NGROK_BASIC || null,
49+
token: process.env.NGROK_AUTHTOKEN || process.env.NGROK_TOKEN || null,
50+
buildAssets: true,
51+
domain: process.env.NGROK_DOMAIN || null,
52+
region: process.env.NGROK_REGION || null
53+
},
54+
port: 1337,
55+
...config
56+
});
7157

72-
// Set our defaults.
73-
config.ngrok = {
74-
auth: process.env.NGROK_BASIC || null,
75-
token: process.env.NGROK_AUTHTOKEN || process.env.NGROK_TOKEN || null,
76-
buildAssets: true,
77-
domain: process.env.NGROK_DOMAIN || null,
78-
port: process.env.PORT || config.port || 1337,
79-
region: process.env.NGROK_REGION || null
80-
};
81-
82-
// Read our console config flags.
83-
for (let i = 2; i < process.argv.length; ++i) {
84-
const thisFlag = process.argv[i].toLowerCase();
85-
86-
if (thisFlag === 'nobuild') {
87-
config.ngrok.buildAssets = false;
88-
} else if (thisFlag.startsWith('auth=')) {
89-
config.ngrok.auth = process.argv[i].substring(5); // don't use the lower-cased version
90-
} else if (thisFlag.startsWith('domain=')) {
91-
config.ngrok.domain = thisFlag.substring(7);
92-
} else if (thisFlag.startsWith('port=')) {
93-
config.ngrok.port = thisFlag.substring(5);
94-
} else if (thisFlag.startsWith('region=')) {
95-
config.ngrok.region = thisFlag.substring(7);
96-
} else if (thisFlag.startsWith('token=')) {
97-
config.ngrok.token = process.argv[i].substring(6);
58+
// Read our console config flags.
59+
for (let i = 2; i < process.argv.length; ++i) {
60+
const thisFlag = process.argv[i].toLowerCase();
61+
62+
if (thisFlag === 'nobuild') {
63+
config.ngrok.buildAssets = false;
64+
} else if (thisFlag.startsWith('auth=')) {
65+
config.ngrok.auth = process.argv[i].substring(5); // don't use the lower-cased version
66+
} else if (thisFlag.startsWith('domain=')) {
67+
config.ngrok.domain = thisFlag.substring(7);
68+
} else if (thisFlag.startsWith('port=')) {
69+
config.port = thisFlag.substring(5);
70+
} else if (thisFlag.startsWith('region=')) {
71+
config.ngrok.region = thisFlag.substring(7);
72+
} else if (thisFlag.startsWith('token=')) {
73+
config.ngrok.token = process.argv[i].substring(6);
74+
}
9875
}
99-
}
10076

101-
ngrok.connect({
102-
addr: config.ngrok.port, // Point to Sails
103-
authtoken: config.ngrok.token,
104-
basic_auth: config.ngrok.auth, // eslint-disable-line
105-
domain: config.ngrok.domain,
106-
region: config.ngrok.region,
107-
schemes: ['HTTPS'],
108-
}).then((listener) => {
109-
let origins;
110-
const ngrokUrl = listener.url();
111-
112-
// Smaller helper function, to output the Ngrok URLs.
113-
function sendUrls() {
114-
console.log('');
115-
console.log('Ngrok URL: ' + ngrokUrl);
116-
// console.log('Ngrok Dashboard: https://dashboard.ngrok.com');
117-
console.log('');
118-
}
77+
ngrok.connect({
78+
addr: config.port, // Point to Sails
79+
authtoken: config.ngrok.token,
80+
basic_auth: config.ngrok.auth, // eslint-disable-line
81+
domain: config.ngrok.domain,
82+
region: config.ngrok.region,
83+
schemes: ['HTTPS']
84+
}).then((listener) => {
85+
let origins;
86+
const ngrokUrl = listener.url();
87+
88+
// Smaller helper function, to output the Ngrok URLs.
89+
function sendUrls() {
90+
console.log('');
91+
console.log('Ngrok URL: ' + ngrokUrl);
92+
// console.log('Ngrok Dashboard: https://dashboard.ngrok.com');
93+
console.log('');
94+
}
11995

120-
// Build our assets in the background.
121-
if (config.ngrok.buildAssets) {
122-
const assetBuilderProcess = spawn('npm', ['run', 'build'], {env: {...process.env, BASE_URL: ngrokUrl}});
96+
// Build our assets in the background.
97+
if (config.ngrok.buildAssets) {
98+
const assetBuilderProcess = spawn('npm', ['run', 'build'], {env: {...process.env, BASE_URL: ngrokUrl}});
12399

124-
assetBuilderProcess.stderr.on('data', (data) => {
125-
console.log('Error:');
126-
console.error(data);
127-
});
100+
assetBuilderProcess.stderr.on('data', (data) => {
101+
console.log('Error:');
102+
console.error(data);
103+
});
128104

129-
assetBuilderProcess.on('exit', (code, signal) => {
130-
if (code === 0) {
131-
console.log('Assets successfully built!');
132-
sendUrls();
133-
} else {
134-
console.error('An error occurred while trying to build assets. Signal: ' + signal);
105+
assetBuilderProcess.on('exit', (code, signal) => {
106+
if (code === 0) {
107+
console.log('Assets successfully built!');
108+
sendUrls();
109+
} else {
110+
console.error('An error occurred while trying to build assets. Signal: ' + signal);
135111

136-
process.exit(1);
137-
}
138-
});
112+
process.exit(1);
113+
}
114+
});
139115

140-
console.log('');
141-
console.log('Assets are being built. Starting API...');
142-
console.log('');
143-
}
116+
console.log('');
117+
console.log('Assets are being built. Starting API...');
118+
console.log('');
119+
}
144120

145-
// Add the Ngrok URL to our allowed origins.
146-
if (config.security && config.security.cors && config.security.cors.allowOrigins) {
147-
origins = [...config.security.cors.allowOrigins];
121+
// Add the Ngrok URL to our allowed origins.
122+
if (config.security && config.security.cors && config.security.cors.allowOrigins) {
123+
origins = [...config.security.cors.allowOrigins];
148124

149-
if (!config.security.cors.allowOrigins.contains(ngrokUrl)) {
150-
origins.push(ngrokUrl);
125+
if (!config.security.cors.allowOrigins.includes(ngrokUrl)) {
126+
origins.push(ngrokUrl);
127+
}
128+
} else {
129+
origins = [ngrokUrl];
151130
}
152-
} else {
153-
origins = [ngrokUrl];
154-
}
155131

156-
// Start Sails.
157-
sails.lift({
158-
...rc('sails'),
159-
baseUrl: ngrokUrl,
160-
port: config.ngrok.port,
161-
security: {
162-
cors: {
163-
allowOrigins: origins
132+
// Start Sails.
133+
sails.lift({
134+
...rc('sails'),
135+
baseUrl: ngrokUrl,
136+
port: config.port,
137+
security: {
138+
cors: {
139+
allowOrigins: origins
140+
}
164141
}
165-
}
166-
}, (err) => {
167-
if (err) {
168-
console.error(err);
142+
}, (err) => {
143+
if (err) {
144+
console.error(err);
169145

170-
return process.exit(1);
171-
}
146+
return process.exit(1);
147+
}
172148

173-
// Sails tends to be faster at startup than asset building...
174-
// Assume we are still waiting for assets (which will output URLs when finished)...
175-
if (config.ngrok.buildAssets) {
176-
console.log('');
177-
console.log('Please wait for assets...');
178-
} else {
179-
sendUrls();
180-
}
149+
// Sails tends to be faster at startup than asset building...
150+
// Assume we are still waiting for assets (which will output URLs when finished)...
151+
if (config.ngrok.buildAssets) {
152+
console.log('');
153+
console.log('Please wait for assets...');
154+
} else {
155+
sendUrls();
156+
}
157+
});
158+
}).catch((e) => {
159+
console.log('There was an error starting the Ngrok tunnel. Likely a bad auth token. Here is the error:');
160+
console.log('');
161+
console.log(e);
181162
});
182-
}).catch((e) => {
183-
console.log('There was an error starting the Ngrok tunnel. Likely a bad auth token. Here is the error:');
184-
console.log('');
185-
console.log(e);
186163
});

0 commit comments

Comments
 (0)