From 0117fb4ef39682cb997861db460fd91c28b4acf1 Mon Sep 17 00:00:00 2001 From: Arrizal Amin Date: Tue, 6 Jun 2023 13:18:14 +0700 Subject: [PATCH] feat: generate app without install its dependencies --- packages/generator/package.json | 3 +- packages/generator/src/expo/generate.ts | 78 ++++++++++--------------- 2 files changed, 32 insertions(+), 49 deletions(-) diff --git a/packages/generator/package.json b/packages/generator/package.json index 91632ea..08b18e3 100644 --- a/packages/generator/package.json +++ b/packages/generator/package.json @@ -6,8 +6,7 @@ "dependencies": { "@babel/generator": "^7.20.7", "@babel/types": "^7.20.7", - "@expo/package-manager": "^0.0.57", - "@expo/spawn-async": "^1.7.0", + "@expo/cli": "^0.7.3", "dot-object": "^2.1.4", "prettier": "^2.8.0" }, diff --git a/packages/generator/src/expo/generate.ts b/packages/generator/src/expo/generate.ts index 53783bd..5ee806a 100644 --- a/packages/generator/src/expo/generate.ts +++ b/packages/generator/src/expo/generate.ts @@ -1,13 +1,12 @@ import path from "path"; import fs from "node:fs"; import { AppSchema } from "@batiq/core"; -import * as ExpoPackageManager from "@expo/package-manager"; -import spawnAsync from "@expo/spawn-async"; import { generatePage } from "../codegen"; import Dot from "dot-object"; import { toVariableName } from "../utils/naming"; import { transformIR } from "@batiq/ir"; import { generateNavigation } from "./navigation"; +import { getCombinedKnownVersionsAsync } from "@expo/cli/build/src/start/doctor/dependencies/getVersionedPackages"; const dot = new Dot("__"); @@ -33,6 +32,35 @@ export const installExpo = async (schema: AppSchema) => { ).flat() ) ); + const dependencies = [ + "expo", + "@babel/core", + "@batiq/cli", + "@types/react", + "@types/react-native", + "dotenv", + "typescript", + "react", + "react-dom", + "react-native", + "react-native-web", + "@react-navigation/native-stack", + "@react-navigation/bottom-tabs", + "@react-navigation/native", + "react-native-screens", + "react-native-safe-area-context", + "@expo/webpack-config", + ...componentDependencies, + ]; + const packages = await getCombinedKnownVersionsAsync({ + projectRoot: "./app", + sdkVersion: "48.0.0", + skipCache: false, + }); + const resolvedDependencies = dependencies.reduce( + (acc, dep) => ({ ...acc, [dep]: packages[dep] ?? "*" }), + {} as Record + ); fs.writeFileSync( "package.json", JSON.stringify( @@ -47,56 +75,12 @@ export const installExpo = async (schema: AppSchema) => { web: "expo start --web", }, private: true, + dependencies: resolvedDependencies, }, null, 2 ) ); - const pm = new ExpoPackageManager.NpmPackageManager({ - cwd: process.cwd(), - silent: false, - }); - if (process.env["NODE_ENV"] === "development") { - // remove yarn injected env variables - Object.keys(process.env) - .filter((key) => key.startsWith("npm")) - .forEach((key) => { - delete process.env[key]; - }); - } - await pm.addAsync("expo"); - await pm.addDevAsync( - "@babel/core", - "@batiq/cli", - "@types/react", - "@types/react-native", - "dotenv", - "typescript" - ); - await pm.installAsync(); - const expoInstall = spawnAsync("npx", [ - "expo", - "install", - "react", - "react-dom", - "react-native", - "react-native-web", - "@react-navigation/native-stack", - "@react-navigation/bottom-tabs", - "@react-navigation/native", - "react-native-screens", - "react-native-safe-area-context", - "@expo/webpack-config", - ...componentDependencies, - "--npm", - ]); - expoInstall.child.stdout?.on("data", (data) => { - console.log(data?.toString?.()); - }); - expoInstall.child.stderr?.on("data", (data) => { - console.error(data?.toString?.()); - }); - await expoInstall; }; export const generateExpo = async (schema: AppSchema) => {