binhend 1.5.0 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -1,6 +1,6 @@
1
1
 
2
2
  const { parse } = require('path');
3
- const { scanNestedFiles, cloneFile, writeToFile, makeFullDirPath } = require('./component.file');
3
+ const { scanNestedFiles, cloneFileIfNew, writeToFileIfNew, makeFullDirPath } = require('./component.file');
4
4
  const CodeFormat = require('./code');
5
5
  const Component = require('./component');
6
6
  const UglifyJS = require('uglify-js');
@@ -19,23 +19,24 @@ function processEachFile({ source: sourceRootPath, web: outputRootPath, module:
19
19
  makeFullDirPath(fileOutputPath);
20
20
 
21
21
  if (parse(file.name).ext !== '.js') {
22
- return cloneFile(fileSourcePath, fileOutputPath);
22
+ return cloneFileIfNew(fileSourcePath, fileOutputPath);
23
23
  }
24
24
 
25
25
  try {
26
26
  var component = require(file.path);
27
27
  }
28
28
  catch (error) {
29
- return cloneFile(fileSourcePath, fileOutputPath);
29
+ return cloneFileIfNew(fileSourcePath, fileOutputPath);
30
30
  }
31
31
 
32
32
  if (!(component instanceof Function) || component.constructor !== Component) {
33
- return cloneFile(fileSourcePath, fileOutputPath);
33
+ return cloneFileIfNew(fileSourcePath, fileOutputPath);
34
34
  }
35
35
 
36
36
  var code = buildCodeMethod(component, stageRootPath);
37
37
  code = Component.minification ? minifyCode(code) : beautifyCode(code);
38
- writeToFile(fileOutputPath, code);
38
+
39
+ writeToFileIfNew(fileOutputPath, code);
39
40
  });
40
41
  }
41
42
 
@@ -1,10 +1,7 @@
1
1
 
2
- const { readdir, statSync, existsSync, copyFile, writeFile, mkdirSync } = require('fs');
2
+ const { readdir, statSync, existsSync, copyFile, writeFile, writeFileSync, readFileSync, mkdirSync } = require('fs');
3
3
  const { join, dirname } = require('path');
4
4
 
5
- // TODO
6
- // [-] Enhance code by removing sync logics (except for existsSync, mkdirSync): readdir, readFileSync, (statSync?)
7
-
8
5
  function getItemInfo(itemPath) {
9
6
  var info = {};
10
7
 
@@ -91,6 +88,22 @@ function cloneFile(filepath, outpath) {
91
88
  });
92
89
  }
93
90
 
91
+ function cloneFileIfNew(filepath, outpath) {
92
+ if (isSameFile(filepath, outpath)) return;
93
+ cloneFile(filepath, outpath);
94
+ }
95
+
96
+ function isSameFile(filePathA, filePathB) {
97
+ var sameFile = false;
98
+
99
+ if (existsSync(filePathA) && existsSync(filePathB)) {
100
+ var fileA = readFileSync(filePathA), fileB = readFileSync(filePathB);
101
+ sameFile = fileA.equals(fileB);
102
+ }
103
+
104
+ return sameFile;
105
+ }
106
+
94
107
  function makeFullDirPath(filePath) {
95
108
  var dirPath = dirname(filePath);
96
109
  if (existsSync(dirPath)) return true;
@@ -106,6 +119,29 @@ function writeToFile(fileOutputPath, content) {
106
119
  });
107
120
  }
108
121
 
122
+ function writeToFileIfNew(fileOutputPath, content) {
123
+ try {
124
+ if (isSameContent(fileOutputPath, content)) return false;
125
+ writeFileSync(fileOutputPath, content, { encoding: 'utf8', flag: 'w' });
126
+ return true;
127
+ }
128
+ catch (error) {
129
+ printError('Failed writing file', fileOutputPath, error);
130
+ return false;
131
+ }
132
+ }
133
+
134
+ function isSameContent(filePath, content) {
135
+ var sameContent = false;
136
+
137
+ if (existsSync(filePath)) {
138
+ var current = readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
139
+ sameContent = content === current;
140
+ }
141
+
142
+ return sameContent;
143
+ }
144
+
109
145
  function printError(message, id, error) {
110
146
  console.error(`[BINHEND][COMPONENT] ${message}:`, id);
111
147
  console.error('[BINHEND][COMPONENT] Error details:', error);
@@ -117,7 +153,10 @@ module.exports = {
117
153
  scanFiles,
118
154
  scanNestedFiles,
119
155
  cloneFile,
156
+ cloneFileIfNew,
120
157
  makeFullDirPath,
121
158
  writeToFile,
159
+ writeToFileIfNew,
160
+ isSameContent,
122
161
  printError
123
162
  };
@@ -1,10 +1,7 @@
1
1
 
2
- const { writeFileSync, readFileSync, existsSync } = require('fs');
2
+ const { readFileSync } = require('fs');
3
3
  const { parse } = require('path');
4
- const { scanNestedFiles, cloneFile, printError, makeFullDirPath } = require('./component.file');
5
-
6
- // TODO
7
- // [-] Enhance code by removing sync logics (except for existsSync, mkdirSync): readdir, readFileSync, (statSync?)
4
+ const { scanNestedFiles, cloneFileIfNew, printError, makeFullDirPath, writeToFileIfNew } = require('./component.file');
8
5
 
9
6
  const PREFIX_CODE =
10
7
  `var { context, tag, svg, script, require, css } = binh.context(module, require);
@@ -29,19 +26,17 @@ function generate(sourceRootPath, outputRootPath, callbackDone) {
29
26
 
30
27
  if (fileExtension === '.css') {
31
28
  var cssModuleCode = 'binh.css(module);';
32
- writeFileSync(fileOutputPath + '.js', cssModuleCode, { encoding: 'utf8', flag: 'w' });
29
+ writeToFileIfNew(fileOutputPath + '.js', cssModuleCode);
33
30
  }
34
31
 
35
32
  if (fileExtension !== '.js') {
36
- return cloneFile(fileSourcePath, fileOutputPath);
33
+ return cloneFileIfNew(fileSourcePath, fileOutputPath);
37
34
  }
38
35
 
39
36
  var content = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
40
37
  var code = PREFIX_CODE + content.trim() + '\r\n\r\n;binh.final(module);';
41
38
 
42
- if (!isSameContent(code, fileSourcePath)) {
43
- writeFileSync(fileOutputPath, code, { encoding: 'utf8', flag: 'w' });
44
- }
39
+ writeToFileIfNew(fileOutputPath, code);
45
40
  }
46
41
  catch (error) {
47
42
  printError('Failed formatting file:', fileSourcePath, error);
@@ -49,17 +44,6 @@ function generate(sourceRootPath, outputRootPath, callbackDone) {
49
44
  });
50
45
  }
51
46
 
52
- function isSameContent(code, filePath) {
53
- var sameContent = false;
54
-
55
- if (existsSync(filePath)) {
56
- var current = readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
57
- sameContent = code === current;
58
- }
59
-
60
- return sameContent;
61
- }
62
-
63
47
  module.exports = {
64
48
  generate
65
49
  };