This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[expo-cli] command for syncing credentials.json and credentials on www
- Loading branch information
Showing
6 changed files
with
319 additions
and
2 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
packages/expo-cli/src/commands/eas-build/credentialsSync/action.ts
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,73 @@ | ||
import CommandError from '../../../CommandError'; | ||
import { Context } from '../../../credentials/context'; | ||
import { updateLocalCredentialsJsonAsync } from '../../../credentials/local'; | ||
import { runCredentialsManager } from '../../../credentials/route'; | ||
import { SetupAndroidBuildCredentialsFromLocal } from '../../../credentials/views/SetupAndroidKeystore'; | ||
import { SetupIosBuildCredentialsFromLocal } from '../../../credentials/views/SetupIosBuildCredentials'; | ||
import prompts from '../../../prompts'; | ||
import { BuildCommandPlatform } from '../types'; | ||
|
||
interface Options { | ||
parent: { | ||
nonInteractive?: boolean; | ||
}; | ||
} | ||
|
||
export default async function credentialsSyncAction(projectDir: string, options: Options) { | ||
if (options.parent.nonInteractive) { | ||
throw new CommandError('This command is not supported in --non-interactive mode'); | ||
} | ||
const { update, platform } = await prompts([ | ||
{ | ||
type: 'select', | ||
name: 'update', | ||
message: 'What do you want to do?', | ||
choices: [ | ||
{ | ||
title: 'Update credentials on Expo servers with local credentials.json content', | ||
value: 'remote', | ||
}, | ||
{ title: 'Update local credentials.json with values from Expo servers', value: 'local' }, | ||
], | ||
}, | ||
{ | ||
type: 'select', | ||
name: 'platform', | ||
message: 'Do you want to update credentials for both platforms?', | ||
choices: [ | ||
{ title: 'Android & iOS', value: BuildCommandPlatform.ALL }, | ||
{ title: 'only Android', value: BuildCommandPlatform.ANDROID }, | ||
{ title: 'only iOS', value: BuildCommandPlatform.IOS }, | ||
], | ||
}, | ||
]); | ||
if (update === 'local') { | ||
await updateLocalCredentialsJsonAsync(projectDir, platform); | ||
} else { | ||
await updateRemoteCredentialsAsync(projectDir, platform); | ||
} | ||
} | ||
|
||
async function updateRemoteCredentialsAsync(projectDir: string, platform: BuildCommandPlatform) { | ||
const ctx = new Context(); | ||
await ctx.init(projectDir); | ||
if (!ctx.hasProjectContext) { | ||
throw new Error('project context is required'); // should bb checked earlier | ||
} | ||
if (['all', 'android'].includes(platform)) { | ||
const experienceName = `@${ctx.manifest.owner || ctx.user.username}/${ctx.manifest.slug}`; | ||
await runCredentialsManager(ctx, new SetupAndroidBuildCredentialsFromLocal(experienceName)); | ||
} | ||
if (['all', 'ios'].includes(platform)) { | ||
const bundleIdentifier = ctx.manifest.ios?.bundleIdentifier; | ||
if (!bundleIdentifier) { | ||
throw new Error('"expo.ios.bundleIdentifier" field is required in your app.json'); | ||
} | ||
const appLookupParams = { | ||
accountName: ctx.manifest.owner ?? ctx.user.username, | ||
projectName: ctx.manifest.slug, | ||
bundleIdentifier, | ||
}; | ||
await runCredentialsManager(ctx, new SetupIosBuildCredentialsFromLocal(appLookupParams)); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as credentialsJson } from './credentialsJson'; | ||
export { updateLocalCredentialsJsonAsync } from './update'; |
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,157 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import prompts from 'prompts'; | ||
|
||
import log from '../../log'; | ||
import { Context } from '../context'; | ||
|
||
type Platform = 'android' | 'ios' | 'all'; | ||
|
||
export async function updateLocalCredentialsJsonAsync(projectDir: string, platform: Platform) { | ||
const ctx = new Context(); | ||
await ctx.init(projectDir); | ||
if (!ctx.hasProjectContext) { | ||
throw new Error('project context is required'); // should be checked earlier | ||
} | ||
if (['all', 'android'].includes(platform)) { | ||
log('Updating Android credentials in credentials.json'); | ||
await updateAndroidAsync(ctx); | ||
} | ||
if (['all', 'ios'].includes(platform)) { | ||
log('Updating iOS credentials in credentials.json'); | ||
await updateIosAsync(ctx); | ||
} | ||
} | ||
|
||
async function updateAndroidAsync(ctx: Context) { | ||
const credentialsJsonFilePath = path.join(ctx.projectDir, 'credentials.json'); | ||
let rawCredentialsJsonObject: any = {}; | ||
if (await fs.pathExists(credentialsJsonFilePath)) { | ||
try { | ||
const rawFile = await fs.readFile(credentialsJsonFilePath); | ||
rawCredentialsJsonObject = JSON.parse(rawFile.toString()); | ||
} catch (error) { | ||
log.error(`There was an error while reading credentials.json [${error}]`); | ||
log.error('Make sure that file is correct (or remove it) and rerun this command.'); | ||
throw error; | ||
} | ||
} | ||
const experienceName = `@${ctx.manifest.owner || ctx.user.username}/${ctx.manifest.slug}`; | ||
const keystore = await ctx.android.fetchKeystore(experienceName); | ||
if (!keystore) { | ||
throw new Error('There are no credentials configured for this project on Expo servers'); | ||
} | ||
|
||
const isKeystoreComplete = | ||
keystore.keystore && keystore.keystorePassword && keystore.keyPassword && keystore.keyAlias; | ||
|
||
if (!isKeystoreComplete) { | ||
const { confirm } = await prompts({ | ||
type: 'confirm', | ||
name: 'confirm', | ||
message: | ||
'Credentials on Expo servers might be invalid or incomplete. Are you sure you want to continue?', | ||
}); | ||
if (!confirm) { | ||
log.warn('Aborting...'); | ||
return; | ||
} | ||
} | ||
|
||
const keystorePath = | ||
rawCredentialsJsonObject?.android?.keystorePath ?? './android/keystores/keystore.jks'; | ||
await _updateFileAsync(ctx.projectDir, keystorePath, keystore.keystore); | ||
|
||
rawCredentialsJsonObject.android = { | ||
keystore: { | ||
keystorePath, | ||
keystorePassword: keystore.keystorePassword, | ||
keyAlias: keystore.keyAlias, | ||
keyPassword: keystore.keyPassword, | ||
}, | ||
}; | ||
await fs.writeJson(credentialsJsonFilePath, rawCredentialsJsonObject, { | ||
spaces: 2, | ||
}); | ||
} | ||
|
||
async function updateIosAsync(ctx: Context) { | ||
const credentialsJsonFilePath = path.join(ctx.projectDir, 'credentials.json'); | ||
let rawCredentialsJsonObject: any = {}; | ||
if (await fs.pathExists(credentialsJsonFilePath)) { | ||
try { | ||
const rawFile = await fs.readFile(credentialsJsonFilePath); | ||
rawCredentialsJsonObject = JSON.parse(rawFile.toString()); | ||
} catch (error) { | ||
log.error(`There was an error while reading credentials.json [${error}]`); | ||
log.error('Make sure that file is correct (or remove it) and rerun this command.'); | ||
throw error; | ||
} | ||
} | ||
|
||
const bundleIdentifier = ctx.manifest.ios?.bundleIdentifier; | ||
if (!bundleIdentifier) { | ||
throw new Error('"expo.ios.bundleIdentifier" field is required in your app.json'); | ||
} | ||
const appLookupParams = { | ||
accountName: ctx.manifest.owner ?? ctx.user.username, | ||
projectName: ctx.manifest.slug, | ||
bundleIdentifier, | ||
}; | ||
const pprofilePath = | ||
rawCredentialsJsonObject?.ios?.provisioningProfilePath ?? './ios/certs/profile.mobileprovision'; | ||
const distCertPath = | ||
rawCredentialsJsonObject?.ios?.distributionCertificate.path ?? './ios/certs/dist-cert.p12'; | ||
const appCredentials = await ctx.ios.getAppCredentials(appLookupParams); | ||
const distCredentials = await ctx.ios.getDistCert(appLookupParams); | ||
if (!appCredentials && !distCredentials) { | ||
throw new Error('There are no credentials configured for this project on Expo servers'); | ||
} | ||
|
||
const areCredentialsComplete = | ||
appCredentials?.credentials?.provisioningProfile && | ||
distCredentials?.certP12 && | ||
distCredentials?.certPassword; | ||
|
||
if (!areCredentialsComplete) { | ||
const { confirm } = await prompts({ | ||
type: 'confirm', | ||
name: 'confirm', | ||
message: | ||
'Credentials on Expo servers might be invalid or incomplete. Are you sure you want to continue?', | ||
}); | ||
if (!confirm) { | ||
log.warn('Aborting...'); | ||
return; | ||
} | ||
} | ||
|
||
await _updateFileAsync( | ||
ctx.projectDir, | ||
pprofilePath, | ||
appCredentials?.credentials?.provisioningProfile | ||
); | ||
await _updateFileAsync(ctx.projectDir, distCertPath, distCredentials?.certP12); | ||
|
||
rawCredentialsJsonObject.ios = { | ||
provisioningProfilePath: pprofilePath, | ||
distributionCertificate: { | ||
path: distCertPath, | ||
password: distCredentials?.certPassword, | ||
}, | ||
}; | ||
await fs.writeJson(credentialsJsonFilePath, rawCredentialsJsonObject, { | ||
spaces: 2, | ||
}); | ||
} | ||
|
||
async function _updateFileAsync(projectDir: string, filePath: string, base64Data?: string) { | ||
const absolutePath = path.isAbsolute(filePath) ? filePath : path.join(projectDir, filePath); | ||
if (await fs.pathExists(absolutePath)) { | ||
await fs.remove(absolutePath); | ||
} | ||
if (base64Data) { | ||
await fs.mkdirp(path.dirname(filePath)); | ||
await fs.writeFile(filePath, Buffer.from(base64Data, 'base64')); | ||
} | ||
} |
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