forgecss 0.4.0 → 0.5.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.
Files changed (3) hide show
  1. package/index.d.ts +5 -4
  2. package/index.js +33 -3
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -3,14 +3,15 @@ export type ForgeCSSOptions = {
3
3
  usageFiles?: string[];
4
4
  usageAttributes?: string[];
5
5
  breakpoints?: {
6
- [key: string]: string
6
+ [key: string]: string;
7
7
  };
8
+ verbose?: boolean;
8
9
  };
9
10
 
10
11
  export type ForgeInstance = {
11
- parseDirectory: (options: { dir: string; output?: string }) => Promise<string>;
12
- parseFile: (options: { file: string; output?: string }) => Promise<string>;
13
- parse: (options: { css: string; html?: string; jsx?: string; output?: string }) => Promise<string>;
12
+ parseDirectory: (options: { dir: string; output?: string; watch?: boolean }) => Promise<string>;
13
+ parseFile: (options: { file: string; output?: string; watch?: boolean }) => Promise<string>;
14
+ parse: (options: { css: string; html?: string; jsx?: string; output?: string; }) => Promise<string>;
14
15
  };
15
16
 
16
17
  declare function ForgeCSS(options?: ForgeCSSOptions): ForgeInstance;
package/index.js CHANGED
@@ -1,20 +1,27 @@
1
+ import path from 'path';
1
2
  import { writeFile } from "fs/promises";
2
3
  import getAllFiles from "./lib/getAllFiles.js";
3
4
  import { extractStyles, invalidateInvetory } from "./lib/inventory.js";
4
5
  import { invalidateUsageCache, findUsages } from "./lib/usages.js";
5
6
  import { generateOutputCSS } from "./lib/generator.js";
7
+ import chokidar from "chokidar";
6
8
 
7
9
  const DEFAULT_OPTIONS = {
8
10
  inventoryFiles: ["css", "less", "scss"],
9
11
  usageFiles: ["html", "jsx", "tsx"],
10
12
  usageAttributes: ["class", "className"],
11
- breakpoints: {}
13
+ breakpoints: {},
14
+ verbose: true
12
15
  };
13
16
 
14
17
  export default function ForgeCSS(options) {
15
18
  const config = { ...DEFAULT_OPTIONS };
16
19
 
17
20
  config.breakpoints = Object.assign({}, DEFAULT_OPTIONS.breakpoints, options?.breakpoints ?? {});
21
+ config.inventoryFiles = options?.inventoryFiles ?? DEFAULT_OPTIONS.inventoryFiles;
22
+ config.usageFiles = options?.usageFiles ?? DEFAULT_OPTIONS.usageFiles;
23
+ config.usageAttributes = options?.usageAttributes ?? DEFAULT_OPTIONS.usageAttributes;
24
+ config.verbose = options?.verbose ?? DEFAULT_OPTIONS.verbose;
18
25
 
19
26
  async function result(output) {
20
27
  try {
@@ -22,15 +29,32 @@ export default function ForgeCSS(options) {
22
29
  if (output) {
23
30
  await writeFile(output, `/* ForgeCSS autogenerated file */\n${css}`, "utf-8");
24
31
  }
32
+ console.log("forgecss: Output CSS generated successfully.");
25
33
  return css;
26
34
  } catch (err) {
27
35
  console.error(`forgecss: error generating output CSS: ${err}`);
28
36
  }
29
37
  return null;
30
38
  }
39
+ function runWatcher(what, output, callback) {
40
+ const watcher = chokidar.watch(what, {
41
+ persistent: true,
42
+ ignoreInitial: true,
43
+ ignored: (p, stats) => output && path.resolve(p) === path.resolve(output)
44
+ });
45
+ watcher.on("change", async (filePath) => {
46
+ if (config.verbose) {
47
+ console.log(`forgecss: Detected change in ${filePath}`);
48
+ }
49
+ callback();
50
+ });
51
+ if (config.verbose) {
52
+ console.log("forgecss: Watch mode enabled. Listening for file changes...");
53
+ }
54
+ }
31
55
 
32
56
  return {
33
- async parseDirectory({ dir, output = null }) {
57
+ async parseDirectory({ dir, output = null, watch = false }) {
34
58
  if (!dir) {
35
59
  throw new Error('forgecss: parseDirectory requires "dir" as an argument.');
36
60
  }
@@ -52,10 +76,13 @@ export default function ForgeCSS(options) {
52
76
  } catch (err) {
53
77
  console.error(`forgecss: error extracting usages`, err);
54
78
  }
79
+ watch && runWatcher(dir, output, () => {
80
+ this.parseDirectory({ dir, output, watch: false });
81
+ });
55
82
  // generating the output CSS
56
83
  return result(output);
57
84
  },
58
- async parseFile({ file, output = null }) {
85
+ async parseFile({ file, output = null, watch = false }) {
59
86
  if (!file) {
60
87
  throw new Error('forgecss: parseFile requires "file" as an argument.');
61
88
  }
@@ -77,6 +104,9 @@ export default function ForgeCSS(options) {
77
104
  } catch (err) {
78
105
  console.error(`forgecss: error extracting usages.`, err);
79
106
  }
107
+ watch && runWatcher(file, output, () => {
108
+ this.parseFile({ file, output, watch: false });
109
+ });
80
110
  // generating the output CSS
81
111
  return result(output);
82
112
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "forgecss",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "ForgeCSS turns strings into fully generated responsive CSS using a custom DSL.",
6
6
  "author": "Krasimir Tsonev",