|
| 1 | +const fs = require("fs"); |
| 2 | +const { question } = require("./helpers"); |
| 3 | +const shell = require("shelljs"); |
| 4 | +const semver = require("semver"); |
| 5 | + |
| 6 | +module.exports = async function deployLocal() { |
| 7 | + const done = this.async(); |
| 8 | + |
| 9 | + const { appleID, newVersionNumber, newBuildNumber } = await fetchEnv(); |
| 10 | + |
| 11 | + await checkoutBranch(newVersionNumber, newBuildNumber); |
| 12 | + |
| 13 | + await setBuildInfo(newVersionNumber, newBuildNumber); |
| 14 | + createReleaseCommit(newVersionNumber, newBuildNumber); |
| 15 | + |
| 16 | + await deploy(appleID, newVersionNumber, newBuildNumber); |
| 17 | + |
| 18 | + done(); |
| 19 | +}; |
| 20 | + |
| 21 | +async function setBuildInfo(newVersionNumber, newBuildNumber) { |
| 22 | + const { execa } = await import("execa"); |
| 23 | + const configFilePath = "./Projects/App/xcconfigs/Gabbangzip.shared.xcconfig"; |
| 24 | + const xcodeProjPath = "./Projects/App/App.xcodeproj" |
| 25 | + |
| 26 | + _setXCConfigValue("MARKETING_VERSION", newVersionNumber, configFilePath); |
| 27 | + |
| 28 | + await execa("bundle", ["exec", "fastlane", "run", "increment_build_number", `build_number:${newBuildNumber}`, `xcodeproj:${xcodeProjPath}`], { |
| 29 | + stdio: "inherit", |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function createReleaseCommit(newVersionNumber, newBuildNumber) { |
| 34 | + const configFilePath = "./Projects/App/xcconfigs/Gabbangzip.shared.xcconfig"; |
| 35 | + const commitMessage = `:bookmark: v${newVersionNumber}-${newBuildNumber}`; |
| 36 | + const infoPlistFilePath = "./Projects/App/Info.plist" |
| 37 | + |
| 38 | + if (shell.exec(`git add ${infoPlistFilePath} ${configFilePath} && git commit -m "${commitMessage}" && git push`).code > 0) { |
| 39 | + shell.echo("❌ 'createReleaseCommit' failed"); |
| 40 | + shell.exit(1); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function fetchEnv() { |
| 45 | + const appleID = await question(`🙏 Apple Developer 이메일을 입력해 주세요. (ex. [email protected]) \n> `); |
| 46 | + |
| 47 | + const currentVersion = fetchCurrentVersion(); |
| 48 | + const bumpType = await question( |
| 49 | + `\n🙏 bump type (no, patch, minor, major) 또는, 특정 버전(1.1.0)을 입력하세요. / 현재 버전은, '${currentVersion}' 입니다. \n> ` |
| 50 | + ); |
| 51 | + |
| 52 | + /** @type {string} */ |
| 53 | + const newVersionNumber = _newVersion(currentVersion, bumpType); |
| 54 | + const newBuildNumber = _newBuildNumber(); |
| 55 | + |
| 56 | + shell.echo(`\n===입력 정보 확인===\n🔑 Account: ${appleID}\n🎯 Version: '${newVersionNumber} - ${newBuildNumber}'\n`); |
| 57 | + |
| 58 | + return { |
| 59 | + appleID, |
| 60 | + newVersionNumber, |
| 61 | + newBuildNumber, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +async function deploy(appleID, newVersionNumber, newBuildNumber) { |
| 66 | + const { execa } = await import("execa"); |
| 67 | + await execa("grunt", ["gp"]); |
| 68 | + await execa( |
| 69 | + "bundle", |
| 70 | + ["exec", "fastlane", "ios", "beta", `new_version_number:${newVersionNumber}`, `new_build_number:${newBuildNumber}`], |
| 71 | + { |
| 72 | + env: { ...process.env, APPLE_ID: appleID }, |
| 73 | + stdio: "inherit", |
| 74 | + } |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +async function checkoutBranch(newVersionNumber, newBuildNumber) { |
| 79 | + const { execa } = await import("execa"); |
| 80 | + const res = await question("⚠️ 'git reset --h' 명령어가 실행됩니다. 커밋되지 않은 변경사항은 모두 삭제됩니다. [y/n]\n>"); |
| 81 | + |
| 82 | + switch (res) { |
| 83 | + case "Y": |
| 84 | + case "y": |
| 85 | + shell.echo("\n🌀 'git reset --h' 실행중"); |
| 86 | + await execa("git", ["reset", "--h"]); |
| 87 | + shell.echo("✅ 'git reset --h' 완료\n"); |
| 88 | + break; |
| 89 | + |
| 90 | + case "N": |
| 91 | + case "n": |
| 92 | + shell.echo("👋 종료됨"); |
| 93 | + shell.exit(1); |
| 94 | + default: |
| 95 | + shell.echo("👋 선택지에 없는 입력입니다."); |
| 96 | + shell.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + await execa("git", ["fetch"]); |
| 100 | + shell.echo("✅ 'git fetch' 완료\n"); |
| 101 | + |
| 102 | + shell.echo("🌀 'git checkout develop' 실행중"); |
| 103 | + await execa("git", ["checkout", "develop"]); |
| 104 | + shell.echo("✅ 'git checkout develop' 완료\n"); |
| 105 | + |
| 106 | + await execa("git", ["pull"]); |
| 107 | + shell.echo("✅ 'git pull' 완료\n"); |
| 108 | + |
| 109 | + shell.echo(`🌀 'git checkout -b release/v${newVersionNumber}-${newBuildNumber}' 실행중`); |
| 110 | + await execa("git", ["checkout", "-b", `release/v${newVersionNumber}-${newBuildNumber}`]); |
| 111 | + shell.echo(`✅ 'git checkout -b release/v${newVersionNumber}-${newBuildNumber}' 완료\n`); |
| 112 | + |
| 113 | + shell.echo(`🌀 'git push --set-upstream origin release/v${newVersionNumber}-${newBuildNumber}'실행중`); |
| 114 | + await execa("git", ["push", "--set-upstream", `origin`, `release/v${newVersionNumber}-${newBuildNumber}`]); |
| 115 | + shell.echo(`✅ 'git push --set-upstream origin release/v${newVersionNumber}-${newBuildNumber}' 완료\n`); |
| 116 | +} |
| 117 | + |
| 118 | +function fetchCurrentVersion() { |
| 119 | + const configFilePath = "./Projects/App/xcconfigs/Gabbangzip.shared.xcconfig"; |
| 120 | + |
| 121 | + const currentVersion = _getXCConfigValue("MARKETING_VERSION", configFilePath); |
| 122 | + |
| 123 | + if (currentVersion === undefined) { |
| 124 | + shell.echo("failed to get xcconfig value 'MARKETING_VERSION'"); |
| 125 | + shell.exit(1); |
| 126 | + } |
| 127 | + |
| 128 | + return currentVersion; |
| 129 | +} |
| 130 | + |
| 131 | +function _newVersion(currentVersion, bumpType) { |
| 132 | + switch (bumpType) { |
| 133 | + case "patch": |
| 134 | + case "minor": |
| 135 | + case "major": |
| 136 | + return semver.inc(currentVersion, bumpType); |
| 137 | + case "no": |
| 138 | + return currentVersion; |
| 139 | + default: |
| 140 | + const specificVersion = bumpType; |
| 141 | + return specificVersion; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +function _newBuildNumber() { |
| 146 | + const now = new Date(); |
| 147 | + const year = now.getFullYear().toString().padStart(4, "0"); |
| 148 | + const month = (now.getMonth() + 1).toString().padStart(2, "0"); |
| 149 | + const date = now.getDate().toString().padStart(2, "0"); |
| 150 | + const hour = now.getHours().toString().padStart(2, "0"); |
| 151 | + const minute = now.getMinutes().toString().padStart(2, "0"); |
| 152 | + return `${year}${month}${date}${hour}${minute}`; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * |
| 157 | + * @param {string} key |
| 158 | + * @param {string} value |
| 159 | + * @param {string} configFilePath |
| 160 | + */ |
| 161 | +function _setXCConfigValue(key, value, configFilePath) { |
| 162 | + const entities = _xcconfigEntities(configFilePath); |
| 163 | + const newEntities = entities.map((entity) => { |
| 164 | + if (entity.length !== 2) { |
| 165 | + return entity; |
| 166 | + } |
| 167 | + |
| 168 | + const [_key, _] = entity; |
| 169 | + if (_key !== key) { |
| 170 | + return entity; |
| 171 | + } |
| 172 | + |
| 173 | + return [_key, value]; |
| 174 | + }); |
| 175 | + const lines = newEntities.map((entity) => entity.join("=")); |
| 176 | + const data = lines.join("\n"); |
| 177 | + fs.writeFileSync(configFilePath, data); |
| 178 | +} |
| 179 | + |
| 180 | +/** |
| 181 | + * |
| 182 | + * @param {string} key |
| 183 | + * @param {string} configFilePath |
| 184 | + * @returns {(string|undefined)} value |
| 185 | + */ |
| 186 | +function _getXCConfigValue(key, configFilePath) { |
| 187 | + const entities = _xcconfigEntities(configFilePath); |
| 188 | + const entity = |
| 189 | + entities.find((entity) => { |
| 190 | + return entity[0] === key; |
| 191 | + }) ?? []; |
| 192 | + return entity[1]; |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * |
| 197 | + * @param {string} configFilePath |
| 198 | + * @returns {string[][]} entities |
| 199 | + */ |
| 200 | +function _xcconfigEntities(configFilePath) { |
| 201 | + const xcconfigStr = fs.readFileSync(configFilePath, { |
| 202 | + encoding: "utf8", |
| 203 | + }); |
| 204 | + const lines = xcconfigStr.split("\n"); |
| 205 | + return lines.map((line) => { |
| 206 | + return line.split("=", 2); |
| 207 | + }); |
| 208 | +} |
0 commit comments