@bobfrankston/extractids 1.0.0 → 1.0.1
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/index.js +31 -33
- package/index.js.map +1 -0
- package/package.json +1 -1
- package/tsconfig.json +2 -2
package/index.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
// scripts/generateTypes.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import * as cheerio from 'cheerio';
|
|
5
|
+
import chokidar from 'chokidar';
|
|
8
6
|
// import * as glob from 'glob';
|
|
9
7
|
// Helper function to extract all id attributes from HTML
|
|
10
8
|
function getHtmlIDs(html) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
$('[id]').each(
|
|
14
|
-
|
|
9
|
+
const $ = cheerio.load(html);
|
|
10
|
+
const ids = new Set();
|
|
11
|
+
$('[id]').each((_, elem) => {
|
|
12
|
+
const id = $(elem).attr('id');
|
|
15
13
|
if (id)
|
|
16
14
|
ids.add(id);
|
|
17
15
|
});
|
|
@@ -20,9 +18,9 @@ function getHtmlIDs(html) {
|
|
|
20
18
|
// Helper function to extract all class names from CSS files
|
|
21
19
|
function getCssClasses(css) {
|
|
22
20
|
// Match any .classname { or .classname,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
const classPattern = /\.([a-zA-Z0-9_-]+)\s*[{,]/g;
|
|
22
|
+
const classes = new Set();
|
|
23
|
+
let match;
|
|
26
24
|
while ((match = classPattern.exec(css))) {
|
|
27
25
|
classes.add(match[1]);
|
|
28
26
|
}
|
|
@@ -30,12 +28,11 @@ function getCssClasses(css) {
|
|
|
30
28
|
}
|
|
31
29
|
function globReadCssFiles() {
|
|
32
30
|
// Recursively find all .css files in project folder, ignoring node_modules
|
|
33
|
-
|
|
31
|
+
const result = [];
|
|
34
32
|
function walk(dir) {
|
|
35
|
-
|
|
36
|
-
for (
|
|
37
|
-
|
|
38
|
-
var fullPath = path.join(dir, entry.name);
|
|
33
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
34
|
+
for (const entry of entries) {
|
|
35
|
+
const fullPath = path.join(dir, entry.name);
|
|
39
36
|
if (entry.isDirectory()) {
|
|
40
37
|
if (entry.name === 'node_modules')
|
|
41
38
|
continue;
|
|
@@ -50,28 +47,29 @@ function globReadCssFiles() {
|
|
|
50
47
|
return result;
|
|
51
48
|
}
|
|
52
49
|
function generateTypes(ids, classes) {
|
|
53
|
-
|
|
54
|
-
?
|
|
55
|
-
:
|
|
56
|
-
|
|
57
|
-
?
|
|
58
|
-
:
|
|
50
|
+
const idType = ids.length > 0
|
|
51
|
+
? `export type htmlIDs = ${ids.map(id => `"${id}"`).join(' | ')};\n`
|
|
52
|
+
: `export type htmlIDs = never;\n`;
|
|
53
|
+
const classType = classes.length > 0
|
|
54
|
+
? `export type cssClasses = ${classes.map(cls => `"${cls}"`).join(' | ')};\n`
|
|
55
|
+
: `export type cssClasses = never;\n`;
|
|
59
56
|
return idType + classType;
|
|
60
57
|
}
|
|
61
58
|
function updateTypesFile() {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
cssFiles.forEach(
|
|
67
|
-
|
|
68
|
-
getCssClasses(css).forEach(
|
|
59
|
+
const html = fs.readFileSync('index.html', 'utf8');
|
|
60
|
+
const ids = getHtmlIDs(html);
|
|
61
|
+
const cssFiles = globReadCssFiles();
|
|
62
|
+
const allClasses = new Set();
|
|
63
|
+
cssFiles.forEach(file => {
|
|
64
|
+
const css = fs.readFileSync(file, 'utf8');
|
|
65
|
+
getCssClasses(css).forEach(cls => allClasses.add(cls));
|
|
69
66
|
});
|
|
70
|
-
|
|
67
|
+
const types = generateTypes(ids, Array.from(allClasses));
|
|
71
68
|
fs.writeFileSync('generated-types.ts', types);
|
|
72
69
|
console.log('[Type generation] Updated generated-types.ts');
|
|
73
70
|
}
|
|
74
71
|
// Initial run
|
|
75
72
|
updateTypesFile();
|
|
76
73
|
// Watch for changes on HTML and CSS
|
|
77
|
-
|
|
74
|
+
chokidar.watch(['index.html', '**/*.css'], { ignored: /node_modules/ }).on('change', () => updateTypesFile());
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,gCAAgC;AAEhC,yDAAyD;AACzD,SAAS,UAAU,CAAC,IAAY;IAC5B,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7B,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE;QACvB,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC9B,IAAI,EAAE;YAAE,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;IACH,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAClC,CAAC;AAED,4DAA4D;AAC5D,SAAS,aAAa,CAAC,GAAW;IAC9B,wCAAwC;IACxC,MAAM,YAAY,GAAG,4BAA4B,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,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,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,eAAe,EAAE,CAAC,CAAC"}
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
"target": "esnext",
|
|
4
4
|
"module": "NodeNext",
|
|
5
5
|
"moduleResolution": "NodeNext",
|
|
6
|
-
"allowImportingTsExtensions": true,
|
|
6
|
+
// "allowImportingTsExtensions": true,
|
|
7
7
|
"sourceMap": true,
|
|
8
|
-
"noEmit": true,
|
|
8
|
+
// "noEmit": true,
|
|
9
9
|
"esModuleInterop": true,
|
|
10
10
|
"forceConsistentCasingInFileNames": true,
|
|
11
11
|
"strict": true,
|