diff --git a/scripts/create-template.js b/scripts/create-template.js index c98bdfe..0d0026e 100644 --- a/scripts/create-template.js +++ b/scripts/create-template.js @@ -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); } diff --git a/scripts/task-folder.js b/scripts/task-folder.js new file mode 100644 index 0000000..b0de813 --- /dev/null +++ b/scripts/task-folder.js @@ -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)); + } +}