Skip to content

Commit

Permalink
Merge branch 'main' of github.com:tonik/stapler
Browse files Browse the repository at this point in the history
  • Loading branch information
maneike committed Oct 10, 2024
2 parents 4dc599c + 6912f20 commit dce291b
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 270 deletions.
39 changes: 18 additions & 21 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
import { Command } from "commander";
import inquirer from "inquirer";
import chalk from "chalk";
import { createProject } from "@create-stapler-app/core";
import { Command } from 'commander';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { createProject } from '@create-stapler-app/core';

const asciiArt = `
.&&&% &&&&
Expand All @@ -17,32 +17,32 @@ const asciiArt = `
`;

function displayHeader() {
console.log(chalk.hex("#3100F5").bold(asciiArt));
console.log(chalk.bold("\n๐Ÿธ Welcome to Stapler!\n"));
console.log(chalk.hex('#3100F5').bold(asciiArt));
console.log(chalk.bold('\n๐Ÿธ Welcome to Stapler!\n'));
}

const program = new Command();

program
.name("create-stapler-app")
.description("CLI to bootstrap a new tonik-infused app")
.version("0.1.0")
.hook("preAction", () => {
.name('create-stapler-app')
.description('CLI to bootstrap a new tonik-infused app')
.version('0.1.0')
.hook('preAction', () => {
displayHeader();
});

const createAction = async () => {
const answers = await inquirer.prompt([
{
type: "input",
name: "name",
message: "What is your project named?",
default: "my-stapled-app",
type: 'input',
name: 'name',
message: 'What is your project named?',
default: 'my-stapled-app',
},
{
type: "confirm",
name: "usePayload",
message: "Would you like to add Payload to your app?",
type: 'confirm',
name: 'usePayload',
message: 'Would you like to add Payload to your app?',
default: true,
},
// we dont support Inngest yet
Expand All @@ -57,10 +57,7 @@ const createAction = async () => {
await createProject(answers);
};

program
.command("create")
.description("Create a new tonik-infused app")
.action(createAction);
program.command('create').description('Create a new tonik-infused app').action(createAction);

// Set "create" as the default command
program.action(createAction);
Expand Down
14 changes: 7 additions & 7 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { execSync } from "child_process";
import { createEnvFile } from "./utils/env/createEnvFile";
import { preparePayload } from "./utils/payload/install";
import { installSupabase } from "./utils/supabase/install";
import { prettify } from "./utils/prettier/prettify";
import { prepareDrink } from "./utils/bar/prepareDrink";
import { execSync } from 'child_process';
import { createEnvFile } from './utils/env/createEnvFile';
import { preparePayload } from './utils/payload/install';
import { installSupabase } from './utils/supabase/install';
import { prettify } from './utils/prettier/prettify';
import { prepareDrink } from './utils/bar/prepareDrink';

interface ProjectOptions {
name: string;
Expand All @@ -16,7 +16,7 @@ export async function createProject(options: ProjectOptions) {

console.log(`๐Ÿธ Stapling ${name}...`);
execSync(`npx create-turbo@latest ${name} -m pnpm`, {
stdio: "inherit",
stdio: 'inherit',
});

process.chdir(name);
Expand Down
9 changes: 3 additions & 6 deletions packages/core/templates/supabase/files/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { createBrowserClient } from "@supabase/ssr";
import { createBrowserClient } from '@supabase/ssr';

import type { Database } from "./types";
import type { Database } from './types';

interface CreateClientOptions {
supabaseUrl: string;
supabaseAnonKey: string;
}

export const createClient = ({
supabaseUrl,
supabaseAnonKey,
}: CreateClientOptions) =>
export const createClient = ({ supabaseUrl, supabaseAnonKey }: CreateClientOptions) =>
createBrowserClient<Database>(supabaseUrl, supabaseAnonKey);
6 changes: 3 additions & 3 deletions packages/core/templates/supabase/files/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type * as supabase from "@supabase/supabase-js";
import type * as supabase from '@supabase/supabase-js';

import type { Database } from "./types";
import type { Database } from './types';

export type { CookieStore } from "./types";
export type { CookieStore } from './types';
export type Session = supabase.Session;
export type SupabaseClient = supabase.SupabaseClient<Database>;
export type User = supabase.User;
Expand Down
81 changes: 37 additions & 44 deletions packages/core/templates/supabase/files/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,55 @@
import type { CookieOptions } from "@supabase/ssr";
import type { NextRequest } from "next/server";
import { createServerClient } from "@supabase/ssr";
import type { CookieOptions } from '@supabase/ssr';
import type { NextRequest } from 'next/server';
import { createServerClient } from '@supabase/ssr';

import type { Database } from "./types";
import type { Database } from './types';

interface CreateMiddlewareOptions {
supabaseUrl: string;
supabaseAnonKey: string;
}

export const createMiddleware = ({
supabaseAnonKey,
supabaseUrl,
}: CreateMiddlewareOptions) =>
export const createMiddleware = ({ supabaseAnonKey, supabaseUrl }: CreateMiddlewareOptions) =>
async function middleware(request: NextRequest) {
const cookiesToSet: {
name: string;
value: string;
}[] = [];

const supabase = createServerClient<Database>(
supabaseUrl,
supabaseAnonKey,
{
cookies: {
get(name: string) {
const cookieValue = request.cookies.get(name)?.value;
return cookieValue;
},
set(name: string, value: string, options: CookieOptions) {
request.cookies.set({
name,
value,
...options,
});

cookiesToSet.push({
name,
value,
...options,
});
},
remove(name: string, options: CookieOptions) {
request.cookies.set({
name,
value: "",
...options,
});

cookiesToSet.push({
name,
value: "",
...options,
});
},
const supabase = createServerClient<Database>(supabaseUrl, supabaseAnonKey, {
cookies: {
get(name: string) {
const cookieValue = request.cookies.get(name)?.value;
return cookieValue;
},
set(name: string, value: string, options: CookieOptions) {
request.cookies.set({
name,
value,
...options,
});

cookiesToSet.push({
name,
value,
...options,
});
},
remove(name: string, options: CookieOptions) {
request.cookies.set({
name,
value: '',
...options,
});

cookiesToSet.push({
name,
value: '',
...options,
});
},
},
);
});

const { data } = await supabase.auth.getUser();

Expand Down
11 changes: 4 additions & 7 deletions packages/core/templates/supabase/files/next_api_endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { supabase } from "../../lib/supabase";
import type { NextApiRequest, NextApiResponse } from 'next';
import { supabase } from '../../lib/supabase';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const { data, error } = await supabase.from("your_table").select("*");
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { data, error } = await supabase.from('your_table').select('*');

if (error) {
return res.status(500).json({ error: error.message });
Expand Down
58 changes: 27 additions & 31 deletions packages/core/templates/supabase/files/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CookieOptions } from "@supabase/ssr";
import { createServerClient } from "@supabase/ssr";
import type { CookieOptions } from '@supabase/ssr';
import { createServerClient } from '@supabase/ssr';

import type { CookieStore, Database } from "./types";
import type { CookieStore, Database } from './types';

interface BaseOptions {
supabaseApiUrl: string;
Expand All @@ -14,34 +14,30 @@ interface BaseOptions {
}

export function createClient(configOptions: BaseOptions) {
return createServerClient<Database>(
configOptions.supabaseApiUrl,
configOptions.supabaseKey,
{
cookies: {
get(name: string) {
return configOptions.cookieStore?.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
try {
configOptions.cookieStore?.set({ name, value, ...options });
} catch (error) {
// The `set` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
remove(name: string, options: CookieOptions) {
try {
configOptions.cookieStore?.set({ name, value: "", ...options });
} catch (error) {
// The `set` method was called from a Server Component.
// The `delete` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
return createServerClient<Database>(configOptions.supabaseApiUrl, configOptions.supabaseKey, {
cookies: {
get(name: string) {
return configOptions.cookieStore?.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
try {
configOptions.cookieStore?.set({ name, value, ...options });
} catch (error) {
// The `set` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
remove(name: string, options: CookieOptions) {
try {
configOptions.cookieStore?.set({ name, value: '', ...options });
} catch (error) {
// The `set` method was called from a Server Component.
// The `delete` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
);
});
}
2 changes: 1 addition & 1 deletion packages/core/templates/supabase/files/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type * from "./types.gen";
export type * from './types.gen';
export interface CookieStore {
get(name: string): { value: string } | undefined;
set(options: { name: string; value: string }): void;
Expand Down
12 changes: 6 additions & 6 deletions packages/core/templates/supabase/installConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export const supabaseFiles = [
{
path: "supabase/src/",
files: ["client.ts", "index.ts", "middleware.ts", "server.ts", "types.ts"],
path: 'supabase/src/',
files: ['client.ts', 'index.ts', 'middleware.ts', 'server.ts', 'types.ts'],
},
{
path: "supabase/",
files: ["package"],
path: 'supabase/',
files: ['package'],
rename: [
{
from: "package",
to: "package.json",
from: 'package',
to: 'package.json',
},
],
},
Expand Down
6 changes: 1 addition & 5 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
"declaration": true, // Ensure TypeScript generates declaration files
"emitDeclarationOnly": true // Only emit declaration files without compiling the code
},
"include": [
"index.ts",
"utils/**/*.ts",
"templates/supabase/installConfig.ts"
],
"include": ["index.ts", "utils/**/*.ts", "templates/supabase/installConfig.ts"],
"exclude": ["node_modules", "dist", "templates/supabase/files/**/*"]
}
21 changes: 7 additions & 14 deletions packages/core/utils/bar/prepareDrink.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
const getName = (name: string) => {
if (!name) {
return ".";
return '.';
}

return name;
};

export const prepareDrink = (name: string) => {
setTimeout(() => {
console.log("๐Ÿธ Filling a high ball glass with ice...");
console.log('๐Ÿธ Filling a high ball glass with ice...');
setTimeout(() => {
console.log("๐Ÿธ Adding gin and lime juice...");
console.log('๐Ÿธ Adding gin and lime juice...');
setTimeout(() => {
console.log("๐Ÿธ Topping with", "\x1b[34mTonik\x1b[0m...");
console.log('๐Ÿธ Topping with', '\x1b[34mTonik\x1b[0m...');
setTimeout(() => {
console.log("๐Ÿธ Garnishing with lime wedge...");
console.log('๐Ÿธ Garnishing with lime wedge...');
setTimeout(() => {
console.log(
`\x1b[32m%s\x1b[0m`,
`๐Ÿธ Your Stapled ${getName(name)} is ready!`,
`\x1b[0m`,
);
console.log(`\x1b[32m%s\x1b[0m`, `๐Ÿธ Your Stapled ${getName(name)} is ready!`, `\x1b[0m`);
// I'm too lazy to mess with modules building to allow coloring library to be installed lol
console.log(
`๐Ÿธ You can now run:`,
`\x1b[36mcd ${name} && pnpm dev\x1b[0m`,
);
console.log(`๐Ÿธ You can now run:`, `\x1b[36mcd ${name} && pnpm dev\x1b[0m`);
}, 1000);
}, 1000);
}, 1000);
Expand Down
Loading

0 comments on commit dce291b

Please sign in to comment.