/
npm.ts
84 lines (73 loc) · 2.34 KB
/
npm.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
/**
* This file is part of the vscode-powertools distribution.
* Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://www.e-go-mobile.com/)
*
* vscode-powertools 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-powertools 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 childProcess from 'child_process';
import * as ego_helpers from '../helpers';
import * as ego_workspace from '../workspace';
import * as path from 'path';
import * as util from 'util';
import * as vscode from 'vscode';
/**
* Runs NPM tasks on startup.
*/
export async function runNPMStartupTasks() {
const WORKSPACE: ego_workspace.Workspace = this;
if (WORKSPACE.isInFinalizeState) {
return;
}
if (!WORKSPACE.isInitialized) {
return;
}
const SETTINGS = WORKSPACE.settings;
if (!SETTINGS) {
return;
}
if (!ego_helpers.toBooleanSafe(SETTINGS.runNPMInstall)) {
return;
}
const PACKAGE_JSON = path.resolve(
path.join(
WORKSPACE.rootPath, 'package.json'
)
);
if (!(await ego_helpers.isFile(PACKAGE_JSON, false))) {
return; // no 'package.json' file
}
const NODE_MODULES = path.resolve(
path.join(
WORKSPACE.rootPath, 'node_modules'
)
);
if (await ego_helpers.exists(NODE_MODULES)) {
return; // 'node_modules' already exists
}
try {
await vscode.window.withProgress({
cancellable: false,
location: vscode.ProgressLocation.Window,
title: 'Node.js',
}, async (progress) => {
progress.report({
message: "Running 'npm install' ...",
});
await util.promisify(childProcess.exec)('npm install', {
cwd: WORKSPACE.rootPath,
});
});
} catch (e) {
ego_helpers.showErrorMessage(e);
}
}