From 9f57edd4949607b3b86e52c4745d0a4c3b11fb25 Mon Sep 17 00:00:00 2001 From: Pavel Oreshkin Date: Sun, 27 Aug 2023 13:36:26 +0400 Subject: [PATCH] add new feature "fileName" --- README.md | 3 ++- src/addIdsToFile.js | 5 +++-- src/createReplacer.js | 4 +++- src/generateIdsInAllFiles.js | 4 ++-- src/setId.js | 4 ++-- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 097a76c..ab3e409 100644 --- a/README.md +++ b/README.md @@ -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)
**create** - creates new IDs for those that do not have them (does not update existing ones)
**update** - updates existing IDs (does not create new ones)
**delete** - delete all IDs matching with 'id_name"
**createAndUpdate** - creates and updates IDs +| `action` | "delete" \| "onlyCreate" \| "onlyUpdate" \| "createAndUpdate" \| undefined | optional value ("_createAndUpdate_" is default value)
**create** - creates new IDs for those that do not have them (does not update existing ones)
**update** - updates existing IDs (does not create new ones)
**delete** - delete all IDs matching with 'id_name"
**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. | @@ -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). | diff --git a/src/addIdsToFile.js b/src/addIdsToFile.js index 691a14f..0db8a7d 100644 --- a/src/addIdsToFile.js +++ b/src/addIdsToFile.js @@ -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') { diff --git a/src/createReplacer.js b/src/createReplacer.js index f937229..ce72479 100644 --- a/src/createReplacer.js +++ b/src/createReplacer.js @@ -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) => { @@ -8,6 +8,8 @@ const createReplacer = ({ componentName, tagName, parsedAttr }) => ({ pattern }) } switch (capture) { + case 'fileName': + return fileName; case 'componentName': return componentName; case 'tagName': diff --git a/src/generateIdsInAllFiles.js b/src/generateIdsInAllFiles.js index ef7ace2..38d0a8d 100644 --- a/src/generateIdsInAllFiles.js +++ b/src/generateIdsInAllFiles.js @@ -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'; } diff --git a/src/setId.js b/src/setId.js index 4ea82e1..1fec775 100644 --- a/src/setId.js +++ b/src/setId.js @@ -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') {