From f86e230b8902f5c37069ea22670ffd5c35cd5713 Mon Sep 17 00:00:00 2001 From: YRM Date: Thu, 28 Dec 2023 16:51:42 +0800 Subject: [PATCH] feat: add global settings --- package.json | 60 +++++++++++++++---------------- src/commands/starter.ts | 73 +++++++++++++++++++++----------------- src/config.ts | 16 ++++----- src/shared/vscode/input.ts | 16 ++++----- 4 files changed, 87 insertions(+), 78 deletions(-) diff --git a/package.json b/package.json index a5cc477..4643222 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,37 @@ ], "configuration": [ { - "title": "Create Vue(Official)", + "title": "Global Settings", "order": 1, + "properties": { + "starterTemplates.globalSettings.needsGitInit": { + "type": "boolean", + "default": true, + "markdownDescription": "Initialize git repository?", + "scope": "window" + }, + "starterTemplates.globalSettings.needsInstall": { + "type": "boolean", + "default": true, + "markdownDescription": "Install project dependencies?", + "scope": "window" + }, + "starterTemplates.globalSettings.packageManager": { + "enum": [ + "pnpm", + "npm", + "yarn", + "bun" + ], + "default": "pnpm", + "description": "Package manager choice?", + "scope": "window" + } + } + }, + { + "title": "Create Vue(Official)", + "order": 2, "properties": { "starterTemplates.createVue.needsTypeScript": { "type": "boolean", @@ -89,35 +118,6 @@ "scope": "window" } } - }, - { - "title": "Nuxt3 Minimal Starter(Official)", - "order": 2, - "properties": { - "starterTemplates.nuxt3MinimalStarter.needsGitInit": { - "type": "boolean", - "default": true, - "markdownDescription": "Initialize git repository", - "scope": "window" - }, - "starterTemplates.nuxt3MinimalStarter.needsInstall": { - "type": "boolean", - "default": true, - "markdownDescription": "Install project dependencies", - "scope": "window" - }, - "starterTemplates.nuxt3MinimalStarter.packageManager": { - "enum": [ - "pnpm", - "npm", - "yarn", - "bun" - ], - "default": "pnpm", - "description": "Package manager choice", - "scope": "window" - } - } } ] }, diff --git a/src/commands/starter.ts b/src/commands/starter.ts index e9fbf95..f0cde9b 100644 --- a/src/commands/starter.ts +++ b/src/commands/starter.ts @@ -73,7 +73,6 @@ export class StarterCommands extends BaseCommands { break case 'nuxt3-minimal-starter': await degit('nuxt/starter/#v3').clone(`${projectPath}`) - break case 'vitesse-nuxt3': await degit('antfu/vitesse-nuxt3').clone(`${projectPath}`) @@ -207,7 +206,7 @@ export class StarterCommands extends BaseCommands { title: 'Project Name', validation: s => this.validateProjectName(s, folderPath), value: defaultName, - enableSetting: template.id === 'create-vue' || template.id === 'nuxt3-minimal-starter', + enableSetting: true, }, ) @@ -233,10 +232,48 @@ export class StarterCommands extends BaseCommands { return `A project with this name already exists within the selected directory` } + private getGlobalSettings(): PickableSetting[] { + return [ + { + kind: QuickPickItemKind.Separator, + label: 'Global Settings', + }, + { + currentValue: config.globalNeedsGitInit ? 'Yes' : 'No', + description: config.globalNeedsGitInit ? 'Yes' : 'No', + detail: '', + label: 'Initialize git repository?', + setValue: (newValue: boolean) => config.setGlobalNeedsGitInit(newValue), + settingKind: 'BOOL', + }, + { + currentValue: config.globalNeedsInstall ? 'Yes' : 'No', + description: config.globalNeedsInstall ? 'Yes' : 'No', + detail: '', + label: 'Install project dependencies?', + setValue: (newValue: boolean) => config.setGlobalNeedsInstall(newValue), + settingKind: 'BOOL', + }, + { + currentValue: config.globalPackageManager || 'pnpm', + description: config.globalPackageManager || 'pnpm', + detail: '', + enumValues: ['pnpm', 'npm', 'yarn', 'bun'], + label: 'Package manager choice?', + setValue: (newValue: 'pnpm' | 'npm' | 'yarn' | 'bun') => config.setGlobalPackageManager(newValue), + settingKind: 'ENUM', + }, + ] + } + private getCurrentCreateSettings(template: ProjectTemplate): PickableSetting[] { switch (template.id) { case 'create-vue': const createVueSettings: PickableSetting[] = [ + { + kind: QuickPickItemKind.Separator, + label: 'Create Vue', + }, { currentValue: config.createVueNeedsTypeScript ? 'Yes' : 'No', description: config.createVueNeedsTypeScript ? 'Yes' : 'No', @@ -308,37 +345,9 @@ export class StarterCommands extends BaseCommands { }, ) } - return createVueSettings - case 'nuxt3-minimal-starter': - return [ - { - currentValue: config.nuxt3MinimalStarterNeedsGitInit ? 'Yes' : 'No', - description: config.nuxt3MinimalStarterNeedsGitInit ? 'Yes' : 'No', - detail: '', - label: 'Initialize git repository?', - setValue: (newValue: boolean) => config.setNuxt3MinimalStarterNeedsGitInit(newValue), - settingKind: 'BOOL', - }, - { - currentValue: config.nuxt3MinimalStarterNeedsInstall ? 'Yes' : 'No', - description: config.nuxt3MinimalStarterNeedsInstall ? 'Yes' : 'No', - detail: '', - label: 'Install project dependencies?', - setValue: (newValue: boolean) => config.setNuxt3MinimalStarterNeedsInstall(newValue), - settingKind: 'BOOL', - }, - { - currentValue: config.nuxt3MinimalStarterPackageManager || 'pnpm', - description: config.nuxt3MinimalStarterPackageManager || 'pnpm', - detail: '', - enumValues: ['pnpm', 'npm', 'yarn', 'bun'], - label: 'Package manager choice?', - setValue: (newValue: 'pnpm' | 'npm' | 'yarn' | 'bun') => config.setNuxt3MinimalStarterPackageManager(newValue), - settingKind: 'ENUM', - }, - ] + return [...createVueSettings, ...this.getGlobalSettings()] default: - return [] + return [...this.getGlobalSettings()] } } } diff --git a/src/config.ts b/src/config.ts index 4f10aa3..3147f21 100644 --- a/src/config.ts +++ b/src/config.ts @@ -47,10 +47,10 @@ class Config { get createVueEndToEndTestingSolution(): 'Cypress' | 'Nightwatch' | 'Playwright' | 'No' { return this.getConfig<'Cypress' | 'Nightwatch' | 'Playwright' | 'No'>('createVue.endToEndTestingSolution', 'Cypress') } get createVueNeedsEslint(): boolean { return this.getConfig('createVue.needsEslint', true) } get createVueNeedsPrettier(): boolean { return this.getConfig('createVue.needsPrettier', true) } - // nuxt3 minimal starter - get nuxt3MinimalStarterNeedsGitInit(): boolean { return this.getConfig('nuxt3MinimalStarter.needsGitInit', true) } - get nuxt3MinimalStarterNeedsInstall(): boolean { return this.getConfig('nuxt3MinimalStarter.needsInstall', true) } - get nuxt3MinimalStarterPackageManager(): 'pnpm' | 'npm' | 'yarn' | 'bun' { return this.getConfig<'pnpm' | 'npm' | 'yarn' | 'bun'>('nuxt3MinimalStarter.packageManager', 'pnpm') } + // global settings + get globalNeedsGitInit(): boolean { return this.getConfig('globalSettings.needsGitInit', true) } + get globalNeedsInstall(): boolean { return this.getConfig('globalSettings.needsInstall', true) } + get globalPackageManager(): 'pnpm' | 'npm' | 'yarn' | 'bun' { return this.getConfig<'pnpm' | 'npm' | 'yarn' | 'bun'>('globalSettings.packageManager', 'pnpm') } // create vue public setCreateVueNeedsTypeScript(value: boolean): Promise { return this.setConfig('createVue.needsTypeScript', value, ConfigurationTarget.Global) } @@ -61,10 +61,10 @@ class Config { public setCreateVueEndToEndTestingSolution(value: 'Cypress' | 'Nightwatch' | 'Playwright' | 'No'): Promise { return this.setConfig('createVue.endToEndTestingSolution', value, ConfigurationTarget.Global) } public setCreateVueNeedsEslint(value: boolean): Promise { return this.setConfig('createVue.needsEslint', value, ConfigurationTarget.Global) } public setCreateVueNeedsPrettier(value: boolean): Promise { return this.setConfig('createVue.needsPrettier', value, ConfigurationTarget.Global) } - // nuxt3 minimal starter - public setNuxt3MinimalStarterNeedsGitInit(value: boolean): Promise { return this.setConfig('nuxt3MinimalStarter.needsGitInit', value, ConfigurationTarget.Global) } - public setNuxt3MinimalStarterNeedsInstall(value: boolean): Promise { return this.setConfig('nuxt3MinimalStarter.needsInstall', value, ConfigurationTarget.Global) } - public setNuxt3MinimalStarterPackageManager(value: 'pnpm' | 'npm' | 'yarn' | 'bun'): Promise { return this.setConfig('nuxt3MinimalStarter.packageManager', value, ConfigurationTarget.Global) } + // global settings + public setGlobalNeedsGitInit(value: boolean): Promise { return this.setConfig('globalSettings.needsGitInit', value, ConfigurationTarget.Global) } + public setGlobalNeedsInstall(value: boolean): Promise { return this.setConfig('globalSettings.needsInstall', value, ConfigurationTarget.Global) } + public setGlobalPackageManager(value: 'pnpm' | 'npm' | 'yarn' | 'bun'): Promise { return this.setConfig('globalSettings.packageManager', value, ConfigurationTarget.Global) } public for(uri?: Uri): ResourceConfig { return new ResourceConfig(uri) diff --git a/src/shared/vscode/input.ts b/src/shared/vscode/input.ts index 1b3319d..8476412 100644 --- a/src/shared/vscode/input.ts +++ b/src/shared/vscode/input.ts @@ -92,7 +92,7 @@ export async function editSetting(setting: PickableSetting) { case 'STRING': const stringResult = await vs.window.showInputBox({ prompt, title, value: value as string | undefined }) if (stringResult !== undefined) - await setting.setValue(stringResult) + await setting.setValue!(stringResult) break case 'ENUM': { const quickPick = vs.window.createQuickPick() @@ -110,7 +110,7 @@ export async function editSetting(setting: PickableSetting) { quickPick.dispose() if (enumResult !== undefined) - await setting.setValue(enumResult) + await setting.setValue!(enumResult) break } case 'MULTI_ENUM': { @@ -120,13 +120,13 @@ export async function editSetting(setting: PickableSetting) { quickPick.placeholder = placeholder quickPick.title = title const items: vs.QuickPickItem[] = [] - for (const group of setting.enumValues) { + for (const group of setting.enumValues!) { items.push({ label: group.group, kind: vs.QuickPickItemKind.Separator } as vs.QuickPickItem) for (const value of group.values) items.push({ label: value } as vs.QuickPickItem) } quickPick.items = items - quickPick.selectedItems = quickPick.items.filter(item => setting.currentValue.find(current => current === item.label)) + quickPick.selectedItems = quickPick.items.filter(item => setting.currentValue!.find(current => current === item.label)) const accepted = await new Promise((resolve) => { quickPick.onDidAccept(() => resolve(true)) @@ -136,7 +136,7 @@ export async function editSetting(setting: PickableSetting) { quickPick.dispose() if (accepted) - await setting.setValue(quickPick.selectedItems.map(item => item.label)) + await setting.setValue!(quickPick.selectedItems.map(item => item.label)) break } case 'BOOL': @@ -148,13 +148,13 @@ export async function editSetting(setting: PickableSetting) { { placeHolder: placeholder, title }, ) if (boolResult !== undefined) - await setting.setValue(boolResult.label === 'Yes') + await setting.setValue!(boolResult.label === 'Yes') break } } type UserInputOrSettings = { value: string } | 'SETTINGS' -export type PickableSetting = vs.QuickPickItem & ({ +export type PickableSetting = vs.QuickPickItem & Partial<({ settingKind: 'STRING' | 'ENUM' | 'BOOL' currentValue: any setValue: (newValue: any) => Promise @@ -164,4 +164,4 @@ export type PickableSetting = vs.QuickPickItem & ({ currentValue: any[] setValue: (newValue: any[]) => Promise enumValues: Array<{ group?: string, values: string[] }> -}) +})>