Skip to content

Commit

Permalink
add new feature "fileName"
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Oreshkin committed Aug 27, 2023
1 parent 647e67a commit 9f57edd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Creating a JSON file with a custom name, for example "generator.config.json" in
| Key | Type | Description|
| :---: | :---: | --- |
| `id_name` | string | name of the generated ID |
| `action` | "delete" \| "create" \| "update" \| "createAndUpdate" \| undefined | optional value ("_createAndUpdate_" is default value) <br> **create** - creates new IDs for those that do not have them (does not update existing ones) <br> **update** - updates existing IDs (does not create new ones) <br> **delete** - delete all IDs matching with 'id_name" <br> **createAndUpdate** - creates and updates IDs
| `action` | "delete" \| "onlyCreate" \| "onlyUpdate" \| "createAndUpdate" \| undefined | optional value ("_createAndUpdate_" is default value) <br> **create** - creates new IDs for those that do not have them (does not update existing ones) <br> **update** - updates existing IDs (does not create new ones) <br> **delete** - delete all IDs matching with 'id_name" <br> **createAndUpdate** - creates and updates IDs
| `paths` | string[] | array of paths to the components where the generation needs to be performed, and it also works with folders |
| `rules` | {tag, pattern}[] | rules Rules for generating the ID string |
| `tag` | string \| string[] | tag name of the element to which the pattern will be applied. |
Expand All @@ -80,6 +80,7 @@ Creating a JSON file with a custom name, for example "generator.config.json" in
### Patterns:
| Key | Description|
| :---: | --- |
| `${fileName}` | file name |
| `${componentName}` | component name (currently, if the component is anonymous, the value will be null). |
| `${tagName}` | tag name |
| `${attr:onClick}` | value of any specified attribute from the tag.In this example, it is "onClick". (currently, if the tag does not have the specified attribute, the value will be undefined). |
Expand Down
5 changes: 3 additions & 2 deletions src/addIdsToFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ function addIdsToFile({ filePath, config }) {
traverse(ast, {
JSXOpeningElement(path) {
const { attributes } = path.node || {};
const parsedAttr = parseAttributes(attributes)
const fileName = filePath.match(/\/([^/]+)\.\w+$/)[1]
const tagName = path.node.name.name;
const repacerByPattern = createReplacer({ componentName, tagName, parsedAttr });
const parsedAttr = parseAttributes(attributes)
const repacerByPattern = createReplacer({ fileName, componentName, tagName, parsedAttr });

config.rules.forEach((rule) => {
if (rule.tag && typeof rule.tag === 'string') {
Expand Down
4 changes: 3 additions & 1 deletion src/createReplacer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createReplacer = ({ componentName, tagName, parsedAttr }) => ({ pattern }) => {
const createReplacer = ({ fileName, componentName, tagName, parsedAttr }) => ({ pattern }) => {
const regex = /\$\{([^}]+)\}/g;
return pattern.replace(regex, (match, capture) => {

Expand All @@ -8,6 +8,8 @@ const createReplacer = ({ componentName, tagName, parsedAttr }) => ({ pattern })
}

switch (capture) {
case 'fileName':
return fileName;
case 'componentName':
return componentName;
case 'tagName':
Expand Down
4 changes: 2 additions & 2 deletions src/generateIdsInAllFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const { print } = require("./print");

const textMessegeByAction = (action) => {
if (action === 'delete') return 'deleting from';
if (action === 'create') return 'creating in';
if (action === 'update') return 'updating in';
if (action === 'onlyCreate') return 'creating in';
if (action === 'onlyUpdate') return 'updating in';
if (action === 'createAndUpdate') return 'creating and updating in';
return 'creating and updating in';
}
Expand Down
4 changes: 2 additions & 2 deletions src/setId.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ function setId({ action, attributes, idName, newId }) {
},
}

if (action === 'create') {
if (action === 'onlyCreate') {
return createId({ attributes, attributeIndex, newAttr });
}
if (action === 'update') {
if (action === 'onlyUpdate') {
return updateId({ attributes, attributeIndex, newAttr });
}
if (action === 'createAndUpdate') {
Expand Down

0 comments on commit 9f57edd

Please sign in to comment.