Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 29f0098

Browse files
committed
fix(docz-core): set env vars for commands
1 parent 78b1956 commit 29f0098

File tree

12 files changed

+60
-37
lines changed

12 files changed

+60
-37
lines changed

packages/docz-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"cpy": "^7.0.1",
4343
"deepmerge": "^2.1.1",
4444
"detect-port": "^1.2.3",
45+
"env-dot-prop": "^1.0.2",
4546
"express": "^4.16.3",
4647
"fast-glob": "^2.2.2",
4748
"file-loader": "^1.1.11",

packages/docz-core/src/Bundler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ export class Bundler<C = ConfigObj> {
5656

5757
public getConfig(env: Env): C {
5858
const babelConfig = babelrc(this.args, env)
59-
const config = this.mountConfig(this.config(babelConfig))
59+
const config = this.mountConfig(this.config(babelConfig), env)
6060

61-
return this.args.modifyBundlerConfig(config, !this.isProd(), this.args)
61+
return this.args.modifyBundlerConfig(config, !this.isProd(env), this.args)
6262
}
6363

6464
public async createServer(config: C): Promise<BundlerServer> {
@@ -90,14 +90,14 @@ export class Bundler<C = ConfigObj> {
9090
await this.builder(config, dist)
9191
}
9292

93-
private mountConfig(config: C): any {
93+
private mountConfig(config: C, env: Env): any {
9494
const { plugins } = this.args
9595
const reduce = Plugin.reduceFromPlugins<C>(plugins)
9696

97-
return reduce('modifyBundlerConfig', config, !this.isProd(), this.args)
97+
return reduce('modifyBundlerConfig', config, !this.isProd(env), this.args)
9898
}
9999

100-
private isProd(): boolean {
101-
return process.env.NODE_ENV === 'production'
100+
private isProd(env: Env): boolean {
101+
return env === 'production'
102102
}
103103
}

packages/docz-core/src/bundlers/webpack/build.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@ import webpack, { Configuration as CFG } from 'webpack'
55
import FSR from 'react-dev-utils/FileSizeReporter'
66
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages'
77
import printBuildError from 'react-dev-utils/printBuildError'
8+
import envDotProp from 'env-dot-prop'
89

910
import * as paths from '../../config/paths'
1011

11-
process.env.BABEL_ENV = process.env.BABEL_ENV || 'production'
12-
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
13-
1412
const { measureFileSizesBeforeBuild, printFileSizesAfterBuild } = FSR
1513
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024
1614
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024
1715

18-
const hasCiEnvVar = () =>
19-
process.env.CI &&
20-
(typeof process.env.CI !== 'string' ||
21-
process.env.CI.toLowerCase() !== 'false')
16+
const hasCiEnvVar = () => envDotProp.get('ci', false, { parse: true })
2217

2318
const copyPublicFolder = async (dest: string): Promise<void> => {
2419
if (await fs.pathExists(paths.appPublic)) {

packages/docz-core/src/commands/args.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as fs from 'fs-extra'
22
import humanize from 'humanize-string'
33
import titleize from 'titleize'
4+
import envDotProp from 'env-dot-prop'
45

56
import { Plugin } from '../Plugin'
67
import { BabelRC } from '../utils/babelrc'
78
import * as paths from '../config/paths'
89

10+
const getEnv = (val: string, defaultValue: any = null): any =>
11+
envDotProp.get(val, defaultValue, { parse: true })
12+
913
const removeScope = (name: string) => name.replace(/^@.*\//, '')
1014
const getInitialTitle = (): string => {
1115
const pkg = fs.readJsonSync(paths.packageJson, { throws: false })
@@ -103,27 +107,27 @@ export const args = (yargs: any) => {
103107
})
104108
yargs.positional('debug', {
105109
type: 'boolean',
106-
default: process.env.DEBUG || false,
110+
default: getEnv('debug', false),
107111
})
108112
yargs.positional('protocol', {
109113
type: 'string',
110-
default: process.env.HTTPS === 'true' ? 'https' : 'http',
114+
default: getEnv('https') ? 'https' : 'http',
111115
})
112116
yargs.positional('host', {
113117
type: 'string',
114-
default: process.env.HOST || '127.0.0.1',
118+
default: getEnv('host', '127.0.0.1'),
115119
})
116120
yargs.positional('port', {
117121
alias: 'p',
118122
type: 'number',
119-
default: process.env.PORT || 3000,
123+
default: getEnv('port', 3000),
120124
})
121125
yargs.positional('websocketHost', {
122126
type: 'string',
123-
default: process.env.WEBSOCKET_HOST || '127.0.0.1',
127+
default: getEnv('websocket.host', '127.0.0.1'),
124128
})
125129
yargs.positional('websocketPort', {
126130
type: 'number',
127-
default: process.env.WEBSOCKET_PORT || 8089,
131+
default: getEnv('websocket.port', 8089),
128132
})
129133
}

packages/docz-core/src/commands/build.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
process.env.BABEL_ENV = 'production'
2-
process.env.NODE_ENV = 'production'
3-
41
import * as fs from 'fs-extra'
52
import logger from 'signale'
3+
import envDotProp from 'env-dot-prop'
64

75
import * as paths from '../config/paths'
86
import { loadConfig } from '../utils/load-config'
97
import { webpack } from '../bundlers'
108
import { Entries } from '../Entries'
11-
import { Config, Env } from './args'
9+
import { Config } from './args'
1210
import { Plugin } from '../Plugin'
1311

14-
const env = process.env.NODE_ENV as Env
15-
1612
export const build = async (args: Config) => {
13+
const env = envDotProp.get('node.env')
1714
const config = loadConfig(args)
1815
const bundler = webpack(config, env)
1916
const entries = new Entries(config)

packages/docz-core/src/commands/dev.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
process.env.BABEL_ENV = 'development'
2-
process.env.NODE_ENV = 'development'
3-
41
import * as fs from 'fs-extra'
52
import logger from 'signale'
63
import detectPort from 'detect-port'
4+
import envDotProp from 'env-dot-prop'
75

86
import * as paths from '../config/paths'
9-
import { Config, Env } from './args'
7+
import { Config } from './args'
108
import { DataServer } from '../DataServer'
119
import { webpack } from '../bundlers'
1210
import { Entries } from '../Entries'
1311
import { loadConfig } from '../utils/load-config'
1412

15-
const env = process.env.NODE_ENV as Env
16-
1713
export const dev = async (args: Config) => {
14+
const env = envDotProp.get('node.env')
1815
const config = loadConfig(args)
1916
const port = await detectPort(config.port)
2017
const websocketPort = await detectPort(config.websocketPort)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export { dev } from './dev'
22
export { build } from './build'
3-
export { args } from './args'

packages/docz-core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as commands from './commands'
2+
import { args } from './commands/args'
23

3-
export { commands }
4+
export { commands, args }
45
export { Config } from './commands/args'
56
export { Plugin, createPlugin } from './Plugin'
67
export { BabelRC } from './utils/babelrc'

packages/docz-core/src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare module '@mdx-js/mdx'
33
declare module '@sindresorhus/slugify'
44
declare module 'babylon'
55
declare module 'babel-traverse'
6+
declare module 'env-dot-prop'
67
declare module 'chokidar'
78
declare module 'humanize-string'
89
declare module 'titleize'

packages/docz/bin/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
#!/usr/bin/env node
22

33
const yargs = require('yargs')
4-
const { commands } = require('docz-core')
4+
const envDotProp = require('env-dot-prop')
5+
const { args: defaultArgs } = require('docz-core')
6+
7+
const setEnv = val => {
8+
envDotProp.set('babel.env', val)
9+
envDotProp.set('node.env', val)
10+
}
11+
12+
const execCommand = (cmd, args) => {
13+
require('docz-core').commands[cmd](args)
14+
}
515

616
yargs
7-
.command('dev', 'initialize docz dev server', commands.args, commands.dev)
8-
.command('build', 'build dir as static site', commands.args, commands.build)
17+
.command('dev', 'initialize docz dev server', defaultArgs, args => {
18+
setEnv('development')
19+
execCommand('dev', args)
20+
})
21+
.command('build', 'build dir as static site', defaultArgs, args => {
22+
setEnv('production')
23+
execCommand('build', args)
24+
})
925
.demandCommand()
1026
.help()
1127
.wrap(72)

0 commit comments

Comments
 (0)