Skip to content

Commit

Permalink
feat: now you can install tailwind and make sure the config exist an
Browse files Browse the repository at this point in the history
return it
  • Loading branch information
wildduck2 committed Oct 25, 2024
1 parent cf822aa commit 96ecb9e
Show file tree
Hide file tree
Showing 41 changed files with 680 additions and 231 deletions.
1 change: 1 addition & 0 deletions packages/cli-t/dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/usr/bin/env node
4 changes: 4 additions & 0 deletions packages/cli-t/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli-t/dist/index.js.map

Large diffs are not rendered by default.

101 changes: 75 additions & 26 deletions packages/cli-t/package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,77 @@
{
"name": "duck-ui",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/fs-extra": "^11.0.1",
"@types/node": "^17.0.45",
"chalk": "5.2.0",
"commander": "^10.0.0",
"cosmiconfig": "^8.1.3",
"fast-glob": "^3.3.2",
"fs-extra": "^11.1.0",
"globals": "^15.9.0",
"kleur": "^4.1.5",
"log-symbols": "^7.0.0",
"ora": "^6.1.2",
"tsconfig-paths": "^4.2.0",
"type-fest": "^3.8.0",
"zod": "^3.23.8"
}
"name": "duck-ui",
"version": "0.0.2",
"description": "Add components to your apps.",
"publishConfig": {
"access": "public"
},
"license": "MIT",
"author": "wildduck (https://github.com/wildduck2)",
"repository": {
"type": "git",
"url": "git+https://github.com/gentelduck/ui.git",
"directory": "packages/cli"
},
"files": [
"dist"
],
"keywords": [
"components",
"ui",
"tailwind",
"radix-ui",
"duck-ui"
],
"type": "module",
"exports": "./dist/index.js",
"bin": {
"duck-ui": "dist/index.js"
},
"scripts": {
"dev": "tsup --watch",
"build": "tsup",
"typecheck": "tsc --noEmit",
"clean": "rimraf dist",
"start:dev": "cross-env COMPONENTS_REGISTRY_URL=http://localhost:3003 node dist/index.js",
"start": "node dist/index.js",
"format:write": "prettier --write \"**/*.{ts,tsx,mdx}\" --cache",
"format:check": "prettier --check \"**/*.{ts,tsx,mdx}\" --cache",
"release": "changeset version",
"test": "vitest run"
},
"dependencies": {
"@antfu/ni": "^0.21.12",
"chalk": "5.2.0",
"commander": "^10.0.1",
"cosmiconfig": "^8.3.6",
"execa": "^9.4.1",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"globals": "^15.11.0",
"kleur": "^4.1.5",
"log-symbols": "^7.0.0",
"ora": "^6.3.1",
"prompts": "^2.4.2",
"tsconfig-paths": "^4.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/babel__core": "^7.20.1",
"@types/diff": "^5.0.3",
"@types/fs-extra": "^11.0.1",
"@types/lodash.template": "^4.5.1",
"@types/prompts": "^2.4.2",
"rimraf": "^4.1.3",
"tsup": "^6.6.3",
"type-fest": "^3.8.0",
"typescript": "^4.9.3"
},
"bugs": {
"url": "https://github.com/gentelduck/ui/issues"
},
"homepage": "https://github.com/gentelduck/ui#readme",
"main": "index.js",
"directories": {
"test": "test"
}
}
1 change: 1 addition & 0 deletions packages/cli-t/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './init'
export * from './init.constants'
export * from './init.lib'
export * from './init.dto'
export * from './init.types'
49 changes: 34 additions & 15 deletions packages/cli-t/src/commands/init/init.constants.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
export const init_command_config = {
import { InitCommandConfig } from './init.types'

export const init_command_config: InitCommandConfig = {
name: 'init',
description: 'init the project',
option_1: {
flags: '-y, --yes',
description: 'skip confirmation prompt.',
defaultValue: false
},
option_2: {
flags: '-d, --defaults,',
description: 'use default configuration.',
defaultValue: false
},
option_3: {
flags: '-c, --cwd <cwd>',
description: 'the working directory. defaults to the current directory.',
defaultValue: process.cwd()
options: {
option_1: {
flags: '-y, --yes',
description: 'skip confirmation prompt.',
defaultValue: false
},
option_2: {
flags: '-d, --defaults,',
description: 'use default configuration.',
defaultValue: false
},
option_3: {
flags: '-c, --cwd <cwd>',
description: 'the working directory. defaults to the current directory.',
defaultValue: process.cwd()
},
option_4: {
flags: '-s, --silent',
description: 'silent mode',
defaultValue: false
},
option_5: {
flags: '-f, --force',
description: 'will force and overwrite old configurations.',
defaultValue: false
},
option_6: {
flags: '-sd, --src-dir <src-dir>',
description: 'the source directory. defaults to the current directory.',
defaultValue: process.cwd()
}
}
}
5 changes: 4 additions & 1 deletion packages/cli-t/src/commands/init/init.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { z } from 'zod'
export const init_options_schema = z.object({
yes: z.boolean().default(false),
defaults: z.boolean().default(false),
cwd: z.string().default(process.cwd())
cwd: z.string().default(process.cwd()),
slint: z.boolean().default(false),
force: z.boolean().default(false),
srcDir: z.string().default(process.cwd())
})

export type InitOptions = z.infer<typeof init_options_schema>
60 changes: 31 additions & 29 deletions packages/cli-t/src/commands/init/init.lib.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import path from 'path'
import { init_options_schema, InitOptions } from './init.dto'
import {
checkTailwindCssInstalled,
checkTypeScriptInstalled,
get_project_config
get_project_config,
logger,
pref_light_tailwindcss
} from '@/src/utils'
import { spinner } from '@/src/utils/spinner'
import { REGISTRY_URL } from '@/src/main'

export async function init_command_action(opt: InitOptions) {
const options = init_options_schema.parse(opt)
const cwd = path.resolve(options.cwd)

await checkTailwindCssInstalled(cwd)
logger.info({ args: ['Checking for preflight...'] })

const typescript = await checkTypeScriptInstalled(cwd)
await pref_light_tailwindcss(cwd, typescript)
const config = await get_project_config(cwd)

console.log(config)
}

export async function init_command() {}
function isUrl(path: string) {
try {
new URL(path)
return true
} catch (error) {
return false
}
}

function getRegistryUrl(path: string) {
if (isUrl(path)) {
// If the url contains /chat/b/, we assume it's the v0 registry.
//NOTE: We need to add the /json suffix if it's missing.
const url = new URL(path)
if (url.pathname.match(/\/chat\/b\//) && !url.pathname.endsWith('/json')) {
url.pathname = `${url.pathname}/json`
}

return url.toString()
}

return `${REGISTRY_URL}/${path}`
}
// export async function init_command() {}
// function isUrl(path: string) {
// try {
// new URL(path)
// return true
// } catch (error) {
// return false
// }
// }
//
// function getRegistryUrl(path: string) {
// if (isUrl(path)) {
// // If the url contains /chat/b/, we assume it's the v0 registry.
// //NOTE: We need to add the /json suffix if it's missing.
// const url = new URL(path)
// if (url.pathname.match(/\/chat\/b\//) && !url.pathname.endsWith('/json')) {
// url.pathname = `${url.pathname}/json`
// }
//
// return url.toString()
// }
//
// return `${REGISTRY_URL}/${path}`
// }
6 changes: 5 additions & 1 deletion packages/cli-t/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Command } from 'commander'
import { init_command_config } from './init.constants'
import { init_command_action } from './init.lib'

const { name, description, option_1, option_2, option_3 } = init_command_config
const { name, description, options } = init_command_config
const { option_1, option_2, option_3, option_4, option_5, option_6 } = options

export function init_command(): Command {
const init_command = new Command(name)
Expand All @@ -12,6 +13,9 @@ export function init_command(): Command {
.option(option_1.flags, option_1.description, option_1.defaultValue)
.option(option_2.flags, option_2.description, option_2.defaultValue)
.option(option_3.flags, option_3.description, option_3.defaultValue)
.option(option_4.flags, option_4.description, option_4.defaultValue)
.option(option_5.flags, option_5.description, option_5.defaultValue)
.option(option_6.flags, option_6.description, option_6.defaultValue)
.action(init_command_action)

return init_command
Expand Down
11 changes: 11 additions & 0 deletions packages/cli-t/src/commands/init/init.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type InitCommandConfig = {
name: string
description: string
options: Record<`option_${number}`, OptionType>
}

export type OptionType = {
flags: `-${string}, --${string}`
description: string
defaultValue: boolean | string
}
6 changes: 3 additions & 3 deletions packages/cli-t/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export function init() {
const duck_ui = new Command()
const packageJson = getPackageJson()

duck_ui.name(packageJson.name || config.name)
duck_ui.description(packageJson.description || config.description)
duck_ui.version(packageJson.version || config.version)
duck_ui.name(packageJson?.name || config.name)
duck_ui.description(packageJson?.description || config.description)
duck_ui.version(packageJson?.version || config.version)
duck_ui.addCommand(init_command())

duck_ui.parse()
Expand Down
12 changes: 5 additions & 7 deletions packages/cli-t/src/utils/checkers/checkers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {
IGNORED_DIRECTORIES,
tailwindCssInstallationGuide
} from '../get-project-info/get-project-info.constants'
import { logger } from '../logger'
import fs from 'fs-extra'
import path from 'path'
import fg from 'fast-glob'
import { logger } from '../text-styling'

// Check if TypeScript is installed
export async function checkTypeScriptInstalled(cwd: string) {
Expand All @@ -20,18 +20,16 @@ export async function checkTailwindCssInstalled(cwd: string) {
ignore: IGNORED_DIRECTORIES
})

if (!tailwindcss.length) {
logger.error(`TailwindCss is not configured in this directory.`).break()
logger.info(...Object.values(tailwindCssInstallationGuide)).break()
}

if (!tailwindcss.length) return false
return true
}

// Check if the working directory exists
export function checkDirectoryExist(cwd: string): typeof logger | undefined {
if (!fs.lstatSync(cwd).isDirectory()) {
return logger.error(`The working directory ${cwd} does not exist.`)
return logger.error({
args: [`The working directory ${cwd} does not exist.`]
})
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Agent, detect } from '@antfu/ni'

export async function get_package_manager(
cwd: string
): Promise<Exclude<Agent, 'yarn@berry' | 'pnpm@6'>> {
const packageManager = await detect({
programmatic: true,
cwd
})

if (packageManager === 'yarn@berry') return 'yarn'
if (packageManager === 'pnpm@6') return 'pnpm'
if (!packageManager) return 'npm'

return packageManager
}

export async function getPackageRunner(
cwd: string,
pm: Exclude<Agent, 'yarn@berry' | 'pnpm@6'>
): Promise<'pnpm dlx' | 'bunx' | 'npx'> {
const packageManager = pm ?? (await get_package_manager(cwd))

if (packageManager === 'pnpm') return 'pnpm dlx'
if (packageManager === 'bun') return 'bunx'
return 'npx'
}
1 change: 1 addition & 0 deletions packages/cli-t/src/utils/get-package-manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-package-manager'
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ export const config_cchema = raw_config_schema.extend({
ui: z.string()
})
})

export type ConfigType = z.infer<typeof configSchema>
Loading

0 comments on commit 96ecb9e

Please sign in to comment.