@tiveor/scg 0.1.6 → 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 (52) hide show
  1. package/README.md +303 -49
  2. package/dist/chunk-XKYTW4HW.js +589 -0
  3. package/dist/chunk-XKYTW4HW.js.map +1 -0
  4. package/dist/cli.cjs +779 -0
  5. package/dist/cli.cjs.map +1 -0
  6. package/dist/cli.d.cts +1 -0
  7. package/dist/cli.d.ts +1 -0
  8. package/dist/cli.js +173 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/index.cjs +991 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +653 -0
  13. package/dist/index.d.ts +653 -0
  14. package/dist/index.js +373 -0
  15. package/dist/index.js.map +1 -0
  16. package/package.json +63 -6
  17. package/templates/express-route/controller.ejs +35 -0
  18. package/templates/express-route/routes.ejs +10 -0
  19. package/templates/express-route/scaffold.json +13 -0
  20. package/templates/express-route/service.ejs +16 -0
  21. package/templates/github-action/scaffold.json +12 -0
  22. package/templates/github-action/workflow.ejs +33 -0
  23. package/templates/react-component/component.ejs +25 -0
  24. package/templates/react-component/index.ejs +2 -0
  25. package/templates/react-component/scaffold.json +15 -0
  26. package/templates/react-component/styles.ejs +4 -0
  27. package/templates/react-component/test.ejs +14 -0
  28. package/templates/vue-component/component.ejs +19 -0
  29. package/templates/vue-component/scaffold.json +12 -0
  30. package/templates/vue-component/test.ejs +16 -0
  31. package/.prettierignore +0 -1
  32. package/.prettierrc +0 -13
  33. package/.travis.yml +0 -9
  34. package/.vscode/settings.json +0 -5
  35. package/example/ejs/conditional.ejs +0 -9
  36. package/example/ejs/hello.ejs +0 -8
  37. package/example/handlebars/conditional.handlebars +0 -9
  38. package/example/handlebars/hello.handlebars +0 -6
  39. package/example/index.js +0 -180
  40. package/example/pug/conditional.pug +0 -4
  41. package/example/pug/hello.pug +0 -3
  42. package/index.js +0 -15
  43. package/src/command_helper.js +0 -42
  44. package/src/ejs_helper.js +0 -30
  45. package/src/file_helper.js +0 -142
  46. package/src/handlebars_helper.js +0 -32
  47. package/src/param_helper.js +0 -25
  48. package/src/pug_helper.js +0 -28
  49. package/src/string_helper.js +0 -12
  50. package/src/template_builder.js +0 -38
  51. package/src/template_handlers.js +0 -7
  52. package/test/test.js +0 -11
