Skip to content

Commit 1b0a8d3

Browse files
committed
update: bin — proper tagsHandler and housekeeping
1 parent 9dfbf91 commit 1b0a8d3

File tree

2 files changed

+55
-55
lines changed

2 files changed

+55
-55
lines changed

bin/ewTools.class.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "fs"
22
import path from "path"
33
import dotenv from "dotenv"
4+
import chalk from "chalk"
45

56
class Configuration {
67
static load() {
@@ -15,7 +16,7 @@ class Configuration {
1516

1617
class Logger {
1718
static isDevelopment = Configuration.get("TOOLS_ENV") === "development"
18-
19+
static debugModeSuffix = chalk.gray(`[debug]`)
1920
static Types = Object.freeze({
2021
INFO: "Info",
2122
WARN: "Warning",
@@ -25,25 +26,22 @@ class Logger {
2526

2627
static log(text, type = Logger.Types.EMPTY) {
2728
const typeFormatted = type ? `${type}:` : ""
28-
if (this.isDevelopment) console.log(`${typeFormatted}`, text)
29+
if (this.isDevelopment) console.log(`${Ewtools.programSuffix}${this.debugModeSuffix} ${typeFormatted}`, text)
2930
}
3031
}
3132

3233
class FlagsProcessor {
3334
static formatFlagsString(args) {
3435
if (!Array.isArray(args) || args.length === 0) return ""
35-
let lastIsAttr = false
3636
let isPreviousDump = false
3737

3838
return args.map((token, index) => {
3939
if (index === 0) return token
4040
if (token.startsWith("-") || isPreviousDump) {
41-
lastIsAttr = true
4241
if (token.includes("dump")) { isPreviousDump = true } else { isPreviousDump = false }
4342
return "+" + token
4443
}
4544

46-
lastIsAttr = false
4745
return ";" + token
4846
}).join("")
4947
}
@@ -84,20 +82,20 @@ class FileHandler {
8482
return { filename, filepath }
8583
}
8684

87-
static handleError(error, filename, action = this.ActionTypes.MAKE) {
88-
if (error) return console.error(`Error creating file: ${error.message}`)
89-
console.log(`File "${filename}" ${action === this.ActionTypes.MAKE ? "created" : "removed"} successfully.`)
85+
static handleError(error, filename, { type, action = this.ActionTypes.MAKE }) {
86+
if (error) return console.error(`${Ewtools.programSuffix} Error creating file: ${error.message}`)
87+
console.log(`${Ewtools.programSuffix} File "${type}/${filename}" ${action === this.ActionTypes.MAKE ? "created" : "removed"} successfully.`)
9088
}
9189

92-
static generateFile(title, type, flags, getContentFunction) {
93-
const { filename, filepath } = this.generateFilePath(title, type, flags)
90+
static generateFile(title, entityType, flags, getContentFunction) {
91+
const { filename, filepath } = this.generateFilePath(title, entityType, flags)
9492
const content = getContentFunction(title, flags)
95-
fs.writeFile(filepath, content, (error) => this.handleError(error, filename))
93+
fs.writeFile(filepath, content, (error) => this.handleError(error, filename, { type: entityType }))
9694
}
9795

98-
static removeFile(title, type, flags) {
99-
const { filename, filepath } = this.generateFilePath(title, type, flags)
100-
fs.rm(filepath, (error) => this.handleError(error, filename, this.ActionTypes.DELETE))
96+
static removeFile(title, entityType, flags) {
97+
const { filename, filepath } = this.generateFilePath(title, entityType, flags)
98+
fs.rm(filepath, (error) => this.handleError(error, filename, { type: entityType, action: this.ActionTypes.DELETE }))
10199
}
102100

103101
static getAllActionTypes() {
@@ -110,6 +108,10 @@ class FileHandler {
110108
}
111109

112110
class ContentGenerator {
111+
static tagsHandler(tags) {
112+
return typeof tags === "string" ? tags.split(",").map(tag => `\n - ${tag}`).join("") : ""
113+
}
114+
113115
static getDefaultBlogContent(title, flags = {}) {
114116
return `---
115117
title: ${title}
@@ -118,8 +120,7 @@ author:
118120
draft: true
119121
date: ${new Date().toISOString()}
120122
tags:
121-
- post
122-
${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}
123+
- post${ContentGenerator.tagsHandler(flags.t)}
123124
---\n`
124125
}
125126

@@ -133,8 +134,7 @@ image:
133134
linkDemo:
134135
linkCode:
135136
tags:
136-
- project
137-
${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}
137+
- project${ContentGenerator.tagsHandler(flags.t)}
138138
---\n`
139139
}
140140

@@ -148,13 +148,14 @@ linkDemo:
148148
language:
149149
code: |-
150150
tags:
151-
- prototype
152-
${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}
151+
- prototype${ContentGenerator.tagsHandler(flags.t)}
153152
---\n`
154153
}
155154
}
156155

157156
class Ewtools {
157+
static programSuffix = `${chalk.gray(`[`)}${chalk.green(`EwT`)}${chalk.gray(`/bin]`)}`
158+
158159
constructor(args) {
159160
this.args = args
160161
this.command = args[0]
@@ -167,7 +168,7 @@ class Ewtools {
167168

168169
validateArgs() {
169170
if (this.args.length < 2) {
170-
console.log("Usage: ./ewtools.bat make:blog \"title\"")
171+
console.log(`${Ewtools.programSuffix} Usage: ${chalk.green(`./ewtools.bat make:blog "title"`)}`)
171172
process.exit(1)
172173
}
173174
}
@@ -189,7 +190,7 @@ class Ewtools {
189190
break
190191

191192
default:
192-
console.log(`Invalid Action. Valid Actions: ${FileHandler.getAllActionTypes()}`)
193+
console.log(`${Ewtools.programSuffix} Invalid Action. Valid Actions: ${chalk.green(FileHandler.getAllActionTypes())}`)
193194
}
194195
}
195196

@@ -208,20 +209,20 @@ class Ewtools {
208209
break
209210

210211
default:
211-
console.log(`Invalid Type, Valid Types: ${FileHandler.getAllEntityTypes()}`)
212+
console.log(`${Ewtools.programSuffix} Invalid Type, Valid Types: ${chalk.green(FileHandler.getAllEntityTypes())}`)
212213
}
213214
}
214215

215216
handleDelete() {
216-
if (this.type === "" || this.type === undefined) return console.log(`Invalid Type, Valid Types: ${FileHandler.getAllEntityTypes()}`)
217+
if (this.type === "" || this.type === undefined) return console.log(`${Ewtools.programSuffix} Invalid Type, Valid Types: ${chalk.green(FileHandler.getAllEntityTypes())}`)
217218
FileHandler.removeFile(this.title, this.type, this.flags)
218219
}
219220
}
220221

221222
const args = process.argv.slice(2)
222223

223224
if (args.length <= 0) {
224-
console.log("Usage: ./ewtools.bat make:blog \"title\"")
225+
console.log(`${Ewtools.programSuffix} Usage: ${chalk.green(`./ewtools.bat make:blog "title"`)}`)
225226
} else {
226227
new Ewtools(args).execute()
227228
}

bin/ewTools.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import fs from "fs"
22
import path from "path"
33
import dotenv from "dotenv"
4+
import chalk from "chalk"
45

56
dotenv.config()
7+
const programSuffix = `${chalk.gray(`[`)}${chalk.green(`EwT`)}${chalk.gray(`/bin]`)}`
8+
const debugModeSuffix = chalk.gray(`[debug]`)
69
const isDevelopment = process.env.TOOLS_ENV === "development"
710
const LoggerType = Object.freeze({
811
INFO: "Info",
@@ -17,30 +20,27 @@ const getAllEntityTypes = () => Object.values(EntityTypes).map((entity, index, a
1720

1821
const Logger = (text, type = LoggerType.EMPTY) => {
1922
const typeFormatted = (type === LoggerType.EMPTY) ? `${type}` : `${type}:`
20-
if (isDevelopment) console.log(`${typeFormatted}`, text)
23+
if (isDevelopment) console.log(`${programSuffix}${debugModeSuffix} ${typeFormatted}`, text)
2124
}
2225

2326
const formatFlagsString = (args) => {
2427
if (!Array.isArray(args) || args.length === 0) return "";
25-
let lastIsAttr = false
2628
let isPreviousDump = false
2729

2830
return args.map((token, index) => {
2931
if (index === 0) return token
3032
if (token.startsWith("-") || isPreviousDump) {
31-
lastIsAttr = true
3233
if (token.includes("dump")) { isPreviousDump = true } else { isPreviousDump = false }
3334
return "+" + token
3435
}
3536

36-
lastIsAttr = false
3737
return ";" + token
3838
}).join("")
3939
}
4040

4141
const flagsHandler = (flags) => {
4242
const paramWithValue = flags.split("+")
43-
const flagsObject = new Object();
43+
const flagsObject = {}
4444
paramWithValue.forEach(token => {
4545
let [attr, value] = token.split(";")
4646
if (!value) value = true
@@ -55,7 +55,7 @@ const flagsHandler = (flags) => {
5555

5656
const args = process.argv.slice(2)
5757
if (args.length <= 0) {
58-
console.log("Usage: ./ewtools.bat make:blog \"title\"")
58+
console.log(`${programSuffix} Usage: ${chalk.green(`./ewtools.bat make:blog "title"`)}`)
5959
process.exit()
6060
}
6161
const [command, title, ...restArgs] = args
@@ -79,38 +79,38 @@ const EntityTypes = Object.freeze({
7979
})
8080

8181
if (args.length < 2) {
82-
console.log("Usage: ./ewtools.bat make:blog \"title\"")
82+
console.log(`${programSuffix} Usage: ${chalk.green(`./ewtools.bat make:blog "title"`)}`)
8383
process.exit(1)
8484
}
8585

86-
const handleError = (error, filename, action = ActionTypes.MAKE) => {
87-
if (error) return console.error(`Error creating file: ${error.message}`)
86+
const handleError = (error, filename, { type, action = ActionTypes.MAKE } = {}) => {
87+
if (error) return console.error(`${programSuffix} Error creating file: ${error.message}`)
8888
const currentAction = (action === ActionTypes.MAKE) ? "created" : "removed"
89-
console.log(`File "${filename}" ${currentAction} successfully.`)
89+
console.log(`${programSuffix} File "${type}/${filename}" ${currentAction} successfully.`)
9090
}
9191

9292
const generateFile = (title, entityType, getContent) => {
93-
const { filename, filepath } = generateFilePath(title, entityType);
94-
const content = getContent(title, flagsObject);
95-
fs.writeFile(filepath, content, error => handleError(error, filename));
96-
};
93+
const { filename, filepath } = generateFilePath(title, entityType)
94+
const content = getContent(title, flagsObject)
95+
fs.writeFile(filepath, content, error => handleError(error, filename, { type: entityType }))
96+
}
9797

9898
const generateBlogFile = (title) => generateFile(title, EntityTypes.BLOG, getDefaultBlogContent)
9999

100100
const generateProjectFile = (title) => generateFile(title, EntityTypes.PROJECT, getDefaultProjectContent)
101101

102102
const generatePrototypeFile = (title) => generateFile(title, EntityTypes.PROTOTYPE, getDefaultPrototypeContent)
103103

104-
const removeFile = (title, type) => {
105-
const { filename, filepath } = generateFilePath(title, type)
106-
fs.rm(filepath, (error) => handleError(error, filename, ActionTypes.DELETE))
104+
const removeFile = (title, entityType) => {
105+
const { filename, filepath } = generateFilePath(title, entityType)
106+
fs.rm(filepath, (error) => handleError(error, filename, { action: ActionTypes.DELETE, type: entityType }))
107107
}
108108

109109
const generateFilePath = (title, type) => {
110110
let date = flagsObject.d || new Date().toISOString()
111111
const [year, month, day] = date.split("T")[0].split("-")
112112
if (!(year && month && day)) {
113-
console.log("Invalid date format. Please provide -d yyyy-mm-dd")
113+
console.log(`${programSuffix} Invalid date format. Please provide -d yyyy-mm-dd`)
114114
process.exit(1)
115115
}
116116
const formattedDate = type === EntityTypes.PROTOTYPE ? year : `${year}-${month}-${day}`
@@ -120,17 +120,17 @@ const generateFilePath = (title, type) => {
120120
return { filename, filepath }
121121
}
122122

123+
const tagsHandler = (tags) => typeof tags === "string" ? tags.split(",").map(tag => `\n - ${tag}`).join("") : ""
124+
123125
const getDefaultBlogContent = (title, flags = {}) => `---
124126
title: ${title}
125127
description:
126128
author:
127129
draft: true
128130
date: ${new Date().toISOString()}
129131
tags:
130-
- post
131-
${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}---
132-
133-
`
132+
- post${tagsHandler(flags.t)}
133+
---\n`
134134
const getDefaultProjectContent = (title, flags = {}) => `---
135135
title: ${title}
136136
description:
@@ -140,8 +140,8 @@ image:
140140
linkDemo:
141141
linkCode:
142142
tags:
143-
- project
144-
${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}---`;
143+
- project${tagsHandler(flags.t)}
144+
---\n`
145145
const getDefaultPrototypeContent = (title, flags = {}) => `---
146146
title: ${title}
147147
status: 1
@@ -151,9 +151,8 @@ linkDemo:
151151
language:
152152
code: |-
153153
tags:
154-
- prototype
155-
${typeof flags.t === "string" ? flags.t.split(",").map(tag => ` - ${tag}`).join("\n") : ""}---
156-
`
154+
- prototype${tagsHandler(flags.t)}
155+
---\n`
157156

158157
const handleMake = (type, title) => {
159158
switch (type) {
@@ -167,7 +166,7 @@ const handleMake = (type, title) => {
167166
generatePrototypeFile(title)
168167
break
169168
default:
170-
console.log(`Invalid Type, Valid Types: ${getAllEntityTypes()}`)
169+
console.log(`${programSuffix} Invalid Type, Valid Types: ${getAllEntityTypes()}`)
171170
}
172171
}
173172

@@ -183,7 +182,7 @@ const handleDelete = (type, title) => {
183182
removeFile(title, EntityTypes.PROTOTYPE)
184183
break
185184
default:
186-
console.log(`Invalid Type, Valid Types: ${getAllEntityTypes()}`)
185+
console.log(`${programSuffix} Invalid Type, Valid Types: ${getAllEntityTypes()}`)
187186
}
188187
}
189188

@@ -196,5 +195,5 @@ if (!flagsObject.dump && !flagsObject.dp) switch (action) {
196195
handleDelete(type, title)
197196
break
198197
default:
199-
console.log(`Invalid Action. Valid Actions: ${getAllActionTypes()}`)
198+
console.log(`${programSuffix} Invalid Action. Valid Actions: ${getAllActionTypes()}`)
200199
}

0 commit comments

Comments
 (0)