Skip to content

Commit

Permalink
fix: friendly error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Chieffo2021 authored and Chieffo committed Dec 15, 2021
1 parent 8558415 commit 6bd0158
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
31 changes: 29 additions & 2 deletions src/ast-parse/astParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ export async function astParseRoot (rootDir: string, config: Config): Promise<As
}

// execute the transformation
tempTransformationResult = await transformation.astTransform(fileInfo, transformationParams)
try {
tempTransformationResult = await transformation.astTransform(fileInfo, transformationParams)
} catch (e) {
if (extension === '.js') {
console.warn(
'\n\nFailed to parse source for import analysis because the content contains invalid JS syntax. ' +
'If you are using JSX, make sure to name the file with the .jsx or .tsx extension.'
)
}
console.error(`AST parsing and transformation file failed, filePath: ${filePath}\n`, e)
console.log('skip parsing the error file...')
continue
}

if (tempTransformationResult == null) {
continue
}
Expand Down Expand Up @@ -114,7 +127,21 @@ export async function astParseRoot (rootDir: string, config: Config): Promise<As
}

// parse the file
const parsingResult: ParsingResultOccurrence[] | null = parser.astParse(fileInfo)
let parsingResult: ParsingResultOccurrence[] | null
try {
parsingResult = parser.astParse(fileInfo)
} catch (e) {
if (extension === '.js') {
console.warn(
'\nFailed to parse source for import analysis because the content contains invalid JS syntax. ' +
'If you are using JSX, make sure to name the file with the .jsx or .tsx extension.'
)
}
console.error(`AST parsing file failed, filePath: ${filePath}\n`, e)
console.log('skip parsing the error file...')
continue
}

if (!parsingResult) {
continue
}
Expand Down
5 changes: 1 addition & 4 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { printReport } from '../utils/report'
import cliProgress from 'cli-progress'

const cliInstance = new cliProgress.SingleBar({
format: 'progress [{bar}] {percentage}% | {doSomething} | {value}/{total}',
clearOnComplete: true, // clear the progress bar on complete / stop()
linewrap: true,
fps: 60
format: 'progress [{bar}] {percentage}% | {doSomething} | {value}/{total}'
}, cliProgress.Presets.shades_classic)

const beginTime = Date.now()
Expand Down
65 changes: 39 additions & 26 deletions src/config/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,38 @@ import { VueCliConfig } from './vuecli';
export async function parseWebpackConfig (
configPath: string
): Promise<WebpackConfig> {
// first of all, parse webpack config from path : ${__dirname}/webpack.config.js
let webpackConfig: WebpackConfig = {}
if (fs.existsSync(configPath)) {
await import(configPath).then((config) => {

try {
// first, parse webpack config from path : ${__dirname}/webpack.config.js
if (fs.existsSync(configPath)) {
await import(configPath).then((config) => {
webpackConfig = config
});
return webpackConfig
}
// if webpack.config.js not exists in ${__dirname}/webpack.config.js, scan folder ${__dirname}/build/
const dir = path.dirname(configPath)
let buildDir = path.resolve(dir, 'build')
// if folder ${__dirname}/build/ not exists, scan folder ${__dirname}/webpack/
if (!fs.existsSync(buildDir)) {
buildDir = path.resolve(dir, 'webpack')
}
// default config files: webpack.base.js、webpack.dev.js、webpack.prod.js|webpack.build.js|webpack.production.js
// TODO: production config
const devConfigPath = path.resolve(buildDir, 'webpack.dev.conf.js')
await import(devConfigPath).then((config) => {
webpackConfig = config
});
return webpackConfig
}
// if webpack.config.js not exists in ${__dirname}/webpack.config.js, scan folder ${__dirname}/build/
const dir = path.dirname(configPath)
let buildDir = path.resolve(dir, 'build')
// if folder ${__dirname}/build/ not exists, scan folder ${__dirname}/webpack/
if (!fs.existsSync(buildDir)) {
buildDir = path.resolve(dir, 'webpack')
}
// default config files: webpack.base.js、webpack.dev.js、webpack.prod.js|webpack.build.js|webpack.production.js
// TODO: production config
const devConfigPath = path.resolve(buildDir, 'webpack.dev.conf.js')
if (!fs.existsSync(devConfigPath)) {
console.error(`${devConfigPath} not exists`)
return webpackConfig
})
} catch (e) {
console.error(`\nFailed to parse webpack config from default file path: ${configPath}.`)
console.warn('Note: webpack conversion is based on `webpack.config.js` or' +
' `webpack.base.js/webpack.dev.js/webpack.prod.js` or' +
' `webpack.build.js/webpack.production.js`, map configuration to `vite.config.js`\n' +
'If you are not using configuration files above, you need to convert configurations manually.')
console.error(e)
console.log(`Using default webpack config: ${JSON.stringify(webpackConfig)}.`)
}
await import(devConfigPath).then((config) => {
webpackConfig = config
})

return webpackConfig
}
Expand All @@ -39,10 +46,16 @@ export async function parseVueCliConfig (
configPath: string
): Promise<VueCliConfig> {
let vueCliConfig: VueCliConfig = {}
if (fs.existsSync(configPath)) {
await import(configPath).then((config) => {
vueCliConfig = config
})
try {
if (fs.existsSync(configPath)) {
await import(configPath).then((config) => {
vueCliConfig = config
})
}
} catch (e) {
console.error(`\nFailed to parse vue config from default file path: ${configPath}.`)
console.error(e)
console.log(`Using default vue config: ${JSON.stringify(vueCliConfig)}.`)
}
return vueCliConfig
}

0 comments on commit 6bd0158

Please sign in to comment.