mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
fix(scripts): better handle error case in solution
This commit is contained in:
parent
379f4e1050
commit
d4428a6ade
@ -9,8 +9,12 @@ const main = async () => {
|
||||
const inputFile = await fs.readFile(inputPath)
|
||||
const inputJSON = JSON.parse(inputFile)
|
||||
|
||||
try {
|
||||
const result = solution.apply(null, inputJSON)
|
||||
await fs.writeFile(outputPath, JSON.stringify(result))
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
@ -11,8 +11,12 @@ const main = async () => {
|
||||
const inputFile = await fs.readFile(inputPath)
|
||||
const inputJSON = JSON.parse(inputFile.toString())
|
||||
|
||||
try {
|
||||
const result = solution.apply(null, inputJSON)
|
||||
await fs.writeFile(outputPath, JSON.stringify(result))
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
@ -8,13 +8,23 @@ import deleteAllFilesExceptOne from './utils/deleteAllFilesExceptOne'
|
||||
import emoji from 'node-emoji'
|
||||
import prettyMilliseconds from 'pretty-ms'
|
||||
import { table } from 'table'
|
||||
|
||||
const handleStderror = async (
|
||||
stderr: string,
|
||||
tempPath: string
|
||||
): Promise<void> => {
|
||||
console.log(chalk.bgRedBright.black('Error occurred in your solution :'))
|
||||
console.log(stderr)
|
||||
await deleteAllFilesExceptOne(tempPath, '.gitignore')
|
||||
process.exit(0)
|
||||
}
|
||||
;(async () => {
|
||||
const fs = fsWithCallbacks.promises
|
||||
const exec = util.promisify(childProcess.exec)
|
||||
const args = process.argv.slice(2)
|
||||
const [challengeName, solutionName] = args
|
||||
|
||||
if (!challengeName || !solutionName) {
|
||||
if (challengeName == null || solutionName == null) {
|
||||
console.log(`
|
||||
Please specify the challenge and solution name:
|
||||
${chalk.cyan(`npm run test [challenge-name] [solution-name]`)}
|
||||
@ -55,7 +65,7 @@ import { table } from 'table'
|
||||
|
||||
// Determinate the language to execute
|
||||
const solutionFilesName = await fs.readdir(solutionFolderPath)
|
||||
let solutionFilePath
|
||||
let solutionFilePath = null
|
||||
for (const solutionFileName of solutionFilesName) {
|
||||
const fileName = solutionFileName
|
||||
.split('.')
|
||||
@ -66,7 +76,7 @@ import { table } from 'table'
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!solutionFilePath) {
|
||||
if (solutionFilePath == null) {
|
||||
console.log(`The ${chalk.red('solution')} file was not found.`)
|
||||
process.exit(0)
|
||||
}
|
||||
@ -79,7 +89,7 @@ import { table } from 'table'
|
||||
const languageToExecute = languages.find(
|
||||
language => language.extension === extensionSolution
|
||||
)
|
||||
if (!languageToExecute) {
|
||||
if (languageToExecute == null) {
|
||||
console.log(`Sadly, this ${chalk.red('language')} is not supported yet.`)
|
||||
process.exit(0)
|
||||
}
|
||||
@ -131,11 +141,14 @@ import { table } from 'table'
|
||||
|
||||
// Execute script (create output.json)
|
||||
try {
|
||||
await exec(`${languageToExecute.launch} ${executeLanguageTempPath}`)
|
||||
const { stderr } = await exec(
|
||||
`${languageToExecute.launch} ${executeLanguageTempPath}`
|
||||
)
|
||||
if (stderr.length !== 0) {
|
||||
await handleStderror(stderr, tempPath)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(chalk.bgRedBright.black(error.stderr))
|
||||
await deleteAllFilesExceptOne(tempPath, '.gitignore')
|
||||
process.exit(0)
|
||||
await handleStderror(error.stderr, tempPath)
|
||||
}
|
||||
|
||||
// Read output.json
|
||||
|
Loading…
Reference in New Issue
Block a user