Skip to content

Commit c12e7ee

Browse files
committed
fix: safeparse
1 parent ce6182c commit c12e7ee

File tree

11 files changed

+33
-10
lines changed

11 files changed

+33
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ just add the following `server.js`:
203203
'use strict'
204204

205205
// Read the .env file.
206-
process.loadEnvFile()
206+
try {
207+
process.loadEnvFile()
208+
} catch {}
207209

208210
// Require the framework
209211
const Fastify = require('fastify')

args.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const argv = require('yargs-parser')
44
const { requireModule } = require('./util')
5+
const { safeParse } = require('./env-parser')
56

67
const DEFAULT_IGNORE = 'node_modules build dist .git bower_components logs .swp .nyc_output'
78

@@ -21,7 +22,7 @@ const DEFAULT_ARGUMENTS = {
2122
}
2223

2324
module.exports = function parseArgs (args) {
24-
process.loadEnvFile()
25+
safeParse()
2526
const commandLineArguments = argv(args, {
2627
configuration: {
2728
'populate--': true

env-parser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
safeParse: () => {
3+
try {
4+
process.loadEnvFile()
5+
} catch { }
6+
}
7+
}

generate-swagger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
showHelpForCommand
1313
} = require('./util')
1414
const fp = require('fastify-plugin')
15+
const { safeParse } = require('./env-parser')
1516

1617
let Fastify = null
1718

@@ -55,7 +56,7 @@ async function generateSwagger (args) {
5556
}
5657

5758
async function runFastify (opts) {
58-
process.loadEnvFile()
59+
safeParse()
5960

6061
let file = null
6162

lib/watch/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { GRACEFUL_SHUT } = require('./constants.js')
88

99
const EventEmitter = require('node:events')
1010
const chokidar = require('chokidar')
11+
const { safeParse } = require('../../env-parser.js')
1112
const forkPath = path.join(__dirname, './fork.js')
1213

1314
const watch = function (args, ignoreWatch, verboseWatch) {
@@ -39,7 +40,8 @@ const watch = function (args, ignoreWatch, verboseWatch) {
3940

4041
const run = (event) => {
4142
const childEvent = { childEvent: event }
42-
const env = Object.assign({}, process.loadEnvFile().parsed, process.env, childEvent)
43+
safeParse()
44+
const env = Object.assign({}, process.env, childEvent)
4345
const _child = cp.fork(forkPath, args, {
4446
env,
4547
cwd: process.cwd(),

print-plugins.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict'
44

55
const parseArgs = require('./args')
6+
const { safeParse } = require('./env-parser')
67
const log = require('./log')
78
const {
89
exit,
@@ -38,7 +39,7 @@ function printPlugins (args) {
3839
}
3940

4041
async function runFastify (opts) {
41-
process.loadEnvFile()
42+
safeParse()
4243

4344
let file = null
4445

print-routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict'
44

55
const parseArgs = require('./args')
6+
const { safeParse } = require('./env-parser')
67
const log = require('./log')
78
const {
89
exit,
@@ -38,7 +39,7 @@ function printRoutes (args) {
3839
}
3940

4041
async function runFastify (opts) {
41-
process.loadEnvFile()
42+
safeParse()
4243

4344
let file = null
4445

start.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
'use strict'
44

5-
process.loadEnvFile()
5+
const { safeParse } = require('./env-parser')
6+
safeParse()
67
const isDocker = require('is-docker')
8+
79
const closeWithGrace = require('close-with-grace')
810
const deepmerge = require('@fastify/deepmerge')({
911
cloneProtoObject (obj) { return obj }

templates/eject-esm/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import closeWithGrace from 'close-with-grace'
88
import appService from './app.js'
99

1010
// Dotenv config
11-
process.loadEnvFile()
11+
try {
12+
process.loadEnvFile()
13+
} catch {}
1214

1315
// Instantiate Fastify with some config
1416
const app = Fastify({

templates/eject-ts/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Fastify from 'fastify'
33

44
// Require library to exit fastify process, gracefully (if possible)
55
import closeWithGrace from 'close-with-grace'
6-
process.loadEnvFile()
6+
try {
7+
process.loadEnvFile()
8+
} catch {}
79

810
// Instantiate Fastify with some config
911
const app = Fastify({

0 commit comments

Comments
 (0)