Skip to content

Commit

Permalink
adds --today flag to quickly create a new today file
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoscaz committed Dec 10, 2024
1 parent ec38e8e commit d4a8f46
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"mdast-util-gfm": "^3.0.0",
"micromark-extension-frontmatter": "^2.0.0",
"micromark-extension-gfm": "^3.0.0",
"simple-wcswidth": "^1.0.1"
"simple-wcswidth": "^1.0.1",
"slug": "^10.0.0"
},
"devDependencies": {
"@types/argparse": "^2.0.17",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.9.0",
"@types/slug": "^5.0.9",
"typescript": "^5.6.3"
},
"keywords": [
Expand Down
35 changes: 35 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { fileURLToPath } from 'node:url';
import { cwd } from 'node:process';
import { resolve } from 'node:path';
import { readFileSync } from 'node:fs';
import { writeFile } from 'node:fs/promises';
import slug from 'slug';

import { ArgumentParser } from 'argparse';

Expand All @@ -20,6 +22,7 @@ import {
parseTagFilterExpressions,
parseTagSortExpressions,
} from './tags.js';
import { renderTodayFile } from './today.js';

const pkg_path = resolve(fileURLToPath(import.meta.url), '..', '..', 'package.json');
const pkg_version = JSON.parse(readFileSync(pkg_path, 'utf8')).version;
Expand Down Expand Up @@ -89,6 +92,18 @@ arg_parser.add_argument('-v', '--version', {
version: pkg_version,
});

arg_parser.add_argument('--today', {
required: false,
action: 'store_true',
help: 'generate a new today file at the given path',
});

arg_parser.add_argument('--title', {
required: false,
default: 'Today',
help: 'title for the new today file',
});

arg_parser.add_argument('path', {
default: cwd(),
help: 'working directory',
Expand All @@ -98,6 +113,26 @@ const cli_args = arg_parser.parse_args();

const folder_path = resolve(cwd(), cli_args.path);

// ============================================================================
// TODAY
// ============================================================================

if (cli_args.today) {
const { f_name, f_data } = renderTodayFile(new Date(), cli_args.title.trim());
const f_path = resolve(cli_args.path, f_name);
try {
await writeFile(f_path, f_data, { encoding: 'utf-8', flag: 'wx' });
console.log('created new today file at %s', f_path);
} catch (err) {
if ((err as any).code === 'EEXIST') {
console.error('Error! File %s already exists!', f_path);
} else {
throw err;
}
}
process.exit(0);
}

// ============================================================================
// FILTERING
// ============================================================================
Expand Down
29 changes: 29 additions & 0 deletions src/today.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import slug from 'slug';
import { EOL } from 'os';

const renderTodayFileData = (date: string, title: string): string => {
return [
'---',
'---',
'',
`# ${date} | ${title}`,
'',
'## Worklogs',
'',
'## Notes',
'',
].join(EOL);
}

export const renderTodayFile = (date: Date, title: string): { f_name: string; f_data: string; } => {
const f_date = [
String(date.getFullYear()),
String(date.getMonth() + 1).padStart(2, '0'),
String(date.getDate()).padStart(2, '0'),
].join('-');
const f_slug = slug(title);
const f_name = `${f_date}-${f_slug}.md`;
const f_data = renderTodayFileData(f_date, title);
return { f_name, f_data };
};

0 comments on commit d4a8f46

Please sign in to comment.