Skip to content

Commit

Permalink
feat(themes): inplace init (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Eihab Khan <143792300+eihabkhan1@users.noreply.github.com>
  • Loading branch information
azuradara and eihabkhan authored Aug 13, 2024
1 parent 4284187 commit 437a324
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions packages/theme/lib/cli/commands/theme/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class Init extends ThemeCommand {
description: 'The Git URL to clone from',
default: 'https://github.com/youcan-shop/dusk',
}),
inplace: Flags.boolean({
char: 'i',
default: false,
env: 'YC_FLAG_INPLACE',
description: 'Initialize the current directory as a dev theme instead of cloning',
}),
};

async run(): Promise<any> {
Expand All @@ -32,7 +38,7 @@ class Init extends ThemeCommand {
const session = await Session.authenticate(this);

const answers = await (prompt(this));
const dest = Path.join(flags.path, answers.theme_name);
const dest = Path.join(flags.path, flags.inplace ? '' : answers.theme_name);

await Tasks.run(
{
Expand All @@ -42,24 +48,33 @@ class Init extends ThemeCommand {
}, [
{
title: `Cloning ${flags.url} into ${dest}...`,
skip: () => flags.inplace,
task: async () => {
await Git.clone({ url: flags.url, destination: dest });
},
},
{
title: 'Initializing development theme...',
task: async (ctx) => {
const path = await Filesystem.archived(
Path.resolve(Path.cwd(), answers.theme_name),
answers.theme_name,
const path = await Filesystem.archived(dest, answers.theme_name);

const configPath = Path.join(
Path.cwd(),
THEME_CONFIG_FILENAME,
);

if (flags.inplace && await Filesystem.exists(configPath)) {
throw new Error(`
This directory is already linked to a remote theme,
please delete youcan.app.json if you wish to create a new one
`);
}

Object.assign(ctx.payload, { archive: await Form.file(path) });

const form = Form.convert(ctx.payload);
const res = await Http.post<{ id: string }>(`${Env.apiHostname()}/themes/init`, {
headers: { Authorization: `Bearer ${session.access_token}` },
body: form,
body: Form.convert(ctx.payload),
});

ctx.theme_id = res.id;
Expand All @@ -71,7 +86,7 @@ class Init extends ThemeCommand {
title: 'Cleaning up...',
task: async (ctx) => {
await Filesystem.writeJsonFile(
Path.join(Path.cwd(), ctx.payload.theme_name, THEME_CONFIG_FILENAME),
Path.join(dest, THEME_CONFIG_FILENAME),
{ theme_id: ctx.theme_id as string },
);

Expand Down

0 comments on commit 437a324

Please sign in to comment.