Skip to content

Commit 3568e98

Browse files
committed
Added npm extension that contributes tasks for npm scripts
1 parent 363dcf6 commit 3568e98

8 files changed

Lines changed: 204 additions & 1 deletion

File tree

build/npm/postinstall.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ const extensions = [
3636
'grunt',
3737
'jake',
3838
'merge-conflict',
39-
'emmet'
39+
'emmet',
40+
'npm',
41+
'jake'
4042
];
4143

4244
extensions.forEach(extension => npmInstall(`extensions/${extension}`));

extensions/npm/.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Launch Extension",
6+
"type": "extensionHost",
7+
"request": "launch",
8+
"runtimeExecutable": "${execPath}",
9+
"args": [
10+
"--extensionDevelopmentPath=${workspaceRoot}"
11+
],
12+
"stopOnEntry": false,
13+
"sourceMaps": true,
14+
"outFiles": ["${workspaceRoot}/client/out/**/*.js"],
15+
"preLaunchTask": "npm"
16+
}
17+
]
18+
}

extensions/npm/.vscode/tasks.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": "0.1.0",
3+
"command": "npm",
4+
"isShellCommand": true,
5+
"showOutput": "silent",
6+
"args": ["run", "compile"],
7+
"isBackground": true,
8+
"problemMatcher": "$tsc-watch"
9+
}

extensions/npm/package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "npm",
3+
"publisher": "vscode",
4+
"description": "Extension to add task support for npm scripts.",
5+
"displayName": "Npm support for VSCode",
6+
"version": "0.0.1",
7+
"enableProposedApi": true,
8+
"engines": {
9+
"vscode": "0.10.x"
10+
},
11+
"categories": [
12+
"Other"
13+
],
14+
"scripts": {
15+
"compile": "gulp compile-extension:npm",
16+
"watch": "gulp watch-extension:npm"
17+
},
18+
"dependencies": {
19+
"vscode-nls": "^2.0.2"
20+
},
21+
"devDependencies": {
22+
"@types/node": "^7.0.12"
23+
},
24+
"main": "./out/main",
25+
"activationEvents": [
26+
"onCommand:workbench.action.tasks.runTask",
27+
"onCommand:workbench.action.tasks.build",
28+
"onCommand:workbench.action.tasks.test"
29+
],
30+
"contributes": {
31+
"configuration": {
32+
"id": "npm",
33+
"type": "object",
34+
"title": "Npm",
35+
"properties": {
36+
"npm.autoDetect": {
37+
"type": "string",
38+
"enum": [
39+
"off",
40+
"on"
41+
],
42+
"default": "on",
43+
"description": "%config.npm.autoDetect%"
44+
}
45+
}
46+
}
47+
}
48+
}

extensions/npm/package.nls.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"config.npm.autoDetect": "Controls whether auto detection of npm scripts is on or off. Default is on."
3+
}

extensions/npm/src/main.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
'use strict';
6+
7+
import * as path from 'path';
8+
import * as fs from 'fs';
9+
import * as vscode from 'vscode';
10+
11+
type AutoDetect = 'on' | 'off';
12+
let taskProvider: vscode.Disposable | undefined;
13+
14+
export function activate(_context: vscode.ExtensionContext): void {
15+
let workspaceRoot = vscode.workspace.rootPath;
16+
if (!workspaceRoot) {
17+
return;
18+
}
19+
20+
function onConfigurationChanged() {
21+
let autoDetect = vscode.workspace.getConfiguration('npm').get<AutoDetect>('autoDetect');
22+
if (taskProvider && autoDetect === 'off') {
23+
taskProvider.dispose();
24+
taskProvider = undefined;
25+
} else if (!taskProvider && autoDetect === 'on') {
26+
taskProvider = vscode.workspace.registerTaskProvider({
27+
provideTasks: () => {
28+
return getNpmScriptsAsTasks();
29+
}
30+
});
31+
}
32+
}
33+
vscode.workspace.onDidChangeConfiguration(onConfigurationChanged);
34+
onConfigurationChanged();
35+
}
36+
37+
export function deactivate(): void {
38+
if (taskProvider) {
39+
taskProvider.dispose();
40+
}
41+
}
42+
43+
async function exists(file: string): Promise<boolean> {
44+
return new Promise<boolean>((resolve, _reject) => {
45+
fs.exists(file, (value) => {
46+
resolve(value);
47+
});
48+
});
49+
}
50+
51+
async function readFile(file: string): Promise<string> {
52+
return new Promise<string>((resolve, reject) => {
53+
fs.readFile(file, (err, data) => {
54+
if (err) {
55+
reject(err);
56+
}
57+
resolve(data.toString());
58+
});
59+
});
60+
}
61+
62+
async function getNpmScriptsAsTasks(): Promise<vscode.Task[]> {
63+
let workspaceRoot = vscode.workspace.rootPath;
64+
let emptyTasks: vscode.Task[] = [];
65+
66+
if (!workspaceRoot) {
67+
return emptyTasks;
68+
}
69+
70+
let packageJson = path.join(workspaceRoot, 'package.json');
71+
if (!await exists(packageJson)) {
72+
return emptyTasks;
73+
}
74+
75+
try {
76+
var contents = await readFile(packageJson);
77+
var json = JSON.parse(contents);
78+
if (!json.scripts) {
79+
return Promise.resolve(emptyTasks);
80+
}
81+
82+
const result: vscode.Task[] = [];
83+
Object.keys(json.scripts).forEach(each => {
84+
const task = new vscode.ShellTask(`npm: run ${each}`, `npm run ${each}`);
85+
const lowerCaseTaskName = each.toLowerCase();
86+
if (lowerCaseTaskName === 'build') {
87+
task.group = vscode.TaskGroup.Build;
88+
} else if (lowerCaseTaskName === 'test') {
89+
task.group = vscode.TaskGroup.Test;
90+
}
91+
result.push(task);
92+
});
93+
return Promise.resolve(result);
94+
} catch (e) {
95+
return Promise.resolve(emptyTasks);
96+
}
97+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
/// <reference path='../../../../src/vs/vscode.d.ts'/>
7+
/// <reference path='../../../../src/vs/vscode.proposed.d.ts'/>
8+
/// <reference types='@types/node'/>

extensions/npm/tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"lib": [
6+
"es2016"
7+
],
8+
"outDir": "./out",
9+
"strictNullChecks": true,
10+
"noImplicitAny": true,
11+
"noImplicitReturns": true,
12+
"noUnusedLocals": true,
13+
"noUnusedParameters": true
14+
},
15+
"include": [
16+
"src/**/*"
17+
]
18+
}

0 commit comments

Comments
 (0)