Skip to content

Commit

Permalink
feat: add vite init template (#67)
Browse files Browse the repository at this point in the history
* feat: add vite init template

* docs: update init command
  • Loading branch information
winchesHe authored May 26, 2024
1 parent 933b95e commit e1ffa81
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ nextui init [projectName] [options]
#### Init Options
- `-t --template [string]` The template to use for the new project e.g. app, pages
- `-t --template [string]` The template to use for the new project e.g. app, pages, vite
- `-p --package [string]` The package manager to use for the new project (default: `npm`)
##### Example
Expand All @@ -86,6 +86,7 @@ NextUI CLI v0.2.1
◇ Select a template (Enter to select)
│ ● App (A Next.js 14 with app directory template pre-configured with NextUI (v2) and Tailwind CSS.)
│ ○ Pages (A Next.js 14 with pages directory template pre-configured with NextUI (v2) and Tailwind CSS.)
│ ○ Vite (A Vite template pre-configured with NextUI (v2) and Tailwind CSS.)
◇ New project name (Enter to skip with default name)
│ my-nextui-app
Expand Down
56 changes: 41 additions & 15 deletions src/actions/init-action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Agent} from '@helpers/detect';
import type {GetUnionLastValue} from '@helpers/type';

import {existsSync, renameSync} from 'node:fs';

Expand All @@ -18,17 +19,21 @@ import {
APP_REPO,
PAGES_DIR,
PAGES_NAME,
PAGES_REPO
PAGES_REPO,
VITE_DIR,
VITE_NAME,
VITE_REPO
} from '../../src/constants/templates';

export interface InitActionOptions {
template?: 'app' | 'pages';
template?: 'app' | 'pages' | 'vite';
package?: 'npm' | 'yarn' | 'pnpm';
}

const templatesMap: Record<Required<InitActionOptions>['template'], string> = {
app: APP_NAME,
pages: PAGES_NAME
pages: PAGES_NAME,
vite: VITE_NAME
};

export async function initAction(_projectName: string, options: InitActionOptions) {
Expand Down Expand Up @@ -58,6 +63,9 @@ export async function initAction(_projectName: string, options: InitActionOption
} else if (template === 'pages') {
await generateTemplate(PAGES_REPO);
renameTemplate(PAGES_DIR, projectName);
} else if (template === 'vite') {
await generateTemplate(VITE_REPO);
renameTemplate(VITE_DIR, projectName);
}

/** ======================== Pnpm setup (optional) ======================== */
Expand Down Expand Up @@ -100,22 +108,40 @@ function renameTemplate(originName: string, projectName: string) {
}
}

export type GenerateOptions<T, Last = GetUnionLastValue<T>> = [T] extends [never]
? []
: [
...GenerateOptions<Exclude<T, Last>>,
{
label: string;
value: Last;
hint: string;
}
];

async function getTableInfo(packageName?: string, projectName?: string, template?: string) {
const options: GenerateOptions<Exclude<InitActionOptions['template'], undefined>> = [
{
hint: 'A Next.js 14 with app directory template pre-configured with NextUI (v2) and Tailwind CSS.',
label: chalk.gray('App'),
value: 'app'
},
{
hint: 'A Next.js 14 with pages directory template pre-configured with NextUI (v2) and Tailwind CSS.',
label: chalk.gray('Pages'),
value: 'pages'
},
{
hint: 'A Vite template pre-configured with NextUI (v2) and Tailwind CSS.',
label: chalk.gray('Vite'),
value: 'vite'
}
];

template = (await selectClack({
initialValue: template,
message: 'Select a template (Enter to select)',
options: [
{
hint: 'A Next.js 14 with app directory template pre-configured with NextUI (v2) and Tailwind CSS.',
label: chalk.gray('App'),
value: 'app'
},
{
hint: 'A Next.js 14 with pages directory template pre-configured with NextUI (v2) and Tailwind CSS.',
label: chalk.gray('Pages'),
value: 'pages'
}
]
options
})) as string;

projectName = (await textClack({
Expand Down
3 changes: 3 additions & 0 deletions src/constants/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import type {CheckType} from '@helpers/check';

export const APP_REPO = 'https://codeload.github.com/nextui-org/next-app-template/tar.gz/main';
export const PAGES_REPO = 'https://codeload.github.com/nextui-org/next-pages-template/tar.gz/main';
export const VITE_REPO = 'https://codeload.github.com/nextui-org/vite-template/tar.gz/main';

export const APP_DIR = 'next-app-template-main';
export const PAGES_DIR = 'next-pages-template-main';
export const VITE_DIR = 'vite-template-main';

export const APP_NAME = 'next-app-template';
export const PAGES_NAME = 'next-pages-template';
export const VITE_NAME = 'vite-template';
export const DEFAULT_PROJECT_NAME = 'nextui-app';

export function tailwindTemplate(type: 'all', content?: string): string;
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@ export type ExtractStoreData<T extends StoreKeys> = T extends 'latestVersion' |
: T extends 'nextUIComponentsPackageMap'
? NextUIComponentsMap
: never;

/**
* @example UnionToIntersection<{ foo: string } | { bar: string }> --> { foo: string } & { bar: string }
*/
export type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends (
arg: infer I
) => any
? I
: never;

/**
* @example GetUnionLastValue<0 | 1 | 2> --> 2
*/
export type GetUnionLastValue<T> = UnionToIntersection<
T extends any ? () => T : never
> extends () => infer R
? R
: never;

0 comments on commit e1ffa81

Please sign in to comment.