@bobfrankston/extractids 1.0.10 → 1.0.12

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.
@@ -3,9 +3,11 @@
3
3
  "allow": [
4
4
  "Bash(git add:*)",
5
5
  "Bash(git commit:*)",
6
- "Bash(npm publish:*)"
6
+ "Bash(npm publish:*)",
7
+ "Bash(tsc:*)",
8
+ "Bash(node:*)"
7
9
  ],
8
10
  "deny": [],
9
11
  "ask": []
10
12
  }
11
- }
13
+ }
package/index.js CHANGED
@@ -4,6 +4,7 @@ import * as fs from 'node:fs';
4
4
  import * as path from 'node:path';
5
5
  import * as cheerio from 'cheerio';
6
6
  import chokidar from 'chokidar';
7
+ import pkg from './package.json' with { type: 'json' };
7
8
  // import * as glob from 'glob';
8
9
  // Helper function to extract all id attributes from HTML
9
10
  function getHtmlIDs(html) {
@@ -51,13 +52,36 @@ function generateTypes(ids, classes) {
51
52
  const idType = ids.length > 0
52
53
  ? `export type htmlIDs = ${ids.map(id => `"${id}"`).join(' |\n')};\n`
53
54
  : `export type htmlIDs = never;\n`;
55
+ const queryType = ids.length > 0
56
+ ? `export type queryIDs = ${ids.map(id => `"#${id}"`).join(' |\n')};\n`
57
+ : `export type queryIDs = never;\n`;
54
58
  const classType = classes.length > 0
55
59
  ? `export type cssClasses = ${classes.map(cls => `"${cls}"`).join(' |\n')};\n`
56
60
  : `export type cssClasses = never;\n`;
57
- return idType + classType;
61
+ return idType + queryType + classType;
58
62
  }
59
- function updateTypesFile() {
60
- const html = fs.readFileSync('index.html', 'utf8');
63
+ function updateTypesFile(specifiedHtmlFile) {
64
+ let htmlFile;
65
+ if (specifiedHtmlFile) {
66
+ // Use the specified file if provided
67
+ if (fs.existsSync(specifiedHtmlFile)) {
68
+ htmlFile = specifiedHtmlFile;
69
+ }
70
+ else {
71
+ console.error(`[extractids] Specified HTML file not found: ${specifiedHtmlFile}`);
72
+ return;
73
+ }
74
+ }
75
+ else {
76
+ // Check for HTML files in order of preference
77
+ const possibleHtmlFiles = ['index.html', 'default.html', 'default.htm'];
78
+ htmlFile = possibleHtmlFiles.find(file => fs.existsSync(file));
79
+ if (!htmlFile) {
80
+ console.error('[extractids] No HTML file found. Looking for:', possibleHtmlFiles.join(', '));
81
+ return;
82
+ }
83
+ }
84
+ const html = fs.readFileSync(htmlFile, 'utf8');
61
85
  const ids = getHtmlIDs(html);
62
86
  const cssFiles = globReadCssFiles();
63
87
  const allClasses = new Set();
@@ -69,14 +93,45 @@ function updateTypesFile() {
69
93
  fs.writeFileSync('generated-types.ts', types);
70
94
  console.log('[Type generation] Updated generated-types.ts');
71
95
  }
72
- // Parse CLI arguments for -w or --watch
96
+ // Parse CLI arguments for -w or --watch and optional HTML filename
73
97
  const args = process.argv.slice(2);
98
+ // Check for version flag
99
+ if (args.includes('-v') || args.includes('--version')) {
100
+ console.log(pkg.version);
101
+ process.exit(0);
102
+ }
103
+ // Check for help flags
104
+ if (args.includes('-h') || args.includes('-help') || args.includes('--help')) {
105
+ console.log(`
106
+ Usage: extractids [options] [htmlfile]
107
+
108
+ Options:
109
+ -h, -help, --help Show this help message
110
+ -v, --version Show version number
111
+ -w, -watch, --watch Watch for changes and regenerate types automatically
112
+
113
+ Arguments:
114
+ htmlfile Optional HTML file to parse (default: searches for index.html, default.html, or default.htm)
115
+
116
+ Examples:
117
+ extractids # Parse default HTML file once
118
+ extractids mypage.html # Parse specific HTML file once
119
+ extractids --watch # Watch default HTML file for changes
120
+ extractids mypage.html -w # Watch specific HTML file for changes
121
+ `);
122
+ process.exit(0);
123
+ }
74
124
  const watchMode = args.includes('-w') || args.includes('-watch') || args.includes('--watch');
125
+ // Find first argument that doesn't start with '-' as the HTML filename
126
+ const specifiedHtmlFile = args.find(arg => !arg.startsWith('-'));
75
127
  if (watchMode) {
76
- updateTypesFile();
128
+ updateTypesFile(specifiedHtmlFile);
77
129
  console.log('[extractids] Watching for changes...');
78
- const watcher = chokidar.watch(['index.html', '**/*.css'], { ignored: /node_modules/ });
79
- watcher.on('change', () => updateTypesFile());
130
+ const htmlFilesToWatch = specifiedHtmlFile
131
+ ? [specifiedHtmlFile]
132
+ : ['index.html', 'default.html', 'default.htm'];
133
+ const watcher = chokidar.watch([...htmlFilesToWatch, '**/*.css'], { ignored: /node_modules/ });
134
+ watcher.on('change', () => updateTypesFile(specifiedHtmlFile));
80
135
  watcher.on('ready', () => {
81
136
  console.log('[extractids] Watcher is ready and running.');
82
137
  });
@@ -86,6 +141,6 @@ if (watchMode) {
86
141
  // No need for setInterval; watcher should keep process alive
87
142
  }
88
143
  else {
89
- updateTypesFile();
144
+ updateTypesFile(specifiedHtmlFile);
90
145
  }
91
146
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@bobfrankston/extractids",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Extracd html and css as types",
5
5
  "main": "index.js",
6
6
  "bin": {
7
- "extractids": "index.js",
8
- "generate-importmap": "generate-importmap.js"
7
+ "extractids": "index.js"
9
8
  },
10
9
  "module": "main.js",
11
10
  "scripts": {
@@ -21,7 +20,7 @@
21
20
  "esm": "^3.2.25"
22
21
  },
23
22
  "devDependencies": {
24
- "@types/node": "^24.5.2"
23
+ "@types/node": "^25.2.1"
25
24
  },
26
25
  "repository": {
27
26
  "type": "git",
@@ -30,5 +29,8 @@
30
29
  "bugs": {
31
30
  "url": "https://github.com/BobFrankston/extractids/issues"
32
31
  },
33
- "homepage": "https://github.com/BobFrankston/extractids#readme"
32
+ "homepage": "https://github.com/BobFrankston/extractids#readme",
33
+ "publishConfig": {
34
+ "access": "public"
35
+ }
34
36
  }
@@ -1,85 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Generate ES Module import map from package.json dependencies
4
- * Injects into HTML files for native browser module loading
5
- */
6
-
7
- import fs from 'fs';
8
- import path from 'path';
9
- import chokidar from 'chokidar';
10
-
11
- function generateImportMap(packageJsonPath, htmlFilePath) {
12
- try {
13
- // Read package.json
14
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
15
- const dependencies = packageJson.dependencies || {};
16
-
17
- // Generate import map
18
- const imports = {};
19
- for (const [name, version] of Object.entries(dependencies)) {
20
- imports[name] = `./node_modules/${name}/index.js`;
21
- }
22
-
23
- const importMapScript = `<script type="importmap">
24
- ${JSON.stringify({ imports }, null, 12)}
25
- </script>`;
26
-
27
- // Read HTML file
28
- let html = fs.readFileSync(htmlFilePath, 'utf-8');
29
-
30
- // Replace import map section
31
- const importMapRegex = /<!-- Import map.*?-->|<script type="importmap">[\s\S]*?<\/script>/;
32
-
33
- if (importMapRegex.test(html)) {
34
- html = html.replace(importMapRegex, importMapScript);
35
- } else {
36
- // Insert before </head> if not found
37
- html = html.replace('</head>', ` ${importMapScript}\n</head>`);
38
- }
39
-
40
- // Write back to HTML file
41
- fs.writeFileSync(htmlFilePath, html, 'utf-8');
42
-
43
- console.log('[generate-importmap] Updated import map');
44
- console.log(' Dependencies:', Object.keys(dependencies).join(', ') || '(none)');
45
- } catch (e) {
46
- console.error('[generate-importmap] Error:', e.message);
47
- process.exit(1);
48
- }
49
- }
50
-
51
- // Parse CLI arguments
52
- const args = process.argv.slice(2);
53
- const watchMode = args.includes('-w') || args.includes('--watch');
54
- const packageJsonPath = path.join(process.cwd(), 'package.json');
55
- const htmlFilePath = path.join(process.cwd(), 'index.html');
56
-
57
- // Check if files exist
58
- if (!fs.existsSync(packageJsonPath)) {
59
- console.error('[generate-importmap] Error: package.json not found in current directory');
60
- process.exit(1);
61
- }
62
- if (!fs.existsSync(htmlFilePath)) {
63
- console.error('[generate-importmap] Error: index.html not found in current directory');
64
- process.exit(1);
65
- }
66
-
67
- if (watchMode) {
68
- generateImportMap(packageJsonPath, htmlFilePath);
69
- console.log('[generate-importmap] Watching for changes...');
70
-
71
- const watcher = chokidar.watch([packageJsonPath], {
72
- persistent: true,
73
- ignoreInitial: true
74
- });
75
-
76
- watcher.on('change', () => generateImportMap(packageJsonPath, htmlFilePath));
77
- watcher.on('ready', () => {
78
- console.log('[generate-importmap] Watcher is ready and running.');
79
- });
80
- watcher.on('error', (error) => {
81
- console.error('[generate-importmap] Watcher error:', error.message);
82
- });
83
- } else {
84
- generateImportMap(packageJsonPath, htmlFilePath);
85
- }