From a2752d87fea01c66d526cb516c97ff9cd514774e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Che=C5=82miniak?= Date: Fri, 8 Nov 2024 15:08:43 +0100 Subject: [PATCH] feat: remove createEnvFile and updateEnvFile (#25) --- packages/core/installMachine/index.ts | 29 +---------- .../installSteps/env/createEnvFile.ts | 29 ----------- .../installSteps/supabase/connectProject.ts | 13 ----- packages/core/types.ts | 1 - packages/core/utils/rcFileManager/index.ts | 2 +- packages/core/utils/updateEnvFile.ts | 51 ------------------- 6 files changed, 2 insertions(+), 123 deletions(-) delete mode 100644 packages/core/installMachine/installSteps/env/createEnvFile.ts delete mode 100644 packages/core/utils/updateEnvFile.ts diff --git a/packages/core/installMachine/index.ts b/packages/core/installMachine/index.ts index 468a352..ae1a73f 100644 --- a/packages/core/installMachine/index.ts +++ b/packages/core/installMachine/index.ts @@ -1,6 +1,5 @@ import { createMachine, fromPromise, ActorLogic, AnyEventObject, PromiseSnapshot, createActor, and, not } from 'xstate'; -import { createEnvFile } from './installSteps/env/createEnvFile'; import { initializeRepository } from './installSteps/github/install'; import { preparePayload } from './installSteps/payload/install'; import { prettify } from './installSteps/prettier/prettify'; @@ -59,25 +58,11 @@ const createInstallMachine = (initialContext: InstallMachineContext) => { always: [ { guard: isStepCompleted('initializeProject'), - target: 'createEnvFile', - }, - ], - invoke: { - src: 'initializeProjectActor', - input: ({ context }) => context, - onDone: 'createEnvFile', - onError: 'failed', - }, - }, - createEnvFile: { - always: [ - { - guard: isStepCompleted('createEnvFile'), target: 'installSupabase', }, ], invoke: { - src: 'createEnvFileActor', + src: 'initializeProjectActor', input: ({ context }) => context, onDone: 'installSupabase', onError: 'failed', @@ -288,18 +273,6 @@ const createInstallMachine = (initialContext: InstallMachineContext) => { } }), ), - createEnvFileActor: createStepMachine( - fromPromise(async ({ input }) => { - try { - createEnvFile(input.projectDir); - input.stateData.stepsCompleted.createEnvFile = true; - saveStateToRcFile(input.stateData, input.projectDir); - } catch (error) { - console.error('Error in createEnvFileActor:', error); - throw error; - } - }), - ), installSupabaseActor: createStepMachine( fromPromise(async ({ input }) => { try { diff --git a/packages/core/installMachine/installSteps/env/createEnvFile.ts b/packages/core/installMachine/installSteps/env/createEnvFile.ts deleted file mode 100644 index dc47709..0000000 --- a/packages/core/installMachine/installSteps/env/createEnvFile.ts +++ /dev/null @@ -1,29 +0,0 @@ -import fs from 'fs'; -import path from 'path'; -import { logWithColoredPrefix } from '../../../utils/logWithColoredPrefix'; - -const requiredEnvVariables: Record = { - NEXT_PUBLIC_SUPABASE_URL: 'required', - NEXT_PUBLIC_SUPABASE_ANON_KEY: 'required', - SUPABASE_URL: 'required', - SUPABASE_ANON_KEY: 'required', - SUPABASE_JWT_SECRET: 'required', - SUPABASE_SERVICE_ROLE_KEY: 'required', - // DATABASE_URI:"required", // this is created by Payload in web app directory - // PAYLOAD_SECRET:"required", // this is created by Payload in web app directory -}; - -// Function to create .env file with empty fields -export const createEnvFile = (destinationDirectory: string) => { - logWithColoredPrefix('stapler', 'Creating .env file...'); - let envTemplate = ''; - for (const [key, status] of Object.entries(requiredEnvVariables)) { - envTemplate += `${key}=\n`; - } - - if (destinationDirectory) { - fs.writeFileSync(path.resolve(destinationDirectory, '.env'), envTemplate); - } else { - throw new Error(`Directory does not exist: ${destinationDirectory}`); - } -}; diff --git a/packages/core/installMachine/installSteps/supabase/connectProject.ts b/packages/core/installMachine/installSteps/supabase/connectProject.ts index f5943e6..af093eb 100644 --- a/packages/core/installMachine/installSteps/supabase/connectProject.ts +++ b/packages/core/installMachine/installSteps/supabase/connectProject.ts @@ -3,7 +3,6 @@ import inquirer from 'inquirer'; import { promisify } from 'util'; import chalk from 'chalk'; import { continueOnAnyKeypress } from '../../../utils/continueOnKeypress'; -import { updateEnvFile } from '../../../utils/updateEnvFile'; import { getSupabaseKeys, parseProjectsList } from './utils'; import { logWithColoredPrefix } from '../../../utils/logWithColoredPrefix'; @@ -33,18 +32,6 @@ export const connectSupabaseProject = async (projectName: string, currentDir: st throw new Error('Failed to retrieve Supabase API keys. Please check your project configuration.'); } - const SUPABASE_URL = `https://${newProject.refId}.supabase.co/`; - - logWithColoredPrefix('supabase', `Saving keys to .env...`); - await updateEnvFile({ - currentDir, - pairs: [ - ['SUPABASE_ANON_KEY', anonKey], - ['SUPABASE_SERVICE_ROLE_KEY', serviceRoleKey], - ['SUPABASE_URL', SUPABASE_URL], - ], - }); - logWithColoredPrefix('supabase', 'Linking project...'); execSync(`npx supabase link --project-ref ${newProject.refId}`, { stdio: 'inherit' }); diff --git a/packages/core/types.ts b/packages/core/types.ts index 2b2d834..745e947 100644 --- a/packages/core/types.ts +++ b/packages/core/types.ts @@ -5,7 +5,6 @@ export interface ProjectOptions { export interface StepsCompleted { initializeProject: boolean; - createEnvFile: boolean; installSupabase: boolean; setupDatabaseWithDocker: boolean; installPayload: boolean; diff --git a/packages/core/utils/rcFileManager/index.ts b/packages/core/utils/rcFileManager/index.ts index e9076d1..a8d57cb 100644 --- a/packages/core/utils/rcFileManager/index.ts +++ b/packages/core/utils/rcFileManager/index.ts @@ -16,9 +16,9 @@ export const initializeRcFile = (projectDir: string, name: string, usePayload: b projectName: name, stepsCompleted: { initializeProject: false, - createEnvFile: false, installPayload: false, installSupabase: false, + setupDatabaseWithDocker: false, prettifyCode: false, createDocFiles: false, createSupabaseProject: false, diff --git a/packages/core/utils/updateEnvFile.ts b/packages/core/utils/updateEnvFile.ts deleted file mode 100644 index 19e6947..0000000 --- a/packages/core/utils/updateEnvFile.ts +++ /dev/null @@ -1,51 +0,0 @@ -import * as fs from 'fs'; -import * as path from 'path'; -import * as util from 'util'; - -const readFile = util.promisify(fs.readFile); -const writeFile = util.promisify(fs.writeFile); -const access = util.promisify(fs.access); - -type EnvPair = [string, string]; - -interface EnvUpdateConfig { - currentDir: string; - pairs: EnvPair[]; -} - -// Function to update a single line -const updateLine = (line: string, key: string, value: string): string => { - if (line.startsWith(`${key}=`)) { - return `${key}=${value}`; - } - return line; -}; - -export const updateEnvFile = async ({ currentDir, pairs }: EnvUpdateConfig): Promise => { - const envFilePath = path.join(currentDir, '.env'); - try { - // Check if the .env file exists - try { - await access(envFilePath); // Check if file exists - } catch { - // If it doesn't exist, create it with default values - await writeFile(envFilePath, '', 'utf8'); - } - - const data = await readFile(envFilePath, 'utf8'); - let lines = data.split('\n'); - - lines = lines.map((line) => { - for (const [key, value] of pairs) { - line = updateLine(line, key, value); - } - return line; - }); - - const updatedContent = lines.join('\n'); - await writeFile(envFilePath, updatedContent, 'utf8'); - } catch (error) { - console.error('Error updating .env file:', error); - throw error; // Re-throw the error for the caller to handle - } -};