package/README.md CHANGED
@@ -1,64 +1,318 @@
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
- =============================
1
+ # SCG - Simple Code Generator
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@tiveor/scg.svg)](https://www.npmjs.com/package/@tiveor/scg)
4
+ [![CI](https://github.com/tiveor/scg/actions/workflows/ci.yml/badge.svg)](https://github.com/tiveor/scg/actions/workflows/ci.yml)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D18-brightgreen.svg)](https://nodejs.org/)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-strict-blue.svg)](https://www.typescriptlang.org/)
8
+
9
+ A utility library for code generation and template processing in Node.js. Provides helpers for template rendering (EJS, Handlebars, Pug), string manipulation, file operations, command execution, CLI parameter parsing, a scaffold engine, a template pipeline, and a file watcher.
10
+
11
+ Written in TypeScript with full type definitions. Supports both ESM and CommonJS.
5
12
 
6
13
  ## Installation
14
+
7
15
  ```bash
8
16
  npm install @tiveor/scg
9
17
  ```
10
18
 
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
- ```
19
+ ## Quick Start
20
+
21
+ ```typescript
22
+ // ESM
23
+ import {
24
+ StringHelper,
25
+ FileHelper,
26
+ CommandHelper,
27
+ ParamHelper,
28
+ TemplateBuilder,
29
+ Pipeline,
30
+ Scaffold,
31
+ Watcher,
32
+ TEMPLATE_HANDLERS
33
+ } from '@tiveor/scg';
17
34
 
18
- ## TemplateBuilder usage
19
- ```javascript
35
+ // CommonJS
20
36
  const { TemplateBuilder, TEMPLATE_HANDLERS } = require('@tiveor/scg');
37
+ ```
38
+
39
+ ## API Reference
40
+
41
+ Full API documentation is available via TypeDoc:
42
+
43
+ ```bash
44
+ npm run docs # Generates HTML docs in ./docs
45
+ ```
46
+
47
+ ### TemplateBuilder
48
+
49
+ Unified interface to render templates with EJS, Handlebars, Pug, or any custom engine.
50
+
51
+ ```typescript
52
+ const builder = new TemplateBuilder(TEMPLATE_HANDLERS.EJS);
53
+
54
+ // Render from string
55
+ const html = await builder.render('Hello <%= name %>', { name: 'World' });
56
+
57
+ // Render from file
58
+ const page = await builder.renderFile('template.ejs', { title: 'Home' });
59
+ ```
60
+
61
+ Available handlers: `TEMPLATE_HANDLERS.EJS`, `TEMPLATE_HANDLERS.HANDLEBARS`, `TEMPLATE_HANDLERS.PUG`
62
+
63
+ #### Plugin System
64
+
65
+ Register custom template engines without modifying the library:
66
+
67
+ ```typescript
68
+ import nunjucks from 'nunjucks';
69
+
70
+ TemplateBuilder.registerEngine('nunjucks', {
71
+ render: async (source, data) => nunjucks.renderString(source, data),
72
+ renderFile: async (file, data) => nunjucks.render(file, data),
73
+ });
74
+
75
+ const builder = new TemplateBuilder('nunjucks');
76
+ const result = await builder.render('Hello {{ name }}', { name: 'World' });
77
+
78
+ // List all available engines
79
+ TemplateBuilder.getRegisteredEngines();
80
+ // => ['HANDLEBARS', 'EJS', 'PUG', 'nunjucks']
81
+ ```
82
+
83
+ ### Pipeline
84
+
85
+ Chain transformations on template output:
86
+
87
+ ```typescript
88
+ import { Pipeline } from '@tiveor/scg';
89
+
90
+ const result = await new Pipeline()
91
+ .fromTemplate('component.ejs', { name: 'Button' }, 'EJS')
92
+ .transform((content) => content.toUpperCase())
93
+ .transform(addLicenseHeader)
94
+ .writeTo('src/components/Button.tsx');
95
+
96
+ // Other input methods
97
+ new Pipeline().fromString('raw content');
98
+ new Pipeline().fromFile('input.txt');
99
+ new Pipeline().fromTemplateString('<%= name %>', { name: 'World' }, 'EJS');
100
+ ```
101
+
102
+ ### Scaffold
103
+
104
+ Generate directory structures from manifests:
105
+
106
+ ```typescript
107
+ import { Scaffold } from '@tiveor/scg';
108
+
109
+ const result = await Scaffold.from({
110
+ engine: 'EJS',
111
+ templateDir: './templates/react-component',
112
+ outputDir: './src/components/{{name}}',
113
+ variables: { name: 'UserProfile', style: 'module' },
114
+ structure: [
115
+ { template: 'component.ejs', output: '{{name}}.tsx' },
116
+ { template: 'styles.ejs', output: '{{name}}.module.css' },
117
+ { template: 'test.ejs', output: '{{name}}.test.tsx' },
118
+ { template: 'index.ejs', output: 'index.ts' },
119
+ ]
120
+ });
121
+
122
+ console.log(result.files); // List of created file paths
123
+
124
+ // Preview without writing files
125
+ const preview = await Scaffold.from({ ...options, dryRun: true });
126
+ ```
127
+
128
+ ### Watcher
129
+
130
+ Watch templates and regenerate on changes:
131
+
132
+ ```typescript
133
+ import { Watcher } from '@tiveor/scg';
134
+
135
+ const watcher = new Watcher({
136
+ templateDir: './templates',
137
+ outputDir: './generated',
138
+ engine: 'EJS',
139
+ variables: { project: 'MyApp' },
140
+ onRebuild: (file) => console.log(`Rebuilt: ${file}`),
141
+ onError: (err, file) => console.error(`Error in ${file}: ${err.message}`),
142
+ });
143
+
144
+ watcher.start();
145
+ // watcher.stop();
146
+ ```
147
+
148
+ ### StringHelper
149
+
150
+ Static methods for string manipulation.
151
+
152
+ ```typescript
153
+ // Replace all occurrences of a token (regex-safe)
154
+ StringHelper.replace('Hello {{name}}!', '{{name}}', 'World');
155
+ // => "Hello World!"
156
+
157
+ // Works safely with regex special characters
158
+ StringHelper.replace('Price: $10.00', '$10.00', '$20.00');
159
+ // => "Price: $20.00"
160
+
161
+ // Capitalize first character
162
+ StringHelper.capitalize('hello');
163
+ // => "Hello"
164
+
165
+ // Escape regex special characters
166
+ StringHelper.escapeRegex('$100.00 (test)');
167
+ // => "\\$100\\.00 \\(test\\)"
168
+ ```
169
+
170
+ ### FileHelper
171
+
172
+ Static methods for file system operations (sync and async).
173
+
174
+ ```typescript
175
+ // Sync
176
+ const content = FileHelper.readFileToString('config.txt');
177
+ const config = FileHelper.convertJsonFileToObject('config.json');
178
+ FileHelper.createFolder('output/components');
179
+ FileHelper.removeFolder('output/temp');
180
+ FileHelper.removeFile('output/old.txt');
181
+
182
+ // Async
183
+ const data = await FileHelper.readFileAsync('config.txt');
184
+ const json = await FileHelper.readJsonFileAsync('config.json');
185
+ await FileHelper.writeFileAsync('output/file.txt', 'content');
186
+ await FileHelper.createFolderAsync('output/components');
187
+ await FileHelper.removeFolderAsync('output/temp');
188
+ await FileHelper.removeFileAsync('output/old.txt');
189
+ const exists = await FileHelper.existsAsync('some/path');
190
+
191
+ // Generate file from template with variable replacement
192
+ await FileHelper.createFileFromFile({
193
+ template: 'templates/component.txt',
194
+ newFile: 'output/Button.tsx',
195
+ variables: [
196
+ { token: '{{name}}', value: 'Button' },
197
+ { token: '{{style}}', value: 'primary' }
198
+ ]
199
+ });
200
+ ```
201
+
202
+ ### CommandHelper
203
+
204
+ Execute shell commands as promises (with input sanitization).
205
+
206
+ ```typescript
207
+ // Run a command
208
+ const output = await CommandHelper.run('.', 'echo hello');
209
+
210
+ // Chain multiple commands
211
+ const result = await CommandHelper.run('.', 'git add .', 'git status');
212
+
213
+ // Run and clean output (strips newlines)
214
+ const version = await CommandHelper.runClean('.', 'node --version');
215
+ ```
216
+
217
+ ### ParamHelper
218
+
219
+ Parse CLI parameters from `process.argv`.
220
+
221
+ ```typescript
222
+ // Add custom parameters
223
+ ParamHelper.addCustomParam('--env=production');
224
+
225
+ // Get parameter by index
226
+ const script = ParamHelper.getCommandByIndex(1);
227
+
228
+ // Parse all --key=value parameters
229
+ // Example: node script.js --name=Alice --port=3000
230
+ const params = ParamHelper.getParams();
231
+ // => { name: "Alice", port: "3000" }
232
+ ```
233
+
234
+ ## CLI
235
+
236
+ SCG includes a command-line interface for scaffolding and template rendering.
237
+
238
+ ```bash
239
+ # Initialize a scaffold manifest
240
+ npx @tiveor/scg init
241
+
242
+ # Generate files from a scaffold manifest
243
+ npx @tiveor/scg generate --manifest=scaffold.json --vars=name=Button
244
+
245
+ # Preview without writing files
246
+ npx @tiveor/scg generate --manifest=scaffold.json --vars=name=Button --dry-run
247
+
248
+ # Render a single template
249
+ npx @tiveor/scg render template.ejs --data='{"name":"World"}'
250
+
251
+ # Render and save to file
252
+ npx @tiveor/scg render template.ejs --data='{"name":"World"}' --output=output.html
253
+ ```
254
+
255
+ ## Predefined Template Packs
256
+
257
+ SCG ships with ready-to-use template packs in the `templates/` directory:
258
+
259
+ | Pack | Files | Description |
260
+ |------|-------|-------------|
261
+ | **react-component** | component, test, styles, index | React TSX component with CSS modules |
262
+ | **vue-component** | component, test | Vue 3 SFC with `<script setup>` |
263
+ | **express-route** | routes, controller, service | Express REST route with MVC pattern |
264
+ | **github-action** | workflow | GitHub Actions CI workflow |
265
+
266
+ Each pack includes a `scaffold.json` manifest. Use them with the CLI:
267
+
268
+ ```bash
269
+ npx @tiveor/scg generate --manifest=templates/react-component/scaffold.json --vars=name=Button,style=module
270
+ ```
271
+
272
+ Or programmatically:
273
+
274
+ ```typescript
275
+ import { Scaffold } from '@tiveor/scg';
276
+ import manifest from './templates/react-component/scaffold.json';
277
+
278
+ await Scaffold.from({ ...manifest, variables: { name: 'Button', style: 'module' } });
279
+ ```
280
+
281
+ ## Running Examples
282
+
283
+ ```bash
284
+ npm run build
285
+ node example/index.js # Original template examples
286
+ node example/pipeline-demo.js # Pipeline API demo
287
+ node example/scaffold-demo.js # Scaffold engine demo
288
+ node example/plugin-demo.js # Plugin system demo
289
+ ```
290
+
291
+ ## Running Tests
292
+
293
+ ```bash
294
+ npm test
295
+ ```
296
+
297
+ ## Development
21
298
 
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
299
  ```bash
52
- node example/index.js
300
+ npm run build # Build CJS + ESM + .d.ts
301
+ npm run dev # Build in watch mode
302
+ npm test # Run tests
303
+ npm run test:watch # Run tests in watch mode
304
+ npm run lint # Lint source code
305
+ npm run typecheck # Type check with tsc
306
+ npm run format # Format with Prettier
307
+ npm run docs # Generate API docs with TypeDoc
53
308
  ```
54
309
 
55
- For more information about templates visit the official documentation for each one:
310
+ ## Template Engine Documentation
56
311
 
57
- * EJS <br/>
58
- https://ejs.co/#docs
312
+ - [EJS](https://ejs.co/#docs)
313
+ - [Pug](https://pugjs.org/api/getting-started.html)
314
+ - [Handlebars](https://handlebarsjs.com/guide/)
59
315
 
60
- * PUG <br/>
61
- https://pugjs.org/api/getting-started.html
316
+ ## License
62
317
 
63
- * Handlebars <br>
64
- https://handlebarsjs.com/guide/
318
+ MIT - Alvaro Orellana - AlvaroTech.dev