Skip to content

Commit 0ff9c25

Browse files
authored
Merge pull request #96 from grafana/new-dashboard-snippet
New dashboard snippet
2 parents 539bde7 + 79e2494 commit 0ff9c25

File tree

7 files changed

+739
-29
lines changed

7 files changed

+739
-29
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ node_modules
44
.idea
55
dist
66
*.vsix
7-
out
87
.vscode-test
8+
out

package.json

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"publisher": "Grafana",
1717
"categories": [
18-
"Visualization"
18+
"Visualization",
19+
"Snippets"
1920
],
2021
"keywords": [
2122
"Grafana",
@@ -24,7 +25,7 @@
2425
"activationEvents": [
2526
"onStartupFinished"
2627
],
27-
"main": "./out/extension.js",
28+
"main": "./dist/extension.js",
2829
"contributes": {
2930
"customEditors": [
3031
{
@@ -40,14 +41,14 @@
4041
"commands": [
4142
{
4243
"command": "grafana-vscode.openUrl",
43-
"title": "Edit in Grafana"
44+
"title": "Grafana: Edit in Grafana"
4445
}
4546
],
4647
"menus": {
4748
"commandPalette": [
4849
{
4950
"command": "grafana-vscode.openUrl",
50-
"when": "false"
51+
"when": "resourceExtname == .json || resourceExtname == .yaml || resourceExtname == .yml"
5152
}
5253
],
5354
"explorer/context": [
@@ -57,6 +58,12 @@
5758
}
5859
]
5960
},
61+
"snippets": [
62+
{
63+
"language": "json",
64+
"path": "./snippets.json"
65+
}
66+
],
6067
"iconThemes": [
6168
{
6269
"id": "grafana",
@@ -105,10 +112,13 @@
105112
}
106113
},
107114
"scripts": {
108-
"vscode:prepublish": "yarn run compile",
109-
"compile": "tsc -p ./",
110-
"watch": "tsc -watch -p ./",
111-
"pretest": "yarn run compile && yarn run lint",
115+
"vscode:prepublish": "yarn run package",
116+
"compile": "webpack",
117+
"watch": "webpack --watch",
118+
"package": "webpack --mode production --devtool hidden-source-map",
119+
"compile-tests": "tsc -p . --outDir out",
120+
"watch-tests": "tsc -p . -w --outDir out",
121+
"pretest": "yarn run compile-tests && yarn run compile && yarn run lint",
112122
"lint": "eslint src --ext ts",
113123
"test": "node ./out/test/runTest.js"
114124
},
@@ -129,7 +139,10 @@
129139
"glob": "^8.1.0",
130140
"mocha": "^10.2.0",
131141
"prettier": "3.0.2",
132-
"typescript": "^4.9.5"
142+
"ts-loader": "^9.4.2",
143+
"typescript": "^4.9.5",
144+
"webpack": "^5.75.0",
145+
"webpack-cli": "^5.0.1"
133146
},
134147
"dependencies": {
135148
"cors": "^2.8.5",

snippets.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"New Grafana dashboard": {
3+
"prefix": [
4+
"grafana_dashboard",
5+
"dashboard"
6+
],
7+
"description": "Create a new Grafana dashboard",
8+
"isFileTemplate": true,
9+
"body": [
10+
"{",
11+
" \"title\": \"${1:New dashboard title}\",",
12+
" \"uid\": \"${2:new-dashboard-uid}\",",
13+
" \"annotations\": {",
14+
" \"list\": [",
15+
" {",
16+
" \"builtIn\": 1,",
17+
" \"datasource\": {",
18+
" \"type\": \"grafana\",",
19+
" \"uid\": \"-- Grafana --\"",
20+
" },",
21+
" \"enable\": true,",
22+
" \"hide\": true,",
23+
" \"iconColor\": \"rgba(0, 211, 255, 1)\",",
24+
" \"name\": \"Annotations & Alerts\",",
25+
" \"type\": \"dashboard\"",
26+
" }",
27+
" ]",
28+
" },",
29+
" \"editable\": true,",
30+
" \"fiscalYearStartMonth\": 0,",
31+
" \"graphTooltip\": 0,",
32+
" \"links\": [],",
33+
" \"panels\": [],",
34+
" \"preload\": false,",
35+
" \"schemaVersion\": 39,",
36+
" \"tags\": [],",
37+
" \"templating\": {",
38+
" \"list\": []",
39+
" },",
40+
" \"time\": {",
41+
" \"from\": \"now-6h\",",
42+
" \"to\": \"now\"",
43+
" },",
44+
" \"timepicker\": {},",
45+
" \"timezone\": \"browser\",",
46+
" \"version\": 0,",
47+
" \"weekStart\": \"\"",
48+
"}"
49+
]
50+
}
51+
}

src/extension.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ export async function activate(ctx: vscode.ExtensionContext) {
1818
ctx.subscriptions.push(
1919
vscode.commands.registerCommand(
2020
"grafana-vscode.openUrl",
21-
(uri: vscode.Uri) => {
21+
(uri?: vscode.Uri) => {
2222
sendTelemetry(ctx);
23+
24+
// This command can be invoked from a contextual menu, in which case uri
25+
// has a value.
26+
// It can also be invoked from the command palette, in which case we try to find
27+
// the active document.
28+
let actualUri = uri || vscode.window.activeTextEditor?.document.uri;
29+
if (!actualUri) {
30+
return;
31+
}
32+
2333
vscode.commands.executeCommand(
2434
"vscode.openWith",
25-
uri,
35+
actualUri,
2636
GrafanaEditorProvider.viewType,
2737
);
2838
}),

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"lib": ["ES2021"],
77
"sourceMap": true,
88
"rootDir": "src",
9-
"outDir": "out",
109
"strict": true /* enable all strict type-checking options */
1110
/* Additional Checks */
1211
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */

webpack.config.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//@ts-check
2+
3+
"use strict";
4+
5+
const path = require("path");
6+
7+
//@ts-check
8+
/** @typedef {import('webpack').Configuration} WebpackConfig **/
9+
10+
/** @type WebpackConfig */
11+
const extensionConfig = {
12+
target: "node", // VS Code extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/
13+
mode: "none", // this leaves the source code as close as possible to the original (when packaging we set this to 'production')
14+
15+
entry: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
16+
output: {
17+
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
18+
path: path.resolve(__dirname, "dist"),
19+
filename: "extension.js",
20+
libraryTarget: "commonjs2",
21+
},
22+
externals: {
23+
vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
24+
// modules added here also need to be added in the .vscodeignore file
25+
},
26+
resolve: {
27+
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
28+
extensions: [".ts", ".js"],
29+
},
30+
module: {
31+
rules: [
32+
{
33+
test: /\.ts$/,
34+
exclude: /node_modules/,
35+
use: [
36+
{
37+
loader: "ts-loader",
38+
},
39+
],
40+
},
41+
],
42+
},
43+
devtool: "nosources-source-map",
44+
infrastructureLogging: {
45+
level: "log", // enables logging required for problem matchers
46+
},
47+
};
48+
module.exports = [extensionConfig];

0 commit comments

Comments
 (0)