Skip to content

Commit

Permalink
- "replace" and "path" pattern functions now on patterns folder
Browse files Browse the repository at this point in the history
- return 1 when there is an error generating a file
  • Loading branch information
jaumesegarra committed Oct 12, 2019
1 parent bfccbc3 commit 2e2660f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 41 deletions.
47 changes: 11 additions & 36 deletions generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ const fs = require('fs-extra');
const tmp = require('tmp');
const ejs = require('ejs');
const utils = require('./utils');
const relative = require('relative');

const patterns = require('./patterns');

const EJS_EXTENSION = '.ejs';
const WINDOWS = 'win32';
const DS_Store = '.DS_Store';

const getStuffName = (currentPath) => {
return path.basename(currentPath);
};

const onError = (message, err, reject) => {
console.error(message, err);
throw console.error(message, err);
reject(err);
}

Expand Down Expand Up @@ -51,8 +49,9 @@ const resourcePatronize = (resource, stuffName, delimiter, vars) => {
res = replacer(v, vars[v]);
};

for(let p in patterns){
res = replacer(p, patterns[p]);
for(let p in patterns.list){
if(patterns.notAvailableOnFilenames.indexOf(p) === -1)
res = replacer(p, patterns.list[p]);
};

return res;
Expand All @@ -63,40 +62,16 @@ const generateFileFromTemplate = (stuffName, resourcePath, destinyPath, delimite
if(err) onError('Error reading the file "'+file+'"', err, reject);

try {
global[utils.GLOBAL_CURRENT_EJS_FILE_CONTEXT] = {
absoluteStuffPath,
cacheStuffFolderPath,
destinyPath
};

const component = ejs.render(data, {
...vars,
"name": stuffName,
"replace": (exp, replacement, v) => {
return v.replace(new RegExp(exp), replacement);
},
...patterns,
"path": (v) => {
let relativePath = '';

try {
const filePath = path.join(utils.getPackageFolder(), v);

relativePath = relative(absoluteStuffPath, filePath, false);

let separatorExp = /\//g;
if(process.platform === WINDOWS){
relativePath = relativePath.replace(/\\/g, '/'); // Fix windows path
separatorExp = /\\/g;
}

/* Relative with stuff subfolders: */
let subfoldersNumber = 0;
const subfolders = destinyPath.replace(cacheStuffFolderPath, '').match(separatorExp);
if(subfolders) subfoldersNumber = subfolders.length-1;

for(let i = 0; i < subfoldersNumber; i++)
relativePath = '../'+relativePath;
}catch (err) {
console.info(`Error obtain the relative path for ${filePath}`, err);
}

return relativePath;
}
...patterns.list
}, {delimiter: delimiter});

const p = destinyPath.replace(new RegExp(EJS_EXTENSION+'$'), '');
Expand Down
21 changes: 18 additions & 3 deletions patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@ const lc = require('./lc');
const cz = require('./cz');
const ls = require('./ls');
const now = require('./now');
const replace = require('./replace');
const path = require('./path');

module.exports = {
username,
const lettercasePatterns = {
cc,
sc,
kc,
uc,
lc,
cz,
ls,
ls
};

const infoPatterns = {
username,
now
};

module.exports = {
notAvailableOnFilenames: ['path', 'replace'],
list: {
...lettercasePatterns,
...infoPatterns,
path,
replace
}
}
2 changes: 1 addition & 1 deletion patterns/now/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const moment = require('moment');

module.exports = (format) => {
let value = '';
let value = '';

try{
const momm = moment();
Expand Down
35 changes: 35 additions & 0 deletions patterns/path/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const path = require('path');
const relative = require('relative');

const utils = require('../../utils');
const WINDOWS = 'win32';

module.exports = (v) => {
const _this = utils.getCurrentEJSFileContext();

let relativePath = '';

try {
const filePath = path.join(utils.getPackageFolder(), v);

relativePath = relative(_this.absoluteStuffPath, filePath, false);

let separatorExp = /\//g;
if(process.platform === WINDOWS){
relativePath = relativePath.replace(/\\/g, '/'); // Fix windows path
separatorExp = /\\/g;
}

/* Relative with stuff subfolders: */
let subfoldersNumber = 0;
const subfolders = _this.destinyPath.replace(_this.cacheStuffFolderPath, '').match(separatorExp);
if(subfolders) subfoldersNumber = subfolders.length-1;

for(let i = 0; i < subfoldersNumber; i++)
relativePath = '../'+relativePath;
}catch (err) {
console.info(`Error obtain the relative path for ${filePath}`, err);
}

return relativePath;
}
3 changes: 3 additions & 0 deletions patterns/replace/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (exp, replacement, v) => {
return v.replace(new RegExp(exp), replacement);
}
7 changes: 6 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ const getXtuffPackageConfig = () => new Promise((resolve, reject) => {
} else resolve({});
});

const GLOBAL_CURRENT_EJS_FILE_CONTEXT = 'EJS_FILE_CONTEXT';
const getCurrentEJSFileContext = () => global[GLOBAL_CURRENT_EJS_FILE_CONTEXT];

module.exports = {
getPackageFolder,
getTemplatesFolder,
getXtuffPackageConfig,
TEMPLATE_FOLDER_NAME
TEMPLATE_FOLDER_NAME,
GLOBAL_CURRENT_EJS_FILE_CONTEXT,
getCurrentEJSFileContext
}

0 comments on commit 2e2660f

Please sign in to comment.