euler(run): use ts-node

This commit is contained in:
newt 2024-10-09 18:02:37 +01:00
parent 6a9e214501
commit ffa6923e66
6 changed files with 290 additions and 2265 deletions

View file

@ -1,19 +0,0 @@
const gulp = require('gulp');
const typescript = require('gulp-typescript');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
gulp.task('build', () => {
const tsc = typescript.createProject('tsconfig.json');
return gulp
.src('src/**/*.ts')
.pipe(tsc())
.pipe(uglify({ mangle: { toplevel: true } }))
.pipe(
rename(path => {
path.basename = path.basename.split('-')[0].trim();
})
)
.pipe(gulp.dest('build'));
});

View file

@ -5,14 +5,13 @@
"start": "node run.js" "start": "node run.js"
}, },
"devDependencies": { "devDependencies": {
"@swc/core": "^1.2.125",
"@swc/helpers": "^0.3.2",
"@types/node": "^16.11.6", "@types/node": "^16.11.6",
"chalk": "^4.1.2", "chalk": "^4.1.2",
"execution-time": "^1.4.1", "execution-time": "^1.4.1",
"gulp": "^4.0.2", "regenerator-runtime": "^0.13.9",
"gulp-cli": "^2.3.0", "ts-node": "^10.4.0",
"gulp-rename": "^2.0.0",
"gulp-typescript": "^6.0.0-alpha.1",
"gulp-uglify": "^3.0.2",
"typescript": "^4.4.4" "typescript": "^4.4.4"
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -1,26 +1,32 @@
const fs = require('fs'); const fs = require('fs');
const chalk = require('chalk'); const chalk = require('chalk');
const { spawnSync, spawn } = require('child_process'); const { spawnSync } = require('child_process');
const perf = require('execution-time')(); const perf = require('execution-time')();
// todo: move to ts-node // todo: move to ts-node
const run = (file, puzzleName) => { const generateBanner = text => {
// Calculate the length of the divider for the puzzle // Calculate the length of the divider
let divider = '--'; let divider = '--';
for (let j = 0; j < puzzleName.length; j++) { for (let j = 0; j < text.length; j++) {
divider += '-'; divider += '-';
} }
// Styling return `${divider}
console.log(divider); ${chalk.bold(chalk.greenBright(text))}
console.log(chalk.bold(chalk.greenBright(puzzleName))); ${divider}`;
console.log(divider); };
const run = file => {
console.log(generateBanner(file.split('.ts')[0]));
// Execute the file // Execute the file
perf.start(); perf.start();
spawnSync('node', [`build/${file}`], { shell: true, stdio: 'inherit' }); spawnSync('npx', ['ts-node', `"src/${file}"`], {
shell: true,
stdio: 'inherit'
});
const results = perf.stop(); const results = perf.stop();
// Print time results // Print time results
@ -28,49 +34,35 @@ const run = (file, puzzleName) => {
console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`))); console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`)));
}; };
new Promise(async resolve => { // Get files
// Rebuild the files const tsFiles = fs
const gulp = spawn('npx', ['gulp-cli', 'build'], { shell: true, stdio: 'pipe' }); .readdirSync('src')
.filter(f => f.endsWith('.ts'))
.filter(f => f !== 'utils.ts');
gulp.stdout.on('data', data => { // Extract the puzzle number
const message = data.toString(); const puzzleNumber = process.argv[2];
const match = message.match(/Starting '(.*)'/)?.[1];
if (match) console.log(chalk.red(`[gulp] Running '${match}' task`));
});
gulp.on('close', () => { if (puzzleNumber === 'all' || !puzzleNumber) {
console.log(); tsFiles
resolve(); .sort((a, b) => {
}); a = parseInt(a.split('-')[0]);
}).then(() => { b = parseInt(b.split('-')[0]);
// Get files
const tsFiles = fs
.readdirSync('src')
.filter(f => f.endsWith('.ts'))
.filter(f => f !== 'utils.ts');
const jsFiles = fs
.readdirSync('build')
.filter(f => f.endsWith('.js'))
.filter(f => f !== 'utils.js');
try { return a > b ? 1 : -1;
// Extract the puzzle number })
const puzzleNumber = process.argv[2]; .forEach(file => {
if (isNaN(puzzleNumber)) throw Error(); run(file);
// Find the associated puzzle
const tsFile = tsFiles.filter(f => f.startsWith(puzzleNumber))[0];
const puzzleName = tsFile.split('.ts')[0];
run(`${puzzleNumber}.js`, puzzleName);
} catch (error) {
for (let i = 0; i < jsFiles.length; i++) {
const file = jsFiles[i];
const tsFile = tsFiles[i];
const puzzleName = tsFile.split('.ts')[0];
run(file, puzzleName);
console.log(); console.log();
} });
} } 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.')
)
);
}

View file

@ -1,7 +1,8 @@
export {}; export {};
const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b); const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);
const firstXDigits = (number: number, x: number) => BigInt(number).toString().substr(0, x); const firstXDigits = (number: number, x: number) =>
parseInt(BigInt(number).toString().substr(0, x));
console.log( console.log(
firstXDigits( firstXDigits(

View file

@ -3,8 +3,11 @@
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"outDir": "build", "outDir": "build",
"downlevelIteration": true "skipLibCheck": true
}, },
"include": ["src/**/*.ts"], "files": ["src/**/*.ts"],
"exclude": ["node_modules"] "ts-node": {
"transpileOnly": true,
"transpiler": "ts-node/transpilers/swc-experimental"
}
} }