-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added expo strategy and updater (#1646)
* feat: added expo strategy and updater Added `strategies/expo` and `updaters/expo/app-json` for versioning of Expo based React Native projects. This updater performs the same functions as the `node` updater but also updates the `app.json` file found in Expo projects. * chore: added expo to the strategy list in the docs * chore: updated copyright year on all expo strategy/updater files * chore: added expo to src/factory Added `expo` as a type available on the global factory. Also updated the Expo SDK version number passed between the Expo strategy and updater to use a `Version` object instead of a string. * chore: fixed expo strategy expo sdk version test * chore: fixed broken CLI snapshot tests * fix: treat expo ios and android build numbers as optional If a project has not specified the `expo.ios.buildNumber` or `expo.android.versionCode`, don't attempt to set those. Co-authored-by: Jeff Ching <[email protected]>
- Loading branch information
Showing
12 changed files
with
521 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
exports['AppJson updateContent updates the app versions 1'] = ` | ||
{ | ||
"expo": { | ||
"owner": "some-owner", | ||
"name": "Some Name", | ||
"slug": "some-slug", | ||
"version": "3.2.1", | ||
"orientation": "portrait", | ||
"icon": "./assets/icon-inverse.png", | ||
"scheme": "someschema", | ||
"splash": { | ||
"image": "./assets/splash.png", | ||
"resizeMode": "cover", | ||
"backgroundColor": "#FFFFFF" | ||
}, | ||
"updates": { | ||
"fallbackToCacheTimeout": 0, | ||
"url": "some-url-here" | ||
}, | ||
"assetBundlePatterns": [ | ||
"**/*" | ||
], | ||
"ios": { | ||
"bundleIdentifier": "com.somedomain", | ||
"buildNumber": "3.2.1", | ||
"supportsTablet": true, | ||
"config": { | ||
"usesNonExemptEncryption": false | ||
} | ||
}, | ||
"android": { | ||
"package": "com.somedomain", | ||
"versionCode": "440030201", | ||
"adaptiveIcon": { | ||
"foregroundImage": "./assets/icon-inverse.png", | ||
"backgroundColor": "#FFFFFF" | ||
} | ||
}, | ||
"web": { | ||
"favicon": "./assets/favicon.png" | ||
} | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {BuildUpdatesOptions} from './base'; | ||
import {Node} from './node'; | ||
import {Update} from '../update'; | ||
import {AppJson} from '../updaters/expo/app-json'; | ||
import {Version} from '../version'; | ||
|
||
/** | ||
* Strategy for building Expo based React Native projects. This strategy extends | ||
* the Node strategy to additionally update the `app.json` file of a project. | ||
*/ | ||
export class Expo extends Node { | ||
protected async buildUpdates( | ||
options: BuildUpdatesOptions | ||
): Promise<Update[]> { | ||
const version = options.newVersion; | ||
const updates = await super.buildUpdates(options); | ||
const expoSDKVersion = await this.getExpoSDKVersion(); | ||
|
||
updates.push({ | ||
path: this.addPath('app.json'), | ||
createIfMissing: false, | ||
updater: new AppJson({version, expoSDKVersion}), | ||
}); | ||
|
||
return updates; | ||
} | ||
|
||
/** | ||
* Determine the Expo SDK version by parsing the package.json dependencies. | ||
*/ | ||
async getExpoSDKVersion(): Promise<Version> { | ||
const pkgJsonContents = await this.getPkgJsonContents(); | ||
const pkg = JSON.parse(pkgJsonContents.parsedContent); | ||
return Version.parse( | ||
pkg.dependencies?.expo || | ||
pkg.devDependencies?.expo || | ||
pkg.peerDependencies?.expo || | ||
pkg.optionalDependencies?.expo || | ||
'0.0.0' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {jsonStringify} from '../../util/json-stringify'; | ||
import {logger as defaultLogger, Logger} from '../../util/logger'; | ||
import {DefaultUpdater, UpdateOptions} from '../default'; | ||
import {Version} from '../../version'; | ||
|
||
export interface AppJson { | ||
expo: { | ||
version: string; | ||
ios?: { | ||
buildNumber?: string; | ||
}; | ||
android?: { | ||
versionCode?: string; | ||
}; | ||
}; | ||
} | ||
|
||
export interface AppJsonOptions extends UpdateOptions { | ||
expoSDKVersion: Version; | ||
} | ||
|
||
/** | ||
* This updates a React Natve Expo project app.json file's main, ios and android | ||
* versions. All values except the `android.versionCode` are standard semver | ||
* version numbers. For the `android.versionCode`, the semver number is used as | ||
* the basis for the `versionCode`. | ||
*/ | ||
export class AppJson extends DefaultUpdater { | ||
expoSDKVersion: Version; | ||
constructor(options: AppJsonOptions) { | ||
super(options); | ||
this.expoSDKVersion = options.expoSDKVersion; | ||
} | ||
/** | ||
* Given initial file contents, return updated contents. | ||
*/ | ||
updateContent(content: string, logger: Logger = defaultLogger): string { | ||
const parsed = JSON.parse(content) as AppJson; | ||
|
||
logger.info( | ||
`updating Expo version from ${parsed.expo.version} to ${this.version}` | ||
); | ||
parsed.expo.version = this.version.toString(); | ||
|
||
if (parsed.expo.ios?.buildNumber) { | ||
logger.info( | ||
`updating iOS version from ${parsed.expo.ios.buildNumber} to ${this.version}` | ||
); | ||
parsed.expo.ios.buildNumber = this.version.toString(); | ||
} | ||
|
||
if (parsed.expo.android?.versionCode) { | ||
// Android versionCode | ||
// https://developer.android.com/studio/publish/versioning#appversioning | ||
let expoMajorVersion = 0; | ||
try { | ||
expoMajorVersion = this.expoSDKVersion.major; | ||
} catch (e) { | ||
// Rethrow with a nice error message. | ||
throw new Error( | ||
'Unable to determine the Expo SDK version for this project. Make sure that the expo package is installed for your project.' | ||
); | ||
} | ||
|
||
// Implements the `versionCode` strategy described by Maxi Rosson | ||
// @see https://medium.com/@maxirosson/versioning-android-apps-d6ec171cfd82 | ||
const versionCode = | ||
expoMajorVersion * 10000000 + | ||
this.version.major * 10000 + | ||
this.version.minor * 100 + | ||
this.version.patch; | ||
logger.info( | ||
`updating Android version from ${parsed.expo.android.versionCode} to ${versionCode}` | ||
); | ||
parsed.expo.android.versionCode = versionCode.toString(); | ||
} | ||
|
||
return jsonStringify(parsed, content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "node-test-repo", | ||
"version": "0.123.4", | ||
"repository": { | ||
"url": "[email protected]:samples/node-test-repo.git" | ||
}, | ||
"dependencies": { | ||
"expo": "44.0.0" | ||
} | ||
} |
Oops, something went wrong.