-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
161 lines (148 loc) · 4.88 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
const vscode = require('vscode')
const fs = require('fs')
const download = require('download')
/**
* @param {vscode.ExtensionContext} context
*/
async function activate(context) {
let prettier = vscode.commands.registerCommand('prettier-config', async function (folder) {
// 获取配置项
const flag = await vscode.workspace.getConfiguration('prettier-config').get('tip')
const gist = await vscode.workspace.getConfiguration('prettier-config').get('gist')
const createIgnoreFile = await vscode.workspace
.getConfiguration('prettier-config')
.get('ignore')
let tool = await vscode.workspace.getConfiguration('prettier-config').get('tool')
// 获取工作区路径
let workspace
if (folder && Object.keys(folder).length > 0) {
// 使用菜单
workspace = folder.fsPath
} else {
// 使用命令
let rootPath = ''
const tmp = vscode.workspace.workspaceFolders
if (tmp === undefined) vscode.window.showErrorMessage('Please open a workspace!')
// 如果工作区中存在的多个文件夹,显示选择框
if (tmp.length > 1) {
const pick = await vscode.window.showWorkspaceFolderPick()
if (!pick) return
rootPath = pick.uri.fsPath
} else {
const pick = tmp[0]
rootPath = pick.uri.fsPath
}
workspace = rootPath
}
// default
const defaultConfigFile = fs.readFileSync(`${__dirname}/template/.prettierrc`, 'utf-8')
const defaultIgnoreFile = fs.readFileSync(`${__dirname}/template/.prettierignore`, 'utf-8')
// handle
async function copyHandle() {
if (gist && (gist.configID || gist.configRaw)) {
if (gist.configID) {
tipConfigGist()
return
}
if (gist.configRaw) {
vscode.window.withProgress(
{ location: vscode.ProgressLocation.Notification },
async progress => {
progress.report({ message: 'Downloading ...' })
fs.writeFileSync(`${workspace}/.prettierrc`, await download(gist.configRaw))
if (gist.ignoreRaw) {
fs.writeFileSync(`${workspace}/.prettierignore`, await download(gist.ignoreRaw))
}
}
)
}
} else {
fs.writeFileSync(`${workspace}/.prettierrc`, defaultConfigFile)
if (createIgnoreFile) fs.writeFileSync(`${workspace}/.prettierignore`, defaultIgnoreFile)
}
}
// tip
function tip() {
if (!flag) return
vscode.window
.showInformationMessage('Do you need to install prettier?', 'Install', 'Already Done')
.then(answer => {
if (answer === 'Install') {
// 确定参数
if (tool === '' || tool === undefined) {
return
}
let param
tool === 'npm' ? (param = 'i') : (param = 'add')
let command = `${tool} ${param} -D prettier`
// 安装依赖
exec(command)
}
})
}
// 判断工作区是否存在配置文件
if (!fs.existsSync(`${workspace}/.prettierrc`)) {
copyHandle()
tip()
} else
vscode.window
.showWarningMessage(
'An .prettierrc file already exists in this workspace.',
'Replace',
'OK'
)
.then(value => {
if (value === 'Replace') {
copyHandle()
}
})
})
context.subscriptions.push(prettier)
}
function deactivate() {}
module.exports = {
activate,
deactivate,
}
// new config tip
const tipConfigGist = () => {
vscode.window
.showWarningMessage(
'PrettierConfig for VS Code 1.4.0 NEW!',
{
modal: true,
detail: `Sorry, now you need to use raw URL for gist.
Issues: If you see this dialog many times, please switch to the other profile which installed this extension and replace 'configID' with 'configRaw'.
For the details, please refer to the extension page.`,
},
'Global Settings',
'Workspace Settings'
)
.then(value => {
if (value === undefined) {
return
}
if (value === 'Global Settings') {
vscode.commands.executeCommand('workbench.action.openSettingsJson')
vscode.workspace.getConfiguration('prettier-config').update('gist', { configRaw: '' }, true)
}
if (value === 'Workspace Settings') {
vscode.commands.executeCommand('workbench.action.openWorkspaceSettingsFile')
vscode.workspace
.getConfiguration('prettier-config')
.update('gist', { configRaw: '' }, false)
}
})
.catch(err => vscode.window.showErrorMessage(err))
}
function exec(command) {
try {
const terminal = vscode.window.createTerminal({
name: 'prettier',
})
terminal.show()
terminal.sendText(command)
} catch (err) {
vscode.window.showErrorMessage(`请手动安装依赖!prettier`)
}
}