Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f13965

Browse files
committed
Update scripts/setup.js, dotenv.config({ silent })
1 parent 413d7c9 commit 6f13965

File tree

6 files changed

+25
-39
lines changed

6 files changed

+25
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"babel-register": "^6.18.0",
4242
"chai": "^3.5.0",
4343
"chai-http": "^3.0.0",
44-
"cp-file": "^4.1.1",
4544
"eslint": "^3.12.0",
4645
"eslint-config-airbnb-base": "^11.0.0",
4746
"eslint-plugin-flowtype": "^2.29.1",
@@ -52,6 +51,7 @@
5251
"rimraf": "^2.5.4"
5352
},
5453
"scripts": {
54+
"postinstall": "node scripts/setup.js",
5555
"lint": "eslint src test scripts --ignore-pattern scripts/start.js",
5656
"check": "flow check",
5757
"test": "mocha test/unit --compilers js:babel-register --require dotenv/config",

scripts/build.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99

1010
const fs = require('fs');
1111
const cp = require('child_process');
12-
const copyFile = require('cp-file');
1312
const pkg = require('../package.json');
1413
const clean = require('./clean');
15-
const setup = require('./setup');
1614
const task = require('../src/utils/task');
1715

1816
module.exports = task('build', () => Promise.resolve()
1917
.then(clean)
20-
.then(setup)
2118
.then(() => new Promise((resolve) => {
2219
cp.spawn('node', [
2320
'node_modules/babel-cli/bin/babel.js',
@@ -37,13 +34,15 @@ module.exports = task('build', () => Promise.resolve()
3734
process.stdout.write(data);
3835
});
3936
}))
40-
.then(() => copyFile.bind('yarn.lock', 'build/yarn.lock'))
41-
.then(() => new Promise((resolve) => {
37+
.then(() => {
38+
fs.writeFileSync('build/yarn.lock', fs.readFileSync('yarn.lock', 'utf8'), 'utf8');
39+
console.log('yarn.lock -> build/yarn.lock');
40+
})
41+
.then(() => {
4242
fs.writeFileSync('build/package.json', JSON.stringify({
4343
engines: pkg.engines,
4444
dependencies: pkg.dependencies,
4545
scripts: { start: 'node server.js' },
4646
}, null, ' '), 'utf8');
4747
console.log('package.json -> build/package.json');
48-
resolve();
49-
})));
48+
}));

scripts/setup.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,13 @@ const task = require('../src/utils/task');
1212

1313
// Creates .env file from the template (.env.example)
1414
// with application settings for the development environment
15-
module.exports = task('setup', () => new Promise((resolve, reject) => {
16-
fs.open('.env', 'wx', (err, fd) => {
17-
if (err) {
18-
if (err.code === 'EEXIST') {
19-
resolve();
20-
} else {
21-
reject(err);
22-
}
23-
} else {
24-
fs.readFile('.env.example', 'utf8', (err2, data) => {
25-
if (err2) {
26-
reject(err2);
27-
} else {
28-
fs.write(fd, data, 'utf8', (err3) => {
29-
if (err3) {
30-
reject(err3);
31-
} else {
32-
console.log('.env.example -> .env');
33-
resolve();
34-
}
35-
});
36-
}
37-
});
38-
}
39-
});
40-
}));
15+
module.exports = task('setup', () => {
16+
if (!fs.existsSync('.env.example')) {
17+
return;
18+
}
19+
20+
if (!fs.existsSync('.env')) {
21+
fs.writeFileSync('.env', fs.readFileSync('.env.example', 'utf8'), 'utf8');
22+
console.log('.env.example -> .env');
23+
}
24+
});

scripts/start.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
const path = require('path');
1111
const nodemon = require('nodemon');
12-
const setup = require('./setup');
1312
const task = require('../src/utils/task');
1413

15-
module.exports = task('start', () => setup().then(() => new Promise((resolve) => {
14+
module.exports = task('start', () => new Promise((resolve) => {
1615
let start;
1716

1817
const nm = nodemon({
@@ -37,4 +36,4 @@ module.exports = task('start', () => setup().then(() => new Promise((resolve) =>
3736

3837
process.on('exit', () => nm.emit.bind(nm, 'exit'));
3938
process.once('SIGINT', process.exit.bind(process, 0));
40-
})));
39+
}));

src/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
/* @flow */
1111

12-
import 'dotenv/config';
12+
import dotenv from 'dotenv';
1313
import app from './app';
1414

15+
dotenv.config({ silent: true });
16+
1517
const server = app.listen(process.env.PORT, () => {
1618
process.stdout.write(`Node.js app is listening on http://localhost:${String(process.env.PORT)}/\n`);
1719
});

src/utils/task.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* Finished 'db:create' in 25ms
1818
*/
1919

20-
require('dotenv/config');
20+
const dotenv = require('dotenv');
21+
22+
dotenv.config({ silent: true });
2123

2224
function run(task, action) {
2325
const command = process.argv[2];

0 commit comments

Comments
 (0)