UNPKG

@modea/modea-increment-version

Version:

Increments version and build numbers in specified files

146 lines (131 loc) 4.84 kB
const fm = require('./increment/fileManipulation'); const inc = require('./increment/increment'); const gitManip = require('./increment/git/gitManipulation'); const { cosmiconfig } = require('cosmiconfig'); const explorer = cosmiconfig('@modea/modea-increment-version'); const util = require('util'); const exec = util.promisify(require('child_process').exec); async function run(options) { const configurations = await getConfig(options.configFilePath); if (options.logging) { console.log("Logging turned on") console.log(options) } if (configurations && configurations.files) { const changesMade = []; for (const file of configurations.files) { const change = { fileChanged: file.colloquialName, changes: increment(file, options.incrementType), }; changesMade.push(change); } const ticketPrefix = configurations.ticketPrefix ?? undefined; const changeDescription = await createDescription(changesMade, ticketPrefix, options.logging); print(changeDescription.replace(/\n\n/g, '\n')); if (configurations.git && options.pushToGit) { configurations.git.branchName = interpretBranchName(configurations.git.branchName, changesMade); configurations.git['description'] = changeDescription; configurations.git['simplePush'] = options.simplePush; configurations.git['currentBranch'] = options.currentBranch; gitManip.pushChangesToGit(configurations.git, options.incrementType); } } else { console.error( '\x1b[31m', 'No configuration file found, please supply a .incrementrc file in the root of your project' ); } } async function getConfig(filePath) { const config = await explorer .load(filePath) .then((res) => { if (!JSON.stringify(res.config).includes('\\\\n')) { return res.config; } else { return console.error( '\x1b[31m', 'Single quotes in .incrementrc file not supported. Please use proper JSON formatting.' ); } }) .catch((err) => console.error(err)); return config; } function increment(file, incrementType) { const placesToChange = fm.findPlacesToChange(file); const changesToMake = inc.increment(placesToChange.changes, incrementType); fm.writeChanges(file.path, placesToChange.fileString, changesToMake); return changesToMake; } function print(changesMade) { console.log(changesMade); } async function createDescription(changesMade, ticketPrefix, logging) { let description = 'Incremented values: '; for (const fileChange of changesMade) { description += `\n\n- ${fileChange.fileChanged}: `; for (const change of fileChange.changes) { description += `\n\n\t- ${change.type}: ${change.value}`; } } if (ticketPrefix) { const ticketIds = await getTicketIds(ticketPrefix, await getSyncCommitHash(logging), logging); if (ticketIds.length > 0) { description += '\n\n'; description += ticketIds; } } return description; } // returns a string with ticketId's committed since passed hash, seperated by new lines async function getTicketIds(ticketPrefix, syncCommitHash, logging) { const command = `git log ${syncCommitHash.trim()}..HEAD --pretty=%B | grep -oE "(${ticketPrefix}-[0-9]+)"`; try { const { stdout, _ } = await exec(command); if (logging) { console.log("Ticket ids found since last sync:\n", stdout) } return stdout; } catch (err) { if (logging) { console.log("No ticket ids", err) } // this usually means there aren't any ticket id's return ''; } } // the the commit hash of the last time we synced dev with master async function getSyncCommitHash(logging) { const command = `git log --oneline | grep "Syncing branch development with master after increment changes" | head -c 8`; try { const { stdout, _ } = await exec(command); if (logging) { console.log("Last sync commit hash:", stdout) } return stdout; } catch (err) { // this is very not good, means we've probably changed this increment script to commit something else to master console.log( 'The script was not able to find the last commit made by the increment script, did you change the commit message?', err ); return 0; } } // replaces keywords with values, ex. `release/v{{ios.version}}` would become `release/v1.9.4` // if the increment script changed the version in a file with colloquial name ios to 1.9.4 function interpretBranchName(rawName, changesMade) { let name = rawName; for (const fileChange of changesMade) { for (const change of fileChange.changes) { const stringToReplace = `{{${fileChange.fileChanged}.${change.type}}}`; name = name.replace(stringToReplace, change.value); } } return name; } module.exports = { run, };