Update dependencies and improve build system
This commit is contained in:
17
service/utils/getLocalIP.js
Normal file
17
service/utils/getLocalIP.js
Normal file
@@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
const os = require('os')
|
||||
|
||||
module.exports = function getLocalIP() {
|
||||
const interfaces = os.networkInterfaces()
|
||||
|
||||
for (const devName in interfaces) {
|
||||
const iface = interfaces[devName]
|
||||
for (let i = 0; i < iface.length; i++) {
|
||||
const alias = iface[i]
|
||||
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
|
||||
return alias.address
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
39
service/utils/loadEnv.js
Normal file
39
service/utils/loadEnv.js
Normal file
@@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
const dotenv = require('dotenv')
|
||||
const dotenvExpand = require('dotenv-expand')
|
||||
const { error } = require('./logger')
|
||||
|
||||
module.exports = function loadEnv(mode) {
|
||||
const basePath = path.resolve(process.cwd(), `.env${mode ? `.${mode}` : ``}`)
|
||||
const localPath = `${basePath}.local`
|
||||
|
||||
const load = (envPath) => {
|
||||
try {
|
||||
const env = dotenv.config({ path: envPath, debug: process.env.DEBUG })
|
||||
dotenvExpand(env)
|
||||
} catch (err) {
|
||||
// only ignore error if file is not found
|
||||
if (err.toString().indexOf('ENOENT') < 0) {
|
||||
error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
load(localPath)
|
||||
load(basePath)
|
||||
|
||||
// by default, NODE_ENV and BABEL_ENV are set to "development" unless mode
|
||||
// is production or test. However the value in .env files will take higher
|
||||
// priority.
|
||||
if (mode) {
|
||||
const defaultNodeEnv = mode === 'production' || mode === 'test' ? mode : 'development'
|
||||
if (process.env.NODE_ENV == null) {
|
||||
process.env.NODE_ENV = defaultNodeEnv
|
||||
}
|
||||
if (process.env.BABEL_ENV == null) {
|
||||
process.env.BABEL_ENV = defaultNodeEnv
|
||||
}
|
||||
}
|
||||
}
|
||||
72
service/utils/logger.js
Normal file
72
service/utils/logger.js
Normal file
@@ -0,0 +1,72 @@
|
||||
'use strict'
|
||||
|
||||
const chalk = require('chalk')
|
||||
const stripAnsi = require('strip-ansi')
|
||||
const readline = require('readline')
|
||||
const EventEmitter = require('events')
|
||||
|
||||
exports.events = new EventEmitter()
|
||||
|
||||
function _log(type, tag, message) {
|
||||
if (process.env.VUE_CLI_API_MODE && message) {
|
||||
exports.events.emit('log', {
|
||||
message,
|
||||
type,
|
||||
tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const format = (label, msg) => {
|
||||
return msg
|
||||
.split('\n')
|
||||
.map((line, i) => {
|
||||
return i === 0 ? `${label} ${line}` : line.padStart(stripAnsi(label).length)
|
||||
})
|
||||
.join('\n')
|
||||
}
|
||||
|
||||
const chalkTag = (msg) => chalk.bgBlackBright.white.dim(` ${msg} `)
|
||||
|
||||
exports.log = (msg = '', tag = null) => {
|
||||
tag ? console.log(format(chalkTag(tag), msg)) : console.log(msg)
|
||||
_log('log', tag, msg)
|
||||
}
|
||||
|
||||
exports.info = (msg, tag = null) => {
|
||||
console.log(format(chalk.bgBlue.black(' INFO ') + (tag ? chalkTag(tag) : ''), msg))
|
||||
_log('info', tag, msg)
|
||||
}
|
||||
|
||||
exports.done = (msg, tag = null) => {
|
||||
console.log(format(chalk.bgGreen.black(' DONE ') + (tag ? chalkTag(tag) : ''), msg))
|
||||
_log('done', tag, msg)
|
||||
}
|
||||
|
||||
exports.warn = (msg, tag = null) => {
|
||||
console.warn(
|
||||
format(chalk.bgYellow.black(' WARN ') + (tag ? chalkTag(tag) : ''), chalk.yellow(msg))
|
||||
)
|
||||
_log('warn', tag, msg)
|
||||
}
|
||||
|
||||
exports.error = (msg, tag = null) => {
|
||||
console.error(format(chalk.bgRed(' ERROR ') + (tag ? chalkTag(tag) : ''), chalk.red(msg)))
|
||||
_log('error', tag, msg)
|
||||
if (msg instanceof Error) {
|
||||
console.error(msg.stack)
|
||||
_log('error', tag, msg.stack)
|
||||
}
|
||||
}
|
||||
|
||||
exports.clearConsole = (title) => {
|
||||
if (process.stdout.isTTY) {
|
||||
const blank = '\n'.repeat(process.stdout.rows)
|
||||
console.log(blank)
|
||||
readline.cursorTo(process.stdout, 0, 0)
|
||||
readline.clearScreenDown(process.stdout)
|
||||
if (title) {
|
||||
console.log(title)
|
||||
}
|
||||
}
|
||||
}
|
||||
9
service/utils/paths.js
Normal file
9
service/utils/paths.js
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const path = require('path')
|
||||
|
||||
// gen static file path
|
||||
exports.getAssetPath = (...args) => path.posix.join('static', ...args)
|
||||
|
||||
// gen absolute path
|
||||
exports.resolve = (...args) => path.posix.join(process.cwd(), ...args)
|
||||
22
service/utils/resolveClientEnv.js
Normal file
22
service/utils/resolveClientEnv.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const prefixRE = /^VUE_APP_/
|
||||
|
||||
module.exports = function resolveClientEnv(options, raw) {
|
||||
const env = {}
|
||||
Object.keys(process.env).forEach((key) => {
|
||||
if (prefixRE.test(key) || key === 'NODE_ENV') {
|
||||
env[key] = process.env[key]
|
||||
}
|
||||
})
|
||||
env.PUBLIC_PATH = options.publicPath
|
||||
|
||||
if (raw) {
|
||||
return env
|
||||
}
|
||||
|
||||
for (const key in env) {
|
||||
env[key] = JSON.stringify(env[key])
|
||||
}
|
||||
return {
|
||||
'process.env': env,
|
||||
}
|
||||
}
|
||||
57
service/utils/spinner.js
Normal file
57
service/utils/spinner.js
Normal file
@@ -0,0 +1,57 @@
|
||||
'use strict'
|
||||
|
||||
const ora = require('ora')
|
||||
const chalk = require('chalk')
|
||||
|
||||
const spinner = ora()
|
||||
let lastMsg = null
|
||||
let isPaused = false
|
||||
|
||||
exports.logWithSpinner = (symbol, msg) => {
|
||||
if (!msg) {
|
||||
msg = symbol
|
||||
symbol = chalk.green('✔')
|
||||
}
|
||||
if (lastMsg) {
|
||||
spinner.stopAndPersist({
|
||||
symbol: lastMsg.symbol,
|
||||
text: lastMsg.text,
|
||||
})
|
||||
}
|
||||
spinner.text = ' ' + msg
|
||||
lastMsg = {
|
||||
symbol: symbol + ' ',
|
||||
text: msg,
|
||||
}
|
||||
spinner.start()
|
||||
}
|
||||
|
||||
exports.stopSpinner = (persist) => {
|
||||
if (lastMsg && persist !== false) {
|
||||
spinner.stopAndPersist({
|
||||
symbol: lastMsg.symbol,
|
||||
text: lastMsg.text,
|
||||
})
|
||||
} else {
|
||||
spinner.stop()
|
||||
}
|
||||
lastMsg = null
|
||||
}
|
||||
|
||||
exports.pauseSpinner = () => {
|
||||
if (spinner.isSpinning) {
|
||||
spinner.stop()
|
||||
isPaused = true
|
||||
}
|
||||
}
|
||||
|
||||
exports.resumeSpinner = () => {
|
||||
if (isPaused) {
|
||||
spinner.start()
|
||||
isPaused = false
|
||||
}
|
||||
}
|
||||
|
||||
exports.failSpinner = (text) => {
|
||||
spinner.fail(text)
|
||||
}
|
||||
Reference in New Issue
Block a user