Skip to content

Instantly share code, notes, and snippets.

@sr2ds
Last active September 2, 2020 20:13
Show Gist options
  • Save sr2ds/7593d68be2d93ac241a4d0a98482ebeb to your computer and use it in GitHub Desktop.
Save sr2ds/7593d68be2d93ac241a4d0a98482ebeb to your computer and use it in GitHub Desktop.
NodeJs script to get all tags in json format from multiple local git repositories
const moment = require('moment')
// Define your local projects in here
const projects = [
{ name: 'painel', source: '~/apps/apis/api-carrinho', tags: [] },
{ name: 'pagamento', source: '~/apps/apis/api-pagamento', tags: [] }
]
// https://gist.github.com/sr2ds/20666cbae5621cf2ede8e7bffec8ae0d
const gitCommand = ` \
git tag -l \
--format='{ \n
"tag": "%(tag)", \n \
"subject": "%(subject)", \n \
"created": "%(creatordate)" \n \
},'\
`
const getGitTags = async (source) => {
const output = await execShellCommand(`cd ${source}; ${gitCommand}`)
return JSON.parse(`[ ${output.slice(0, -2)} ]`)
}
// https://medium.com/@ali.dev/how-to-use-promise-with-exec-in-node-js-a39c4d7bbf77
function execShellCommand(cmd) {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.warn(error);
}
resolve(stdout ? stdout : stderr);
});
});
}
const getProjectsWithTags = (async (minDate) => {
for (let index = 0; index < projects.length; index++) {
let projectTags = await getGitTags(projects[index]['source'])
projects[index]['tags'] = projectTags
if (minDate) {
projects[index]['tags'] = projectTags.filter(tag => {
const tagCreated = moment(new Date(tag.created))
const minDateFilter = moment(minDate)
return tagCreated.isSameOrAfter(minDateFilter)
})
}
}
console.table(projects)
return projects
})('2020-07-10'); // What day your sprint started?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment