-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
369 lines (350 loc) · 14.6 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
var fs = require('fs');
var vscode = require('vscode');
var files = require('./files');
var getJSONFile = files.getJSONFile;
var config = files.config;
var lang = {};
let rootPath = __dirname;
let localPathConfig = vscode.workspace.rootPath + '/.vscode/generator-vue-components/';
let localPath = vscode.workspace.rootPath + '';
let menu = [];
let terminal = vscode.window.createTerminal('generator-vue -components');
//terminal.show()
/* ----------------------------------- Загрузка файлов конфигураций ----------------------------------- */
// Загрузка файлов config.json, lang.json и локального файла config.json в открытом проекте
files.setGlobalRoot(rootPath)
files.setLocalRoot(localPathConfig)
Promise.all([getJSONFile('config.json'), getJSONFile('lang.json')]).then(([config_, lang_]) => {
config = config_;
lang = lang_[config.lang];
getJSONFile('config.json', true)
.then(localConfig => {
lang = lang_[localConfig.lang];
if (localConfig.lang) config.lang = localConfig.lang;
if (localConfig.default) {
config.default.template = localConfig.default.template;
config.default.name = localConfig.default.name;
config.default.path = localConfig.default.path;
}
if (localConfig.lists) {
config.lists.template = localConfig.lists.template;
config.lists.name = localConfig.lists.name;
config.lists.path = localConfig.lists.path;
}
})
.catch(err => {
console.log(lang['Local configuration file not found']);
})
}).catch(err => {
console.error(err);
vscode.window.showErrorMessage(err.message);
}).then(values => {
//Получение списка глобальных шаблонов
files.getNamesGlobalTemplates().then(templates => {
config.templates.global = templates
})
})
/* ----------------------------------- Генерация нового компонента ----------------------------------- */
// Выбор имени компонента
function getName() {
return new Promise(function (resolve, reject) {
if (config.lists.name) {
let itemsListNames = config.lists.name.map(i => i);
itemsListNames.push(lang['Entering text without saving']);
itemsListNames.push(lang['Entering text with saving']);
resolve(vscode.window.showQuickPick(itemsListNames, { ignoreFocusOut: true, placeHolder: lang['The name of the component?'] }));
} else {
resolve(vscode.window.showInputBox({ prompt: lang['The name of the component?'], placeHolder: config.default.name, /*value: config.default.name//*/ }));
}
}).then(name => {
if (name == '') return config.default.name;
if (name == undefined) return Promise.reject('cancel')
if (name == lang['Entering text without saving']) {
return new Promise(async function (resolve, reject) {
let name = await vscode.window.showInputBox({ prompt: lang['The name of the component?'], value: config.default.name })
if (name) resolve(name);
else reject('cancel')
})
}
if (name == lang['Entering text with saving']) {
return new Promise(async function (resolve, reject) {
let name = await vscode.window.showInputBox({ prompt: lang['The name of the component?'], value: config.default.name })
if (name) resolve(name)
else reject('cancel')
}).then(name => {
// Обработка сохранения имени в локальнй конфиг (при отсутствии генерация конфига)
files.getLocalConfig()
.catch(err => {
if (err.code == 'ENOENT')
return files.createLocalConfig()
})
.then(values => {
config.lists.name.push(name)
files.push_name(name)
})
return name;
})
}
return name
})
}
// Выбор пути создания компонента
function getPath() {
return new Promise(function (resolve, reject) {
if (config.lists.path) {
let itemsListPaths = config.lists.path.map(i => i);
itemsListPaths.push(lang['Entering text without saving']);
itemsListPaths.push(lang['Entering text with saving']);
resolve(vscode.window.showQuickPick(itemsListPaths, { ignoreFocusOut: true, placeHolder: lang['The path to the component?'] }));
} else {
resolve(vscode.window.showInputBox({ prompt: lang['The path to the component?'], placeHolder: config.default.path /*, value: config.default.path//*/ }));
}
}).then(path => {
if (path == '') return config.default.path;
if (path == undefined) return Promise.reject('cancel')
if (path == lang['Entering text without saving']) {
return new Promise(async function (resolve, reject) {
let path = await vscode.window.showInputBox({ prompt: lang['The path to the component?'], value: config.default.path })
if (path) resolve(path);
else reject('cancel')
})
}
if (path == lang['Entering text with saving']) {
return new Promise(async function (resolve, reject) {
let path = await vscode.window.showInputBox({ prompt: lang['The path to the component?'], value: config.default.path })
if (path) resolve(path);
else reject('cancel')
}).then(path => {
// Обработка сохранения имени в локальнй конфиг (при отсутствии генерация конфига)
files.getLocalConfig()
.catch(err => {
if (err.code == 'ENOENT')
return files.createLocalConfig()
})
.then(values => {
config.lists.path.push(path)
files.push_path(path)
})
return path;
})
}
return path
})
}
// Выбор шаблона
function getTemplates() {
if (config.lists.template) {
return new Promise((resolve, reject) => {
resolve(files.getNamesLocalTemplates())
})
.catch(err => {
return []
})
.then((LocalNames = []) => {
config.templates.local = LocalNames
let names = []
if (LocalNames.length) {
names = config.templates.local.map(n => `${lang['local']}\t` + n).join(';') + ';'
names += config.templates.global.map(n => `${lang['global']}\t` + n).join(';');
names = names.split(';')
} else {
names = config.templates.global
}
return new Promise((resolve, reject) => {
resolve(vscode.window.showQuickPick(names, {
placeHolder: lang['Select a template'],
ignoreFocusOut: true
}))
})
.then(nameTemplate => {
if (nameTemplate == undefined) return Promise.reject('cancel')
return nameTemplate
})
})
} else {
return new Promise((resolve, reject) => {
resolve(config.default.template)
})
}
}
// Меню генерации нового компонента
function generate() {
getTemplates()
.then(template => getName().then((name, err) => {
console.log(err)
return { template, name }
}))
.then(({ template, name }) => getPath().then(path => {
return { template, name, path }
}))
.then(({ template, name, path }) => {
let isLocal = false;
if (template.indexOf("\t") != -1) {
isLocal = template.split("\t")[0] == lang['local'] ? true : false;
template = template.split("\t")[1];
}
return files.getFile('/templates/' + template, isLocal)
.then(text => {
console.log(`template: ${template}, name: ${name}, path: ${path}`)
return { template, name, path, text }
})
})
.then(({ template, name, path, text }) => {
let dir = path.split('/');
dir = dir[dir.length - 1]
text = text.replace(/{{name}}/g, name).replace(/{{dir}}/g, dir);
if (!fs.existsSync(localPath + '/' + path)) {
fs.mkdirSync(localPath + '/' + path);
}
console.log(localPath + '/' + path + '/' + name + '.vue')
fs.writeFile(localPath + '/' + path + '/' + name + '.vue', text, function (err) {
if (!err) {
vscode.window.showInformationMessage(lang['Component is a created'] + `. Name: "${name}", Path: "${path}", Template: "${template}"`);
terminal.sendText('code "' + localPath + '/' + path + '/' + name + '.vue"')
} else
vscode.window.showInformationMessage(lang['Error writing file']);
}); //*/
})
}
/* ----------------------------------- Подменю config.json ----------------------------------- */
function menu_config() {
menu.length = 0;
menu[0] = 'config'
return Promise.resolve(vscode.window.showQuickPick([lang['Глобальный'], lang['Локальный']], { ignoreFocusOut: true, placeHolder: 'Какой файл config открыть?' }))
.then((value) => {
if (value == undefined) return;
if (value == lang['Глобальный']) {
//Открытие глобального файла config
menu[2] = 'global'
menu[3] = 'open'
terminal.sendText('code "' + rootPath + '/config.json"')
}
if (value == lang['Локальный']) {
//Открытие локального файла config
menu[2] = 'local'
files.getLocalConfig()
.catch(err => {
if (err.code == 'ENOENT') {
menu[3] = 'create'
return vscode.window.showQuickPick([lang['Да'], lang['Нет']], { placeHolder: lang['Локальный файл настроек не найден. Создать его?'] }).then(result => {
if (result == lang['Да']) {
return files.createLocalConfig()
.then(result => {
vscode.window.showInformationMessage(lang['Создал локальный файл config.json']);
menu[3] = 'open';
})
.catch(err => vscode.window.showErrorMessage(err)) //*/
} else {
return Promise.reject('cancel')
}
})
} else {
vscode.window.showErrorMessage(err.message)
}
})
.then(value => {
menu[3] = 'open'
terminal.sendText('code "' + localPathConfig + '/config.json"')
})
}
})
}
/* ----------------------------------- Подменю создания шаблона ----------------------------------- */
function menu_create_template() {
let list = [lang['Глобальный'], lang['Локальный']]
return Promise.resolve(vscode.window.showQuickPick(list, { placeHolder: lang['Создать шаблон глобально или локально?'], ignoreFocusOut: true }))
.then(place => {
if (place == undefined) return Promise.reject('cancel')
return new Promise((resolve, reject) => { resolve(vscode.window.showInputBox({ prompt: lang['Какое имя файла шаблона?'], placeHolder: 'component', ignoreFocusOut: true })) })
.then(name => {
return { place, name }
})
})
.then(({ place, name }) => {
if (name == '') name = 'component'
if (name == undefined) return Promise.reject('cancel')
files.createEmptyTemplate(name, place == lang['Глобальный'])
.then(content => {
terminal.sendText('code "' + (place ? rootPath + '/templates/' : files.localRoot) + '/' + name + '.vue"')
})
.catch(err => {
vscode.window.showErrorMessage(err.message || err)
})
})
}
/* ----------------------------------- Подменю редактирования шаблона ----------------------------------- */
function menu_edit_template() {
return new Promise((resolve, reject) => {
resolve(files.getNamesLocalTemplates())
})
.catch(err => {
return []
})
.then((LocalNames = []) => {
config.templates.local = LocalNames
let names = []
if (LocalNames.length) {
names = config.templates.local.map(n => `${lang['local']}\t` + n).join(';') + ';'
names += config.templates.global.map(n => `${lang['global']}\t` + n).join(';');
names = names.split(';')
} else {
names = config.templates.global
}
return new Promise((resolve, reject) => {
resolve(vscode.window.showQuickPick(names, {
placeHolder: lang['Select a template'],
ignoreFocusOut: true
}))
})
.then(nameTemplate => {
if (nameTemplate == undefined) return Promise.reject('cancel')
let type = 'global'
if (nameTemplate.indexOf('\t') != -1) {
if (nameTemplate.split('\t')[0] == lang['local']) { type = 'local' }
nameTemplate = nameTemplate.split('\t')[1]
}
return { type, name: nameTemplate }
})
.then(({ type, name }) => {
let path = (type == 'local' ? localPathConfig : rootPath) + '/templates/' + name
terminal.sendText('code "' + path + '"')
})
})
}
/* ----------------------------------- Меню настройки ----------------------------------- */
function options() {
let itemsMenu = [
lang['Создать новый шаблон'],
lang['Изменить существующий шаблон'],
lang['Открыть файл config.json'],
]
menu = [];
new Promise((resolve, reject) => {
return resolve(vscode.window.showQuickPick(itemsMenu, { ignoreFocusOut: true, placeHolder: 'Выберите пункт меню' }))
}).then(item => {
switch (item) {
case undefined:
return Promise.reject('cancel');
case lang['Создать новый шаблон']:
return menu_create_template()
case lang['Изменить существующий шаблон']:
return menu_edit_template()
case lang['Открыть файл config.json']:
return menu_config()
default:
return Promise.reject('cancel');
}
}).then((value) => {
if (value == undefined) return Promise.reject('cancel')
})
}
/* ----------------------------------- Активация функций ----------------------------------- */
function activate(context) {
context.subscriptions.push(vscode.commands.registerCommand('extension.generateComponent', generate));
context.subscriptions.push(vscode.commands.registerCommand('extension.options', options));
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;
/* ----------------------------------- //Активация функций ----------------------------------- */