Skip to content

Commit 9c2b0fd

Browse files
author
Celso Wo
committed
Added support to function in output configuration
1 parent f375ca1 commit 9c2b0fd

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

lazy-generate.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const template = program.args[0];
2828
const templateArgs = program.args.slice(1);
2929

3030
let templatePath = `${cwd}/.lazy/templates/${template}`;
31-
let templateSettings = JSON.parse(fs.readFileSync(`${templatePath}/template-settings.json`));
31+
let templateSettings = require(`${templatePath}/template-settings.js`);
3232

3333
if (templateArgs.lentgh < templateSettings.inputs.length) {
3434
console.error(' ERROR - Missing template arguments');
@@ -50,12 +50,18 @@ for (let key in templateSettings.inputs) {
5050
let outputs = {};
5151
for (let file in templateSettings.outputs) {
5252
let output = templateSettings.outputs[file];
53-
54-
for (let input in inputs) {
55-
output = output.replace(`%{${input}}`, inputs[input]);
53+
let outputPath = '';
54+
55+
if (typeof output == 'function') {
56+
outputPath = output(inputs);
57+
} else {
58+
outputPath = output;
59+
for (let input in inputs) {
60+
outputPath = outputPath.replace(`%{${input}}`, inputs[input]);
61+
}
5662
}
5763

58-
outputs[file] = output;
64+
outputs[file] = outputPath;
5965
}
6066

6167
for (let file in outputs) {
@@ -80,6 +86,11 @@ for (let file in outputs) {
8086
Object.assign(templateParams, {path, outputs});
8187
Object.assign(templateParams, inputs);
8288

89+
if (fs.existsSync(`${cwd}/${output}`)) {
90+
console.log(` Destination file already exists, skiping...`);
91+
continue;
92+
}
93+
8394
consolidate[lazySettings.engine](`${templatePath}/${file}`, templateParams, (err, result) => {
8495
if (err) throw err;
8596

lazy-init.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ fs.closeSync(fd);
2626
console.log("\n Creating sample template");
2727
fs.mkdirSync(`${cwd}/.lazy/templates`);
2828
fs.mkdirSync(`${cwd}/.lazy/templates/sample`);
29-
fd = fs.openSync(`${cwd}/.lazy/templates/sample/template-settings.json`, 'w');
30-
fs.writeSync(fd, '{\n');
29+
fd = fs.openSync(`${cwd}/.lazy/templates/sample/template-settings.js`, 'w');
30+
fs.writeSync(fd, 'module.exports = {\n');
3131
fs.writeSync(fd, ' "inputs": ["entity"],\n');
3232
fs.writeSync(fd, ' "outputs": {\n');
3333
fs.writeSync(fd, ' "entity.ejs": "output/entity/%{entity}.java",\n');
3434
fs.writeSync(fd, ' "repository.ejs": "output/repository/%{entity}Repository.java"\n');
3535
fs.writeSync(fd, ' }\n');
36-
fs.writeSync(fd, '}\n');
36+
fs.writeSync(fd, '};\n');
3737
fs.closeSync(fd);
3838

3939
fd = fs.openSync(`${cwd}/.lazy/templates/sample/entity.ejs`, 'w');

lazy-list.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if (fs.existsSync(`${cwd}/.lazy`)) {
1212

1313
let files = fs.readdirSync(`${cwd}/.lazy/templates`);
1414
files.forEach(file => {
15-
let templateSettings = JSON.parse(fs.readFileSync(`${cwd}/.lazy/templates/${file}/template-settings.json`));
15+
let templateSettings = require(`${cwd}/.lazy/templates/${file}/template-settings.js`);
1616

1717
let inputs = '';
1818
for (let key in templateSettings.inputs) {

lazy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
const fs = require('fs');
77
const program = require('commander');
88
const cwd = process.cwd();
9-
const version = '0.0.1';
9+
const version = '0.0.3';
1010

1111
console.log(`\n [Lazy] Version ${version}`);
1212
console.log(`\n Checking lazy configuration...`);

0 commit comments

Comments
 (0)