Skip to content

Commit

Permalink
feat: Add mock generator to component context menu (Close #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvsde committed Oct 14, 2022
1 parent 54b72ad commit 0ddda37
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
"title": "Lint All Components",
"enablement": "miyagi.canLint"
},
{
"command": "miyagi.generateMock",
"category": "miyagi",
"title": "Generate Mock",
"enablement": "miyagi.canGenerateMock"
},
{
"command": "miyagi.reload",
"category": "miyagi",
Expand All @@ -65,6 +71,10 @@
{
"command": "miyagi.lintComponent",
"when": "never"
},
{
"command": "miyagi.generateMock",
"when": "never"
}
],
"explorer/context": [
Expand All @@ -84,6 +94,11 @@
"group": "component@2",
"when": "explorerResourceIsFolder && miyagi.canLint"
},
{
"command": "miyagi.generateMock",
"group": "component@3",
"when": "explorerResourceIsFolder && miyagi.canGenerateMock"
},
{
"command": "miyagi.reload",
"group": "control@1",
Expand Down
46 changes: 46 additions & 0 deletions src/commands/generate-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import path from 'node:path'

import vscode from 'vscode'

import { outputChannel } from '../lib/output-channel'
import { getProject } from '../lib/projects'
import { runMiyagi } from '../lib/run'

export function generateMock (uri?: vscode.Uri) {
if (!uri) {
return
}

const project = getProject(uri)

if (!project) {
return
}

const cwd = project.uri.path
const componentsFolder = project.config.components.folder
const componentPath = path.relative(cwd, uri.path)

if (!uri.path.includes(componentsFolder)) {
vscode.window.showWarningMessage(`miyagi: Select a component in "${componentsFolder}"`)
return
}

const result = runMiyagi({
args: ['mocks', componentPath],
cwd
})

if (result.status !== 0) {
vscode.window.showErrorMessage('miyagi: Error generating mock')
}

const stderr = result?.stderr.toString()

if (stderr) {
outputChannel.appendLine(stderr)
outputChannel.show()
}
}

export const generateMockCompatibility = '3.3.2'
7 changes: 7 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import vscode from 'vscode'

import { generateMock, generateMockCompatibility } from './commands/generate-mock'
import { lint, lintCompatibility } from './commands/lint'
import { newComponent, newComponentCompatibility } from './commands/new-component'
import { MIYAGI_CONFIG_GLOB, SCHEMA_GLOB } from './constants'
Expand All @@ -16,6 +17,7 @@ import { SemVer } from './utils/semver'
const contextHasMiyagi = new ContextKey('miyagi.hasMiyagi')
const contextCanLint = new ContextKey('miyagi.canLint')
const contextCanNewComponent = new ContextKey('miyagi.canNewComponent')
const contextCanGenerateMock = new ContextKey('miyagi.canGenerateMock')

async function reload () {
const projectList = await getProjectList({ refresh: true })
Expand All @@ -26,6 +28,9 @@ async function reload () {

const canNewComponent = projectList.every(project => new SemVer(project.version).gte(newComponentCompatibility))
contextCanNewComponent.set(canNewComponent)

const canGenerateMock = projectList.every(project => new SemVer(project.version).gte(generateMockCompatibility))
contextCanGenerateMock.set(canGenerateMock)
}

export async function activate (context: vscode.ExtensionContext) {
Expand All @@ -44,6 +49,7 @@ export async function activate (context: vscode.ExtensionContext) {
const commandNewComponent = vscode.commands.registerCommand('miyagi.newComponent', newComponent)
const commandLintComponent = vscode.commands.registerCommand('miyagi.lintComponent', lint)
const commandLintAllComponents = vscode.commands.registerCommand('miyagi.lintAllComponents', lint)
const commandGenerateMock = vscode.commands.registerCommand('miyagi.generateMock', generateMock)
const commandReload = vscode.commands.registerCommand('miyagi.reload', reload)

// Providers
Expand All @@ -59,6 +65,7 @@ export async function activate (context: vscode.ExtensionContext) {
commandNewComponent,
commandLintComponent,
commandLintAllComponents,
commandGenerateMock,
commandReload,
providerDocumentLinksMocks,
providerDocumentLinksSchema,
Expand Down

0 comments on commit 0ddda37

Please sign in to comment.