-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from ZACHSTRIVES/dev
Dev
- Loading branch information
Showing
9 changed files
with
871 additions
and
191 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import * as fs from 'fs'; | ||
import * as readline from 'readline'; | ||
import {DataTypeCategory} from "@/constants/enums"; | ||
import {enumUtils} from "./utils/enumUtils"; | ||
import {addLinesToLocal} from "./utils/localValuesUtils"; | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
const categories = Object.values(DataTypeCategory).filter((category) => category !== DataTypeCategory.ALL); | ||
|
||
const createGenerator = (generatorName: string) => { | ||
rl.question('➡️ Please select the category for the generator(enter the corresponding digit):\n' + | ||
categories.map((option, index) => `${index + 1}. ${option}`).join('\n') + | ||
'\n', (answer) => { | ||
|
||
const selectedIndex = parseInt(answer) - 1; | ||
if (isNaN(selectedIndex) || selectedIndex < 0 || selectedIndex >= categories.length) { | ||
console.log('❌Invalid option, please try again.'); | ||
createGenerator(generatorName); | ||
return; | ||
} else { | ||
const path = `./src/core/generators/${generatorName}`; | ||
|
||
// add DataType enum option | ||
enumUtils('./src/constants/enums.ts', 'DataType', generatorName.toUpperCase(), generatorName.toLowerCase()); | ||
|
||
// create directory | ||
fs.mkdirSync(path, {recursive: true}); | ||
|
||
// create index.ts | ||
fs.writeFileSync(`${path}/index.ts`, writeGeneratorIndexFile(generatorName, categories[selectedIndex]), 'utf8'); | ||
|
||
// create GENERATOR_NAME.tsx | ||
const fileName = `${generatorName}.tsx`; | ||
fs.writeFileSync(`${path}/${fileName}`, writeGeneratorTsxFile(generatorName), 'utf8'); | ||
|
||
// add generator | ||
addGeneratorToIndex(generatorName); | ||
|
||
// add generator name to locale | ||
addLinesToLocal(/\/\/\s+data\s+types\s*/, | ||
[`// ${generatorName.toLowerCase()}`, | ||
`"dataType.${generatorName.toLowerCase()}": "${generatorName}",`] | ||
); | ||
|
||
console.log(`✨ Successfully created generator ${generatorName}!\n See ${path} for details.`); | ||
rl.close(); | ||
} | ||
}); | ||
} | ||
|
||
rl.question('💥 Please enter the name of the new generator:', (generatorName: string) => { | ||
const folderPath = `./src/core/generators`; | ||
generatorName = formatFolderName(generatorName); | ||
if (checkIfGeneratorExists(generatorName)) { | ||
rl.close(); | ||
return; | ||
} | ||
createGenerator(generatorName); | ||
}); | ||
|
||
// utils | ||
const checkIfGeneratorExists = (generatorName: string): boolean => { | ||
const targetPath = `./src/core/generators/${generatorName}`; | ||
if (fs.existsSync(targetPath)) { | ||
console.log('❌ The generator already exists, please try again.'); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const formatFolderName = (folderName: string): string => { | ||
const firstLetter = folderName.charAt(0).toUpperCase(); | ||
const restLetters = folderName.slice(1).toLowerCase(); | ||
return `${firstLetter}${restLetters}`; | ||
}; | ||
|
||
const addGeneratorToIndex = (generatorName: string) => { | ||
const generatorsIndexFilePath = './src/core/generators/index.ts'; | ||
const fileContent = fs.readFileSync(generatorsIndexFilePath, 'utf8'); | ||
const contentAfterImport = `${`import {${generatorName}Generator} from "@/core/generators/${generatorName}";`}\n${fileContent}`; | ||
const generatorLine = ` [DataType.${generatorName.toUpperCase()}]: ${generatorName}Generator,`; | ||
const updatedContent = contentAfterImport.replace(/export\s+const\s+generators\s*=\s*{/, `export const generators = {\n${generatorLine}`); | ||
fs.writeFileSync(generatorsIndexFilePath, updatedContent, 'utf8'); | ||
|
||
} | ||
|
||
const writeGeneratorTsxFile = (generatorName: string) => { | ||
return `import React from "react"; | ||
import {GenerateResult, GeneratorOptionsComponentInterface} from "@/types/generator"; | ||
import {ExportValueType} from "@/constants/enums"; | ||
// ------------------------------------------------------------------------------------------------------------- | ||
// types | ||
export interface ${generatorName}GeneratorOptions { | ||
// TODO: add your own options type here | ||
} | ||
// ------------------------------------------------------------------------------------------------------------- | ||
// default options | ||
export const ${generatorName}GeneratorDefaultOptions:${generatorName}GeneratorOptions = { | ||
// TODO: add your own default options here | ||
} | ||
// ------------------------------------------------------------------------------------------------------------- | ||
// generate method | ||
export const generate = (options: any): GenerateResult => { | ||
// TODO: implement your own generate method here | ||
return { | ||
value: 'NOT IMPLEMENTED', | ||
stringValue: 'NOT IMPLEMENTED', | ||
type: ExportValueType.STRING | ||
} | ||
} | ||
// ------------------------------------------------------------------------------------------------------------- | ||
// options component | ||
export const ${generatorName}GeneratorOptionsComponent: React.FunctionComponent<GeneratorOptionsComponentInterface> = ({...props}) => { | ||
const {options, onOptionsChange} = props; | ||
// TODO: implement your own options component here | ||
return ( | ||
<div> | ||
NOT IMPLEMENTED | ||
</div> | ||
) | ||
}`; | ||
} | ||
|
||
const writeGeneratorIndexFile = (generatorName: string, category: string) => { | ||
return `import {Generator} from "@/types/generator"; | ||
import {DataType, DataTypeCategory} from "@/constants/enums"; | ||
import {${generatorName}GeneratorDefaultOptions, ${generatorName}GeneratorOptionsComponent, generate} from "./${generatorName}"; | ||
export const ${generatorName}Generator: Generator = { | ||
type: DataType.${generatorName.toUpperCase()}, | ||
category: DataTypeCategory.${category.toUpperCase()}, | ||
generate: generate, | ||
optionsComponent: ${generatorName}GeneratorOptionsComponent, | ||
defaultOptions: ${generatorName}GeneratorDefaultOptions, | ||
exampleLines: ["NOT IMPLEMENTED", "NOT IMPLEMENTED", "NOT IMPLEMENTED"] | ||
} | ||
`; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import fs from "fs"; | ||
|
||
export const enumUtils = (enumFilePath: string, enumName: string, optionName: string, optionValue: string) => { | ||
const enumFileContent = fs.readFileSync(enumFilePath, 'utf8'); | ||
const enumRegex = new RegExp(`enum\\s+${enumName}\\s*{`); | ||
const enumMatch = enumRegex.exec(enumFileContent); | ||
|
||
if (enumMatch) { | ||
const insertPosition = enumMatch.index + enumMatch[0].length; | ||
const indentation = ' '; | ||
const optionLine = `${indentation} ${optionName} = "${optionValue}",`; | ||
const updatedEnumContent = `${enumFileContent.slice(0, insertPosition)}\n${optionLine}${enumFileContent.slice(insertPosition)}`; | ||
fs.writeFileSync(enumFilePath, updatedEnumContent, 'utf8'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fs from "fs"; | ||
import * as path from 'path'; | ||
|
||
const TRANSLATION_FILE_PATH = 'src/locale/translations'; | ||
|
||
export const addLinesToLocal = (targetLine: RegExp, lines: string[]) => { | ||
|
||
fs.readdirSync(TRANSLATION_FILE_PATH).forEach((file) => { | ||
const filePath = path.join(TRANSLATION_FILE_PATH, file); | ||
if (fs.statSync(filePath).isFile()) { | ||
const fileContent = fs.readFileSync(filePath, 'utf8'); | ||
const updatedContent = fileContent.replace(targetLine, (match) => { | ||
const indent = ' '; | ||
const addedLines = lines.map((line) => `${indent}${line}`).join('\n'); | ||
return `${match}\n${addedLines}\n\n${indent}`; | ||
}); | ||
fs.writeFileSync(filePath, updatedContent, 'utf8'); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cc547ae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dummyi – ./
dummyi.vercel.app
dummyi-zachstrives.vercel.app
www.dummyi.com
dummyi-git-master-zachstrives.vercel.app
dummyi.com