@tiveor/scg 0.1.2 → 0.1.6

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.
@@ -0,0 +1 @@
1
+ *.ejs
package/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: node_js
2
+ sudo: false
3
+ node_js:
4
+ - "10"
5
+ - "12"
6
+ - "14"
7
+ branches:
8
+ only:
9
+ - master
@@ -0,0 +1,5 @@
1
+ {
2
+ "emmet.includeLanguages": {
3
+ "ejs": "html"
4
+ }
5
+ }
package/README.md CHANGED
@@ -1,2 +1,64 @@
1
- # scg
2
- SCG a Random Library for Generators
1
+ SCG a Random Library for Generators<br/>
2
+ [![Build Status](https://travis-ci.org/tiveor/scg.svg?branch=master)](https://travis-ci.org/tiveor/scg)
3
+ [![npm version](https://badge.fury.io/js/%40tiveor%2Fscg.svg)](https://badge.fury.io/js/%40tiveor%2Fscg)
4
+ =============================
5
+
6
+ ## Installation
7
+ ```bash
8
+ npm install @tiveor/scg
9
+ ```
10
+
11
+ ## Basic usage
12
+ ```javascript
13
+ const { StringHelper } = require('@tiveor/scg');
14
+ const replaced = StringHelper.replace('This is a {{test}}', '{{test}}', 'joke');
15
+ // replaced = "This is a joke"
16
+ ```
17
+
18
+ ## TemplateBuilder usage
19
+ ```javascript
20
+ const { TemplateBuilder, TEMPLATE_HANDLERS } = require('@tiveor/scg');
21
+
22
+ const ejsBuilder = new TemplateBuilder(TEMPLATE_HANDLERS.EJS);
23
+ ejsBuilder
24
+ .render('This is a <%= test %>', {
25
+ test: 'joke'
26
+ })
27
+ .then((replaced) => {
28
+ // replaced = "This is a joke"
29
+ });
30
+
31
+ const pugBuilder = new TemplateBuilder(TEMPLATE_HANDLERS.PUG);
32
+ pugBuilder
33
+ .render('This is a #{test}', {
34
+ test: 'joke'
35
+ })
36
+ .then((replaced) => {
37
+ // replaced = "This is a joke"
38
+ });
39
+
40
+ const handlebarsBuilder = new TemplateBuilder(TEMPLATE_HANDLERS.HANDLEBARS);
41
+ handlebarsBuilder
42
+ .render('This is a {{test}}', {
43
+ test: 'joke'
44
+ })
45
+ .then((replaced) => {
46
+ // replaced = "This is a joke"
47
+ });
48
+ ```
49
+
50
+ ## Example
51
+ ```bash
52
+ node example/index.js
53
+ ```
54
+
55
+ For more information about templates visit the official documentation for each one:
56
+
57
+ * EJS <br/>
58
+ https://ejs.co/#docs
59
+
60
+ * PUG <br/>
61
+ https://pugjs.org/api/getting-started.html
62
+
63
+ * Handlebars <br>
64
+ https://handlebarsjs.com/guide/
@@ -0,0 +1,9 @@
1
+ <% if (isDark) { %>
2
+ <div style="background: #333; color:#F0F0F0">
3
+ <%= text %>
4
+ </div>
5
+ <% } else { %>
6
+ <div style="background: #F0F0F0; color: #333">
7
+ <%= text %>
8
+ </div>
9
+ <% } %>
@@ -0,0 +1,8 @@
1
+ <div class="container">
2
+ <h1>
3
+ <%= title %>
4
+ </h1>
5
+ <div class="body">
6
+ <%= body %>
7
+ </div>
8
+ </div>
@@ -0,0 +1,9 @@
1
+ {{#if isDark}}
2
+ <div style="background: #333; color:#F0F0F0">
3
+ {{text}}
4
+ </div>
5
+ {{else}}
6
+ <div style="background: #F0F0F0; color: #333">
7
+ {{text}}
8
+ </div>
9
+ {{/if}}
@@ -0,0 +1,6 @@
1
+ <div class="container">
2
+ <h1>{{title}}</h1>
3
+ <div class="body">
4
+ {{body}}
5
+ </div>
6
+ </div>
@@ -0,0 +1,180 @@
1
+ const { StringHelper } = require('../src/string_helper');
2
+ const { FileHelper } = require('../src/file_helper');
3
+ const { CommandHelper } = require('../src/command_helper');
4
+ const { ParamHelper } = require('../src/param_helper');
5
+ const { TemplateBuilder } = require('../src/template_builder');
6
+ const TEMPLATE_HANDLERS = require('../src/template_handlers');
7
+
8
+ const initTest = (title) => {
9
+ console.log(`=================${title}=================`);
10
+ };
11
+
12
+ const endTest = () => {
13
+ console.log('================================');
14
+ };
15
+
16
+ //Usage StringHelper
17
+ const testStringHelper = () => {
18
+ initTest('STRING HELPER');
19
+ const replaced = StringHelper.replace(
20
+ 'This is a {{test}}',
21
+ '{{test}}',
22
+ 'joke'
23
+ );
24
+ console.log(replaced);
25
+ endTest();
26
+ };
27
+
28
+ //Usage FileHelper
29
+ const testFileHelper = () => {
30
+ initTest('FILE HELPER');
31
+ const packageObject = FileHelper.convertJsonFileToObject('package.json');
32
+ console.log(JSON.stringify(packageObject));
33
+ endTest();
34
+ };
35
+
36
+ //Usage ParamHelper
37
+ const testParamHelper = () => {
38
+ initTest('Param HELPER');
39
+ ParamHelper.addCustomParam(`--config=123`);
40
+ ParamHelper.addCustomParam(`--test="this is a test"`);
41
+ const params = ParamHelper.getParams();
42
+ console.log('params', params);
43
+ const config = ParamHelper.getCommandByIndex(2);
44
+ const test = ParamHelper.getCommandByIndex(3);
45
+ console.log('config', config);
46
+ console.log('test', test);
47
+ endTest();
48
+ };
49
+
50
+ //Usage CommandHelper
51
+ const testCommandHelper = () => {
52
+ initTest('Command HELPER');
53
+ CommandHelper.runClean('.', 'pwd').then((res) => {
54
+ console.log(res);
55
+ endTest();
56
+ });
57
+ };
58
+
59
+ //Usage TemplateBuilder with EJS
60
+ const testTemplateEJS = async () => {
61
+ initTest('TemplateBuilder with EJS');
62
+ const builder = new TemplateBuilder(TEMPLATE_HANDLERS.EJS);
63
+ builder
64
+ .renderFile(
65
+ 'example/ejs/hello.ejs',
66
+ { title: 'Hello EJS world', body: 'this is the body' },
67
+ {}
68
+ )
69
+ .then((html) => {
70
+ console.log(html);
71
+ endTest();
72
+ });
73
+
74
+ builder
75
+ .renderFile(
76
+ 'example/ejs/conditional.ejs',
77
+ { isDark: true, text: 'Hello Dark World' },
78
+ {}
79
+ )
80
+ .then((html) => {
81
+ console.log(html);
82
+ endTest();
83
+ });
84
+
85
+ builder
86
+ .renderFile(
87
+ 'example/ejs/conditional.ejs',
88
+ { isDark: false, text: 'Hello Light World' },
89
+ {}
90
+ )
91
+ .then((html) => {
92
+ console.log(html);
93
+ endTest();
94
+ });
95
+ };
96
+
97
+ //Usage TemplateBuilder with handlebars
98
+ const testTemplateHandlebars = async () => {
99
+ initTest('TemplateBuilder with handlebars');
100
+ const builder = new TemplateBuilder(TEMPLATE_HANDLERS.HANDLEBARS);
101
+ builder
102
+ .renderFile(
103
+ 'example/handlebars/hello.handlebars',
104
+ { title: 'Hello handlebars world', body: 'this is the body' },
105
+ {}
106
+ )
107
+ .then((html) => {
108
+ console.log(html);
109
+ endTest();
110
+ });
111
+
112
+ builder
113
+ .renderFile(
114
+ 'example/handlebars/conditional.handlebars',
115
+ { isDark: true, text: 'Hello Dark World' },
116
+ {}
117
+ )
118
+ .then((html) => {
119
+ console.log(html);
120
+ endTest();
121
+ });
122
+
123
+ builder
124
+ .renderFile(
125
+ 'example/handlebars/conditional.handlebars',
126
+ { isDark: false, text: 'Hello Light World' },
127
+ {}
128
+ )
129
+ .then((html) => {
130
+ console.log(html);
131
+ endTest();
132
+ });
133
+ };
134
+
135
+ //Usage TemplateBuilder with pug
136
+ const testTemplatePug = async () => {
137
+ initTest('TemplateBuilder with pug');
138
+ const builder = new TemplateBuilder(TEMPLATE_HANDLERS.PUG);
139
+ builder
140
+ .renderFile(
141
+ 'example/pug/hello.pug',
142
+ { title: 'Hello pug world', body: 'this is the body' },
143
+ {}
144
+ )
145
+ .then((html) => {
146
+ console.log(html);
147
+ endTest();
148
+ });
149
+
150
+ builder
151
+ .renderFile(
152
+ 'example/pug/conditional.pug',
153
+ { isDark: true, text: 'Hello Dark World' },
154
+ {}
155
+ )
156
+ .then((html) => {
157
+ console.log(html);
158
+ endTest();
159
+ });
160
+
161
+ builder
162
+ .renderFile(
163
+ 'example/pug/conditional.pug',
164
+ { isDark: false, text: 'Hello Light World' },
165
+ {}
166
+ )
167
+ .then((html) => {
168
+ console.log(html);
169
+ endTest();
170
+ });
171
+ };
172
+
173
+ console.log('\nSCG Examples\n');
174
+ //testStringHelper();
175
+ //testFileHelper();
176
+ //testParamHelper();
177
+ //testCommandHelper();
178
+ //testTemplateEJS();
179
+ //testTemplatePug();
180
+ testTemplateHandlebars();
@@ -0,0 +1,4 @@
1
+ if isDark
2
+ div(style="background: #333; color:#F0F0F0") #{text}
3
+ else
4
+ div(style="background: #F0F0F0; color: #333") #{text}
@@ -0,0 +1,3 @@
1
+ div(class="container")
2
+ h1 #{title}
3
+ div(class="body") #{body}
package/index.js CHANGED
@@ -1,9 +1,15 @@
1
- const { StringHelper } = require("./src/string_helper");
2
- const { FileHelper } = require("./src/file_helper");
3
- const { CommandHelper } = require("./src/command_helper");
1
+ const { StringHelper } = require('./src/string_helper');
2
+ const { FileHelper } = require('./src/file_helper');
3
+ const { CommandHelper } = require('./src/command_helper');
4
+ const { ParamHelper } = require('./src/param_helper');
5
+ const { TemplateBuilder } = require('./src/template_builder');
6
+ const TEMPLATE_HANDLERS = require('./src/template_handlers');
4
7
 
5
8
  module.exports = {
6
9
  StringHelper,
7
10
  FileHelper,
8
- CommandHelper
9
- };
11
+ CommandHelper,
12
+ ParamHelper,
13
+ TemplateBuilder,
14
+ TEMPLATE_HANDLERS
15
+ };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@tiveor/scg",
3
- "version": "0.1.2",
3
+ "version": "0.1.6",
4
4
  "description": "SCG a Random Library for Generators",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "mocha ./test"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
@@ -17,6 +17,12 @@
17
17
  },
18
18
  "homepage": "https://github.com/tiveor/scg#readme",
19
19
  "devDependencies": {
20
+ "mocha": "^8.3.0",
20
21
  "prettier": "^2.2.1"
22
+ },
23
+ "dependencies": {
24
+ "ejs": "^3.1.6",
25
+ "handlebars": "^4.7.7",
26
+ "pug": "^3.0.2"
21
27
  }
22
28
  }
@@ -0,0 +1,30 @@
1
+ const ejs = require("ejs");
2
+
3
+ //https://ejs.co/
4
+ //https://github.com/mde/ejs
5
+ class EjsHelper {
6
+ static render(source, data, options) {
7
+ return new Promise((resolve, reject) => {
8
+ try {
9
+ const template = ejs.render(source, data, options);
10
+ resolve(template);
11
+ } catch (error) {
12
+ reject(error);
13
+ }
14
+ });
15
+ }
16
+
17
+ static renderFile(fileName, data, options) {
18
+ return new Promise((resolve, reject) => {
19
+ ejs.renderFile(fileName, data, options, function (err, str) {
20
+ if (err) {
21
+ reject(err)
22
+ return;
23
+ }
24
+
25
+ resolve(str);
26
+ });
27
+ });
28
+ }
29
+ }
30
+ exports.EjsHelper = EjsHelper;
@@ -1,12 +1,16 @@
1
1
  const fs = require("fs");
2
2
  const readline = require("readline");
3
- const { exec } = require("child_process");
4
3
  const { StringHelper } = require("./string_helper");
4
+ const { CommandHelper } = require("./command_helper");
5
5
 
6
6
  class FileHelper {
7
+ static readFileToString(fileName) {
8
+ return fs.readFileSync(fileName, 'utf8');
9
+ }
10
+
7
11
  static convertJsonFileToObject(fileName) {
8
- let rawdata = fs.readFileSync(fileName);
9
- return JSON.parse(rawdata);
12
+ const rawString = this.readFileToString(fileName);
13
+ return JSON.parse(rawString);
10
14
  }
11
15
 
12
16
  static simpleReplace(line, replacement) {
@@ -122,15 +126,16 @@ class FileHelper {
122
126
  fs.mkdirSync(`./${folderName}/`, { recursive: true });
123
127
  }
124
128
 
125
- static removeFolder(folderName, callback) {
126
- exec(`rm -Rf '${folderName}'`, function (err, stdout, stderr) {
127
- if (err || stderr) {
128
- console.log("err:", err);
129
- console.log("stderr:", stderr);
130
- } else {
131
- callback();
132
- }
133
- });
129
+ static removeFolder(folderName) {
130
+ return CommandHelper.runClean(
131
+ ".",
132
+ `rm -Rf '${folderName}'`);
133
+ }
134
+
135
+ static removeFile(filename) {
136
+ return CommandHelper.runClean(
137
+ ".",
138
+ `rm -f '${filename}'`);
134
139
  }
135
140
  }
136
141
 
@@ -0,0 +1,32 @@
1
+ const Handlebars = require("handlebars");
2
+ const { FileHelper } = require("./file_helper");
3
+
4
+ //https://handlebarsjs.com/
5
+ //https://github.com/handlebars-lang/handlebars.js
6
+ class HandlebarsHelper {
7
+ static render(source, data, options) {
8
+ return new Promise((resolve, reject) => {
9
+ try {
10
+ const template = Handlebars.compile(source, options);
11
+ const res = template(data);
12
+ resolve(res);
13
+ } catch (error) {
14
+ reject(error);
15
+ }
16
+ });
17
+ }
18
+
19
+ static renderFile(fileName, data, options) {
20
+ return new Promise((resolve, reject) => {
21
+ try {
22
+ const source = FileHelper.readFileToString(fileName);
23
+ const template = Handlebars.compile(source, options);
24
+ const res = template(data);
25
+ resolve(res);
26
+ } catch (error) {
27
+ reject(error);
28
+ }
29
+ });
30
+ }
31
+ }
32
+ exports.HandlebarsHelper = HandlebarsHelper;
@@ -0,0 +1,25 @@
1
+ class ParamHelper {
2
+ static addCustomParam(customParam) {
3
+ process.argv.push(customParam);
4
+ }
5
+
6
+ static getCommandByIndex(index) {
7
+ return process.argv.length > index ? process.argv[index] : "";
8
+ }
9
+
10
+ static getParams() {
11
+ const argv = (() => {
12
+ let paramsObj = {};
13
+ process.argv.slice(2).map((element) => {
14
+ const matches = element.match('--([a-zA-Z0-9]+)=(.*)');
15
+ if (matches) {
16
+ paramsObj[matches[1]] = matches[2]
17
+ .replace(/^['"]/, '').replace(/['"]$/, '');
18
+ }
19
+ });
20
+ return paramsObj;
21
+ })();
22
+ return argv;
23
+ }
24
+ }
25
+ exports.ParamHelper = ParamHelper;
@@ -0,0 +1,28 @@
1
+ const pug = require('pug');
2
+
3
+ //https://github.com/pugjs/pug
4
+ //https://pugjs.org/api/getting-started.html
5
+ class PugHelper {
6
+ static render(source, data, options) {
7
+ return new Promise((resolve, reject) => {
8
+ try {
9
+ const template = pug.compile(source, options);
10
+ resolve(template(data));
11
+ } catch (error) {
12
+ reject(error);
13
+ }
14
+ });
15
+ }
16
+
17
+ static renderFile(fileName, data, options) {
18
+ return new Promise((resolve, reject) => {
19
+ try {
20
+ const template = pug.compileFile(fileName, options);
21
+ resolve(template(data));
22
+ } catch (error) {
23
+ reject(error);
24
+ }
25
+ });
26
+ }
27
+ }
28
+ exports.PugHelper = PugHelper;
@@ -3,7 +3,7 @@ class StringHelper {
3
3
  return line.replace(new RegExp(token, "g"), value);
4
4
  }
5
5
 
6
- static capitalize = (s) => {
6
+ static capitalize(s) {
7
7
  if (typeof s !== "string") return "";
8
8
  return s.charAt(0).toUpperCase() + s.slice(1);
9
9
  }
@@ -0,0 +1,38 @@
1
+ const { HandlebarsHelper } = require('./handlebars_helper');
2
+ const { EjsHelper } = require('./ejs_helper');
3
+ const { PugHelper } = require('./pug_helper');
4
+ const TEMPLATE_HANDLERS = require('./template_handlers');
5
+
6
+ class TemplateBuilder {
7
+ constructor(templateHandler) {
8
+ this.templateHandler = templateHandler;
9
+ }
10
+
11
+ render(source, data, options) {
12
+ switch (this.templateHandler) {
13
+ case TEMPLATE_HANDLERS.HANDLEBARS:
14
+ return HandlebarsHelper.render(source, data, options);
15
+ case TEMPLATE_HANDLERS.EJS:
16
+ return EjsHelper.render(source, data, options);
17
+ case TEMPLATE_HANDLERS.PUG:
18
+ return PugHelper.render(fileName, data, options);
19
+ default:
20
+ return HandlebarsHelper.render(source, data, options);
21
+ }
22
+ }
23
+
24
+ renderFile(fileName, data, options) {
25
+ switch (this.templateHandler) {
26
+ case TEMPLATE_HANDLERS.HANDLEBARS:
27
+ return HandlebarsHelper.renderFile(fileName, data, options);
28
+ case TEMPLATE_HANDLERS.EJS:
29
+ return EjsHelper.renderFile(fileName, data, options);
30
+ case TEMPLATE_HANDLERS.PUG:
31
+ return PugHelper.renderFile(fileName, data, options);
32
+ default:
33
+ return HandlebarsHelper.renderFile(fileName, data, options);
34
+ }
35
+ }
36
+ }
37
+
38
+ exports.TemplateBuilder = TemplateBuilder;
@@ -0,0 +1,7 @@
1
+ const TEMPLATE_HANDLERS = {
2
+ HANDLEBARS: 'HANDLEBARS',
3
+ EJS: 'EJS',
4
+ PUG: 'PUG'
5
+ };
6
+
7
+ exports.TEMPLATE_HANDLERS = TEMPLATE_HANDLERS;
package/test/test.js ADDED
@@ -0,0 +1,11 @@
1
+ var assert = require('assert');
2
+ const { StringHelper } = require("../src/string_helper");
3
+
4
+ describe("StringHelper", () => {
5
+ describe("replace", () => {
6
+ it('should search and replace the test param', function () {
7
+ const replaced = StringHelper.replace("This is a {{test}}", "{{test}}", "joke");
8
+ assert.strictEqual(replaced, "This is a joke");
9
+ });
10
+ })
11
+ });
package/example.js DELETED
@@ -1,27 +0,0 @@
1
- const { StringHelper } = require("./src/string_helper");
2
- const { FileHelper } = require("./src/file_helper");
3
- const { CommandHelper } = require("./src/command_helper");
4
-
5
- console.log("\nSCG Examples\n");
6
-
7
- //Usage StringHelper
8
- console.log("================================");
9
- console.log("STRING HELPER");
10
- const replaced = StringHelper.replace("This is a {{test}}", "{{test}}", "joke");
11
- console.log(replaced);
12
- console.log("================================");
13
-
14
- //Usage FileHelper
15
- console.log("================================");
16
- console.log("FILE HELPER");
17
- const packageObject = FileHelper.convertJsonFileToObject("package.json");
18
- console.log(JSON.stringify(packageObject));
19
- console.log("================================");
20
-
21
- //Usage CommandHelper
22
- console.log("================================");
23
- console.log("Command HELPER");
24
- CommandHelper.runClean(".", "pwd").then((res) => {
25
- console.log(res);
26
- console.log("================================");
27
- });