2021-10-29 00:00:43 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const chalk = require('chalk');
|
2022-01-03 00:58:31 +00:00
|
|
|
const { spawnSync } = require('child_process');
|
2021-10-29 00:36:21 +00:00
|
|
|
const perf = require('execution-time')();
|
2021-10-29 00:00:43 +00:00
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
const generateBanner = text => {
|
|
|
|
// Calculate the length of the divider
|
2021-10-29 00:00:43 +00:00
|
|
|
let divider = '--';
|
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
for (let j = 0; j < text.length; j++) {
|
2021-10-29 00:00:43 +00:00
|
|
|
divider += '-';
|
|
|
|
}
|
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
return `${divider}
|
|
|
|
${chalk.bold(chalk.greenBright(text))}
|
|
|
|
${divider}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const run = file => {
|
|
|
|
console.log(generateBanner(file.split('.ts')[0]));
|
2021-10-29 00:00:43 +00:00
|
|
|
|
2021-10-29 00:36:21 +00:00
|
|
|
// Execute the file
|
|
|
|
perf.start();
|
2022-01-03 00:58:31 +00:00
|
|
|
spawnSync('npx', ['ts-node', `"src/${file}"`], {
|
|
|
|
shell: true,
|
|
|
|
stdio: 'inherit'
|
|
|
|
});
|
2021-10-29 00:36:21 +00:00
|
|
|
const results = perf.stop();
|
|
|
|
|
|
|
|
// Print time results
|
|
|
|
console.log();
|
|
|
|
console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`)));
|
2021-10-29 00:00:43 +00:00
|
|
|
};
|
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
// Get files
|
|
|
|
const tsFiles = fs
|
|
|
|
.readdirSync('src')
|
|
|
|
.filter(f => f.endsWith('.ts'))
|
|
|
|
.filter(f => f !== 'utils.ts');
|
2021-10-29 00:00:43 +00:00
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
// Extract the puzzle number
|
|
|
|
const puzzleNumber = process.argv[2];
|
2022-01-03 00:39:38 +00:00
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
if (puzzleNumber === 'all' || !puzzleNumber) {
|
|
|
|
tsFiles
|
|
|
|
.sort((a, b) => {
|
|
|
|
a = parseInt(a.split('-')[0]);
|
|
|
|
b = parseInt(b.split('-')[0]);
|
2022-01-03 00:39:38 +00:00
|
|
|
|
2022-01-03 00:58:31 +00:00
|
|
|
return a > b ? 1 : -1;
|
|
|
|
})
|
|
|
|
.forEach(file => {
|
|
|
|
run(file);
|
2022-01-03 00:39:38 +00:00
|
|
|
console.log();
|
2022-01-03 00:58:31 +00:00
|
|
|
});
|
|
|
|
} else if (!isNaN(puzzleNumber)) {
|
|
|
|
// Find the associated puzzle
|
|
|
|
const [file] = tsFiles.filter(f => f.startsWith(puzzleNumber));
|
|
|
|
run(file);
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
chalk.bold(
|
|
|
|
chalk.red('Please ensure that you input the number of the puzzle to run - thank you.')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|