UNPKG

@brettdh/standard-version-expo

Version:

Automatic Expo versioning with Standard Version

87 lines (86 loc) 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const os_1 = __importDefault(require("os")); const plist_1 = __importDefault(require("plist")); const androidAppVersionRegex = /^(.+versionName +["']?)([^"']+)(["']?.*)$/; const androidBuildnumRegex = /^(.+versionCode +["']?)([0-9]+)(["']?.*)$/; const replaceMatchingLines = (contents, rx, version) => { // it's a PITA to make sure we insert it in the right place, // and it's always there in the generated android project, // so if we don't find it, throw an error. if (!findMatchingLine(contents, rx)) { let field; if (rx === androidAppVersionRegex) { field = 'versionName'; } else if (rx === androidBuildnumRegex) { field = 'versionCode'; } else { throw new Error('NOTREACHED'); } throw new Error(`Could not find ${field} in build.gradle`); } return contents.split(os_1.default.EOL) .map(line => line.replace(rx, `$1${version}$3`)) .join(os_1.default.EOL); }; const findMatchingLine = (contents, rx) => { for (const line of contents.split(os_1.default.EOL)) { const match = line.match(rx); if (match) { return match[2]; } } return ''; }; /** * The default android app version reader. * It reads the value from `android/app/build.gradle` and returns it as string. */ exports.androidAppVersionReader = (contents) => (findMatchingLine(contents, androidAppVersionRegex)); /** * The default android buildnum reader. * It reads the value from `android/app/build.gradle` and returns it as string. */ exports.androidBuildnumReader = (contents) => (findMatchingLine(contents, androidBuildnumRegex)); /** * The default android app version writer. * It replaces the value in the `android/app/build.gradle` contents and * returns the new contents as string. */ exports.androidAppVersionWriter = (contents, version) => (replaceMatchingLines(contents, androidAppVersionRegex, version)); /** * The default android buildnum writer. * It replaces the value in the `android/app/build.gradle` contents and * returns the new contents as string. */ exports.androidBuildnumWriter = (contents, version) => (replaceMatchingLines(contents, androidBuildnumRegex, version)); const iosReadVersion = (contents, key) => (plist_1.default.parse(contents)[key] || ''); /** * The default ios app version reader. * It reads the value from `Info.plist` and returns it as string. */ exports.iosAppVersionReader = (contents) => (iosReadVersion(contents, 'CFBundleShortVersionString')); /** * The default ios buildnum reader. * It reads the value from `Info.plist` and returns it as string. */ exports.iosBuildnumReader = (contents) => (iosReadVersion(contents, 'CFBundleVersion')); const iosWriteVersion = (contents, key, value) => (plist_1.default.build(Object.assign(Object.assign({}, plist_1.default.parse(contents)), { [key]: value }))); /** * The default ios app version writer. * It replaces the value in the `Info.plist` contents and returns the new contents as string. */ exports.iosAppVersionWriter = (contents, version) => (iosWriteVersion(contents, 'CFBundleShortVersionString', // trim any prerelease suffixes; this string // must strictly be an x.y.z version. version.split('-')[0])); /** * The default ios buildnum writer. * It replaces the value in the `Info.plist` contents and returns the new contents as string. */ exports.iosBuildnumWriter = (contents, version) => (iosWriteVersion(contents, 'CFBundleVersion', version));