-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathminimap-plugin-generator-element.js
More file actions
198 lines (155 loc) · 5.34 KB
/
minimap-plugin-generator-element.js
File metadata and controls
198 lines (155 loc) · 5.34 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use strict"
import { dasherize } from "./deps/underscore-plus"
import { getHomeDirectory, existsSync } from "fs-plus"
import path from "path"
import { BufferedProcess } from "atom"
import element from "./decorators/element"
const TAG_NAME = "minimap-plugin-generator"
/** @access private */
class MinimapPluginGeneratorElement extends HTMLElement {
static initClass() {
this.registerCommands()
element(this, TAG_NAME)
}
static registerCommands() {
atom.commands.add("minimap-plugin-generator", {
"core:confirm"() {
this.confirm()
},
"core:cancel"() {
this.detach()
},
})
}
createdCallback() {
this.previouslyFocusedElement = null
this.mode = null
this.modal = document.createElement("atom-panel")
this.modal.classList.add("minimap-plugin-generator")
this.modal.classList.add("modal")
this.modal.classList.add("overlay")
this.modal.classList.add("from-top")
this.editor = atom.workspace.buildTextEditor({ mini: true })
this.editorElement = atom.views.getView(this.editor)
this.error = document.createElement("div")
this.error.classList.add("error")
this.message = document.createElement("div")
this.message.classList.add("message")
this.modal.appendChild(this.editorElement)
this.modal.appendChild(this.error)
this.modal.appendChild(this.message)
this.appendChild(this.modal)
}
connectedCallback() {
this.previouslyFocusedElement = document.activeElement
this.message.textContent = "Enter plugin path"
this.setPathText("my-minimap-plugin")
this.editorElement.focus()
}
attach() {
atom.views.getView(atom.workspace).appendChild(this)
}
setPathText(placeholderName, rangeToSelect) {
if (!rangeToSelect) {
rangeToSelect = [0, placeholderName.length]
}
const packagesDirectory = getPackagesDirectory()
this.editor.setText(path.join(packagesDirectory, placeholderName))
const pathLength = this.editor.getText().length
const endOfDirectoryIndex = pathLength - placeholderName.length
this.editor.setSelectedBufferRange([
[0, endOfDirectoryIndex + rangeToSelect[0]],
[0, endOfDirectoryIndex + rangeToSelect[1]],
])
}
detach() {
if (!this.parentNode) {
return
}
if (this.previouslyFocusedElement) {
this.previouslyFocusedElement.focus()
}
this.parentNode.removeChild(this)
}
confirm() {
if (this.validPackagePath()) {
this.removeChild(this.modal)
this.message.innerHTML = `
<span class='loading loading-spinner-tiny inline-block'></span>
Generate plugin at <span class="text-primary">${this.getPackagePath()}</span>
`
this.createPackageFiles(() => {
const packagePath = this.getPackagePath()
atom.open({ pathsToOpen: [packagePath], devMode: atom.config.get("minimap.createPluginInDevMode") })
this.message.innerHTML = '<span class="text-success">Plugin successfully generated, opening it now...</span>'
setTimeout(() => {
this.detach()
}, 2000)
})
}
}
getPackagePath() {
const packagePath = this.editor.getText()
const packageName = dasherize(path.basename(packagePath))
return path.join(path.dirname(packagePath), packageName)
}
validPackagePath() {
if (existsSync(this.getPackagePath())) {
this.error.textContent = `Path already exists at '${this.getPackagePath()}'`
this.error.style.display = "block"
return false
} else {
return true
}
}
initPackage(packagePath, callback) {
const templatePath = path.resolve(__dirname, path.join("..", "templates", `plugin-${this.template}`))
runCommand(atom.packages.getApmPath(), ["init", "-p", `${packagePath}`, "--template", templatePath], callback)
}
createPackageFiles(callback) {
const packagePath = this.getPackagePath()
if (isStoredInDotAtom(packagePath)) {
this.initPackage(packagePath, () => {
installPackage(packagePath, callback)
})
} else {
this.initPackage(packagePath, () => {
linkPackage(packagePath, () => {
installPackage(packagePath, callback)
})
})
}
}
}
MinimapPluginGeneratorElement.initClass()
export function createMinimapPluginGeneratorElement() {
const element = document.createElement(TAG_NAME)
element.createdCallback()
return element
}
function linkPackage(packagePath, callback) {
const args = ["link"]
if (atom.config.get("minimap.createPluginInDevMode")) {
args.push("--dev")
}
args.push(packagePath.toString())
runCommand(atom.packages.getApmPath(), args, callback)
}
function installPackage(packagePath, callback) {
const args = ["install"]
runCommand(atom.packages.getApmPath(), args, callback, { cwd: packagePath })
}
function getPackagesDirectory() {
return atom.config.get("core.projectHome") || process.env.ATOM_REPOS_HOME || path.join(getHomeDirectory(), "github")
}
function isStoredInDotAtom(packagePath) {
const packagesPath = path.join(atom.getConfigDirPath(), "packages", path.sep)
if (packagePath.indexOf(packagesPath) === 0) {
return true
}
const devPackagesPath = path.join(atom.getConfigDirPath(), "dev", "packages", path.sep)
return packagePath.indexOf(devPackagesPath) === 0
}
function runCommand(command, args, exit, options = {}) {
return new BufferedProcess({ command, args, exit, options })
}