@osmn-byhn/css-formatter-cli 1.0.0 → 1.0.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/cli.js +80 -78
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osmn-byhn/css-formatter-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "description": "Command line interface for @osmn-byhn/css-formatter to inline or extract CSS from HTML",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -4,93 +4,95 @@ import { Command } from 'commander';
4
4
  import chalk from 'chalk';
5
5
  import fs from 'fs/promises';
6
6
  import path from 'path';
7
- import { inlineCSS, reverseCSSInternal, reverseCSSExternal } from '@osmn-byhn/css-formatter';
7
+ import { createRequire } from 'module';
8
+ const require = createRequire(import.meta.url);
9
+ const { inlineCSS, reverseCSSInternal, reverseCSSExternal } = require('@osmn-byhn/css-formatter');
8
10
 
9
11
  const program = new Command();
10
12
 
11
13
  program
12
- .name('css-formatter')
13
- .description('CLI tool for inlining and extracting CSS using @osmn-byhn/css-formatter')
14
- .version('1.0.0');
14
+ .name('css-formatter')
15
+ .description('CLI tool for inlining and extracting CSS using @osmn-byhn/css-formatter')
16
+ .version('1.0.0');
15
17
 
16
18
  program
17
- .command('inline')
18
- .description('Inlines CSS into the HTML file')
19
- .argument('<input>', 'Input HTML file path')
20
- .option('-o, --output <path>', 'Output file path (defaults to overwriting input)')
21
- .action(async (input, options) => {
22
- try {
23
- const inputPath = path.resolve(input);
24
- const html = await fs.readFile(inputPath, 'utf-8');
25
-
26
- console.log(chalk.blue(`🚀 Inlining CSS in ${input}...`));
27
- const result = await inlineCSS(html);
28
-
29
- const outputPath = options.output ? path.resolve(options.output) : inputPath;
30
- await fs.writeFile(outputPath, result);
31
-
32
- console.log(chalk.green(`✅ Successfully inlined CSS. Saved to: ${outputPath}`));
33
- } catch (error) {
34
- console.error(chalk.red(`❌ Error: ${error.message}`));
35
- process.exit(1);
36
- }
37
- });
19
+ .command('inline')
20
+ .description('Inlines CSS into the HTML file')
21
+ .argument('<input>', 'Input HTML file path')
22
+ .option('-o, --output <path>', 'Output file path (defaults to overwriting input)')
23
+ .action(async (input, options) => {
24
+ try {
25
+ const inputPath = path.resolve(input);
26
+ const html = await fs.readFile(inputPath, 'utf-8');
27
+
28
+ console.log(chalk.blue(`🚀 Inlining CSS in ${input}...`));
29
+ const result = await inlineCSS(html);
30
+
31
+ const outputPath = options.output ? path.resolve(options.output) : inputPath;
32
+ await fs.writeFile(outputPath, result);
33
+
34
+ console.log(chalk.green(`✅ Successfully inlined CSS. Saved to: ${outputPath}`));
35
+ } catch (error) {
36
+ console.error(chalk.red(`❌ Error: ${error.message}`));
37
+ process.exit(1);
38
+ }
39
+ });
38
40
 
39
41
  program
40
- .command('internal')
41
- .description('Extracts inline styles into a <style> tag')
42
- .argument('<input>', 'Input HTML file path')
43
- .option('-o, --output <path>', 'Output file path (defaults to overwriting input)')
44
- .action(async (input, options) => {
45
- try {
46
- const inputPath = path.resolve(input);
47
- const html = await fs.readFile(inputPath, 'utf-8');
48
-
49
- console.log(chalk.blue(`🚀 Extracting inline styles to internal <style> tag in ${input}...`));
50
- const result = await reverseCSSInternal(html);
51
-
52
- const outputPath = options.output ? path.resolve(options.output) : inputPath;
53
- await fs.writeFile(outputPath, result);
54
-
55
- console.log(chalk.green(`✅ Successfully extracted styles to internal <style>. Saved to: ${outputPath}`));
56
- } catch (error) {
57
- console.error(chalk.red(`❌ Error: ${error.message}`));
58
- process.exit(1);
59
- }
60
- });
42
+ .command('internal')
43
+ .description('Extracts inline styles into a <style> tag')
44
+ .argument('<input>', 'Input HTML file path')
45
+ .option('-o, --output <path>', 'Output file path (defaults to overwriting input)')
46
+ .action(async (input, options) => {
47
+ try {
48
+ const inputPath = path.resolve(input);
49
+ const html = await fs.readFile(inputPath, 'utf-8');
50
+
51
+ console.log(chalk.blue(`🚀 Extracting inline styles to internal <style> tag in ${input}...`));
52
+ const result = await reverseCSSInternal(html);
53
+
54
+ const outputPath = options.output ? path.resolve(options.output) : inputPath;
55
+ await fs.writeFile(outputPath, result);
56
+
57
+ console.log(chalk.green(`✅ Successfully extracted styles to internal <style>. Saved to: ${outputPath}`));
58
+ } catch (error) {
59
+ console.error(chalk.red(`❌ Error: ${error.message}`));
60
+ process.exit(1);
61
+ }
62
+ });
61
63
 
