@idomatic/core 1.1.69 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -71
- package/bin/index.js +6 -5
- package/const/defaultConfig.js +13 -0
- package/const/ignoredFiles.js +14 -0
- package/lib/core.js +10 -4
- package/lib/readConfig.js +6 -3
- package/package.json +26 -2
package/README.md
CHANGED
|
@@ -1,104 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
idomatic is a CLI tool that automatically adds id attributes to your HTML/JSX/TSX components. It helps improve your testing and QA processes by ensuring that key elements always have unique, predictable identifiers.
|
|
1
|
+
# @idomatic/core
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
**Automatically add stable, readable `id` attributes to your components — so your Playwright, Cypress and Testing Library tests stop breaking.**
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Automatic ID Injection: Scans your project files and injects id attributes into components that are missing them.
|
|
5
|
+
[](https://www.npmjs.com/package/@idomatic/core)
|
|
6
|
+
[](./LICENSE)
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
idomatic is a CLI that scans your React/HTML/Vue/Angular code and injects **human-readable, stable** test selectors into elements that are missing them. No more brittle CSS/XPath selectors, no more `nth-child`, no more tests that break when you reorder a `<div>`.
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
<!-- npm renders READMEs in isolation, so this must be an absolute URL. -->
|
|
11
|
+
<p align="center">
|
|
12
|
+
<img src="https://raw.githubusercontent.com/AmarSmajlovic/idomatic/main/docs/demo.gif" alt="idomatic adding id attributes to a React component" width="820" />
|
|
13
|
+
</p>
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
## Why
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
E2E tests break because they target selectors that were never meant to be stable — classes, text, DOM position. The fix is a dedicated, stable selector on every element you test. Adding those by hand is tedious, so idomatic does it for you — and the ids it generates are readable and deterministic:
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
```jsx
|
|
20
|
+
// before
|
|
21
|
+
<button aria-label="Submit login">Log in</button>
|
|
22
|
+
<input name="email" placeholder="Email address" />
|
|
19
23
|
|
|
20
|
-
|
|
24
|
+
// after `npx idomatic scan --write`
|
|
25
|
+
<button aria-label="Submit login" id="button-submit-login">Log in</button>
|
|
26
|
+
<input name="email" placeholder="Email address" id="input-email" />
|
|
27
|
+
```
|
|
21
28
|
|
|
22
|
-
```
|
|
23
|
-
|
|
29
|
+
```ts
|
|
30
|
+
await page.locator("#button-submit-login").click();
|
|
24
31
|
```
|
|
25
32
|
|
|
26
|
-
|
|
33
|
+
> Prefer a dedicated test attribute? Set `"attributeName": "data-testid"` and idomatic emits `data-testid="button-submit-login"` instead.
|
|
27
34
|
|
|
28
|
-
##
|
|
35
|
+
## Features
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
- **Semantic ids** — derived from `aria-label`, `name`, `placeholder`, `alt`, text content or tag, not random UUIDs.
|
|
38
|
+
- **Idempotent** — re-running never renames or duplicates an existing id; diffs stay clean and it's safe in CI.
|
|
39
|
+
- **Framework-aware** — React (JS/JSX/TS/TSX) via a real AST parser; HTML, Vue and Angular templates too.
|
|
40
|
+
- **Dry-run first** — preview every change before touching a file.
|
|
41
|
+
- **Configurable** — change the attribute name, add a prefix/namespace, exclude tags or files.
|
|
32
42
|
|
|
33
|
-
|
|
43
|
+
## Installation
|
|
34
44
|
|
|
35
45
|
```shell
|
|
36
|
-
|
|
46
|
+
npm init @idomatic
|
|
37
47
|
```
|
|
38
48
|
|
|
39
|
-
|
|
49
|
+
This walks you through picking your framework, installs the right parser, and creates a `.idomatic.config.json` in your project root.
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
40
52
|
|
|
41
53
|
```shell
|
|
42
|
-
npx idomatic scan --
|
|
54
|
+
npx idomatic scan --dry # preview changes without writing
|
|
55
|
+
npx idomatic scan --write # apply changes
|
|
43
56
|
```
|
|
44
57
|
|
|
45
|
-
|
|
58
|
+
Running `npx idomatic scan` with no flag prints usage.
|
|
59
|
+
|
|
60
|
+
## Configuration
|
|
46
61
|
|
|
47
|
-
|
|
48
|
-
The configuration file .idomatic.config.json is created during the setup process. Here is an example configuration for a React project:
|
|
62
|
+
`.idomatic.config.json` is created during setup:
|
|
49
63
|
|
|
50
|
-
```
|
|
64
|
+
```json
|
|
51
65
|
{
|
|
52
|
-
"attributeName": "id",
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
66
|
+
"attributeName": "id",
|
|
67
|
+
"idStrategy": "semantic",
|
|
68
|
+
"prefix": "",
|
|
69
|
+
"excludeTags": ["html", "head", "script"],
|
|
70
|
+
"includeExtensions": ["js", "jsx", "ts", "tsx"],
|
|
71
|
+
"excludeFiles": ["node_modules", "public"]
|
|
57
72
|
}
|
|
58
73
|
```
|
|
59
74
|
|
|
60
|
-
|
|
75
|
+
- **`attributeName`** — the attribute to inject. Defaults to `id`; set `"data-testid"` (or `"data-test"`, …) if you prefer a dedicated test attribute.
|
|
76
|
+
- **`idStrategy`** — `"semantic"` (default) for readable ids like `button-submit`, or `"random"` for `${prefix}${uuid}`.
|
|
77
|
+
- **`prefix`** — optional namespace, e.g. `"qa-"` → `qa-button-submit`. Leave empty for clean ids.
|
|
78
|
+
- **`excludeTags`** — tags to skip.
|
|
79
|
+
- **`includeExtensions`** — for HTML/Vue/Angular this is `["html", "vue", "ng.html"]`.
|
|
80
|
+
- **`excludeFiles`** — directories to ignore.
|
|
61
81
|
|
|
62
|
-
|
|
63
|
-
It is recommended to use templateUrl with a separate .html file rather than defining inline templates in .ts files.
|
|
64
|
-
idomatic processes external HTML templates correctly but does not support inline templates in Angular components. Using templateUrl ensures your component's HTML is picked up during scanning.
|
|
82
|
+
## How it works
|
|
65
83
|
|
|
66
|
-
|
|
84
|
+
idomatic finds files matching your extensions (respecting `.gitignore`), then rewrites them through the right parser — `@idomatic/parser-js` for JS/JSX/TSX (recast + Babel AST) or `@idomatic/parser-html` for HTML/Vue/Angular (formatting-preserving). An id is added only to elements that don't already have one, using a name derived from context and made unique on collision.
|
|
67
85
|
|
|
68
|
-
|
|
69
|
-
Run the setup command (e.g npm init @idomatic) to choose your framework and create a configuration file.
|
|
70
|
-
|
|
71
|
-
Scanning:
|
|
72
|
-
The tool uses your configuration to search for files matching the specified extensions.
|
|
73
|
-
|
|
74
|
-
Dry Run: Lists the files that would be processed without modifying them.
|
|
75
|
-
|
|
76
|
-
Write Mode: Processes each file, adding id attributes where needed.
|
|
77
|
-
|
|
78
|
-
Parsing:
|
|
79
|
-
Depending on the file type, idomatic imports the appropriate parser (@idomatic/parser-js for JavaScript/JSX/TSX or @idomatic/parser-html for HTML-based files) to modify your code using an Abstract Syntax Tree (AST).
|
|
80
|
-
|
|
81
|
-
## Example
|
|
82
|
-
|
|
83
|
-
Imagine you have a React component without an id attribute:
|
|
84
|
-
|
|
85
|
-
```js
|
|
86
|
-
function Button() {
|
|
87
|
-
return <button>Click me</button>;
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
After running idomatic in write mode, the component might be transformed to:
|
|
92
|
-
|
|
93
|
-
```js
|
|
94
|
-
function Button() {
|
|
95
|
-
return (
|
|
96
|
-
<button id="auto-id-123e4567-e89b-12d3-a456-426614174000">Click me</button>
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
```
|
|
86
|
+
> **Angular:** use `templateUrl` with a separate `.html` file. idomatic processes external templates but not inline templates defined in `.ts` files.
|
|
100
87
|
|
|
101
|
-
|
|
88
|
+
## Contributing
|
|
102
89
|
|
|
103
|
-
|
|
104
|
-
Contributions are welcome! Feel free to open issues or pull requests on GitHub.
|
|
90
|
+
Contributions welcome — open an issue or PR on GitHub.
|
package/bin/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import runInitScan from "../lib/scan.js";
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
const isDry = args[0] === "scan" && args[1] === "--dry";
|
|
4
|
+
const [command, flag] = process.argv.slice(2);
|
|
6
5
|
|
|
7
|
-
if (
|
|
8
|
-
await runInitScan(
|
|
6
|
+
if (command === "scan" && (flag === "--dry" || flag === "--write")) {
|
|
7
|
+
await runInitScan(flag === "--dry");
|
|
9
8
|
} else {
|
|
10
|
-
console.log(
|
|
9
|
+
console.log(
|
|
10
|
+
"👉 Use: 'idomatic scan --dry' to preview, or 'idomatic scan --write' to apply."
|
|
11
|
+
);
|
|
11
12
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import excludeFiles from "./ignoredFiles.js";
|
|
2
|
+
|
|
3
|
+
const defaultConfig = {
|
|
4
|
+
attributeName: "id",
|
|
5
|
+
// "semantic" -> readable ids (button-submit); "random" -> ${prefix}${uuid}
|
|
6
|
+
idStrategy: "semantic",
|
|
7
|
+
prefix: "",
|
|
8
|
+
excludeTags: [], //html,head,script...
|
|
9
|
+
includeExtensions: ["js", "jsx", "ts", "tsx"],
|
|
10
|
+
excludeFiles: excludeFiles,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default defaultConfig;
|
package/lib/core.js
CHANGED
|
@@ -29,10 +29,16 @@ export async function processFile(filePath, config, dry = false) {
|
|
|
29
29
|
const result = await parser.default(filePath, config, dry);
|
|
30
30
|
return result;
|
|
31
31
|
} catch (err) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
if (err.code === "MODULE_NOT_FOUND") {
|
|
33
|
+
console.error(
|
|
34
|
+
`❌ Missing parser for ${filePath} — install it with: npm i -D ${parserPackage}`
|
|
35
|
+
);
|
|
36
|
+
} else {
|
|
37
|
+
console.error(
|
|
38
|
+
`❌ Failed to load parser "${parserPackage}" for ${filePath}`,
|
|
39
|
+
err
|
|
40
|
+
);
|
|
41
|
+
}
|
|
36
42
|
return false;
|
|
37
43
|
}
|
|
38
44
|
}
|
package/lib/readConfig.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
+
import defaultConfig from "../const/defaultConfig.js";
|
|
3
4
|
|
|
4
5
|
export default function getUserConfig() {
|
|
5
6
|
const configFilePath = path.join(process.cwd(), ".idomatic.config.json");
|
|
@@ -8,13 +9,15 @@ export default function getUserConfig() {
|
|
|
8
9
|
try {
|
|
9
10
|
const configContent = fs.readFileSync(configFilePath, "utf-8");
|
|
10
11
|
const userConfig = JSON.parse(configContent);
|
|
11
|
-
return userConfig;
|
|
12
|
+
return { ...defaultConfig, ...userConfig };
|
|
12
13
|
} catch (err) {
|
|
13
14
|
console.error(
|
|
14
15
|
"Error reading the .idomatic.config.json file. Using default config."
|
|
15
16
|
);
|
|
17
|
+
return defaultConfig;
|
|
16
18
|
}
|
|
17
|
-
} else {
|
|
18
|
-
console.warn("No .idomatic.config.json file found. Using default config.");
|
|
19
19
|
}
|
|
20
|
+
|
|
21
|
+
console.warn("No .idomatic.config.json file found. Using default config.");
|
|
22
|
+
return defaultConfig;
|
|
20
23
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idomatic/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "CLI that injects stable, human-readable id attributes into React/HTML/Vue/Angular elements so Playwright, Cypress and Testing Library tests stop breaking.",
|
|
4
5
|
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/AmarSmajlovic/idomatic.git",
|
|
9
|
+
"directory": "packages/core"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://www.idomatic.dev",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/AmarSmajlovic/idomatic/issues"
|
|
14
|
+
},
|
|
5
15
|
"bin": {
|
|
6
16
|
"idomatic": "./bin/index.js"
|
|
7
17
|
},
|
|
8
18
|
"files": [
|
|
9
19
|
"bin",
|
|
10
|
-
"lib"
|
|
20
|
+
"lib",
|
|
21
|
+
"const"
|
|
11
22
|
],
|
|
12
23
|
"dependencies": {
|
|
13
24
|
"globby": "^14.1.0",
|
|
@@ -16,6 +27,19 @@
|
|
|
16
27
|
"keywords": [
|
|
17
28
|
"idomatic",
|
|
18
29
|
"cli",
|
|
30
|
+
"data-testid",
|
|
31
|
+
"test-id",
|
|
32
|
+
"testid",
|
|
33
|
+
"selectors",
|
|
34
|
+
"test-automation",
|
|
35
|
+
"e2e",
|
|
36
|
+
"playwright",
|
|
37
|
+
"cypress",
|
|
38
|
+
"testing-library",
|
|
39
|
+
"qa",
|
|
40
|
+
"react",
|
|
41
|
+
"html",
|
|
42
|
+
"vue",
|
|
19
43
|
"ids",
|
|
20
44
|
"precommit",
|
|
21
45
|
"parser"
|