Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: template create automation #23

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ Zadania są dostępne na stronie [opanujfrontend.pl/advent](https://opanujfronte

Każdego dnia wykonaj w repozytorium polecenie `npm run create` a następnie skopiuj kod ze strony.

Użyj komendy `npm run create all`, jeśli chcesz wygenerować foldery z plikami startowymi
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to reference explicit names of npm scripts just to be consistent.

npm run create:month as suggested by @MarcinParda
npm run create:? - I can't find any single-word name for "up until today"

na każdy z dni wyzwania.

Użyj komendy `npm run create with-previous`, aby utworzyć brakujące zadania od aktualnej daty do
dnia pierwszego wyzwania.

Teraz możesz skupić się na wykonaniu zadania i weryfikacji testów poprzez polecenie `npm test`.

## 💜 Kontrybutorzy

Nasz projekt wspierają ([zobacz typ kontrybucji](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"license": "ISC",
"scripts": {
"create": "node ./scripts/create-template.js",
"create-all": "node ./scripts/create-template.js all",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use semicolons like with test:all and create:all if you don't mind.

"create-previous": "node ./scripts/create-template.js with-previous",
"test": "jest ./tasks/$(date +'%Y-%m-%d')/index.test.ts",
"test:all": "jest"
},
Expand Down
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());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow-up, would be nice to reuse createTaskFoldersFromRange with just one day. I do understand it requires more work, so only FYI, not a blocker.

}

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));
}
}