UNPKG

note

Version:
128 lines (127 loc) 4.95 kB
#!/usr/bin/env node /** * CLI entry point for the note command. * * @since 0.1.0 */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = exports.WriteError = exports.FileAlreadyExists = exports.ExistingArgFile = void 0; var _cli = require("@effect/cli"); var _platform = require("@effect/platform"); var _platformNode = require("@effect/platform-node"); var Console = _interopRequireWildcard(require("effect/Console")); var Data = _interopRequireWildcard(require("effect/Data")); var Effect = _interopRequireWildcard(require("effect/Effect")); var Match = _interopRequireWildcard(require("effect/Match")); var _Note = require("./Note.js"); var _Validate = require("./Validate.js"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } /** * Error when an argument matches an existing file in the current directory. * * @since 0.1.0 * @category Errors */ class ExistingArgFile extends /*#__PURE__*/Data.TaggedError("ExistingArgFile") { get message() { return `"${this.word}" matches an existing file. You may have confused this tool with another command.`; } } /** * Error when the target note file already exists. * * @since 0.1.0 * @category Errors */ exports.ExistingArgFile = ExistingArgFile; class FileAlreadyExists extends /*#__PURE__*/Data.TaggedError("FileAlreadyExists") { get message() { return `File "${this.filename}" already exists. Choose a different title or remove the existing file.`; } } /** * Error when file write fails. * * @since 0.1.0 * @category Errors */ exports.FileAlreadyExists = FileAlreadyExists; class WriteError extends /*#__PURE__*/Data.TaggedError("WriteError") { get message() { return `Failed to write "${this.filename}".`; } } /** * Format a NoteError for display. * * @since 0.1.0 * @category Errors */ exports.WriteError = WriteError; const formatError = error => Match.value(error).pipe(Match.tag("ExistingArgFile", e => `Error: ${e.message}\nUsage: note <title words...>`), Match.tag("FileAlreadyExists", e => `Error: ${e.message}`), Match.tag("WriteError", e => `Error: ${e.message}`), Match.exhaustive); /** * Handle NoteError by printing pretty message and exiting. * * @since 0.1.0 * @category Errors */ const handleNoteError = error => Console.error(formatError(error)).pipe(Effect.andThen(Effect.sync(() => process.exit(1)))); const titleArgs = /*#__PURE__*/_cli.Args.text({ name: "title" }).pipe(/*#__PURE__*/_cli.Args.atLeast(1), /*#__PURE__*/_cli.Args.withSchema(_Validate.TitleInput)); const noteCommand = /*#__PURE__*/_cli.Command.make("note", { title: titleArgs }, ({ title }) => Effect.gen(function* () { const fs = yield* _platform.FileSystem.FileSystem; const path = yield* _platform.Path.Path; const cwd = yield* Effect.sync(() => process.cwd()); // Check if any arg matches an existing file for (const word of title.words) { const filePath = path.join(cwd, word); const exists = yield* fs.exists(filePath); if (exists) { return yield* new ExistingArgFile({ word }); } } // Generate filename and check if it already exists const now = new Date(); const filename = (0, _Note.makeFilename)(title.title, now); const filePath = path.join(cwd, filename); const targetExists = yield* fs.exists(filePath); if (targetExists) { return yield* new FileAlreadyExists({ filename }); } // Create the note const content = (0, _Note.makeContent)(title.title, now); yield* fs.writeFileString(filePath, content).pipe(Effect.mapError(cause => new WriteError({ filename, cause }))); yield* Console.log(`\u2705 Created: ${filename}`); }).pipe(Effect.catchTag("ExistingArgFile", handleNoteError), Effect.catchTag("FileAlreadyExists", handleNoteError), Effect.catchTag("WriteError", handleNoteError))).pipe(/*#__PURE__*/_cli.Command.withDescription("Create a timestamped markdown note")); /** * Run the CLI with the given arguments. * * @since 0.1.0 */ const run = args => _cli.Command.run(noteCommand, { name: "note", version: "0.1.0" })(args); // Run if executed directly (not when imported as module for testing) exports.run = run; if (process.argv[1]?.includes("bin")) { run(process.argv).pipe(Effect.provide(_platformNode.NodeContext.layer), _platformNode.NodeRuntime.runMain({ disableErrorReporting: true })); } //# sourceMappingURL=bin.js.map