Skip to content

Commit

Permalink
My updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagiera committed Apr 2, 2024
1 parent 62865e5 commit 77bf453
Show file tree
Hide file tree
Showing 16 changed files with 13,253 additions and 86 deletions.
4 changes: 2 additions & 2 deletions packages/vscode-extension/lib/expo_go_download.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { downloadExpoGoAsync } = requireFromAppDir("@expo/cli/build/src/utils/dow
async function main() {
let platform = process.argv[2]; // 'Android' or 'iOS'

if (!platform) {
throw new Error('Please provide both platform ("Android" or "iOS").');
if (platform !== "Android" && platform !== "iOS") {
throw new Error("Platform not selected.");
}
const { exp } = getConfig(appRoot);
const sdkVersion = exp.sdkVersion;
Expand Down
83 changes: 22 additions & 61 deletions packages/vscode-extension/src/builders/BuildManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Platform } from "../common/DeviceManager";
import { extensionContext, getAppRootFolder } from "../utilities/extensionContext";
import { exec } from "../utilities/subprocess";
import { Disposable, OutputChannel, window } from "vscode";
import path from "path";
import { downloadExpoGo, shouldUseExpoGo } from "./expoGo";

const ANDROID_BUILD_CACHE_KEY = "android_build_cache";
const IOS_BUILD_CACHE_KEY = "ios_build_cache";
Expand Down Expand Up @@ -88,19 +88,14 @@ export class BuildManager {
public startBuild(
platform: Platform,
forceCleanBuild: boolean,
progressListener: (newProgress: number) => void,
useExpoGo: boolean
progressListener: (newProgress: number) => void
) {
if (platform === Platform.Android && useExpoGo) {
return this.getAndroidExpoGoBuild();
} else if (platform === Platform.Android) {
if (platform === Platform.Android) {
const cancelToken = new CancelToken();
return new DisposableBuildImpl(
this.startAndroidBuild(forceCleanBuild, cancelToken, progressListener),
cancelToken
);
} else if (platform === Platform.IOS && useExpoGo) {
return this.getIOSExpoGoBuild();
} else {
const cancelToken = new CancelToken();
return new DisposableBuildImpl(
Expand Down Expand Up @@ -141,6 +136,16 @@ export class BuildManager {
cancelToken: CancelToken,
progressListener: (newProgress: number) => void
) {
const isExpoGoProject = await shouldUseExpoGo();
if (isExpoGoProject) {
const apkPath = await downloadExpoGo(Platform.Android, cancelToken);
return {
platform: Platform.Android,
apkPath,
packageName: EXPO_GO_PACKAGE_NAME,
} as AndroidBuildResult;
}

const newFingerprint = await generateWorkspaceFingerprint();
if (!forceCleanBuild) {
const buildResult = await this.loadAndroidCachedBuild(newFingerprint);
Expand Down Expand Up @@ -207,6 +212,15 @@ export class BuildManager {
cancelToken: CancelToken,
progressListener: (newProgress: number) => void
) {
const isExpoGoProject = await shouldUseExpoGo();
if (isExpoGoProject) {
const appPath = await downloadExpoGo(Platform.IOS, cancelToken);
return {
platform: Platform.IOS,
appPath,
bundleID: EXPO_GO_BUNDLE_ID,
} as IOSBuildResult;
}
const newFingerprint = await generateWorkspaceFingerprint();
if (!forceCleanBuild) {
const buildResult = await this.loadIOSCachedBuild(newFingerprint);
Expand Down Expand Up @@ -238,57 +252,4 @@ export class BuildManager {

return { ...build, platform: Platform.IOS } as IOSBuildResult;
}

private getIOSExpoGoBuild(): DisposableBuildImpl<BuildResult> {
const cancelToken = new CancelToken();
const buildPromise: Promise<BuildResult> = new Promise((resolve, reject) => {
downloadExpoGo(Platform.IOS)
.then((appPath) => {
const build = {
platform: Platform.IOS,
appPath,
bundleID: EXPO_GO_BUNDLE_ID,
} as IOSBuildResult;
resolve(build);
})
.catch((e) => {
Logger.error("Failed to download Expo Go", e);
reject(e);
});
});
return new DisposableBuildImpl(buildPromise, cancelToken);
}

private getAndroidExpoGoBuild(): DisposableBuildImpl<BuildResult> {
const cancelToken = new CancelToken();
const buildPromise: Promise<BuildResult> = new Promise((resolve, reject) => {
downloadExpoGo(Platform.Android)
.then((apkPath) => {
const build = {
platform: Platform.Android,
apkPath,
packageName: EXPO_GO_PACKAGE_NAME,
} as AndroidBuildResult;
resolve(build);
})
.catch((e) => {
Logger.error("Failed to download Expo Go", e);
reject(e);
});
});
return new DisposableBuildImpl(buildPromise, cancelToken);
}
}

async function downloadExpoGo(platform: Platform) {
const libPath = path.join(extensionContext.extensionPath, "lib");
const { stdout } = await exec(`node`, [path.join(libPath, "expo_go_download.js"), platform], {
cwd: getAppRootFolder(),
});

// While expo downloads the file, it prints '- Fetching Expo Go' and at the last line it prints the path to the downloaded file
// we want to wait until the file is downloaded before we return the path
const lines = stdout.split("\n");
const filepath = lines[lines.length - 1];
return filepath;
}
17 changes: 17 additions & 0 deletions packages/vscode-extension/src/builders/expoGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from "path";
import { extensionContext, getAppRootFolder } from "../utilities/extensionContext";
import http from "http";
import { exec } from "../utilities/subprocess";
import { Platform } from "../common/DeviceManager";
import { CancelToken } from "./BuildManager";

type ExpoDeeplinkChoice = "expo-go" | "expo-dev-client";

Expand Down Expand Up @@ -42,3 +44,18 @@ export function fetchExpoLaunchDeeplink(
req.end();
});
}

export async function downloadExpoGo(platform: Platform, cancelToken: CancelToken) {
const downloadScript = path.join(extensionContext.extensionPath, "lib", "expo_go_download.js");
const { stdout } = await cancelToken.adapt(
exec(`node`, [downloadScript, platform], {
cwd: getAppRootFolder(),
})
);

// While expo downloads the file, it prints '- Fetching Expo Go' and at the last line it prints the path to the downloaded file
// we want to wait until the file is downloaded before we return the path
const lines = stdout.split("\n");
const filepath = lines[lines.length - 1];
return filepath;
}
2 changes: 0 additions & 2 deletions packages/vscode-extension/src/common/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export interface ProjectEventListener<T> {
}

export interface ProjectInterface {
useExpoGo(): Promise<boolean>;

getProjectState(): Promise<ProjectState>;
restart(forceCleanBuild: boolean): Promise<void>;
selectDevice(deviceInfo: DeviceInfo): Promise<void>;
Expand Down
9 changes: 1 addition & 8 deletions packages/vscode-extension/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { EventEmitter } from "stream";
import { openFileAtPosition } from "../utilities/openFileAtPosition";
import { extensionContext } from "../utilities/extensionContext";
import stripAnsi from "strip-ansi";
import { shouldUseExpoGo } from "../builders/expoGo";

const LAST_SELECTED_DEVICE_KEY = "lastSelectedDevice";

Expand Down Expand Up @@ -54,10 +53,6 @@ export class Project implements Disposable, MetroDelegate, ProjectInterface {
this.deviceManager.addListener("deviceRemoved", this.removeDeviceListener);
}

async useExpoGo() {
return await shouldUseExpoGo();
}

onBundleError(): void {
this.updateProjectState({ status: "bundleError" });
}
Expand Down Expand Up @@ -353,14 +348,12 @@ export class Project implements Disposable, MetroDelegate, ProjectInterface {
});
// wait for metro/devtools to start before we continue
await Promise.all([this.metro.ready(), this.devtools.ready()]);
const useExpoGo = await this.useExpoGo();
const build = this.buildManager.startBuild(
deviceInfo.platform,
forceCleanBuild,
throttle((stageProgress: number) => {
this.reportStageProgress(stageProgress, StartupMessage.Building);
}, 100),
useExpoGo
}, 100)
);
Logger.debug("Metro & devtools ready");
newDeviceSession = new DeviceSession(device, this.devtools, this.metro, build);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createContext, useCallback, useContext, useEffect, useState } from "react";
import { vscode } from "../utilities/vscode";
import { useProject } from "./ProjectProvider";

export interface DependencyData {
installed?: boolean;
Expand Down Expand Up @@ -55,18 +54,14 @@ interface DependenciesProviderProps {

export default function DependenciesProvider({ children }: DependenciesProviderProps) {
const [dependencies, setDependencies] = useState<Dependencies>({});
const [useExpoGo, setUseExpoGo] = useState(false);

const { project } = useProject();
// `isReady` is true when all dependencies were checked
const isReady = Object.keys(dependencies).every(
(key) => dependencies[key as keyof Dependencies] !== undefined
);
const isError = Object.keys(dependencies).some((key) => {
// Skips Pods check if project is using Expo Go
if (key === "Pods" && useExpoGo) return false;
return dependencies[key as keyof Dependencies]?.error !== undefined;
});
const isError = Object.keys(dependencies).some(
(key) => dependencies[key as keyof Dependencies]?.error !== undefined
);

const rerunDiagnostics = useCallback(() => {
// set `.installed` and .error to undefined, leave other data as is
Expand All @@ -85,10 +80,6 @@ export default function DependenciesProvider({ children }: DependenciesProviderP
runDiagnostics();
}, []);

const checkExpoGo = async () => {
setUseExpoGo(await project.useExpoGo());
};

useEffect(() => {
const listener = (event: MessageEvent<any>) => {
const message = event.data;
Expand Down Expand Up @@ -127,7 +118,6 @@ export default function DependenciesProvider({ children }: DependenciesProviderP
};

runDiagnostics();
checkExpoGo();
window.addEventListener("message", listener);

return () => window.removeEventListener("message", listener);
Expand Down
35 changes: 35 additions & 0 deletions test-apps/expo/expo-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files

# dependencies
node_modules/

# Expo
.expo/
dist/
web-build/

# Native
*.orig.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision

# Metro
.metro-health-check*

# debug
npm-debug.*
yarn-debug.*
yarn-error.*

# macOS
.DS_Store
*.pem

# local env files
.env*.local

# typescript
*.tsbuildinfo
20 changes: 20 additions & 0 deletions test-apps/expo/expo-go/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';

export default function App() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
30 changes: 30 additions & 0 deletions test-apps/expo/expo-go/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"expo": {
"name": "expo-go",
"slug": "expo-go",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}
Binary file added test-apps/expo/expo-go/assets/adaptive-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-apps/expo/expo-go/assets/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-apps/expo/expo-go/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-apps/expo/expo-go/assets/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test-apps/expo/expo-go/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
Loading

0 comments on commit 77bf453

Please sign in to comment.