@prostojs/rewrite 0.2.1 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -35,7 +35,7 @@ async function getPath() {
35
35
  return _path;
36
36
  }
37
37
  async function readDir(dirPath) {
38
- if (__NODE_JS__) {
38
+ {
39
39
  const path = await getPath();
40
40
  const fs = await getFs();
41
41
  async function* getFiles(dir) {
@@ -51,7 +51,7 @@ async function readDir(dirPath) {
51
51
  throw new Error(errorMessage);
52
52
  }
53
53
  async function readFile(filePath) {
54
- if (__NODE_JS__) {
54
+ {
55
55
  const fs = await getFs();
56
56
  try {
57
57
  return (await fs.promises.readFile(filePath)).toString();
@@ -62,7 +62,7 @@ async function readFile(filePath) {
62
62
  throw new Error(errorMessage);
63
63
  }
64
64
  async function writeFile(targetPath, data) {
65
- if (__NODE_JS__) {
65
+ {
66
66
  const path = await getPath();
67
67
  const fs = await getFs();
68
68
  const dirname = path.dirname(targetPath);
@@ -73,7 +73,7 @@ async function writeFile(targetPath, data) {
73
73
  throw new Error(errorMessage);
74
74
  }
75
75
  async function copyFile(filePath, targetPath) {
76
- if (__NODE_JS__) {
76
+ {
77
77
  if (filePath === targetPath) return;
78
78
  const path = await getPath();
79
79
  const fs = await getFs();
@@ -85,12 +85,10 @@ async function copyFile(filePath, targetPath) {
85
85
  throw new Error(errorMessage);
86
86
  }
87
87
  async function pathResolve(p) {
88
- if (__NODE_JS__) return (await getPath()).resolve(p);
89
- throw new Error(errorMessage);
88
+ return (await getPath()).resolve(p);
90
89
  }
91
90
  async function pathJoin(...parts) {
92
- if (__NODE_JS__) return (await getPath()).join(...parts);
93
- throw new Error(errorMessage);
91
+ return (await getPath()).join(...parts);
94
92
  }
95
93
  //#endregion
96
94
  //#region src/string-expression.ts
@@ -144,7 +142,7 @@ function escapeRegex(s) {
144
142
  return s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
145
143
  }
146
144
  function debug(message) {
147
- console.log(__DYE_YELLOW__ + __DYE_BOLD__ + __DYE_DIM__ + "[rewrite] DEBUG: " + message + __DYE_RESET__);
145
+ console.log("\x1B[33m\x1B[1m\x1B[2m[rewrite] DEBUG: " + message + "\x1B[0m");
148
146
  }
149
147
  //#endregion
150
148
  //#region src/rw-common.ts
@@ -243,7 +241,7 @@ function throwErrorFromFunction(e, code) {
243
241
  const row = parseInt(match[1], 10) - 2;
244
242
  const col = parseInt(match[2], 10) - 1;
245
243
  const output = renderCodeFragmentSimple(code.split("\n"), row, col);
246
- console.error(__DYE_RED_BRIGHT__ + e.message + __DYE_COLOR_OFF__);
244
+ console.error("\x1B[91m" + e.message + "\x1B[39m");
247
245
  console.error(output);
248
246
  }
249
247
  throw e;
@@ -743,13 +741,13 @@ var ProstoRewrite = class {
743
741
  this.options = {
744
742
  defaultMode: options?.defaultMode || "auto",
745
743
  debug: options?.debug || false,
746
- htmlPattern: __NODE_JS__ ? (options?.htmlPattern || ["*.{html,xhtml,xml,svg}"]).map((p) => new minimatch.Minimatch(p, { matchBase: true })) : [],
747
- textPattern: __NODE_JS__ ? (options?.textPattern || [
744
+ htmlPattern: (options?.htmlPattern || ["*.{html,xhtml,xml,svg}"]).map((p) => new minimatch.Minimatch(p, { matchBase: true })),
745
+ textPattern: (options?.textPattern || [
748
746
  "*.{js,jsx,ts,tsx,txt,json,yml,yaml,md,ini}",
749
747
  "Dockerfile",
750
748
  "*config",
751
749
  ".gitignore"
752
- ]).map((p) => new minimatch.Minimatch(p, { matchBase: true })) : [],
750
+ ]).map((p) => new minimatch.Minimatch(p, { matchBase: true })),
753
751
  html: {
754
752
  exprDelimiters: defaultDelimiters,
755
753
  attrExpression: ":",
@@ -794,20 +792,19 @@ var ProstoRewrite = class {
794
792
  const mode = this.determineMode(opts.mode || this.options.defaultMode, opts.input, file);
795
793
  let output = file;
796
794
  if (mode) {
797
- this.debug("Parsing file in " + mode + " mode. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.input);
795
+ this.debug("Parsing file in " + mode + " mode. \x1B[4m\x1B[22m" + opts.input);
798
796
  output = this.mixedRewriter[mode].rewrite(file, context);
799
- } else this.debug("File isn't recognized as text nor as html. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.input);
797
+ } else this.debug("File isn't recognized as text nor as html. \x1B[4m\x1B[22m" + opts.input);
800
798
  if (opts.output) if (mode) {
801
799
  await writeFile(opts.output, output);
802
- this.debug("File has been rewritten in " + mode + " mode. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.output);
800
+ this.debug("File has been rewritten in " + mode + " mode. \x1B[4m\x1B[22m" + opts.output);
803
801
  } else {
804
802
  await copyFile(opts.input, opts.output);
805
- this.debug("File rewriting was skipped. File copied. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.output);
803
+ this.debug("File rewriting was skipped. File copied. \x1B[4m\x1B[22m" + opts.output);
806
804
  }
807
805
  return output;
808
806
  }
809
807
  async rewriteDir(opts, context) {
810
- if (__BROWSER__) return;
811
808
  const files = await readDir(opts.baseDir);
812
809
  const dirPath = await pathResolve(opts.baseDir);
813
810
  const include = opts.include && opts.include.length ? opts.include.map((s) => new minimatch.Minimatch(s, { matchBase: true })) : null;
package/dist/index.mjs CHANGED
@@ -34,7 +34,7 @@ async function getPath() {
34
34
  return _path;
35
35
  }
36
36
  async function readDir(dirPath) {
37
- if (__NODE_JS__) {
37
+ {
38
38
  const path = await getPath();
39
39
  const fs = await getFs();
40
40
  async function* getFiles(dir) {
@@ -50,7 +50,7 @@ async function readDir(dirPath) {
50
50
  throw new Error(errorMessage);
51
51
  }
52
52
  async function readFile(filePath) {
53
- if (__NODE_JS__) {
53
+ {
54
54
  const fs = await getFs();
55
55
  try {
56
56
  return (await fs.promises.readFile(filePath)).toString();
@@ -61,7 +61,7 @@ async function readFile(filePath) {
61
61
  throw new Error(errorMessage);
62
62
  }
63
63
  async function writeFile(targetPath, data) {
64
- if (__NODE_JS__) {
64
+ {
65
65
  const path = await getPath();
66
66
  const fs = await getFs();
67
67
  const dirname = path.dirname(targetPath);
@@ -72,7 +72,7 @@ async function writeFile(targetPath, data) {
72
72
  throw new Error(errorMessage);
73
73
  }
74
74
  async function copyFile(filePath, targetPath) {
75
- if (__NODE_JS__) {
75
+ {
76
76
  if (filePath === targetPath) return;
77
77
  const path = await getPath();
78
78
  const fs = await getFs();
@@ -84,12 +84,10 @@ async function copyFile(filePath, targetPath) {
84
84
  throw new Error(errorMessage);
85
85
  }
86
86
  async function pathResolve(p) {
87
- if (__NODE_JS__) return (await getPath()).resolve(p);
88
- throw new Error(errorMessage);
87
+ return (await getPath()).resolve(p);
89
88
  }
90
89
  async function pathJoin(...parts) {
91
- if (__NODE_JS__) return (await getPath()).join(...parts);
92
- throw new Error(errorMessage);
90
+ return (await getPath()).join(...parts);
93
91
  }
94
92
  //#endregion
95
93
  //#region src/string-expression.ts
@@ -143,7 +141,7 @@ function escapeRegex(s) {
143
141
  return s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
144
142
  }
145
143
  function debug(message) {
146
- console.log(__DYE_YELLOW__ + __DYE_BOLD__ + __DYE_DIM__ + "[rewrite] DEBUG: " + message + __DYE_RESET__);
144
+ console.log("\x1B[33m\x1B[1m\x1B[2m[rewrite] DEBUG: " + message + "\x1B[0m");
147
145
  }
148
146
  //#endregion
149
147
  //#region src/rw-common.ts
@@ -242,7 +240,7 @@ function throwErrorFromFunction(e, code) {
242
240
  const row = parseInt(match[1], 10) - 2;
243
241
  const col = parseInt(match[2], 10) - 1;
244
242
  const output = renderCodeFragmentSimple(code.split("\n"), row, col);
245
- console.error(__DYE_RED_BRIGHT__ + e.message + __DYE_COLOR_OFF__);
243
+ console.error("\x1B[91m" + e.message + "\x1B[39m");
246
244
  console.error(output);
247
245
  }
248
246
  throw e;
@@ -742,13 +740,13 @@ var ProstoRewrite = class {
742
740
  this.options = {
743
741
  defaultMode: options?.defaultMode || "auto",
744
742
  debug: options?.debug || false,
745
- htmlPattern: __NODE_JS__ ? (options?.htmlPattern || ["*.{html,xhtml,xml,svg}"]).map((p) => new Minimatch(p, { matchBase: true })) : [],
746
- textPattern: __NODE_JS__ ? (options?.textPattern || [
743
+ htmlPattern: (options?.htmlPattern || ["*.{html,xhtml,xml,svg}"]).map((p) => new Minimatch(p, { matchBase: true })),
744
+ textPattern: (options?.textPattern || [
747
745
  "*.{js,jsx,ts,tsx,txt,json,yml,yaml,md,ini}",
748
746
  "Dockerfile",
749
747
  "*config",
750
748
  ".gitignore"
751
- ]).map((p) => new Minimatch(p, { matchBase: true })) : [],
749
+ ]).map((p) => new Minimatch(p, { matchBase: true })),
752
750
  html: {
753
751
  exprDelimiters: defaultDelimiters,
754
752
  attrExpression: ":",
@@ -793,20 +791,19 @@ var ProstoRewrite = class {
793
791
  const mode = this.determineMode(opts.mode || this.options.defaultMode, opts.input, file);
794
792
  let output = file;
795
793
  if (mode) {
796
- this.debug("Parsing file in " + mode + " mode. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.input);
794
+ this.debug("Parsing file in " + mode + " mode. \x1B[4m\x1B[22m" + opts.input);
797
795
  output = this.mixedRewriter[mode].rewrite(file, context);
798
- } else this.debug("File isn't recognized as text nor as html. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.input);
796
+ } else this.debug("File isn't recognized as text nor as html. \x1B[4m\x1B[22m" + opts.input);
799
797
  if (opts.output) if (mode) {
800
798
  await writeFile(opts.output, output);
801
- this.debug("File has been rewritten in " + mode + " mode. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.output);
799
+ this.debug("File has been rewritten in " + mode + " mode. \x1B[4m\x1B[22m" + opts.output);
802
800
  } else {
803
801
  await copyFile(opts.input, opts.output);
804
- this.debug("File rewriting was skipped. File copied. " + __DYE_UNDERSCORE__ + __DYE_BOLD_OFF__ + opts.output);
802
+ this.debug("File rewriting was skipped. File copied. \x1B[4m\x1B[22m" + opts.output);
805
803
  }
806
804
  return output;
807
805
  }
808
806
  async rewriteDir(opts, context) {
809
- if (__BROWSER__) return;
810
807
  const files = await readDir(opts.baseDir);
811
808
  const dirPath = await pathResolve(opts.baseDir);
812
809
  const include = opts.include && opts.include.length ? opts.include.map((s) => new Minimatch(s, { matchBase: true })) : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prostojs/rewrite",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Easy and light templates renderer",
5
5
  "keywords": [
6
6
  "prostojs",
@@ -52,7 +52,7 @@
52
52
  "minimatch": "^10.2.4"
53
53
  },
54
54
  "devDependencies": {
55
- "@prostojs/dye": "^0.4.9",
55
+ "@prostojs/dye": "^0.4.10",
56
56
  "@types/node": "^20.19.35",
57
57
  "conventional-changelog": "^5.1.0",
58
58
  "conventional-changelog-cli": "^4.1.0",