|
| 1 | +const path = require('path') |
| 2 | +const fs = require('fs') |
| 3 | +const _ = require('lodash') |
| 4 | +const execSync = require('child_process').execSync |
| 5 | +const prompt = require('prompt-sync')() |
| 6 | +const csvParse = require('csv-parse/lib/sync') |
| 7 | +const yaml = require('js-yaml') |
| 8 | + |
| 9 | +const colors = require('./colors') |
| 10 | + |
| 11 | +// |
| 12 | +// Functions |
| 13 | +// |
| 14 | +const getMaxLength = (types, headName) => { |
| 15 | + return _(types).map(headName).map((n) => { return n.length }).max() |
| 16 | +} |
| 17 | + |
| 18 | +const createChoices = (types) => { |
| 19 | + const maxLengths = {} |
| 20 | + _(types[0]).keys().forEach((key) => { |
| 21 | + maxLengths[key] = getMaxLength(types, key) |
| 22 | + }) |
| 23 | + |
| 24 | + return _.map(types, (type) => { |
| 25 | + return _.map(type, (value, key) => { |
| 26 | + return _.padEnd(type[key], maxLengths[key]) |
| 27 | + }).join(" ") |
| 28 | + }).join("\n") |
| 29 | +} |
| 30 | + |
| 31 | +const selectTypes = (types) => { |
| 32 | + const header = "choice types by <TAB>" |
| 33 | + const choices = createChoices(types) |
| 34 | + const command = `echo "${choices.replace(/"/g, '\\"')}" | fzf -m --reverse --header="${header}"` |
| 35 | + const selectedValues = execSync(command, { stdio: ['inherit', 'pipe', 'inherit'], shell: true }).toString().trim().split("\n") |
| 36 | + |
| 37 | + const selectedNames = selectedValues.map((v) => { return _.words(v)[0] }) |
| 38 | + |
| 39 | + return _.filter(types, (type) => { |
| 40 | + return selectedNames.includes(type.name) |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +const promptBool = (text, defaultValue = true) => { |
| 45 | + const defaultStr = defaultValue ? '(Y/n)' : '(y/N)' |
| 46 | + const input = prompt(`${text} ${defaultStr}: `) |
| 47 | + |
| 48 | + if (defaultValue) { |
| 49 | + if ((/^n(o)?/i).test(input)) { |
| 50 | + return false |
| 51 | + } else { |
| 52 | + return true |
| 53 | + } |
| 54 | + } else { |
| 55 | + if ((/^y(es)?/i).test(input)) { |
| 56 | + return true |
| 57 | + } else { |
| 58 | + return false |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +const generateTemplateFile = (templateFileName, templateDefines, dryRun = false) => { |
| 64 | + let typeName = '' |
| 65 | + if(templateDefines.type) { |
| 66 | + if(templateDefines.type.useEmoji) { |
| 67 | + typeName = '<emoji> ' |
| 68 | + } else { |
| 69 | + typeName = '<type>: ' |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const file = `${typeName}<subject> |
| 74 | +
|
| 75 | +<body>` |
| 76 | + |
| 77 | + writeFile(path.join('.', templateFileName), file, dryRun) |
| 78 | +} |
| 79 | + |
| 80 | +const generateDefinitionsFile = (definitionsFileName, templateDefines, dryRun = false) => { |
| 81 | + const content = {} |
| 82 | + |
| 83 | + if(templateDefines.type) { |
| 84 | + const name = templateDefines.type.useEmoji ? 'emoji' : 'type' |
| 85 | + |
| 86 | + let values = [] |
| 87 | + if(templateDefines.type.useEmoji) { |
| 88 | + values = _.map(templateDefines.type.types, (type) => { |
| 89 | + return { |
| 90 | + name: type.emojiSign, |
| 91 | + description: `${type.meaning}: ${type.description}`, |
| 92 | + } |
| 93 | + }) |
| 94 | + } else { |
| 95 | + values = _.map(templateDefines.type.types, (type) => { |
| 96 | + return { |
| 97 | + name: type.name, |
| 98 | + description: `${type.meaning}: ${type.description}`, |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + content[name] = { |
| 104 | + type: 'enum', |
| 105 | + required: true, |
| 106 | + description: 'commit type', |
| 107 | + values: values, |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + content.subject = { |
| 112 | + type: 'string', |
| 113 | + required: true, |
| 114 | + description: 'The subject contains succinct description of the change', |
| 115 | + rules: { |
| 116 | + firstLatter: templateDefines.subject.lowerCaseStart ? 'lower' : 'upper', |
| 117 | + dotAtEnd: templateDefines.subject.dotAtEnd, |
| 118 | + ascii: false, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + content.body = { |
| 123 | + type: 'text', |
| 124 | + default: '', |
| 125 | + required: false, |
| 126 | + description: 'The body contains details of the change', |
| 127 | + rules: { |
| 128 | + firstLatter: 'upper', |
| 129 | + dotAtEnd: true, |
| 130 | + ascii: false, |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + writeFile(path.join('.', definitionsFileName), yaml.safeDump(content), dryRun) |
| 135 | +} |
| 136 | + |
| 137 | +const writeFile = (path, text, dryRun = false) => { |
| 138 | + if (fs.existsSync(path)) { |
| 139 | + if(promptBool(`${colors.warning}'${path}' is already exists. over write it?${colors.reset}`, false)) { |
| 140 | + if (dryRun) { |
| 141 | + console.log("-------------------------") |
| 142 | + console.log(text) |
| 143 | + console.log("-------------------------") |
| 144 | + } else { |
| 145 | + fs.writeFileSync(path, text) |
| 146 | + } |
| 147 | + } else { |
| 148 | + console.log(`Canceled generate '${path}'.`) |
| 149 | + } |
| 150 | + } else { |
| 151 | + if (dryRun) { |
| 152 | + console.log("-------------------------") |
| 153 | + console.log(text) |
| 154 | + console.log("-------------------------") |
| 155 | + } else { |
| 156 | + fs.writeFileSync(path, text) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// |
| 162 | +// Main |
| 163 | +// |
| 164 | +module.exports = (program, templateFileName, definitionsFileName, dryRun = false) => { |
| 165 | + const templateDefines = {} |
| 166 | + |
| 167 | + if(promptBool(`${colors.message}Use Type?${colors.reset}`, true)) { |
| 168 | + const typesCsv = fs.readFileSync(path.join('lib', 'types.csv')) |
| 169 | + const types = csvParse(typesCsv, {columns: true}) |
| 170 | + |
| 171 | + templateDefines.type = { |
| 172 | + types: selectTypes(types), |
| 173 | + useEmoji: promptBool(`${colors.message}Use Emoji?${colors.reset}`, false), |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + templateDefines.subject = { |
| 178 | + lowerCaseStart: promptBool(`${colors.message}Does the subject start with lower case?${colors.reset}`, true), |
| 179 | + dotAtEnd: promptBool(`${colors.message}Does the subject put dot (.) at end?${colors.reset}`, false), |
| 180 | + } |
| 181 | + |
| 182 | + generateTemplateFile(templateFileName, templateDefines, program.dryRun) |
| 183 | + generateDefinitionsFile(definitionsFileName, templateDefines, program.dryRun) |
| 184 | + |
| 185 | + console.log(`Generated '${templateFileName}' and '${definitionsFileName}'.`) |
| 186 | + console.log(`You can edit them freely.`) |
| 187 | + console.log(`Enjoy!`) |
| 188 | +} |
0 commit comments