Skip to content

Commit

Permalink
perf: use EdamError thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Sep 5, 2018
1 parent 08fe86c commit 845bafe
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
3 changes: 2 additions & 1 deletion packages/edam/src/core/Compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import hookify from './hookify'
import matchMeta from './matchMeta'
import parseQueryString from '../../lib/parseQueryString'
import DefaultLogger from '../DefaultLogger'
import EdamError from '../EdamError';

const debug = require('debug')('edam:Compiler')

Expand Down Expand Up @@ -138,7 +139,7 @@ export default class Compiler extends AwaitEventEmitter {
loader = this.loaders[id]

if (!loader) {
throw new Error(`loaderId: ${id} is not matched.`)
throw new EdamError(`loaderId: ${id} is not matched.`)
}
return await this.transform(
input,
Expand Down
5 changes: 3 additions & 2 deletions packages/edam/src/core/plugins/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Edam } from '../../index'
import * as nps from 'path'
import * as _ from 'lodash'
import * as mm from 'micromatch'
import EdamError from '../EdamError';

const tildify = require('tildify')

Expand All @@ -23,12 +24,12 @@ export default async function filter(/*options*/) {
// const templateConfig = edam.templateConfig

if (!templateConfig.root) {
throw new Error('templateConfig.root is required on pre treat: filter')
throw new EdamError('templateConfig.root is required on pre treat: filter')
}
const compiler = edam.compiler
let { root, ignore } = templateConfig
if (!fileSystem.isDirectory(root)) {
throw new Error(`templateConfig.root ${tildify(root)} is illegal, maybe it is not existed or is file path.`)
throw new EdamError(`templateConfig.root ${tildify(root)} is illegal, maybe it is not existed or is file path.`)
}

if (_.isFunction(ignore)) {
Expand Down
17 changes: 6 additions & 11 deletions packages/edam/src/core/promptProcessor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Prompt, Variables } from '../../types/TemplateConfig'
import evaluate from '../../lib/eval'
import cliProcess from './cli'
import * as _ from 'lodash'
import EdamError from '../EdamError';

const debug = require('debug')('edam:promptProcessor')

Expand All @@ -17,12 +18,12 @@ export default async function prompt(
{ yes = true, promptProcess = cliProcess, context = {} } = {}
): Promise<Variables> {
if (!_.isFunction(promptProcess)) {
throw new Error('prompt is missing the process')
throw new EdamError('prompt is missing the process')
}
debug('input: %O', prompts)
const res = await pReduce(
prompts,
async function(set, prompt) {
async (set, prompt) => {
prompt = { ...prompt }
let allow = true
if (_.isString(prompt.when)) {
Expand Down Expand Up @@ -65,16 +66,10 @@ export default async function prompt(
if (_.isFunction(validate)) {
let message = await validate(value, set, context)
if (typeof message === 'string') {
value = prompt.default
this.logger &&
this.logger.warn(
'validate %s failed, error message: %s, so the value "%s" fallback to default value.',
prompt.name,
message,
value
)
return set
// value = prompt.default
throw new EdamError(`Validate ${JSON.stringify(prompt.name)} failed, error message: ${message}`)
}
return set
}

Object.assign(set, {
Expand Down
4 changes: 2 additions & 2 deletions packages/edam/src/core/pull/file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Source } from '../../types/Options'
import Error from '../EdamError'
import EdamError from '../EdamError'
import * as nps from 'path'

module.exports = function(source: Source, /*edam: Edam*/): string {
// console.log(source.url)

if (!nps.isAbsolute(source.url)) {
throw new Error(
throw new EdamError(
`check the source: ${source.url} is not absolute path of file`
)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/edam/src/core/pull/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EdamConfig, Source } from '../../types/Options'
import gitClone, { checkout, pullForce } from '../../lib/gitClone'
import { join } from 'path'
import fileSystem from '../../lib/fileSystem'
import EdamError from '../EdamError';

const filenamify = require('filenamify')
const npmInstall = require('../../lib/yarnInstall')
Expand Down Expand Up @@ -81,7 +82,7 @@ module.exports = async function gitPull(
production: true
})
} catch (err) {
throw new Error(
throw new EdamError(
`Install package from git "${source.url}" failed \n` +
err.stack
)
Expand Down Expand Up @@ -127,7 +128,7 @@ module.exports = async function gitPull(
await pullForce(target)
await checkout(source.checkout || 'master', { targetPath: target })
} catch (err) {
throw new Error(err.message + '\n running in ' + tildify(target))
throw new EdamError(err.message + '\n running in ' + tildify(target))
}
log('Using cached git assets from %s', c.cyan.bold(tildify(target)))
await installFromPkg(target)
Expand Down Expand Up @@ -172,7 +173,7 @@ module.exports = async function gitPull(
}

default:
throw new Error(
throw new EdamError(
`Git pull type "${git}" is not allowed, please use one of 'clone' | 'download'`
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/edam/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const tildify = require('tildify')
const debug = require('debug')('edam:core')

function throwEdamError(err, message) {
if (err && err.id === '') {
if (err && err.id === 'EDAM_ERROR') {
throw err
}
throw `${message} ${err.stack}`
throw new EdamError(`${message} ${err.stack}`)
}

export class Edam extends AwaitEventEmitter {
Expand Down
4 changes: 3 additions & 1 deletion packages/edam/src/lib/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Options } from '../core/normalizeSource'
import safeRequireResolve from './safeRequireResolve'
import EdamError from '../core/EdamError';

const JSON5 = require('json5')
const cosmiconfig = require('cosmiconfig')
const nps = require('path')
const fileSystem = require('./fileSystem').default
const tildify = require('tildify')
const explorer = cosmiconfig('edam', { rcStrictJson: true })

const debug = require('debug')('edam:loadConfig')
Expand All @@ -14,7 +16,7 @@ async function parseJSONFile(filename) {
const text = await fileSystem.readFile(filename, { encoding: 'utf8' })
return JSON5.parse(text)
} catch (err) {
throw new Error(`${err.message} in file: "${filename}"`)
throw new EdamError(`${err.message} in file: "${tildify(filename)}"`)
}
}

Expand Down

0 comments on commit 845bafe

Please sign in to comment.