Skip to content

Commit

Permalink
feat: move supabase, vercel, gh cli and login check (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Chełminiak <[email protected]>
  • Loading branch information
KarolinaKopacz and maneike authored Dec 6, 2024
1 parent c5c6c18 commit bde39ff
Show file tree
Hide file tree
Showing 49 changed files with 318 additions and 140 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"main": "index.js",
"dependencies": {
"chalk": "^5.3.0",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.6",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -51,6 +52,7 @@
"eslint-plugin-unicorn": "^48.0.1",
"eslint-scope": "^7.2.2",
"eslint-visitor-keys": "^3.4.3",
"gradient-string": "^3.0.0",
"prettier-plugin-packagejson": "^2.4.6"
},
"keywords": [],
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import chalk from 'chalk';
import { Command } from 'commander';
import gradient from 'gradient-string';
import inquirer from 'inquirer';
import { findUnfinishedProjects, UnfinishedProject } from './utils/findUnfinishedProjects';
import { createProject } from 'stplr-core';
import { checkAuthentication } from './utils/checkAuthentication';
import { checkTools } from './utils/checkTools';
import { findUnfinishedProjects, UnfinishedProject } from './utils/findUnfinishedProjects';

const asciiArt = `
.&&&% &&&&
Expand Down Expand Up @@ -146,6 +148,9 @@ const createAction = async (options: Flags) => {

const finalOptions = { name: projectName, ...payloadAnswer };

await checkAuthentication();
await checkTools();

await createProject(finalOptions, projectDir).catch((error) => {
console.error(chalk.red('Error creating project:', error));
});
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"stplr-core": "workspace:*",
"stplr-utils": "workspace:*",
"@types/inquirer": "^9.0.7",
"@types/node": "^22.5.4",
"tsup": "^8.2.4",
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/utils/checkAuthentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { authenticateGitHub } from "stplr-core/installMachine/installSteps/github/repositoryManager";
import { authenticateSupabase } from 'stplr-core/installMachine/installSteps/supabase/authenticateSupabase';
import { authenticateVercel } from 'stplr-core/installMachine/installSteps/vercel/authenticateVercel';

export const checkAuthentication = async () => {
await authenticateGitHub();
await authenticateSupabase();
await authenticateVercel();
};
9 changes: 9 additions & 0 deletions packages/cli/utils/checkTools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { checkGitHubCLI } from 'stplr-core/installMachine/installSteps/github/checkGitHubCLI';
import { checkSupabaseCLI } from 'stplr-core/installMachine/installSteps/supabase/checkSupabaseCLI';
import { checkVercelCLI } from 'stplr-core/installMachine/installSteps/vercel/checkVercelCLI';

export const checkTools = async () => {
await checkGitHubCLI();
await checkSupabaseCLI();
await checkVercelCLI();
};
31 changes: 15 additions & 16 deletions packages/core/installMachine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import { ActorLogic, AnyEventObject, PromiseSnapshot, and, createActor, createMa

import { InstallMachineContext, StepsCompleted } from '../types';
import { saveStateToRcFile } from '../utils/rcFileManager';
import { prepareDrink } from './installSteps/stapler/prepareDrink';
import { createDocFiles } from './installSteps/stapler/createDocFiles';
import { initializeRepository } from './installSteps/github/install';
import { pushToGitHub } from './installSteps/github/repositoryManager';
import { modifyHomepage } from './installSteps/homepage/install';
import { preparePayload } from './installSteps/payload/install';
import { prettify } from './installSteps/prettier/prettify';
import { createDocFiles } from './installSteps/stapler/createDocFiles';
import { modifyGitignore } from './installSteps/stapler/modifyGitignore';
import { prepareDrink } from './installSteps/stapler/prepareDrink';
import { connectSupabaseProject } from './installSteps/supabase/connectProject';
import { createSupabaseProject } from './installSteps/supabase/createProject';
import { installSupabase } from './installSteps/supabase/install';
import { installTailwind } from './installSteps/tailwind/install';
import { createTurboRepo } from './installSteps/turbo/create';
import { createTurbo } from './installSteps/turbo/install';
import { chooseVercelTeam } from './installSteps/vercel/chooseTeam';
import { deployVercelProject } from './installSteps/vercel/deploy';
import { linkVercelProject } from './installSteps/vercel/link';
import { updateVercelProjectSettings } from './installSteps/vercel/updateProjectSettings';
import { chooseVercelTeam } from './installSteps/vercel/chooseTeam';
import { modifyGitignore } from './installSteps/stapler/modifyGitignore';

const isStepCompleted = (stepName: keyof StepsCompleted) => {
return ({ context }: { context: InstallMachineContext; event: AnyEventObject }) => {
Expand Down Expand Up @@ -55,18 +55,18 @@ const createInstallMachine = (initialContext: InstallMachineContext) => {
const installMachine = createMachine(
{
id: 'installProcess',
initial: 'initializeProject',
initial: 'createTurbo',
context: initialContext,
states: {
initializeProject: {
createTurbo: {
always: [
{
guard: isStepCompleted('initializeProject'),
guard: isStepCompleted('createTurbo'),
target: 'modifyGitignore',
},
],
invoke: {
src: 'initializeProjectActor',
src: 'createTurboActor',
input: ({ context }) => context,
onDone: 'modifyGitignore',
onError: 'failed',
Expand Down Expand Up @@ -329,24 +329,23 @@ const createInstallMachine = (initialContext: InstallMachineContext) => {
},
},
actors: {
initializeProjectActor: createStepMachine(
createTurboActor: createStepMachine(
fromPromise<void, InstallMachineContext, AnyEventObject>(async ({ input }) => {
try {
const { name } = input.stateData.options;
await createTurboRepo(name);
process.chdir(name);
input.stateData.stepsCompleted.initializeProject = true;
await createTurbo(input.stateData.options.name);
process.chdir(input.projectDir);
input.stateData.stepsCompleted.createTurbo = true;
saveStateToRcFile(input.stateData, input.projectDir);
} catch (error) {
console.error('Error in initializeProjectActor:', error);
console.error('Error in createTurboActor:', error);
throw error;
}
}),
),
modifyGitignoreActor: createStepMachine(
fromPromise<void, InstallMachineContext, AnyEventObject>(async ({ input }) => {
try {
await modifyGitignore('.staplerrc');
await modifyGitignore('.initializeRcFile');
input.stateData.stepsCompleted.modifyGitignore = true;
saveStateToRcFile(input.stateData, input.projectDir);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inquirer from 'inquirer';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { isGitHubCLIInstalled, installGitHubCLI } from './ghInstaller';

export const checkGitHubCLI = async () => {
Expand All @@ -22,14 +22,14 @@ export const checkGitHubCLI = async () => {
console.error('GitHub CLI installation failed. Exiting...');
process.exit(1);
}
spinner.succeed('GitHub CLI installed successfully.');
spinner.succeed('CLI: Ready');
} else {
spinner.fail('GitHub CLI is not installed. Please install GitHub CLI and try again.');
console.error('GitHub CLI is not installed. Please install GitHub CLI and try again.');
process.exit(1);
}
} else {
spinner.succeed('GitHub CLI is already installed.');
spinner.succeed('CLI: Ready');
}
});
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { logger } from '../../../utils/logger';
import { isGitHubAuthenticated, authenticateGitHub } from './repositoryManager';
import { logger } from 'stplr-utils';
import { authenticateGitHub, isGitHubAuthenticated } from './repositoryManager';

export const ensureGitHubAuthentication = async () => {
await logger.withSpinner('github', 'Checking authentication status...', async (spinner) => {
if (isGitHubAuthenticated()) {
spinner.succeed('You are already logged in.');
return; // Exit early if authenticated
spinner.succeed('Logged in');
return;
}

spinner.fail("It looks like you're not logged in...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execAsync } from '../../../utils/execAsync';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';

export const fetchOrganizations = async (): Promise<{ name: string; writable: boolean }[]> => {
return await logger.withSpinner('github', 'Fetching organizations you belong to...', async (spinner) => {
Expand Down
17 changes: 2 additions & 15 deletions packages/core/installMachine/installSteps/github/ghInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import * as os from 'os';
import { logger } from '../../../utils/logger';
import { execAsync } from '../../../utils/execAsync';

const getLinuxDistro = async (): Promise<string> => {
try {
const osRelease = execAsync('cat /etc/os-release').toString();
if (osRelease.includes('Ubuntu')) return 'ubuntu';
if (osRelease.includes('Debian')) return 'debian';
if (osRelease.includes('Fedora')) return 'fedora';
if (osRelease.includes('CentOS')) return 'centos';
if (osRelease.includes('Red Hat')) return 'rhel';
return 'unknown';
} catch (error) {
return 'unknown';
}
};
import { getLinuxDistro } from '../../../utils/getLinuxDistro';
import { logger } from 'stplr-utils';

export const isGitHubCLIInstalled = async (): Promise<boolean> => {
try {
Expand Down
7 changes: 1 addition & 6 deletions packages/core/installMachine/installSteps/github/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { checkGitHubCLI } from './checkGitHubCLI';
import { ensureGitHubAuthentication } from './ensureGitHubAuthentication';
import { createGitHubRepository, fetchGitHubUsername, setupGitRepository } from './repositoryManager';
import { InstallMachineContext } from '../../../types';
import { createGitHubRepository, fetchGitHubUsername, setupGitRepository } from './repositoryManager';

interface ProjectRepositoryOptions {
projectName: string;
Expand All @@ -11,9 +9,6 @@ interface ProjectRepositoryOptions {
export const initializeRepository = async (options: ProjectRepositoryOptions) => {
const { projectName, stateData } = options;

await checkGitHubCLI();
await ensureGitHubAuthentication();

const username = await fetchGitHubUsername();

await createGitHubRepository(projectName, username, stateData);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from 'child_process';
import { execSync, spawnSync } from 'child_process';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { execAsync } from '../../../utils/execAsync';
import { InstallMachineContext } from '../../../types';
import { fetchOrganizations } from './fetchOrganizations';
Expand Down Expand Up @@ -40,17 +40,26 @@ export const isGitHubAuthenticated = (): boolean => {
export const authenticateGitHub = async () => {
await logger.withSpinner('github', 'Attempting to authenticate...', async (spinner) => {
try {
spinner.stop();

execSync('gh auth login', { stdio: 'inherit' });

spinner.start('Authenticating...');
const isAuthenticated = isGitHubAuthenticated();

if (isAuthenticated) {
spinner.succeed('Authentication successful.');
spinner.succeed('Logged in');
return true;
}

spinner.stop();

try {
const result = spawnSync('gh', ['auth', 'login'], {
stdio: 'inherit',
shell: true,
});
spinner.succeed('Logged in');
return true;
} else {
spinner.fail('Authentication failed.');
} catch (error) {
spinner.fail('Logging in failed.');
console.error('Logging in failed:', error);
return false;
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { homepageFiles } from '../../../templates/homepage/installConfig';
import { templateGenerator } from '../../../utils/generator/generator';
import { getTemplateDirectory } from '../../../utils/getTemplateDirectory';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';

export const modifyHomepage = async (destinationDirectory: string) => {
await logger.withSpinner('tailwind', 'Setting up your welcome homepage...', async (spinner) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { execAsync } from '../../../utils/execAsync';

export const moveFilesToAppDir = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'fs';
import fs from 'fs/promises';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { join } from 'path';

export const preparePayloadConfig = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { promisify } from 'util';

const readFileAsync = promisify(fs.readFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { promisify } from 'util';

const readFileAsync = promisify(fs.readFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execAsync } from '../../../utils/execAsync';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { loadEnvFile } from './utils/loadEnvFile';

export const runInstallCommand = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { execAsync } from '../../../utils/execAsync';

export const updatePackages = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execSync } from 'child_process';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';

export const prettify = async () => {
await logger.withSpinner('prettier', 'Prettifying...', async (spinner) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { docFiles } from '../../../templates/docs/installConfig';
import { templateGenerator } from '../../../utils/generator/generator';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { getTemplateDirectory } from '../../../utils/getTemplateDirectory';

export const createDocFiles = async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { execAsync } from '../../../utils/execAsync';
import { logger } from 'stplr-utils';

export const initializeStapler = async (name: string) => {
await logger.withSpinner('stapler', 'Initializing...', async (spinner) => {
try {
await execAsync(`mkdir ${name}`);
spinner.succeed('Initialized.');
} catch (error) {
spinner.fail('Failed to initialize.');
console.error(error);
}
});
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';
import { promisify } from 'util';

const readFileAsync = promisify(fs.readFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import { supabaseFiles } from '../../../templates/supabase/installConfig';
import { templateGenerator } from '../../../utils/generator/generator';
import { getTemplateDirectory } from '../../../utils/getTemplateDirectory';
import { logger } from '../../../utils/logger';
import { logger } from 'stplr-utils';

export const addTemplateFiles = async (destinationDirectory: string) => {
await logger.withSpinner('supabase', 'Adding files from template...', async (spinner) => {
Expand Down
Loading

0 comments on commit bde39ff

Please sign in to comment.