import { PLUGIN_CONFIG_RELATIVE_PATH, DEFAULT_CLI_PLUGIN_PACKAGE_NAME } from '@ones-open/cli-utils' import { getYamlConfigContent, updateYamlConfigContent, getNpm, getCliVersionByMetaPath, } from '@ones-open/cli-utils' import type { ProjectPluginConfig } from '@ones-open/cli-utils' import boxen from 'boxen' import { execa } from 'execa' import { join } from 'path' import { cwd } from 'process' async function getPluginConfigContent(configPath?: string) { const pluginConfigPath = configPath ?? join(cwd(), PLUGIN_CONFIG_RELATIVE_PATH) const pluginConfig = await getYamlConfigContent(pluginConfigPath) return pluginConfig } async function updatePluginConfigContent(content: ProjectPluginConfig, configPath?: string) { const pluginConfigPath = configPath || join(cwd(), PLUGIN_CONFIG_RELATIVE_PATH) await updateYamlConfigContent(pluginConfigPath, content) } async function installProjectNpmDependencies(currentWorkingDirectory: string) { const installCommand = getNpm() const installArgs = ['install', '--force'] await execa(installCommand, installArgs, { cwd: currentWorkingDirectory }) } async function createUpgradeMessage() { let message = null const currentCliPluginVerson = await getCliVersionByMetaPath(import.meta.url) const npmCommand = getNpm() const viewArgs = ['info', DEFAULT_CLI_PLUGIN_PACKAGE_NAME, '--json'] const { stdout } = await execa(npmCommand, viewArgs, { cwd: cwd() }) const { 'dist-tags': { latest: latestVersion }, } = JSON.parse(stdout) // 当前版本不是最新版本便提示,即使是当前大版本下的最新版本! if (currentCliPluginVerson !== latestVersion) { message = boxen(` A new @ones-open/cli-plugin version (${latestVersion}) is available! Upgrade now: npx op upgrade Read full changelog here: https://developer.ones.com/docs/tools/cli/changelog `) } return message } // 这个值本来是'# yaml-language-server: $schema=../node_modules/@ones-open/plugin-config-schema/dist/plugin-schema.json\n' // 但是由于维护这份schema比较麻烦,所以这里废弃掉算了,后面重构cli不用这种模式了 const pluginConfigSchema = '' export { getPluginConfigContent, updatePluginConfigContent, installProjectNpmDependencies, createUpgradeMessage, pluginConfigSchema, }