binhend 1.2.0 → 1.2.2

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/bin/backend.js ADDED
@@ -0,0 +1,3 @@
1
+ module.exports = () => {
2
+ console.log('Generate back-end structure...');
3
+ };
@@ -0,0 +1,3 @@
1
+ module.exports = () => {
2
+ console.log('Generate front-end structure...');
3
+ };
package/bin/index.js ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+ // console.log('Welcome to the binh!');
3
+ // console.log('process.argv', process.argv);
4
+
5
+ const argves = process.argv;
6
+ const command = argves[2];
7
+
8
+ // switch (command) {
9
+ // case 'backend':
10
+ // console.log('Generate back-end structure...');
11
+ // break;
12
+
13
+ // case 'frontend':
14
+ // console.log('Generate front-end structure...');
15
+ // break;
16
+
17
+ // default:
18
+ // console.log('Unknown command...');
19
+ // break;
20
+ // }
21
+
22
+ const map = {
23
+ backend: require('./backend'),
24
+ frontend: require('./frontend'),
25
+ }
26
+
27
+ const handler = map[command];
28
+
29
+ if (handler) handler();
30
+ else console.log('Unknown command...');
31
+
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
7
7
  "license": "UNLICENSED",
8
+ "bin": {
9
+ "binh": "./bin/index.js"
10
+ },
8
11
  "scripts": {
9
12
  "test": "echo \"Error: no test specified\" && exit 1",
10
13
  "example-api": "node example_api PORT=5555",
package/src/binh.js CHANGED
@@ -1,3 +1,4 @@
1
+ const fs = require('fs');
1
2
  const path = require('path');
2
3
  const Crypto = require('./cryptography');
3
4
 
@@ -30,6 +31,15 @@ function Binh() {
30
31
  return rootpath;
31
32
  };
32
33
 
34
+ this.remove = function(path, callbackError) {
35
+ try {
36
+ fs.rmSync(path, { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 });
37
+ }
38
+ catch (error) {
39
+ callbackError(error);
40
+ }
41
+ };
42
+
33
43
  function binh(modulePath) {
34
44
  return modulePath == undefined ? module.exports : require(path.join(rootpath, modulePath));
35
45
  };
@@ -101,39 +101,75 @@ function WebBuilder(binh, Binh) {
101
101
  this.alias = name;
102
102
  }
103
103
 
104
- Binh.web = function(buildpath, sourcepath) {
105
- if (typeof buildpath !== 'string') return this;
106
-
107
- var rootpath = Binh.getRootpath(),
108
- destination = path.join(rootpath, buildpath);
104
+ Binh.web = function(webPath, sourcePath, modulePath) {
105
+ switch (arguments.length) {
106
+ case 1:
107
+ Binh.webStatic(webPath);
108
+ break;
109
+
110
+ case 2:
111
+ Binh.webApp(sourcePath, webPath);
112
+ break;
109
113
 
110
- if (typeof sourcepath === 'string') {
111
- var source = path.join(rootpath, sourcepath);
112
- Component.generate(source, destination);
114
+ default:
115
+ if (arguments.length > 2) {
116
+ Binh.webApp(webPath, sourcePath, modulePath);
117
+ }
118
+ break;
113
119
  }
114
120
 
115
- Binh.web.value = destination;
121
+ return Binh;
122
+ };
116
123
 
124
+ Binh.webStatic = function(webPath) {
125
+ if (!isString(webPath)) return Binh;
126
+ Binh.web.value = getAbsolutePaths(webPath).web;
117
127
  return Binh;
118
128
  };
119
129
 
120
- Binh.web2 = function(buildpath, sourcepath) {
121
- if (typeof buildpath !== 'string') return this;
130
+ Binh.webApp = function(sourcePath, webPath, modulePath) {
131
+ if (!isString(sourcePath)) return Binh;
122
132
 
123
- var rootpath = Binh.getRootpath(),
124
- destination = path.join(rootpath, buildpath);
133
+ modulePath = isString(modulePath) ? modulePath : isString(webPath) ? webPath : null;
134
+
135
+ var { source, web, module } = getAbsolutePaths(webPath, sourcePath, modulePath);
136
+
137
+ ComponentFormat.generate(source, module, () => ComponentBuild.generate(source, web, module));
138
+
139
+ Binh.web.value = web;
140
+
141
+ return Binh;
142
+ };
143
+
144
+ Binh.webModule = function(sourcePath, modulePath) {
145
+ if (!isString(sourcePath)) return Binh;
125
146
 
126
- if (typeof sourcepath === 'string') {
127
- var source = path.join(rootpath, sourcepath);
128
- ComponentFormat.generate(source, destination, () => {
129
- ComponentBuild.generate(source, destination);
130
- });
131
- }
147
+ var { source, module } = getAbsolutePaths(null, sourcePath, modulePath);
132
148
 
133
- Binh.web.value = destination;
149
+ ComponentFormat.generate(source, module);
134
150
 
135
151
  return Binh;
136
152
  };
153
+
154
+ function getAbsolutePaths(webPath, sourcePath, modulePath) {
155
+ webPath = isString(webPath) ? webPath : 'web/app';
156
+ modulePath = isString(modulePath) ? modulePath : 'web/modules';
157
+
158
+ var rootpath = Binh.getRootpath(),
159
+ sourcePathAbsolute = isString(sourcePath) ? path.join(rootpath, sourcePath) : null,
160
+ webPathAbsolute = path.join(rootpath, webPath),
161
+ modulePathAbsolute = path.join(rootpath, modulePath);
162
+
163
+ return {
164
+ web: webPathAbsolute,
165
+ source: sourcePathAbsolute,
166
+ module: modulePathAbsolute
167
+ };
168
+ }
169
+
170
+ function isString(input) {
171
+ return typeof input === 'string';
172
+ }
137
173
  }
138
174
 
139
175
  module.exports = {
@@ -1,43 +1,40 @@
1
1
 
2
- const { writeFile } = require('fs');
3
2
  const { parse } = require('path');
4
- const { scanNestedFiles, cloneFile } = require('./component.file');
3
+ const { scanNestedFiles, cloneFile, writeToFile } = require('./component.file');
5
4
  const CodeFormat = require('./code');
6
5
  const Component = require('./component');
7
6
 
8
7
  // TODO
9
8
  // [-] Able to minify/beautify generated files
10
9
 
11
- function generate(source, destination) {
12
- if (source == undefined || destination == undefined) return;
13
- console.info('[BINHEND][COMPONENT] Build web components from:', destination);
14
- console.info('[BINHEND][COMPONENT] to:', destination);
10
+ function generate(sourceRootPath, outputRootPath, stageRootPath) {
11
+ if (sourceRootPath == undefined || outputRootPath == undefined) return;
12
+ stageRootPath = stageRootPath || outputRootPath;
13
+ console.info('[BINHEND][COMPONENT] Build web components from:', stageRootPath);
14
+ console.info('[BINHEND][COMPONENT] to:', outputRootPath);
15
15
 
16
- scanNestedFiles(destination, (file) => {
17
- var sourcepath = file.path.replace(destination, source);
18
- var outpath = file.path;
16
+ scanNestedFiles(stageRootPath, (file) => {
17
+ var fileSourcePath = file.path.replace(stageRootPath, sourceRootPath);
18
+ var fileOutputPath = file.path.replace(stageRootPath, outputRootPath);
19
+
20
+ if (parse(file.name).ext !== '.js') {
21
+ return cloneFile(fileSourcePath, fileOutputPath);
22
+ }
19
23
 
20
- if (parse(file.name).ext !== '.js') return;
21
-
22
24
  try {
23
25
  var component = require(file.path);
24
26
  }
25
27
  catch (error) {
26
- return cloneFile(sourcepath, outpath);
28
+ return cloneFile(fileSourcePath, fileOutputPath);
27
29
  }
28
30
 
29
31
  if (!(component instanceof Function) || component.constructor !== Component) {
30
- return cloneFile(sourcepath, outpath);
32
+ return cloneFile(fileSourcePath, fileOutputPath);
31
33
  }
32
34
 
33
- var code = joinCodes(component, destination);
35
+ var code = joinCodes(component, outputRootPath);
34
36
 
35
- writeFile(outpath, code, { encoding: 'utf8', flag: 'w' }, function(error) {
36
- if (error) {
37
- showError('Failed writing file', filepath, error);
38
- return;
39
- }
40
- });
37
+ writeToFile(fileOutputPath, code);
41
38
  });
42
39
  }
43
40
 
@@ -50,11 +47,6 @@ function joinCodes(component, rootPath) {
50
47
  return fullcode;
51
48
  }
52
49
 
53
- function showError(message, id, error) {
54
- console.log(`[BINHEND][COMPONENT] ${message}:`, id);
55
- console.log('[BINHEND][COMPONENT] Error details:', error);
56
- }
57
-
58
50
  module.exports = {
59
51
  generate
60
52
  };
@@ -1,5 +1,5 @@
1
1
 
2
- const { readdir, statSync, copyFile } = require('fs');
2
+ const { readdir, statSync, copyFile, writeFile } = require('fs');
3
3
  const { join } = require('path');
4
4
 
5
5
  // TODO
@@ -91,10 +91,25 @@ function cloneFile(filepath, outpath) {
91
91
  });
92
92
  }
93
93
 
94
+ function writeToFile(fileOutputPath, content) {
95
+ writeFile(fileOutputPath, content, { encoding: 'utf8', flag: 'w' }, function(error) {
96
+ if (error) {
97
+ printError('Failed writing file', filepath, error);
98
+ }
99
+ });
100
+ }
101
+
102
+ function printError(message, id, error) {
103
+ console.error(`[BINHEND][COMPONENT] ${message}:`, id);
104
+ console.error('[BINHEND][COMPONENT] Error details:', error);
105
+ }
106
+
94
107
  module.exports = {
95
108
  scanItems,
96
109
  scanNestedItems,
97
110
  scanFiles,
98
111
  scanNestedFiles,
99
- cloneFile
112
+ cloneFile,
113
+ writeToFile,
114
+ printError
100
115
  };
@@ -1,7 +1,7 @@
1
1
 
2
2
  const { writeFileSync, existsSync, mkdirSync, readFileSync } = require('fs');
3
3
  const { dirname, parse } = require('path');
4
- const { scanNestedFiles, cloneFile } = require('./component.file');
4
+ const { scanNestedFiles, cloneFile, printError } = require('./component.file');
5
5
 
6
6
  // TODO
7
7
  // [-] Enhance code by removing sync logics (except for existsSync, mkdirSync): readdir, readFileSync, (statSync?)
@@ -11,43 +11,39 @@ const PREFIX_CODE =
11
11
  binh.component(context, ui, service, style);
12
12
  var ui = null, service = null, style = null;\r\n`;
13
13
 
14
- function generate(source, destination, callbackDone) {
15
- if (source == undefined || destination == undefined) return;
16
- console.log('[BINHEND][COMPONENT] Formatting files from:', source);
17
- console.log('[BINHEND][COMPONENT] to:', destination);
14
+ function generate(sourceRootPath, outputRootPath, callbackDone) {
15
+ if (sourceRootPath == undefined || outputRootPath == undefined) return;
16
+ console.log('[BINHEND][COMPONENT] Format files from:', sourceRootPath);
17
+ console.log('[BINHEND][COMPONENT] to:', outputRootPath);
18
18
 
19
- scanNestedFiles(source, (file, done) => {
19
+ scanNestedFiles(sourceRootPath, (file, done) => {
20
20
  if (done) return callbackDone instanceof Function ? callbackDone() : null;
21
21
 
22
- var outpath = file.path.replace(source, destination);
22
+ var fileSourcePath = file.path;
23
+ var fileOutputPath = fileSourcePath.replace(sourceRootPath, outputRootPath);
23
24
 
24
- ensureDirectoryExistence(outpath);
25
+ ensureDirectoryExistence(fileOutputPath);
25
26
 
26
27
  if (parse(file.name).ext !== '.js') {
27
- return cloneFile(file.path, outpath);
28
+ return cloneFile(fileSourcePath, fileOutputPath);
28
29
  }
29
30
 
30
31
  try {
31
- var content = readFileSync(file.path, { encoding: 'utf8', flag: 'r' });
32
- var output = PREFIX_CODE + content + '\r\n;binh.final(module);';
33
- writeFileSync(outpath, output, { encoding: 'utf8', flag: 'w' });
32
+ var content = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
33
+ var code = PREFIX_CODE + content + '\r\n;binh.final(module);';
34
+ writeFileSync(fileOutputPath, code, { encoding: 'utf8', flag: 'w' });
34
35
  }
35
36
  catch (error) {
36
- showError('Failed building file:', file.path, error);
37
+ printError('Failed formatting file:', fileSourcePath, error);
37
38
  }
38
39
  });
39
40
  }
40
41
 
41
- function ensureDirectoryExistence(filepath) {
42
- var dirpath = dirname(filepath);
43
- if (existsSync(dirpath)) return true;
44
- ensureDirectoryExistence(dirpath);
45
- mkdirSync(dirpath);
46
- }
47
-
48
- function showError(message, id, error) {
49
- console.log(`[BINHEND][COMPONENT] ${message}:`, id);
50
- console.log('[BINHEND][COMPONENT] Error details:', error);
42
+ function ensureDirectoryExistence(filePath) {
43
+ var dirPath = dirname(filePath);
44
+ if (existsSync(dirPath)) return true;
45
+ ensureDirectoryExistence(dirPath);
46
+ mkdirSync(dirPath);
51
47
  }
52
48
 
53
49
  module.exports = {