diff --git a/packages/cli/commands/index.ts b/packages/cli/commands/index.ts index bcf49f1..f038ce2 100644 --- a/packages/cli/commands/index.ts +++ b/packages/cli/commands/index.ts @@ -1,5 +1,6 @@ import * as createCmd from './create' +import * as localCmd from './local' import * as searchCmd from './search' -const cmdList: CMD[] = [createCmd, searchCmd] +const cmdList: CMD[] = [createCmd, searchCmd, localCmd] export default cmdList diff --git a/packages/cli/commands/local.ts b/packages/cli/commands/local.ts new file mode 100644 index 0000000..9b20cd8 --- /dev/null +++ b/packages/cli/commands/local.ts @@ -0,0 +1,19 @@ +import fs from 'node:fs' +import path from 'node:path' +import chalk from 'chalk' +import { setLocalTemplatePath } from '../utils/config' + +export const cmd = 'local ' +export const cmdDesc = 'Set local template folder path' +export const opt = '' +export const optDesc = '' + +export async function action(url: string): Promise { + const absPath = path.resolve(url) + if (!fs.existsSync(absPath) || !fs.statSync(absPath).isDirectory()) { + console.log(chalk.red(`Path "${absPath}" does not exist or is not a directory.`)) + return + } + setLocalTemplatePath(absPath) + console.log(chalk.green(`Local template path set to: ${absPath}`)) +} diff --git a/packages/cli/utils/alias.ts b/packages/cli/utils/alias.ts index 6fab547..249b0e7 100644 --- a/packages/cli/utils/alias.ts +++ b/packages/cli/utils/alias.ts @@ -1,10 +1,20 @@ import { resolve } from 'node:path' import { fileURLToPath } from 'node:url' +import { getLocalTemplatePath } from './config' import { getFolders } from './fs' export const root = process.cwd() -export const inkartTemps = fileURLToPath(import.meta.resolve('@inkart/temps')) -export const subFolders = getFolders(resolve(inkartTemps, '../')) + +export function getTemplateRoot() { + // Try to get user-configured local template path + const localPath = getLocalTemplatePath() + if (localPath) + return localPath + // Fallback to default + return fileURLToPath(import.meta.resolve('@inkart/temps')) +} + +export const subFolders = getFolders(resolve(getTemplateRoot(), '../')) /** * get target path @@ -16,4 +26,4 @@ export const getTargetPath = (projectName: string): string => resolve(root, proj * get template path * @param templateName */ -export const getTemplatePath = (templateName: string): string => resolve(inkartTemps, `../${templateName}`) +export const getTemplatePath = (templateName: string): string => resolve(getTemplateRoot(), `../${templateName}`) diff --git a/packages/cli/utils/config.ts b/packages/cli/utils/config.ts new file mode 100644 index 0000000..e54ddf2 --- /dev/null +++ b/packages/cli/utils/config.ts @@ -0,0 +1,36 @@ +import fs from 'node:fs' +import path from 'node:path' + +const CONFIG_FILE = path.resolve(__dirname, '../../config.json') + +interface Config { + localTemplatePath?: string +} + +function readConfig(): Config { + if (!fs.existsSync(CONFIG_FILE)) + return {} + + try { + const raw = fs.readFileSync(CONFIG_FILE, 'utf-8') + return JSON.parse(raw) + } + catch { + return {} + } +} + +function writeConfig(config: Config) { + fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf-8') +} + +export function setLocalTemplatePath(localPath: string) { + const config = readConfig() + config.localTemplatePath = localPath + writeConfig(config) +} + +export function getLocalTemplatePath(): string | undefined { + const config = readConfig() + return config.localTemplatePath +}