Skip to content

Commit

Permalink
feat: improve create template script
Browse files Browse the repository at this point in the history
Refined the create-template script to introduce additional automation
with two new arguments: "all" and "with-previous". The "all" parameter
facilitates the creation of folders for all tasks from the event's start
to its end, streamlining the process. The "with-previous" parameter
option enables the generation of missing tasks from the current date
to the event's start date.
  • Loading branch information
kumiega committed Dec 6, 2023
1 parent fa6fee6 commit 81f2b01
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
37 changes: 18 additions & 19 deletions scripts/create-template.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import fs from 'fs';
import path from 'path';
import fs from "fs";
import { createTaskFolder, createTaskFoldersFromRange } from "./task-folder.js";

const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const folderName = `${year}-${month}-${day}`;
const arg = process.argv[2];

const folderPath = path.join('tasks', folderName);
if (!fs.existsSync("tasks")) {
fs.mkdirSync("tasks");
}

if (!fs.existsSync('tasks')) {
fs.mkdirSync('tasks');
if (!arg) {
createTaskFolder(new Date());
}

if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
const indexFilePath = path.join(folderPath, 'index.ts');
fs.writeFileSync(indexFilePath, '// Tutaj skopiuj kod zadania');
if (arg === "all") {
const startDate = new Date("2023-12-01");
const endDate = new Date("2023-12-24");

createTaskFoldersFromRange(startDate, endDate);
}

const testFilePath = path.join(folderPath, 'index.test.ts');
fs.writeFileSync(testFilePath, '// Tutaj skopiuj testy dla zadania. Uruchom je poleceniem `npm test`');
if (arg === "with-previous") {
const startDate = new Date("2023-12-01");
const endDate = new Date();

console.log(`Przygotowano szablon na zadanie w folderze tasks/${folderName} 🎄`)
} else {
console.log(`Folder na dzisiejsze zadania już istnieje (tasks/${folderName}) 🤔`);
createTaskFoldersFromRange(startDate, endDate);
}
50 changes: 50 additions & 0 deletions scripts/task-folder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fs from "fs";
import path from "path";

export function getFolderName(date) {
if (!date) {
date = new Date();
}

const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const folderName = `${year}-${month}-${day}`;

return folderName;
}

export function createTaskFolder(date = new Date()) {
const folderName = getFolderName(date);
const folderPath = path.join("tasks", folderName);

if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
const indexFilePath = path.join(folderPath, "index.ts");
fs.writeFileSync(indexFilePath, "// Tutaj skopiuj kod zadania");

const testFilePath = path.join(folderPath, "index.test.ts");
fs.writeFileSync(
testFilePath,
"// Tutaj skopiuj testy dla zadania. Uruchom je poleceniem `npm test`"
);

console.log(
`Przygotowano szablon na zadanie w folderze tasks/${folderName} 🎄`
);
} else {
console.log(
`Folder na dzisiejsze zadania już istnieje (tasks/${folderName}) 🤔`
);
}
}

export function createTaskFoldersFromRange(startDate, endDate) {
for (
let date = startDate;
date <= endDate;
date.setDate(date.getDate() + 1)
) {
createTaskFolder(new Date(date));
}
}

0 comments on commit 81f2b01

Please sign in to comment.