mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Build: add @grafana/data package (#17436)
first step in moving non-ui components to their own package
This commit is contained in:
3
packages/grafana-data/README.md
Normal file
3
packages/grafana-data/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Grafana Data Library
|
||||
|
||||
The core data components
|
7
packages/grafana-data/index.js
Normal file
7
packages/grafana-data/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
module.exports = require('./index.production.js');
|
||||
} else {
|
||||
module.exports = require('./index.development.js');
|
||||
}
|
41
packages/grafana-data/package.json
Normal file
41
packages/grafana-data/package.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "@grafana/data",
|
||||
"version": "6.3.0-alpha.0",
|
||||
"description": "Grafana Data Library",
|
||||
"keywords": [
|
||||
"typescript"
|
||||
],
|
||||
"main": "src/index.ts",
|
||||
"scripts": {
|
||||
"tslint": "tslint -c tslint.json --project tsconfig.json",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"clean": "rimraf ./dist ./compiled",
|
||||
"build": "rollup -c rollup.config.ts"
|
||||
},
|
||||
"author": "Grafana Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/jest": "23.3.14",
|
||||
"@types/jquery": "1.10.35",
|
||||
"@types/lodash": "4.14.123",
|
||||
"@types/node": "10.14.1",
|
||||
"@types/papaparse": "4.5.9",
|
||||
"@types/pretty-format": "20.0.1",
|
||||
"@types/react": "16.8.16",
|
||||
"awesome-typescript-loader": "^5.2.1",
|
||||
"lodash": "^4.17.10",
|
||||
"pretty-format": "^24.5.0",
|
||||
"rollup": "1.6.0",
|
||||
"rollup-plugin-commonjs": "9.2.1",
|
||||
"rollup-plugin-node-resolve": "4.0.1",
|
||||
"rollup-plugin-sourcemaps": "0.4.2",
|
||||
"rollup-plugin-terser": "4.0.4",
|
||||
"rollup-plugin-typescript2": "0.19.3",
|
||||
"rollup-plugin-visualizer": "0.9.2",
|
||||
"typescript": "3.4.1"
|
||||
},
|
||||
"resolutions": {
|
||||
"@types/lodash": "4.14.119"
|
||||
}
|
||||
}
|
50
packages/grafana-data/rollup.config.ts
Normal file
50
packages/grafana-data/rollup.config.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import resolve from 'rollup-plugin-node-resolve';
|
||||
import commonjs from 'rollup-plugin-commonjs';
|
||||
import sourceMaps from 'rollup-plugin-sourcemaps';
|
||||
import { terser } from 'rollup-plugin-terser';
|
||||
|
||||
const pkg = require('./package.json');
|
||||
|
||||
const libraryName = pkg.name;
|
||||
|
||||
const buildCjsPackage = ({ env }) => {
|
||||
return {
|
||||
input: `compiled/index.js`,
|
||||
output: [
|
||||
{
|
||||
file: `dist/index.${env}.js`,
|
||||
name: libraryName,
|
||||
format: 'cjs',
|
||||
sourcemap: true,
|
||||
exports: 'named',
|
||||
globals: {},
|
||||
},
|
||||
],
|
||||
external: ['lodash'], // Use Lodash from grafana
|
||||
plugins: [
|
||||
commonjs({
|
||||
include: /node_modules/,
|
||||
namedExports: {
|
||||
'../../node_modules/lodash/lodash.js': [
|
||||
'flatten',
|
||||
'find',
|
||||
'upperFirst',
|
||||
'debounce',
|
||||
'isNil',
|
||||
'isNumber',
|
||||
'flattenDeep',
|
||||
'map',
|
||||
'chunk',
|
||||
'sortBy',
|
||||
'uniqueId',
|
||||
'zip',
|
||||
],
|
||||
},
|
||||
}),
|
||||
resolve(),
|
||||
sourceMaps(),
|
||||
env === 'production' && terser(),
|
||||
],
|
||||
};
|
||||
};
|
||||
export default [buildCjsPackage({ env: 'development' }), buildCjsPackage({ env: 'production' })];
|
2
packages/grafana-data/src/index.ts
Normal file
2
packages/grafana-data/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './utils/index';
|
||||
export * from './types/index';
|
1
packages/grafana-data/src/types/index.ts
Normal file
1
packages/grafana-data/src/types/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './navModel';
|
30
packages/grafana-data/src/types/navModel.ts
Normal file
30
packages/grafana-data/src/types/navModel.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export interface NavModelItem {
|
||||
text: string;
|
||||
url?: string;
|
||||
subTitle?: string;
|
||||
icon?: string;
|
||||
img?: string;
|
||||
id?: string;
|
||||
active?: boolean;
|
||||
hideFromTabs?: boolean;
|
||||
hideFromMenu?: boolean;
|
||||
divider?: boolean;
|
||||
children?: NavModelItem[];
|
||||
breadcrumbs?: NavModelBreadcrumb[];
|
||||
target?: string;
|
||||
parentItem?: NavModelItem;
|
||||
showOrgSwitcher?: boolean;
|
||||
}
|
||||
|
||||
export interface NavModel {
|
||||
main: NavModelItem;
|
||||
node: NavModelItem;
|
||||
breadcrumbs?: NavModelItem[];
|
||||
}
|
||||
|
||||
export interface NavModelBreadcrumb {
|
||||
title: string;
|
||||
url?: string;
|
||||
}
|
||||
|
||||
export type NavIndex = { [s: string]: NavModelItem };
|
1
packages/grafana-data/src/utils/index.ts
Normal file
1
packages/grafana-data/src/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './string';
|
53
packages/grafana-data/src/utils/string.test.ts
Normal file
53
packages/grafana-data/src/utils/string.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { stringToJsRegex, stringToMs } from './string';
|
||||
|
||||
describe('stringToJsRegex', () => {
|
||||
it('should parse the valid regex value', () => {
|
||||
const output = stringToJsRegex('/validRegexp/');
|
||||
expect(output).toBeInstanceOf(RegExp);
|
||||
});
|
||||
|
||||
it('should throw error on invalid regex value', () => {
|
||||
const input = '/etc/hostname';
|
||||
expect(() => {
|
||||
stringToJsRegex(input);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('stringToMs', () => {
|
||||
it('should return zero if no input', () => {
|
||||
const output = stringToMs('');
|
||||
expect(output).toBe(0);
|
||||
});
|
||||
|
||||
it('should return its input, as int, if no unit is supplied', () => {
|
||||
const output = stringToMs('1000');
|
||||
expect(output).toBe(1000);
|
||||
});
|
||||
|
||||
it('should convert 3s to 3000', () => {
|
||||
const output = stringToMs('3s');
|
||||
expect(output).toBe(3000);
|
||||
});
|
||||
|
||||
it('should convert 2m to 120000', () => {
|
||||
const output = stringToMs('2m');
|
||||
expect(output).toBe(120000);
|
||||
});
|
||||
|
||||
it('should convert 2h to 7200000', () => {
|
||||
const output = stringToMs('2h');
|
||||
expect(output).toBe(7200000);
|
||||
});
|
||||
|
||||
it('should convert 2d to 172800000', () => {
|
||||
const output = stringToMs('2d');
|
||||
expect(output).toBe(172800000);
|
||||
});
|
||||
|
||||
it('should throw on unsupported unit', () => {
|
||||
expect(() => {
|
||||
stringToMs('1y');
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
57
packages/grafana-data/src/utils/string.ts
Normal file
57
packages/grafana-data/src/utils/string.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
export function stringToJsRegex(str: string): RegExp {
|
||||
if (str[0] !== '/') {
|
||||
return new RegExp('^' + str + '$');
|
||||
}
|
||||
|
||||
const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$'));
|
||||
|
||||
if (!match) {
|
||||
throw new Error(`'${str}' is not a valid regular expression.`);
|
||||
}
|
||||
|
||||
return new RegExp(match[1], match[2]);
|
||||
}
|
||||
|
||||
export function stringToMs(str: string): number {
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const nr = parseInt(str, 10);
|
||||
const unit = str.substr(String(nr).length);
|
||||
const s = 1000;
|
||||
const m = s * 60;
|
||||
const h = m * 60;
|
||||
const d = h * 24;
|
||||
|
||||
switch (unit) {
|
||||
case 's':
|
||||
return nr * s;
|
||||
case 'm':
|
||||
return nr * m;
|
||||
case 'h':
|
||||
return nr * h;
|
||||
case 'd':
|
||||
return nr * d;
|
||||
default:
|
||||
if (!unit) {
|
||||
return isNaN(nr) ? 0 : nr;
|
||||
}
|
||||
throw new Error('Not supported unit: ' + unit);
|
||||
}
|
||||
}
|
||||
|
||||
export function toNumberString(value: number | undefined | null): string {
|
||||
if (value !== null && value !== undefined && Number.isFinite(value as number)) {
|
||||
return value.toString();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
export function toIntegerOrUndefined(value: string): number | undefined {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
const v = parseInt(value, 10);
|
||||
return isNaN(v) ? undefined : v;
|
||||
}
|
4
packages/grafana-data/tsconfig.build.json
Normal file
4
packages/grafana-data/tsconfig.build.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["dist", "node_modules", "**/*.test.ts", "**/*.test.tsx"]
|
||||
}
|
19
packages/grafana-data/tsconfig.json
Normal file
19
packages/grafana-data/tsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx", "../../public/app/types/jquery/*.ts"],
|
||||
"exclude": ["dist", "node_modules"],
|
||||
"compilerOptions": {
|
||||
"rootDirs": ["."],
|
||||
"module": "esnext",
|
||||
"outDir": "compiled",
|
||||
"declaration": true,
|
||||
"declarationDir": "dist",
|
||||
"strict": true,
|
||||
"alwaysStrict": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"typeRoots": ["./node_modules/@types", "types"],
|
||||
"skipLibCheck": true, // Temp workaround for Duplicate identifier tsc errors,
|
||||
"removeComments": false
|
||||
}
|
||||
}
|
6
packages/grafana-data/tslint.json
Normal file
6
packages/grafana-data/tslint.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "../../tslint.json",
|
||||
"rules": {
|
||||
"import-blacklist": [true, ["^@grafana/data.*"], ["^@grafana/ui.*"], ["^@grafana/runtime.*"]]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user