Skip to content

Commit

Permalink
Update flow
Browse files Browse the repository at this point in the history
  • Loading branch information
usulpro committed May 30, 2024
1 parent 4e8ae2c commit 814e0f2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.initial
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MAX_NUMBER_OF_PROJECTS=7
REPO_PROD_BRANCH=main
REPO_ID=807949953
REPO_NAME=usulpro/test6
2 changes: 2 additions & 0 deletions .github/workflows/new-repo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
echo "REPO_ID=${{ github.event.repository.id }}" > .env.initial
echo "Repository Name: ${{ github.repository }}"
echo "REPO_NAME=${{ github.repository }}" >> .env.initial
echo "MAX_NUMBER_OF_PROJECTS=7" >> .env.initial
echo "REPO_PROD_BRANCH=main" >> .env.initial
- name: Commit and Push .env.initial
env:
Expand Down
22 changes: 1 addition & 21 deletions src/rollout-tools/local/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { localRollout, checkEnvVariables } from './localRollout.mjs';
import { loadEnvVariables } from './loadEnvVariables.mjs';
import { appendOrUpdateEnv, loadEnvVariables } from './loadEnvVariables.mjs';
import {
fetchSanityOrganizations,
fetchSanityUserInfo,
Expand All @@ -11,26 +11,6 @@ import inquirer from 'inquirer';
import fs from 'fs';
import crypto from 'crypto';

/// Helper function to append or update to .env file
const appendOrUpdateEnv = (key, value) => {
const envFilePath = '.env';
const envContent = fs.existsSync(envFilePath)
? fs.readFileSync(envFilePath, 'utf8')
: '';
const envLines = envContent.split('\n').filter((line) => line.trim() !== ''); // Remove empty lines

const existingIndex = envLines.findIndex((line) =>
line.startsWith(`${key}=`),
);
if (existingIndex >= 0) {
envLines[existingIndex] = `${key}=${value}`;
} else {
envLines.push(`${key}=${value}`);
}

fs.writeFileSync(envFilePath, envLines.join('\n') + '\n');
};

// Helper function to copy variables from .env.initial to .env
const copyInitialEnvVariables = () => {
const initialEnvFilePath = '.env.initial';
Expand Down
20 changes: 20 additions & 0 deletions src/rollout-tools/local/loadEnvVariables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ export function loadEnvVariables(envFilePath = '.env') {

return envVars;
}

/// Helper function to append or update to .env file
export const appendOrUpdateEnv = (key, value) => {
const envFilePath = '.env';
const envContent = fs.existsSync(envFilePath)
? fs.readFileSync(envFilePath, 'utf8')
: '';
const envLines = envContent.split('\n').filter((line) => line.trim() !== ''); // Remove empty lines

const existingIndex = envLines.findIndex((line) =>
line.startsWith(`${key}=`),
);
if (existingIndex >= 0) {
envLines[existingIndex] = `${key}=${value}`;
} else {
envLines.push(`${key}=${value}`);
}

fs.writeFileSync(envFilePath, envLines.join('\n') + '\n');
};
29 changes: 24 additions & 5 deletions src/rollout-tools/local/localRollout.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import ora from 'ora';
import inquirer from 'inquirer';
import {
createSanityProject,
createVercelProject,
getVercelProjects,
createSanityReadToken,
} from './services.mjs';
import { isValidEmail } from './email.mjs';
import { loadEnvVariables } from './loadEnvVariables.mjs';
import { loadEnvVariables, appendOrUpdateEnv } from './loadEnvVariables.mjs';
import { localFlow } from './localFlow.mjs';

export function checkEnvVariables(envVars) {
loadEnvVariables();
envVars.forEach((envVar) => {
if (!process.env[envVar]) {
const spinner = ora(`Missing environment variable: ${envVar}`).fail();
ora(`Missing environment variable: ${envVar}`).fail();
process.exit(1);
}
});
Expand All @@ -31,6 +32,22 @@ export async function localRollout({ inputs, secrets }) {
.slice(0, 90); // prevent project name from being too long
const finalProjectName = `${username}-${projectName}`;

// Check if NEXT_PUBLIC_SANITY_PROJECT_ID is already set
const existingSanityProjectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
if (existingSanityProjectId) {
const { createNewProject } = await inquirer.prompt({
type: 'confirm',
name: 'createNewProject',
message:
'A Sanity project is already created. Do you want to create a new project?',
default: false,
});
if (!createNewProject) {
ora('Using existing Sanity project ID.').info();
return;
}
}

const existingProjectsSpinner = ora(
'Fetching existing Vercel projects...',
).start();
Expand All @@ -47,9 +64,12 @@ export async function localRollout({ inputs, secrets }) {
if (allowToCreateProject && !existingProject) {
const sanityProjectSpinner = ora('Creating Sanity project...').start();
const sanityProjectId = await createSanityProject(finalProjectName);
sanityProjectSpinner.succeed('Sanity project created.');

if (sanityProjectId) {
// Save new Sanity project ID to .env and update environment
appendOrUpdateEnv('NEXT_PUBLIC_SANITY_PROJECT_ID', sanityProjectId);
process.env.NEXT_PUBLIC_SANITY_PROJECT_ID = sanityProjectId;
sanityProjectSpinner.succeed('Sanity project created.');

const sanityReadTokenSpinner = ora(
'Creating Sanity read token...',
).start();
Expand All @@ -66,7 +86,6 @@ export async function localRollout({ inputs, secrets }) {
vercelProjectSpinner.succeed('Vercel project created.');

if (projectData) {
console.log('🚀 ~ localRollout ~ projectData:\n', projectData);
const localFlowSpinner = ora('Starting local flow...').start();
const result = await localFlow({
inputs: {
Expand Down

0 comments on commit 814e0f2

Please sign in to comment.