Skip to content

Instantly share code, notes, and snippets.

@plskz
Last active September 19, 2023 02:08
Show Gist options
  • Save plskz/b85fe24aebc16732f9749726b97dafdb to your computer and use it in GitHub Desktop.
Save plskz/b85fe24aebc16732f9749726b97dafdb to your computer and use it in GitHub Desktop.
My automation for 100 Days of Code (old version)
  1. init package

    npm init -y
    
  2. replace your package.json to

    {
      "name": "100-days-of-coding",
      "version": "1.0.0",
      "type": "module",
      "main": "index.js",
      "scripts": {
        "start": "node .",
        "prev": "node index.js prev",
        "next": "node index.js next"
      }
    }
  3. create index.js and helpers.js

  4. modify the user in your helpers.js file

    image

  5. commands

  • node . or node index.js : creates current day
  • npm run prev or node . prev : creates previous day
  • npm run next or node . next : creates next day
const user = {
name: 'Zai Santillan',
github: 'plskz',
start: '6/1/2022',
};
// -------- IGNORE BELOW --------
import { exec } from 'child_process';
const RED = '\x1b[31m%s\x1b[0m';
const GREEN = '\x1b[32m%s\x1b[0m';
const weeks = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
export const pad = (x, y = 3) => x.toString().padStart(y, '0');
export const error = (msg) => console.log(RED, msg);
export const success = (msg) => console.log(GREEN, msg);
export const open = (path) => {
exec(`code ${path}`, (error, stdout, stderr) => {
if (error) return console.log(`error: ${error.message}`);
if (stderr) return console.log(`stderr: ${stderr}`);
// console.log(`stdout: ${stdout}`);
success(`\n\topening ${path}`);
});
};
// -------------------------------------------------------------
let d = new Date();
switch (process.argv[2]) {
case 'prev':
const prev = new Date(d.getTime());
prev.setDate(d.getDate() - 1);
d = prev;
break;
case 'next':
const next = new Date(d.getTime());
next.setDate(d.getDate() + 1);
d = next;
break;
}
const month = d.getMonth();
const day = d.getDate();
const year = d.getFullYear();
const CURRENT = `${month + 1}/${day}/${year}`;
const weekName = weeks[d.getDay()];
const monthName = months[month];
export const diff = Math.floor((Date.parse(CURRENT) - Date.parse(user.start)) / 86400000) + 1;
export const [prevDay, nextDay] = [diff - 1, diff + 1];
export const currentDayChallenge = `Day ${diff}`;
export const currentDate = `${monthName} ${day}, ${year} - ${weekName}`;
export const today = `\n\t\t${currentDayChallenge}\n\t${currentDate}\n`;
export const header = `<div align="center">
<h1>Round 1</h1>
<p>${currentDayChallenge}</p>
<sub>
Author: <a href="https://github.com/${user.github}" target="_blank">${user.name}</a>
<br>
<small>${currentDate}</small>
</sub>
</div>`;
export const content = `### Today's Progress:\n\n- TODO\n\n### Notes:\n\n- TODO\n\n### Thoughts:\n\n- TODO\n\n### Resources:\n\n- TODO`;
export const footer = `[<< Day ${prevDay}](day${pad(prevDay)}.md) | [Day ${nextDay} >>](day${pad(nextDay)}.md)`;
export const template = `${header}\n\n${footer}\n\n${content}\n\n${footer}\n`;
import fs from 'fs';
import { pad, open, diff, today, template, success, error } from './helpers.js';
console.log(today);
try {
const path = `Round-1/day${pad(diff)}.md`;
if (fs.existsSync(path)) {
// for safety reasons, to avoid overwriting existing files
error(`${path} already exists.`);
error(`delete if you want to override it`);
open(path);
} else {
fs.writeFileSync(path, template);
success(`${path} created successfully`);
open(path);
}
} catch (err) {
console.error(err);
}
@plskz
Copy link
Author

plskz commented Sep 19, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment