Skip to content

Commit

Permalink
fix: display config validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 10, 2019
1 parent 0fd677a commit a06de4d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 130 deletions.
39 changes: 20 additions & 19 deletions packages/saber/src/cli-commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ module.exports = function(cli) {
.option('--skip-compilation', 'Skip the webpack compilation process')
.option('--inspect-webpack', 'Inspect webpack config in your editor')
.option('--no-cache', 'Disable cache')
.action((cwd = '.', options) => {
setNodeEnv('production')
.action(
handleError((cwd = '.', options) => {
setNodeEnv('production')

if (cli.matchedCommandName === 'generate') {
require('saber-log').log.warn(
`The "generate" command is now deprecated, please use "build" instead.`
)
}
if (cli.matchedCommandName === 'generate') {
require('saber-log').log.warn(
`The "generate" command is now deprecated, please use "build" instead.`
)
}

const { skipCompilation, cache } = options
delete options.skipCompilation
delete options.cache
const { skipCompilation, cache } = options
delete options.skipCompilation
delete options.cache

return require('..')
.createSaber(Object.assign({ cwd, dev: false }, options), {
build: {
cache
}
})
.build({ skipCompilation })
.catch(handleError)
})
return require('..')
.createSaber(Object.assign({ cwd, dev: false }, options), {
build: {
cache
}
})
.build({ skipCompilation })
})
)
}
41 changes: 21 additions & 20 deletions packages/saber/src/cli-commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,26 @@ module.exports = function(cli) {
.option('--host <host>', 'Server host', { default: '0.0.0.0' })
.option('--inspect-webpack', 'Inspect webpack config in your editor')
.option('--no-cache', 'Disable cache')
.action((cwd = '.', options) => {
setNodeEnv('development')
.action(
handleError((cwd = '.', options) => {
setNodeEnv('development')

const { host, port, lazy, cache } = options
delete options.host
delete options.port
delete options.lazy
return require('..')
.createSaber(Object.assign({ cwd, dev: true }, options), {
server: {
host,
port
},
build: {
lazy,
cache
}
})
.serve()
.catch(handleError)
})
const { host, port, lazy, cache } = options
delete options.host
delete options.port
delete options.lazy
return require('..')
.createSaber(Object.assign({ cwd, dev: true }, options), {
server: {
host,
port
},
build: {
lazy,
cache
}
})
.serve()
})
)
}
133 changes: 62 additions & 71 deletions packages/saber/src/cli-commands/eject-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,94 +30,87 @@ module.exports = function(cli) {
.option('--path <path>', 'Ejected theme destination', {
default: './theme'
})
.action(async (cwd = '.', options) => {
cwd = path.resolve(cwd)
const { git } = options
const mergeDependencies = options['merge-dependencies']

const config =
configLoader.load({ cwd, files: configLoader.CONFIG_FILES }).data || {}
if (!config.theme) {
handleError('No theme specified in config.')
}

const destPath = path.join(cwd, options.path)
const relativeDest = path.relative(cwd, destPath)
if (await fs.pathExists(destPath)) {
handleError(
`The path ${options.path} already exists. Please specify a different one using "--path".`
)
}
.action(
handleError(async (cwd = '.', options) => {
cwd = path.resolve(cwd)
const { git } = options
const mergeDependencies = options['merge-dependencies']

const config =
configLoader.load({ cwd, files: configLoader.CONFIG_FILES }).data ||
{}
if (!config.theme) {
throw new Error('No theme specified in config.')
}

const themePath = resolvePackage(config.theme, {
prefix: 'saber-theme-',
cwd
})
if (!themePath) {
handleError(
`Theme "${config.theme}" could not be found in your node_modules.`
)
}
const destPath = path.join(cwd, options.path)
const relativeDest = path.relative(cwd, destPath)
if (await fs.pathExists(destPath)) {
throw new Error(
`The path ${options.path} already exists. Please specify a different one using "--path".`
)
}

const themePackage = configLoader.load({
cwd: themePath,
files: ['package.json']
}).data
const themePath = resolvePackage(config.theme, {
prefix: 'saber-theme-',
cwd
})
if (!themePath) {
throw new Error(
`Theme "${config.theme}" could not be found in your node_modules.`
)
}

const themePackage = configLoader.load({
cwd: themePath,
files: ['package.json']
}).data

if (git) {
const repo = themePackage.repository
if (git) {
const repo = themePackage.repository

if (repo && (!repo.type || repo.type === 'git')) {
const tmp = path.join(cwd, '.saber', 'theme-tmp')
if (repo && (!repo.type || repo.type === 'git')) {
const tmp = path.join(cwd, '.saber', 'theme-tmp')

const { shortcut, url } = normalizeRepo(themePackage.repository)
const downloadError = await downloadRepo(shortcut || url, tmp, {
clone: Boolean(shortcut)
})
const { shortcut, url } = normalizeRepo(themePackage.repository)
const downloadError = await downloadRepo(shortcut || url, tmp, {
clone: Boolean(shortcut)
})

if (downloadError) {
handleError(downloadError)
}
if (downloadError) {
throw downloadError
}

try {
await fs.move(
repo.directory ? path.join(tmp, repo.directory) : tmp,
destPath
)

await fs.remove(tmp)
} catch (error) {
handleError(error)
}

log.success('Downloaded theme source via Git.')
log.success('Downloaded theme source via Git.')
} else {
throw new Error(
'The theme has no git repository specified within its package.json.'
)
}
} else {
handleError(
'The theme has no git repository specified within its package.json.'
)
}
} else {
try {
await fs.copy(themePath, destPath, {
filter: src => !src.endsWith('/node_modules')
})

log.info('Copied theme from node_modules.')
} catch (error) {
handleError(error)
}
}

if (mergeDependencies) {
const dependencies = themePackage.dependencies || {}
const devDependencies = themePackage.devDependencies || {}
if (mergeDependencies) {
const dependencies = themePackage.dependencies || {}
const devDependencies = themePackage.devDependencies || {}

const projectPackage = configLoader.load({
cwd,
files: ['package.json']
}).data
const projectPackage = configLoader.load({
cwd,
files: ['package.json']
}).data

try {
await fs.writeJson(
path.join(cwd, 'package.json'),
{
Expand Down Expand Up @@ -147,13 +140,11 @@ module.exports = function(cli) {
}

log.success('Merged theme dependencies.')
} catch (error) {
handleError(error)
}
}

log.info(
`Please change "theme" in your Saber config to "./${relativeDest}".`
)
})
log.info(
`Please change "theme" in your Saber config to "./${relativeDest}".`
)
})
)
}
31 changes: 16 additions & 15 deletions packages/saber/src/cli-commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ module.exports = function(cli) {
.command('serve [app-path]', 'Serve the output directory')
.option('--host <host>', 'Server host', { default: '0.0.0.0' })
.option('--port <port>', 'Server port', { default: 3000 })
.action((cwd = '.', options) => {
setNodeEnv('production')
.action(
handleError((cwd = '.', options) => {
setNodeEnv('production')

const { host, port } = options
delete options.host
delete options.port
return require('..')
.createSaber(Object.assign({ cwd, dev: true }, options), {
server: {
host,
port
}
})
.serveOutDir()
.catch(handleError)
})
const { host, port } = options
delete options.host
delete options.port
return require('..')
.createSaber(Object.assign({ cwd, dev: true }, options), {
server: {
host,
port
}
})
.serveOutDir()
})
)
}
14 changes: 10 additions & 4 deletions packages/saber/src/cli-commands/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ module.exports = {
process.env.NODE_ENV = env
}
},
handleError(err) {
if (typeof err === 'string') err = new Error(err)
require('saber-log').log.error(err.stack)
process.exit(1) // eslint-disable-line
handleError(fn) {
return async (...args) => {
try {
await fn(...args)
} catch (error) {
const message = typeof error === 'string' ? error : error.stack
require('saber-log').log.error(message)
process.exit(1) // eslint-disable-line
}
}
},
spawn(...args) {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/saber/src/utils/validateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function validateConfig(
const [err, result] = schema.validate(config)

if (err) {
throw err
throw new Error(`Invalid Saber config: ${err.message}`)
}

if (dev) {
Expand Down

0 comments on commit a06de4d

Please sign in to comment.