This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
yarn.ts
255 lines (212 loc) · 6.63 KB
/
yarn.ts
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
/**
* This file is part of the vscode-deploy-reloaded distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* vscode-deploy-reloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-deploy-reloaded is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import * as deploy_contracts from '../contracts';
import * as deploy_gui from '../gui';
import * as deploy_helpers from '../helpers';
import * as deploy_log from '../log';
import * as deploy_workspaces from '../workspaces';
import * as i18 from '../i18';
import * as Path from 'path';
import * as vscode from 'vscode';
interface PackageJsonFile {
dependencies?: {
[ module: string ]: any
};
}
const KEY_YARN_TOOLS_USAGE = 'vscdrLastExecutedYarnToolActions';
const KEY_LAST_RUN_YARN_ADD_MODULE = 'vscdrRunYarnAddLastModule';
/**
* Resets the NPM tool usage statistics.
*
* @param {vscode.ExtensionContext} context The extension context.
*/
export function resetYarnToolsUsage(context: vscode.ExtensionContext) {
context.globalState.update(KEY_YARN_TOOLS_USAGE, undefined).then(() => {
}, (err) => {
deploy_log.CONSOLE
.trace(err, 'tools.yarn.resetYarnToolsUsage()');
});
}
async function runYarnAdd(ws: deploy_workspaces.Workspace) {
const EXTENSION = ws.context.extension;
const YARN_MODULE = deploy_helpers.normalizeString(
await vscode.window.showInputBox(
{
placeHolder: ws.t('tools.yarn.moduleExample'),
prompt: ws.t('tools.yarn.runAdd.enterModuleName'),
value: deploy_helpers.normalizeString(
EXTENSION.globalState.get(KEY_LAST_RUN_YARN_ADD_MODULE)
)
}
),
);
if ('' === YARN_MODULE) {
return;
}
EXTENSION.globalState.update(KEY_LAST_RUN_YARN_ADD_MODULE, YARN_MODULE).then(() => {
}, (err) => {
deploy_log.CONSOLE
.trace(err, 'tools.yarn.runYarnAdd(1)');
});
const CMD = `yarn add ${YARN_MODULE}`;
ws.output.appendLine('');
ws.output.append(
ws.t('tools.yarn.executing', CMD) + ' '
);
try {
await ws.exec(CMD);
ws.output.appendLine(
`[${ws.t('ok')}]`
);
}
catch (e) {
ws.output.appendLine(
`[${ws.t('error', e)}]`
);
}
}
async function runYarnRemove(ws: deploy_workspaces.Workspace) {
const PACKAGE_JSON = Path.resolve(
Path.join(
ws.rootPath, 'package.json',
)
);
if (!(await deploy_helpers.exists(PACKAGE_JSON))) {
ws.showWarningMessage(
ws.t('tools.yarn.runRemove.packageFileNotFound',
ws.rootPath),
);
return;
}
let file: PackageJsonFile;
try {
file = JSON.parse(
(await deploy_helpers.readFile(PACKAGE_JSON)).toString('utf8')
);
}
catch (e) {
ws.showErrorMessage(
ws.t('tools.yarn.runRemove.errors.loadingPackageFileFailed',
PACKAGE_JSON, e),
);
return;
}
if (!file) {
file = {};
}
const MODULES: {
name: string,
version: string,
}[] = [];
if (file.dependencies) {
for (const M in file.dependencies) {
const MODULE_NAME = deploy_helpers.toStringSafe(M).trim();
if ('' === MODULE_NAME) {
continue;
}
MODULES.push({
name: MODULE_NAME,
version: deploy_helpers.toStringSafe(file.dependencies[M]).trim(),
});
}
}
const QUICK_PICKS: deploy_contracts.ActionQuickPick[] = MODULES.sort((x, y) => {
return deploy_helpers.compareValuesBy(x, y,
m => deploy_helpers.normalizeString(m.name));
}).map(m => {
return {
action: () => {
return m.name;
},
label: m.name,
description: m.version,
detail: PACKAGE_JSON,
};
});
if (QUICK_PICKS.length < 1) {
ws.showWarningMessage(
ws.t('tools.yarn.runRemove.packageFileContainsNoModules',
PACKAGE_JSON),
);
return;
}
const SELECTED_ITEM = await vscode.window.showQuickPick(
QUICK_PICKS
);
if (!SELECTED_ITEM) {
return;
}
const YARN_MODULE: string = SELECTED_ITEM.action();
const CMD = `yarn remove ${YARN_MODULE}`;
ws.output.appendLine('');
ws.output.append(
ws.t('tools.yarn.executing', CMD) + ' '
);
try {
await ws.exec(CMD);
ws.output.appendLine(
`[${ws.t('ok')}]`
);
}
catch (e) {
ws.output.appendLine(
`[${ws.t('error', e)}]`
);
}
}
export async function showYarnTools(context: vscode.ExtensionContext) {
const SELECTED_WORKSPACE = await deploy_workspaces.showWorkspaceQuickPick(
context,
deploy_workspaces.getActiveWorkspaces(),
{
placeHolder: i18.t('workspaces.selectWorkspace')
}
);
if (!SELECTED_WORKSPACE) {
return;
}
const QUICK_PICKS: deploy_contracts.ActionQuickPick[] = [
{
action: async () => {
await runYarnAdd(SELECTED_WORKSPACE);
},
description: SELECTED_WORKSPACE.t('tools.yarn.runAdd.description'),
detail: SELECTED_WORKSPACE.rootPath,
label: SELECTED_WORKSPACE.t('tools.yarn.runAdd.label'),
state: 0,
},
{
action: async () => {
await runYarnRemove(SELECTED_WORKSPACE);
},
description: SELECTED_WORKSPACE.t('tools.yarn.runRemove.description'),
detail: SELECTED_WORKSPACE.rootPath,
label: SELECTED_WORKSPACE.t('tools.yarn.runRemove.label'),
state: 1,
},
];
const SELECTED_ITEM = await vscode.window.showQuickPick(
deploy_gui.sortQuickPicksByUsage(
QUICK_PICKS,
SELECTED_WORKSPACE.context.extension.globalState,
KEY_YARN_TOOLS_USAGE,
)
);
if (SELECTED_ITEM) {
await SELECTED_ITEM.action();
}
}