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 inputFile = await fs.readFile(inputPath)
|
||||||
const inputJSON = JSON.parse(inputFile)
|
const inputJSON = JSON.parse(inputFile)
|
||||||
|
|
||||||
const result = solution.apply(null, inputJSON)
|
try {
|
||||||
await fs.writeFile(outputPath, JSON.stringify(result))
|
const result = solution.apply(null, inputJSON)
|
||||||
|
await fs.writeFile(outputPath, JSON.stringify(result))
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from solution import solution
|
from solution import solution
|
||||||
|
|
||||||
|
@ -11,8 +11,12 @@ const main = async () => {
|
|||||||
const inputFile = await fs.readFile(inputPath)
|
const inputFile = await fs.readFile(inputPath)
|
||||||
const inputJSON = JSON.parse(inputFile.toString())
|
const inputJSON = JSON.parse(inputFile.toString())
|
||||||
|
|
||||||
const result = solution.apply(null, inputJSON)
|
try {
|
||||||
await fs.writeFile(outputPath, JSON.stringify(result))
|
const result = solution.apply(null, inputJSON)
|
||||||
|
await fs.writeFile(outputPath, JSON.stringify(result))
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
@ -8,13 +8,23 @@ import deleteAllFilesExceptOne from './utils/deleteAllFilesExceptOne'
|
|||||||
import emoji from 'node-emoji'
|
import emoji from 'node-emoji'
|
||||||
import prettyMilliseconds from 'pretty-ms'
|
import prettyMilliseconds from 'pretty-ms'
|
||||||
import { table } from 'table'
|
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 () => {
|
;(async () => {
|
||||||
const fs = fsWithCallbacks.promises
|
const fs = fsWithCallbacks.promises
|
||||||
const exec = util.promisify(childProcess.exec)
|
const exec = util.promisify(childProcess.exec)
|
||||||
const args = process.argv.slice(2)
|
const args = process.argv.slice(2)
|
||||||
const [challengeName, solutionName] = args
|
const [challengeName, solutionName] = args
|
||||||
|
|
||||||
if (!challengeName || !solutionName) {
|
if (challengeName == null || solutionName == null) {
|
||||||
console.log(`
|
console.log(`
|
||||||
Please specify the challenge and solution name:
|
Please specify the challenge and solution name:
|
||||||
${chalk.cyan(`npm run test [challenge-name] [solution-name]`)}
|
${chalk.cyan(`npm run test [challenge-name] [solution-name]`)}
|
||||||
@ -55,7 +65,7 @@ import { table } from 'table'
|
|||||||
|
|
||||||
// Determinate the language to execute
|
// Determinate the language to execute
|
||||||
const solutionFilesName = await fs.readdir(solutionFolderPath)
|
const solutionFilesName = await fs.readdir(solutionFolderPath)
|
||||||
let solutionFilePath
|
let solutionFilePath = null
|
||||||
for (const solutionFileName of solutionFilesName) {
|
for (const solutionFileName of solutionFilesName) {
|
||||||
const fileName = solutionFileName
|
const fileName = solutionFileName
|
||||||
.split('.')
|
.split('.')
|
||||||
@ -66,7 +76,7 @@ import { table } from 'table'
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!solutionFilePath) {
|
if (solutionFilePath == null) {
|
||||||
console.log(`The ${chalk.red('solution')} file was not found.`)
|
console.log(`The ${chalk.red('solution')} file was not found.`)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
@ -79,7 +89,7 @@ import { table } from 'table'
|
|||||||
const languageToExecute = languages.find(
|
const languageToExecute = languages.find(
|
||||||
language => language.extension === extensionSolution
|
language => language.extension === extensionSolution
|
||||||
)
|
)
|
||||||
if (!languageToExecute) {
|
if (languageToExecute == null) {
|
||||||
console.log(`Sadly, this ${chalk.red('language')} is not supported yet.`)
|
console.log(`Sadly, this ${chalk.red('language')} is not supported yet.`)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
@ -131,11 +141,14 @@ import { table } from 'table'
|
|||||||
|
|
||||||
// Execute script (create output.json)
|
// Execute script (create output.json)
|
||||||
try {
|
try {
|
||||||
await exec(`${languageToExecute.launch} ${executeLanguageTempPath}`)
|
const { stderr } = await exec(
|
||||||
|
`${languageToExecute.launch} ${executeLanguageTempPath}`
|
||||||
|
)
|
||||||
|
if (stderr.length !== 0) {
|
||||||
|
await handleStderror(stderr, tempPath)
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(chalk.bgRedBright.black(error.stderr))
|
await handleStderror(error.stderr, tempPath)
|
||||||
await deleteAllFilesExceptOne(tempPath, '.gitignore')
|
|
||||||
process.exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read output.json
|
// Read output.json
|
||||||
|
Loading…
Reference in New Issue
Block a user