-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
80 lines (71 loc) · 2.57 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require('path')
const fs = require('fs')
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
let disposable = vscode.commands.registerCommand('extension.newReactComponent', function ({fsPath}) {
const functionalComponent = {
label: 'Functional Component',
detail: 'A simple, easy to read component with no state or lifecycle methods.',
generate: (componentName) =>
`import React from 'react'
const ${componentName} = (props) => {
return (
<div>
</div>
)
}
export default ${componentName}
`
}
const classComponent = {
label: 'Class Component',
detail: 'A component with the ability to add state and lifecycle methods.',
generate: (componentName) =>
`import React, { Component } from 'react'
class ${componentName} extends Component {
render () {
return (
<div>
</div>
)
}
}
export default ${componentName}
`
}
vscode.window.showQuickPick([
functionalComponent,
classComponent,
]).then((componentType) => {
if (!componentType) return vscode.window.showErrorMessage('No component type selected.')
vscode.window.showInputBox({
prompt: 'File Name'
}).then((fsName) => {
if (!fsName) return vscode.window.showErrorMessage('No file name entered.')
fsName = fsName.substr(0, 1).toUpperCase() + fsName.substr(1)
if (!/\./.test(fsName)) fsName = `${fsName}.js`
var componentName = fsName.split('.')[0]
const fullPath = path.join(fsPath, fsName)
const generate = (componentType.label === functionalComponent.label ? functionalComponent.generate : classComponent.generate)
const componentText = generate(componentName)
if (fs.existsSync(fullPath)) return vscode.window.showErrorMessage(`File '${fsName}' already exists.`)
fs.writeFileSync(fullPath, componentText, 'utf8')
vscode.window.showInformationMessage(`Created component '${componentName}'.`)
vscode.workspace.openTextDocument(fullPath)
.then((doc) => {
vscode.window.showTextDocument(doc)
})
})
})
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;