chore: cleanup and update test

This commit is contained in:
zernonia 2024-02-06 11:34:52 +08:00
parent c041011791
commit 8c7f816502
7 changed files with 32 additions and 50 deletions

View File

@ -99,7 +99,7 @@ export const diff = new Command()
consola.info(` - ${change.filePath}`) consola.info(` - ${change.filePath}`)
} }
console.log('') consola.log('')
consola.info( consola.info(
`Run ${colors.green('diff <component>')} to see the changes.`, `Run ${colors.green('diff <component>')} to see the changes.`,
) )
@ -128,7 +128,7 @@ export const diff = new Command()
for (const change of changes) { for (const change of changes) {
consola.info(`- ${change.filePath}`) consola.info(`- ${change.filePath}`)
printDiff(change.patch) printDiff(change.patch)
console.log('') consola.log('')
} }
} }
catch (error) { catch (error) {

View File

@ -73,11 +73,11 @@ export const init = new Command()
await runInit(cwd, config) await runInit(cwd, config)
console.log('') consola.log('')
consola.info( consola.info(
`${colors.green('Success!')} Project initialization completed.`, `${colors.green('Success!')} Project initialization completed.`,
) )
console.log('') consola.log('')
} }
catch (error) { catch (error) {
handleError(error) handleError(error)
@ -206,7 +206,7 @@ export async function promptForConfig(
} }
// Write to file. // Write to file.
console.log('') consola.log('')
const spinner = ora('Writing components.json...').start() const spinner = ora('Writing components.json...').start()
const targetPath = path.resolve(cwd, 'components.json') const targetPath = path.resolve(cwd, 'components.json')
await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8') await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8')
@ -291,8 +291,8 @@ export async function runInit(cwd: string, config: Config) {
await Promise.allSettled( await Promise.allSettled(
[ [
await addNuxtDevDeps(), addNuxtDevDeps(),
await addDependency(deps, { addDependency(deps, {
cwd, cwd,
silent: true, silent: true,
}), }),

View File

@ -100,23 +100,21 @@ export async function resolveConfigPaths(cwd: string, config: RawConfig) {
utils: resolveImport(config.aliases.utils, tsConfig), utils: resolveImport(config.aliases.utils, tsConfig),
components: resolveImport(config.aliases.components, tsConfig), components: resolveImport(config.aliases.components, tsConfig),
ui: config.aliases['ui'] ui: config.aliases['ui']
? await resolveImport(config.aliases['ui'], tsConfig) ? resolveImport(config.aliases['ui'], tsConfig)
: await resolveImport(config.aliases['components'], tsConfig), : resolveImport(config.aliases['components'], tsConfig),
}, },
}) })
} }
export async function getRawConfig(cwd: string): Promise<RawConfig | null> { export async function getRawConfig(cwd: string): Promise<RawConfig | null> {
try { try {
// const configResult = await explorer.search(cwd)
const configResult = await c12LoadConfig({ const configResult = await c12LoadConfig({
name: 'components', name: 'components',
configFile: 'components.json', configFile: 'components.json',
cwd, cwd,
}) })
if (!configResult) if (!configResult.config || Object.keys(configResult.config).length === 0)
return null return null
return rawConfigSchema.parse(configResult.config) return rawConfigSchema.parse(configResult.config)

View File

@ -1,14 +1,13 @@
import fs from 'node:fs' import fs from 'node:fs'
import path from 'node:path' import path from 'node:path'
import { execa } from 'execa' import { addDependency, addDevDependency } from 'nypm'
import { afterEach, expect, test, vi } from 'vitest' import { afterEach, expect, test, vi } from 'vitest'
import { runInit } from '../../src/commands/init' import { runInit } from '../../src/commands/init'
import { getConfig } from '../../src/utils/get-config' import { getConfig } from '../../src/utils/get-config'
import * as getPackageManger from '../../src/utils/get-package-manager'
import * as registry from '../../src/utils/registry' import * as registry from '../../src/utils/registry'
vi.mock('execa') vi.mock('nypm')
vi.mock('fs/promises', () => ({ vi.mock('fs/promises', () => ({
writeFile: vi.fn(), writeFile: vi.fn(),
mkdir: vi.fn(), mkdir: vi.fn(),
@ -16,7 +15,6 @@ vi.mock('fs/promises', () => ({
vi.mock('ora') vi.mock('ora')
test('init config-full', async () => { test('init config-full', async () => {
vi.spyOn(getPackageManger, 'getPackageManager').mockResolvedValue('pnpm')
vi.spyOn(registry, 'getRegistryBaseColor').mockResolvedValue({ vi.spyOn(registry, 'getRegistryBaseColor').mockResolvedValue({
inlineColors: {}, inlineColors: {},
cssVars: {}, cssVars: {},
@ -67,10 +65,8 @@ test('init config-full', async () => {
expect.stringContaining("import { type ClassValue, clsx } from 'clsx'"), expect.stringContaining("import { type ClassValue, clsx } from 'clsx'"),
'utf8', 'utf8',
) )
expect(execa).toHaveBeenCalledWith( expect(addDependency).toHaveBeenCalledWith(
'pnpm',
[ [
'add',
'tailwindcss-animate', 'tailwindcss-animate',
'class-variance-authority', 'class-variance-authority',
'clsx', 'clsx',
@ -80,6 +76,7 @@ test('init config-full', async () => {
], ],
{ {
cwd: targetDir, cwd: targetDir,
silent: true,
}, },
) )
@ -88,7 +85,6 @@ test('init config-full', async () => {
}) })
test('init config-partial', async () => { test('init config-partial', async () => {
vi.spyOn(getPackageManger, 'getPackageManager').mockResolvedValue('npm')
vi.spyOn(registry, 'getRegistryBaseColor').mockResolvedValue({ vi.spyOn(registry, 'getRegistryBaseColor').mockResolvedValue({
inlineColors: {}, inlineColors: {},
cssVars: {}, cssVars: {},
@ -139,10 +135,8 @@ test('init config-partial', async () => {
expect.stringContaining("import { type ClassValue, clsx } from 'clsx'"), expect.stringContaining("import { type ClassValue, clsx } from 'clsx'"),
'utf8', 'utf8',
) )
expect(execa).toHaveBeenCalledWith( expect(addDependency).toHaveBeenCalledWith(
'npm',
[ [
'install',
'tailwindcss-animate', 'tailwindcss-animate',
'class-variance-authority', 'class-variance-authority',
'clsx', 'clsx',
@ -152,6 +146,7 @@ test('init config-partial', async () => {
], ],
{ {
cwd: targetDir, cwd: targetDir,
silent: true,
}, },
) )

View File

@ -64,7 +64,7 @@ describe('apply color mapping', async () => {
input: input:
'text-destructive border-destructive/50 dark:border-destructive [&>svg]:text-destructive text-destructive', 'text-destructive border-destructive/50 dark:border-destructive [&>svg]:text-destructive text-destructive',
output: output:
'text-red-500 border-red-500/50 dark:border-red-500 [&>svg]:text-red-500 text-red-500 dark:text-red-900 dark:border-red-900/50 dark:dark:border-red-900 dark:[&>svg]:text-red-900 dark:text-red-900', 'text-red-500 border-red-500/50 dark:border-red-500 [&>svg]:text-red-500 dark:text-red-900 dark:border-red-900/50 dark:dark:border-red-900 dark:[&>svg]:text-red-900',
}, },
{ {
input: input:

View File

@ -71,6 +71,11 @@ test('get config', async () => {
'../fixtures/config-partial', '../fixtures/config-partial',
'./components', './components',
), ),
ui: path.resolve(
__dirname,
'../fixtures/config-partial',
'./components',
),
utils: path.resolve( utils: path.resolve(
__dirname, __dirname,
'../fixtures/config-partial', '../fixtures/config-partial',
@ -111,6 +116,11 @@ test('get config', async () => {
'../fixtures/config-full', '../fixtures/config-full',
'./src/components', './src/components',
), ),
ui: path.resolve(
__dirname,
'../fixtures/config-full',
'./src/components',
),
utils: path.resolve( utils: path.resolve(
__dirname, __dirname,
'../fixtures/config-full', '../fixtures/config-full',
@ -152,6 +162,11 @@ test('get config', async () => {
'../fixtures/config-js', '../fixtures/config-js',
'./components', './components',
), ),
ui: path.resolve(
__dirname,
'../fixtures/config-js',
'./components',
),
utils: path.resolve(__dirname, '../fixtures/config-js', './lib/utils'), utils: path.resolve(__dirname, '../fixtures/config-js', './lib/utils'),
}, },
}) })

View File

@ -1,26 +0,0 @@
import path from 'node:path'
import { expect, test } from 'vitest'
import { getPackageManager } from '../../src/utils/get-package-manager'
test('get package manager', async () => {
expect(
await getPackageManager(path.resolve(__dirname, '../fixtures/project-yarn')),
).toBe('yarn')
expect(
await getPackageManager(path.resolve(__dirname, '../fixtures/project-npm')),
).toBe('npm')
expect(
await getPackageManager(path.resolve(__dirname, '../fixtures/project-pnpm')),
).toBe('pnpm')
expect(
await getPackageManager(path.resolve(__dirname, '../fixtures/project-bun')),
).toBe('bun')
expect(
await getPackageManager(path.resolve(__dirname, '../fixtures/next')),
).toBe('pnpm')
})