Skip to content

Commit

Permalink
feat: add global settings
Browse files Browse the repository at this point in the history
  • Loading branch information
yrming committed Dec 28, 2023
1 parent 1550178 commit f86e230
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 78 deletions.
60 changes: 30 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
}
]
},
Expand Down
73 changes: 41 additions & 32 deletions src/commands/starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -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,
},
)

Expand All @@ -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',
Expand Down Expand Up @@ -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()]
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>('createVue.needsEslint', true) }
get createVueNeedsPrettier(): boolean { return this.getConfig<boolean>('createVue.needsPrettier', true) }
// nuxt3 minimal starter
get nuxt3MinimalStarterNeedsGitInit(): boolean { return this.getConfig<boolean>('nuxt3MinimalStarter.needsGitInit', true) }
get nuxt3MinimalStarterNeedsInstall(): boolean { return this.getConfig<boolean>('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<boolean>('globalSettings.needsGitInit', true) }
get globalNeedsInstall(): boolean { return this.getConfig<boolean>('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<void> { return this.setConfig('createVue.needsTypeScript', value, ConfigurationTarget.Global) }
Expand All @@ -61,10 +61,10 @@ class Config {
public setCreateVueEndToEndTestingSolution(value: 'Cypress' | 'Nightwatch' | 'Playwright' | 'No'): Promise<void> { return this.setConfig('createVue.endToEndTestingSolution', value, ConfigurationTarget.Global) }
public setCreateVueNeedsEslint(value: boolean): Promise<void> { return this.setConfig('createVue.needsEslint', value, ConfigurationTarget.Global) }
public setCreateVueNeedsPrettier(value: boolean): Promise<void> { return this.setConfig('createVue.needsPrettier', value, ConfigurationTarget.Global) }
// nuxt3 minimal starter
public setNuxt3MinimalStarterNeedsGitInit(value: boolean): Promise<void> { return this.setConfig('nuxt3MinimalStarter.needsGitInit', value, ConfigurationTarget.Global) }
public setNuxt3MinimalStarterNeedsInstall(value: boolean): Promise<void> { return this.setConfig('nuxt3MinimalStarter.needsInstall', value, ConfigurationTarget.Global) }
public setNuxt3MinimalStarterPackageManager(value: 'pnpm' | 'npm' | 'yarn' | 'bun'): Promise<void> { return this.setConfig('nuxt3MinimalStarter.packageManager', value, ConfigurationTarget.Global) }
// global settings
public setGlobalNeedsGitInit(value: boolean): Promise<void> { return this.setConfig('globalSettings.needsGitInit', value, ConfigurationTarget.Global) }
public setGlobalNeedsInstall(value: boolean): Promise<void> { return this.setConfig('globalSettings.needsInstall', value, ConfigurationTarget.Global) }
public setGlobalPackageManager(value: 'pnpm' | 'npm' | 'yarn' | 'bun'): Promise<void> { return this.setConfig('globalSettings.packageManager', value, ConfigurationTarget.Global) }

public for(uri?: Uri): ResourceConfig {
return new ResourceConfig(uri)
Expand Down
16 changes: 8 additions & 8 deletions src/shared/vscode/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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': {
Expand All @@ -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<boolean>((resolve) => {
quickPick.onDidAccept(() => resolve(true))
Expand All @@ -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':
Expand All @@ -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<void>
Expand All @@ -164,4 +164,4 @@ export type PickableSetting = vs.QuickPickItem & ({
currentValue: any[]
setValue: (newValue: any[]) => Promise<void>
enumValues: Array<{ group?: string, values: string[] }>
})
})>

0 comments on commit f86e230

Please sign in to comment.