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"
},
"devDependencies": {
"@swc/core": "^1.2.125",
"@swc/helpers": "^0.3.2",
"@types/node": "^16.11.6",
"chalk": "^4.1.2",
"execution-time": "^1.4.1",
"gulp": "^4.0.2",
"gulp-cli": "^2.3.0",
"gulp-rename": "^2.0.0",
"gulp-typescript": "^6.0.0-alpha.1",
"gulp-uglify": "^3.0.2",
"regenerator-runtime": "^0.13.9",
"ts-node": "^10.4.0",
"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 chalk = require('chalk');
const { spawnSync, spawn } = require('child_process');
const { spawnSync } = require('child_process');
const perf = require('execution-time')();
// todo: move to ts-node
const run = (file, puzzleName) => {
// Calculate the length of the divider for the puzzle
const generateBanner = text => {
// Calculate the length of the divider
let divider = '--';
for (let j = 0; j < puzzleName.length; j++) {
for (let j = 0; j < text.length; j++) {
divider += '-';
}
// Styling
console.log(divider);
console.log(chalk.bold(chalk.greenBright(puzzleName)));
console.log(divider);
return `${divider}
${chalk.bold(chalk.greenBright(text))}
${divider}`;
};
const run = file => {
console.log(generateBanner(file.split('.ts')[0]));
// Execute the file
perf.start();
spawnSync('node', [`build/${file}`], { shell: true, stdio: 'inherit' });
spawnSync('npx', ['ts-node', `"src/${file}"`], {
shell: true,
stdio: 'inherit'
});
const results = perf.stop();
// Print time results
@ -28,49 +34,35 @@ const run = (file, puzzleName) => {
console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`)));
};
new Promise(async resolve => {
// Rebuild the files
const gulp = spawn('npx', ['gulp-cli', 'build'], { shell: true, stdio: 'pipe' });
gulp.stdout.on('data', data => {
const message = data.toString();
const match = message.match(/Starting '(.*)'/)?.[1];
if (match) console.log(chalk.red(`[gulp] Running '${match}' task`));
});
gulp.on('close', () => {
console.log();
resolve();
});
}).then(() => {
// 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 {
// Extract the puzzle number
const puzzleNumber = process.argv[2];
if (isNaN(puzzleNumber)) throw Error();
// Find the associated puzzle
const tsFile = tsFiles.filter(f => f.startsWith(puzzleNumber))[0];
const puzzleName = tsFile.split('.ts')[0];
if (puzzleNumber === 'all' || !puzzleNumber) {
tsFiles
.sort((a, b) => {
a = parseInt(a.split('-')[0]);
b = parseInt(b.split('-')[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);
return a > b ? 1 : -1;
})
.forEach(file => {
run(file);
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 {};
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(
firstXDigits(

View file

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