Skip to content

Commit

Permalink
Merge pull request #174 from ZACHSTRIVES/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ZACHSTRIVES authored Aug 14, 2023
2 parents 4f22f41 + 5fb5538 commit cc547ae
Show file tree
Hide file tree
Showing 9 changed files with 871 additions and 191 deletions.
821 changes: 658 additions & 163 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,48 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"cypress": "cypress open"
"cypress": "cypress open",
"add:generator": "tsx ./scripts/addNewGenerator.ts"
},
"dependencies": {
"@douyinfe/semi-next": "^2.36.0",
"@douyinfe/semi-next": "^2.37.0",
"@douyinfe/semi-ui": "^2.36.0",
"@faker-js/faker": "^8.0.2",
"@reduxjs/toolkit": "^1.9.5",
"@semi-bot/semi-theme-mockdatanz": "^1.1.4",
"@types/node": "20.1.4",
"@types/react": "18.2.6",
"@types/react-dom": "18.2.4",
"@uiw/codemirror-extensions-langs": "^4.20.2",
"@uiw/codemirror-themes": "^4.21.1",
"@types/react-dom": "18.2.5",
"@uiw/codemirror-extensions-langs": "^4.21.9",
"@uiw/codemirror-themes": "^4.21.9",
"@uiw/react-codemirror": "^4.19.16",
"@vercel/analytics": "^1.0.1",
"autoprefixer": "10.4.14",
"emoji-picker-react": "^4.4.9",
"eslint": "8.41.0",
"eslint-config-next": "13.4.4",
"eslint-config-next": "13.4.6",
"hamburger-react": "^2.5.0",
"next": "13.4.3",
"next": "13.4.13",
"next-redux-wrapper": "^8.1.0",
"next-seo": "^6.0.0",
"next-seo": "^6.1.0",
"nextjs-progressbar": "^0.0.16",
"postcss": "8.4.24",
"react": "18.2.0",
"react-beautiful-dnd": "^13.1.1",
"react-dom": "18.2.0",
"react-intl": "^6.4.2",
"react-intl": "^6.4.4",
"react-redux": "^8.0.5",
"react-reflex": "^4.1.0",
"redux-persist": "^6.0.0",
"sass": "^1.62.1",
"sass": "^1.63.4",
"tailwindcss": "^3.3.2",
"tsx": "^3.12.7",
"uuidjs": "^5.0.1"
},
"devDependencies": {
"cypress": "^12.12.0",
"cypress": "^12.14.0",
"mini-css-extract-plugin": "^2.7.6",
"typescript": "^5.1.3",
"webpack": "^5.83.1"
"webpack": "^5.88.2"
}
}
148 changes: 148 additions & 0 deletions scripts/addNewGenerator.ts
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"]
}
`;

}
15 changes: 15 additions & 0 deletions scripts/utils/enumUtils.ts
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');
}
}
20 changes: 20 additions & 0 deletions scripts/utils/localValuesUtils.ts
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');
}
});
};
2 changes: 1 addition & 1 deletion src/components/Toolbar/src/Toolbar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
border: 1px solid var(--semi-color-border);
border-radius: var(--semi-border-radius-small);
padding: 12px;
background-color: rgba(var(--semi-grey-0), 1);
background-color: rgba(var(--semi-grey-0), 0.3);
position: relative;
z-index: 2;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/generators/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {DataType} from "@/constants/enums";
import {BooleanGenerator} from "@/core/generators/Boolean";
import {NumberGenerator} from "@/core/generators/Number";
import {FullNameGenerator} from "@/core/generators/FullName";
import {EmailGenerator} from "@/core/generators/Email";
import {CompanyNameGenerator} from "@/core/generators/CompanyName";
import {DataType} from "@/constants/enums";

export const generators = {
[DataType.BOOLEAN]: BooleanGenerator,
Expand Down
14 changes: 7 additions & 7 deletions src/locale/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ export const en = {
// -------------------------------------------------------------------------------------------------------------
// data types

// data type category
"dataType.category.all": "All",
"dataType.category.basic": "Basic",
"dataType.category.person": "Person",
"dataType.category.commerce": "Commerce",
"dataType.category.network": "Network",

// number
"dataType.number": "Number",
"dataType.number.kind.label": "Kind",
Expand Down Expand Up @@ -81,6 +74,13 @@ export const en = {
// account number
"dataType.accountNumber": "Account Number",

// data type category
"dataType.category.all": "All",
"dataType.category.basic": "Basic",
"dataType.category.person": "Person",
"dataType.category.commerce": "Commerce",
"dataType.category.network": "Network",

// -------------------------------------------------------------------------------------------------------------
// pages

Expand Down
14 changes: 7 additions & 7 deletions src/locale/translations/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export const zhCN = {
// -------------------------------------------------------------------------------------------------------------
// data types

// data type category
"dataType.category.all": "全部",
"dataType.category.basic": "基础",
"dataType.category.person": "人物",
"dataType.category.commerce": "商业",
"dataType.category.network": "网络",

// number
"dataType.number": "数字",
"dataType.number.kind.label": "种类",
Expand Down Expand Up @@ -80,6 +73,13 @@ export const zhCN = {
// account mi,ner
"dataType.accountNumber": "账号",

// data type category
"dataType.category.all": "全部",
"dataType.category.basic": "基础",
"dataType.category.person": "人物",
"dataType.category.commerce": "商业",
"dataType.category.network": "网络",

// -------------------------------------------------------------------------------------------------------------
// pages

Expand Down

1 comment on commit cc547ae

@vercel
Copy link

@vercel vercel bot commented on cc547ae Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.