diff --git a/package-lock.json b/package-lock.json index e4d254e..61a46a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,8 @@ "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" }, "bin": { "taskparser": "dist/bin.js" @@ -27,6 +28,7 @@ "@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" } }, @@ -76,6 +78,12 @@ "undici-types": "~6.19.8" } }, + "node_modules/@types/slug": { + "version": "5.0.9", + "resolved": "https://registry.npmjs.org/@types/slug/-/slug-5.0.9.tgz", + "integrity": "sha512-6Yp8BSplP35Esa/wOG1wLNKiqXevpQTEF/RcL/NV6BBQaMmZh4YlDwCgrrFSoUE4xAGvnKd5c+lkQJmPrBAzfQ==", + "dev": true + }, "node_modules/@types/unist": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.3.tgz", @@ -1040,6 +1048,14 @@ "resolved": "https://registry.npmjs.org/simple-wcswidth/-/simple-wcswidth-1.0.1.tgz", "integrity": "sha512-xMO/8eNREtaROt7tJvWJqHBDTMFN4eiQ5I4JRMuilwfnFcV5W9u7RUkueNkdw0jPqGMX36iCywelS5yilTuOxg==" }, + "node_modules/slug": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/slug/-/slug-10.0.0.tgz", + "integrity": "sha512-M8s2PWOUeSCdD4S1NH5lCzXg2zFV1fozrtfr0FSKl65x+EF1rUowj+/vyFlnHgxPxWzT+DL0VXKfYc1DHJoymg==", + "bin": { + "slug": "cli.js" + } + }, "node_modules/typescript": { "version": "5.6.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", diff --git a/package.json b/package.json index f4d90e9..d0da16a 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/bin.ts b/src/bin.ts index ef1b631..a5d3423 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -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'; @@ -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; @@ -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', @@ -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 // ============================================================================ diff --git a/src/today.ts b/src/today.ts new file mode 100644 index 0000000..eeab989 --- /dev/null +++ b/src/today.ts @@ -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 }; +};