Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cli/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions packages/cli/commands/local.ts
Original file line number Diff line number Diff line change
@@ -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 <url>'
export const cmdDesc = 'Set local template folder path'
export const opt = ''
export const optDesc = ''

export async function action(url: string): Promise<void> {
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}`))
}
16 changes: 13 additions & 3 deletions packages/cli/utils/alias.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`)
36 changes: 36 additions & 0 deletions packages/cli/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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
}