@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.
- package/README.md +303 -49
- package/dist/chunk-XKYTW4HW.js +589 -0
- package/dist/chunk-XKYTW4HW.js.map +1 -0
- package/dist/cli.cjs +779 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +173 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +991 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +653 -0
- package/dist/index.d.ts +653 -0
- package/dist/index.js +373 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -6
- package/templates/express-route/controller.ejs +35 -0
- package/templates/express-route/routes.ejs +10 -0
- package/templates/express-route/scaffold.json +13 -0
- package/templates/express-route/service.ejs +16 -0
- package/templates/github-action/scaffold.json +12 -0
- package/templates/github-action/workflow.ejs +33 -0
- package/templates/react-component/component.ejs +25 -0
- package/templates/react-component/index.ejs +2 -0
- package/templates/react-component/scaffold.json +15 -0
- package/templates/react-component/styles.ejs +4 -0
- package/templates/react-component/test.ejs +14 -0
- package/templates/vue-component/component.ejs +19 -0
- package/templates/vue-component/scaffold.json +12 -0
- package/templates/vue-component/test.ejs +16 -0
- package/.prettierignore +0 -1
- package/.prettierrc +0 -13
- package/.travis.yml +0 -9
- package/.vscode/settings.json +0 -5
- package/example/ejs/conditional.ejs +0 -9
- package/example/ejs/hello.ejs +0 -8
- package/example/handlebars/conditional.handlebars +0 -9
- package/example/handlebars/hello.handlebars +0 -6
- package/example/index.js +0 -180
- package/example/pug/conditional.pug +0 -4
- package/example/pug/hello.pug +0 -3
- package/index.js +0 -15
- package/src/command_helper.js +0 -42
- package/src/ejs_helper.js +0 -30
- package/src/file_helper.js +0 -142
- package/src/handlebars_helper.js +0 -32
- package/src/param_helper.js +0 -25
- package/src/pug_helper.js +0 -28
- package/src/string_helper.js +0 -12
- package/src/template_builder.js +0 -38
- package/src/template_handlers.js +0 -7
- package/test/test.js +0 -11
package/README.md
CHANGED
|
@@ -1,64 +1,318 @@
|
|
|
1
|
-
SCG
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@tiveor/scg)
|
|
4
|
+
[](https://github.com/tiveor/scg/actions/workflows/ci.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](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
|
-
##
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
310
|
+
## Template Engine Documentation
|
|
56
311
|
|
|
57
|
-
|
|
58
|
-
https://
|
|
312
|
+
- [EJS](https://ejs.co/#docs)
|
|
313
|
+
- [Pug](https://pugjs.org/api/getting-started.html)
|
|
314
|
+
- [Handlebars](https://handlebarsjs.com/guide/)
|
|
59
315
|
|
|
60
|
-
|
|
61
|
-
https://pugjs.org/api/getting-started.html
|
|
316
|
+
## License
|
|
62
317
|
|
|
63
|
-
|
|
64
|
-
https://handlebarsjs.com/guide/
|
|
318
|
+
MIT - Alvaro Orellana - AlvaroTech.dev
|