|
| 1 | +import fs from "fs" |
| 2 | +import path from "path" |
| 3 | +import dotenv from "dotenv" |
| 4 | + |
| 5 | +dotenv.config() |
| 6 | +const isDevelopment = process.env.TOOLS_ENV === "development" |
| 7 | +const LoggerType = Object.freeze({ |
| 8 | + INFO: "Info", |
| 9 | + WARN: "Warning", |
| 10 | + ERROR: "Error", |
| 11 | + EMPTY: "" |
| 12 | +}) |
| 13 | + |
| 14 | +const getAllActionTypes = () => Object.values(ActionTypes).map((action, index, arr) => (index === arr.length - 1 ? ` and \`${action}\`` : ` \`${action}\`,`)).join("") |
| 15 | + |
| 16 | +const getAllEntityTypes = () => Object.values(EntityTypes).map((entity, index, arr) => (index === arr.length - 1 ? ` and \`${entity}\`` : ` \`${entity}\`,`)).join("") |
| 17 | + |
| 18 | +function Logger(text, type = LoggerType.EMPTY) { |
| 19 | + const typeFormatted = (type === LoggerType.EMPTY) ? `${type}` : `${type}:` |
| 20 | + if (isDevelopment) console.log(`${typeFormatted}`, text) |
| 21 | +} |
| 22 | + |
| 23 | +const formatFlagsString = (args) => { |
| 24 | + if (!Array.isArray(args) || args.length === 0) return ""; |
| 25 | + |
| 26 | + return args.map((token, index) => { |
| 27 | + if (index === args.length - 1) return token; |
| 28 | + return (index % 2 == 0) ? `${token};` : `${token}+` |
| 29 | + }).join("") |
| 30 | +} |
| 31 | + |
| 32 | +const flagsHandler = (flags) => { |
| 33 | + const paramWithValue = flags.split("+") |
| 34 | + const flagsObject = new Object(); |
| 35 | + paramWithValue.forEach(token => { |
| 36 | + const [attr, value] = token.split(";") |
| 37 | + flagsObject[attr[1]] = value |
| 38 | + }) |
| 39 | + return flagsObject |
| 40 | +} |
| 41 | + |
| 42 | +const args = process.argv.slice(2) |
| 43 | +if (args.length <= 0) { |
| 44 | + console.log("Usage: ./ewtools.bat make:blog \"title\"") |
| 45 | + process.exit() |
| 46 | +} |
| 47 | +const [command, title, ...restArgs] = args |
| 48 | +const [action, type] = command.split(':') |
| 49 | +const formattedFlags = formatFlagsString(restArgs) |
| 50 | +const flagsObject = flagsHandler(formattedFlags) |
| 51 | +Logger(args, LoggerType.INFO) |
| 52 | +Logger(flagsObject, LoggerType.INFO) |
| 53 | + |
| 54 | +const ActionTypes = Object.freeze({ |
| 55 | + MAKE: "make", |
| 56 | + DELETE: "delete", |
| 57 | + REMOVE: "remove" |
| 58 | +}) |
| 59 | + |
| 60 | +const EntityTypes = Object.freeze({ |
| 61 | + BLOG: "blog", |
| 62 | + PROJECT: "project", |
| 63 | + PROTOTYPE: "prototype" |
| 64 | +}) |
| 65 | + |
| 66 | +if (args.length < 2) { |
| 67 | + console.log("Usage: ./ewtools.bat make:blog \"title\"") |
| 68 | + process.exit(1) |
| 69 | +} |
| 70 | + |
| 71 | +const handleError = (error, filename, action = ActionTypes.MAKE) => { |
| 72 | + if (error) return console.error(`Error creating file: ${error.message}`) |
| 73 | + const currentAction = (action === ActionTypes.MAKE) ? "created" : "removed" |
| 74 | + console.log(`File "${filename}" ${currentAction} successfully.`) |
| 75 | +} |
| 76 | + |
| 77 | +const generateFile = (title, entityType, getContent) => { |
| 78 | + const { filename, filepath } = generateFilePath(title, entityType); |
| 79 | + const content = getContent(title, flagsObject); |
| 80 | + fs.writeFile(filepath, content, error => handleError(error, filename)); |
| 81 | +}; |
| 82 | + |
| 83 | +const generateBlogFile = (title) => generateFile(title, EntityTypes.BLOG, getDefaultBlogContent) |
| 84 | + |
| 85 | +const generateProjectFile = (title) => generateFile(title, EntityTypes.PROJECT, getDefaultProjectContent) |
| 86 | + |
| 87 | +const generatePrototypeFile = (title) => generateFile(title, EntityTypes.PROTOTYPE, getDefaultPrototypeContent) |
| 88 | + |
| 89 | +const removeFile = (title, type) => { |
| 90 | + const { filename, filepath } = generateFilePath(title, type) |
| 91 | + fs.rm(filepath, (error) => handleError(error, filename, ActionTypes.DELETE)) |
| 92 | +} |
| 93 | + |
| 94 | +const generateFilePath = (title, type) => { |
| 95 | + let date = flagsObject.d || new Date().toISOString() |
| 96 | + const [year, month, day] = date.split("T")[0].split("-") |
| 97 | + if (!(year && month && day)) { |
| 98 | + console.log("Invalid date format. Please provide -d yyyy-mm-dd") |
| 99 | + process.exit(1) |
| 100 | + } |
| 101 | + const formattedDate = type === EntityTypes.PROTOTYPE ? year : `${year}-${month}-${day}` |
| 102 | + const filename = `${formattedDate}-${title.replace(/\s+/g, "-").toLowerCase()}.md`.replace(/[<>:"\/\\|?*]+/g, "") |
| 103 | + const filepath = path.join(process.cwd(), `src/articles/${type}`, filename) |
| 104 | + |
| 105 | + return { filename, filepath } |
| 106 | +} |
| 107 | + |
| 108 | +const getDefaultBlogContent = (title, flags = {}) => `--- |
| 109 | +title: ${title} |
| 110 | +description: |
| 111 | +author: |
| 112 | +draft: true |
| 113 | +date: ${new Date().toISOString()} |
| 114 | +tags: |
| 115 | + - post |
| 116 | +${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}--- |
| 117 | +
|
| 118 | +` |
| 119 | +const getDefaultProjectContent = (title, flags = {}) => `--- |
| 120 | +title: ${title} |
| 121 | +description: |
| 122 | +date: |
| 123 | +endDate: |
| 124 | +image: |
| 125 | +linkDemo: |
| 126 | +linkCode: |
| 127 | +tags: |
| 128 | + - project |
| 129 | +${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}---`; |
| 130 | +const getDefaultPrototypeContent = (title, flags = {}) => `--- |
| 131 | +title: ${title} |
| 132 | +status: 1 |
| 133 | +description: |
| 134 | +date: |
| 135 | +linkDemo: |
| 136 | +language: |
| 137 | +code: |- |
| 138 | +tags: |
| 139 | + - prototype |
| 140 | +${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}--- |
| 141 | +` |
| 142 | + |
| 143 | +const handleMake = (type, title) => { |
| 144 | + switch (type) { |
| 145 | + case EntityTypes.BLOG: |
| 146 | + generateBlogFile(title) |
| 147 | + break |
| 148 | + case EntityTypes.PROJECT: |
| 149 | + generateProjectFile(title) |
| 150 | + break |
| 151 | + case EntityTypes.PROTOTYPE: |
| 152 | + generatePrototypeFile(title) |
| 153 | + break |
| 154 | + default: |
| 155 | + console.log(`Invalid Type, Valid Types: ${getAllEntityTypes()}`) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +const handleDelete = (type, title) => { |
| 160 | + switch (type) { |
| 161 | + case EntityTypes.BLOG: |
| 162 | + removeFile(title, EntityTypes.BLOG) |
| 163 | + break |
| 164 | + case EntityTypes.PROJECT: |
| 165 | + removeFile(title, EntityTypes.PROJECT) |
| 166 | + break |
| 167 | + case EntityTypes.PROTOTYPE: |
| 168 | + removeFile(title, EntityTypes.PROTOTYPE) |
| 169 | + break |
| 170 | + default: |
| 171 | + console.log(`Invalid Type, Valid Types: ${getAllEntityTypes()}`) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +switch (action) { |
| 176 | + case ActionTypes.MAKE: |
| 177 | + handleMake(type, title) |
| 178 | + break |
| 179 | + case ActionTypes.DELETE: |
| 180 | + case ActionTypes.REMOVE: |
| 181 | + handleDelete(type, title) |
| 182 | + break |
| 183 | + default: |
| 184 | + console.log(`Invalid Action. Valid Actions: ${getAllActionTypes()}`) |
| 185 | +} |
0 commit comments