62
64
  program
63
- .command('extract')
64
- .description('Extracts inline styles into an external CSS file')
65
- .argument('<input>', 'Input HTML file path')
66
- .argument('[cssOutput]', 'Output CSS file path (defaults to styles.css)')
67
- .option('-o, --output <path>', 'Output HTML file path (defaults to overwriting input)')
68
- .action(async (input, cssOutput, options) => {
69
- try {
70
- const inputPath = path.resolve(input);
71
- const html = await fs.readFile(inputPath, 'utf-8');
72
-
73
- const cssFileName = cssOutput || 'styles.css';
74
- const cssPath = path.resolve(path.dirname(inputPath), cssFileName);
75
-
76
- console.log(chalk.blue(`🚀 Extracting inline styles from ${input} to ${cssFileName}...`));
77
- const { html: htmlOutput, css: cssResult } = await reverseCSSExternal(html);
78
-
79
- // The library's reverseCSSExternal might be putting a default link tag.
80
- // We should check if we need to adjust the link tag href if the user provided a specific path.
81
-
82
- const outputPath = options.output ? path.resolve(options.output) : inputPath;
83
-
84
- await fs.writeFile(outputPath, htmlOutput);
85
- await fs.writeFile(cssPath, cssResult);
86
-
87
- console.log(chalk.green(`✅ Successfully extracted styles.`));
88
- console.log(chalk.green(` HTML saved to: ${outputPath}`));
89
- console.log(chalk.green(` CSS saved to: ${cssPath}`));
90
- } catch (error) {
91
- console.error(chalk.red(`❌ Error: ${error.message}`));
92
- process.exit(1);
93
- }
94
- });
65
+ .command('extract')
66
+ .description('Extracts inline styles into an external CSS file')
67
+ .argument('<input>', 'Input HTML file path')
68
+ .argument('[cssOutput]', 'Output CSS file path (defaults to styles.css)')
69
+ .option('-o, --output <path>', 'Output HTML file path (defaults to overwriting input)')
70
+ .action(async (input, cssOutput, options) => {
71
+ try {
72
+ const inputPath = path.resolve(input);
73
+ const html = await fs.readFile(inputPath, 'utf-8');
74
+
75
+ const cssFileName = cssOutput || 'styles.css';
76
+ const cssPath = path.resolve(path.dirname(inputPath), cssFileName);
77
+
78
+ console.log(chalk.blue(`🚀 Extracting inline styles from ${input} to ${cssFileName}...`));
79
+ const { html: htmlOutput, css: cssResult } = await reverseCSSExternal(html);
80
+
81
+ // The library's reverseCSSExternal might be putting a default link tag.
82
+ // We should check if we need to adjust the link tag href if the user provided a specific path.
83
+
84
+ const outputPath = options.output ? path.resolve(options.output) : inputPath;
85
+
86
+ await fs.writeFile(outputPath, htmlOutput);
87
+ await fs.writeFile(cssPath, cssResult);
88
+
89
+ console.log(chalk.green(`✅ Successfully extracted styles.`));
90
+ console.log(chalk.green(` HTML saved to: ${outputPath}`));
91
+ console.log(chalk.green(` CSS saved to: ${cssPath}`));
92
+ } catch (error) {
93
+ console.error(chalk.red(`❌ Error: ${error.message}`));
94
+ process.exit(1);
95
+ }
96
+ });
95
97
 
96
98
  program.parse();