Skip to content

Commit

Permalink
working file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHi committed Nov 26, 2023
1 parent 7e40cbe commit 2a9bac6
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions Typescript/2023/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@ const nunjucks = require("nunjucks");
const fs = require("fs");
const path = require("path");

// Check if filename and day are provided as command-line arguments
const args = process.argv.slice(2);

if (args.length < 1) {
console.error("Usage: node generateFiles.js <day>");
process.exit(1);
}

const dayValue = args[0];
console.log(dayValue);

const dayTemplate = `
/// AOC 2023 Day {$day}
console.log("Day {$day} Part 01:" + SolvePartOne());
console.log("Day {$day} Part 02:" + SolvePartTwo());
/// AOC 2023 Day {{ day }}
console.log("Day {{ day }} Part 01:" + SolvePartOne());
console.log("Day {{ day }} Part 02:" + SolvePartTwo());
export function SolvePartOne(): number {
console.log("TBD");
Expand All @@ -30,34 +19,40 @@ export function SolvePartTwo(): number {
`;

const createTypeScriptFile = (day) => {
const renderedTemplate = nunjucks.renderString(dayTemplate, { day });
const filename = path.join("src", "days", `${day}.ts`);
const renderedTemplate = nunjucks.renderString(dayTemplate, { day: day }); // Pass day as an object with key 'day'
const filename = path.join("src", "days", `day${day}.ts`);
console.log(filename);
fs.writeFileSync(filename, renderedTemplate);
console.log(`Created TypeScript file: ${day}.ts`);
console.log(`Created TypeScript file: day${day}.ts`);
};

const createTestFile = (filename) => {
const createTestFile = (day) => {
const testTemplate = `
import { SolvePartOne, SolvePartTwo } from "../{$day}";
import { SolvePartOne, SolvePartTwo } from "../day{{ day }}";
import { describe, expect, test } from "vitest";
describe("Day {$day} Part 01", () => {
describe("Day {{ day }} Part 01", () => {
test("Expected result", () => {
expect(SolvePartOne()).toBe(-1);
});
});
describe("Day {$day} Part 02", () => {
describe("Day {{ day }} Part 02", () => {
test("Expected result", () => {
expect(SolvePartTwo()).toBe(-1);
});
});
`;

const testFilename = path.join("src", "__test__", `${filename}.test.ts`);
const testFilename = path.join(
"src",
"days",
"__test__",
`day${day}.test.ts`
);
const renderedTemplate = nunjucks.renderString(testTemplate, { day: day });
console.log(testFilename);
fs.writeFileSync(testFilename, testTemplate);
fs.writeFileSync(testFilename, renderedTemplate);
console.log(`Created test file: ${testFilename}`);
};

Expand All @@ -68,10 +63,20 @@ const createDirectories = (filename) => {
}
};

createDirectories(path.join("src", "days", `${dayValue}`));
createDirectories(path.join("src", "days", "__test__", `${dayValue}.test.ts`));
// Create TypeScript file
createTypeScriptFile(dayValue);
// Leggo!
const args = process.argv.slice(2);

if (args.length < 1) {
console.error("Usage: node generateFiles.js <day>");
process.exit(1);
}

const dayValue = args[0];

// Create test file
createDirectories(path.join("src", "days", `day${dayValue}`));
createDirectories(
path.join("src", "days", "__test__", `day${dayValue}.test.ts`)
);

createTypeScriptFile(dayValue);
createTestFile(dayValue);

0 comments on commit 2a9bac6

Please sign in to comment.