Created
October 11, 2017 20:19
-
-
Save rhogeranacleto/2bd182fc6ac8dbd4597237b9012c1c79 to your computer and use it in GitHub Desktop.
NFLT-1311 [RELATÓRIOS] Colunas Usuário em relatórios aparecer o nome dos usuários (ou o login) NFLT-2344 Alterar models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const dir = './app/src/models'; | |
const MODEL_NAME = /.+define\('(\w+)'.+/m; | |
const ASSOCIATE = /associate/g; | |
function readdir() { | |
return new Promise(function (resolve, reject) { | |
fs.readdir(dir, function (err, fileNames) { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(fileNames); | |
}); | |
}); | |
} | |
function readFile(fileName) { | |
return new Promise(function (resolve, reject) { | |
fs.readFile(dir + '/' + fileName, 'utf-8', function (err, content) { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(content); | |
}); | |
}); | |
} | |
function writeFile(fileName, lines) { | |
return new Promise(function (resolve, reject) { | |
fs.writeFile(dir + '/' + fileName, lines.join('\n'), function (e) { | |
if (e) { | |
return reject(e); | |
} | |
return resolve(); | |
}); | |
}); | |
} | |
function processProp(model, content, prop, recues, n) { | |
let lines = content.split(`\n`); | |
if (content.indexOf(prop + '_by_id') >= 0) { | |
for (var i = n; i < lines.length; i++) { | |
let line = lines[i]; | |
if (recues && new RegExp(`^${'\t'.repeat(recues)}},?$`, 'g', 'm').test(line)) { | |
lines[i] = `${'\t'.repeat(recues)}}`; | |
console.log(model, i + 1); | |
break; | |
} | |
} | |
if (i !== n) { | |
let insideRecues = '\t'.repeat(recues + 1); | |
lines.splice(i, 0, `${insideRecues}models.${model}.belongsTo(models.ClientUser, { | |
${insideRecues} foreignKey: '${prop}_by_id', | |
${insideRecues} as: 'user_${prop}' | |
${insideRecues}});`); | |
} | |
} | |
return lines.join('\n'); | |
} | |
function processFile(fileName) { | |
return readFile(fileName).then(content => { | |
let lines = content.split(`\n`); | |
let model; | |
let recues; | |
for (var i = 0; i < lines.length; i++) { | |
let line = lines[i]; | |
if (MODEL_NAME.test(line) && !model) { | |
model = MODEL_NAME.exec(line)[1]; | |
} | |
if (ASSOCIATE.test(line)) { | |
recues = line.match(/\t/g); | |
recues = recues.length; | |
break; | |
} | |
} | |
lines[i] = `${'\t'.repeat(recues)}associate(models) {`; | |
content = lines.join('\n'); | |
if (recues) { | |
var updatedContent = processProp(model, content, 'created', recues, i); | |
updatedContent = processProp(model, updatedContent || content, 'updated', recues, i); | |
return [model, updatedContent, recues]; | |
} | |
}).then(data => { | |
if (data) { | |
let lines = data[1].split('\n'); | |
let model = data[0]; | |
let recues = data[2] + 1; | |
for (let i = 0; i < lines.length; i++) { | |
let line = lines[i]; | |
if (new RegExp(`^${'\t'.repeat(recues)}models.${model}`, 'g', 'm').test(line)) { | |
if (!!lines[i - 1]) { | |
lines.splice(i, 0, null); | |
} | |
} | |
} | |
return lines; | |
} | |
}).then(lines => { | |
if (lines) { | |
return writeFile(fileName, lines); | |
} | |
}); | |
} | |
readdir().then(function (fileNames) { | |
return Promise.all(fileNames /*.filter((fileName, i) => i < 4)*/ .map(fileName => processFile(fileName))); | |
}).catch(err => { | |
console.error('o ou', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment