Build: add @grafana/data package (#17436)

first step in moving non-ui components to their own package
This commit is contained in:
Ryan McKinley
2019-06-18 08:17:27 -07:00
committed by GitHub
parent efdfb1fce3
commit 401615847c
73 changed files with 223 additions and 88 deletions

View File

@@ -0,0 +1,3 @@
# Grafana Data Library
The core data components

View 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');
}

View 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"
}
}

View 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' })];

View File

@@ -0,0 +1,2 @@
export * from './utils/index';
export * from './types/index';

View File

@@ -0,0 +1 @@
export * from './navModel';

View 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 };

View File

@@ -0,0 +1 @@
export * from './string';

View 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();
});
});

View 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;
}

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["dist", "node_modules", "**/*.test.ts", "**/*.test.tsx"]
}

View 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
}
}

View File

@@ -0,0 +1,6 @@
{
"extends": "../../tslint.json",
"rules": {
"import-blacklist": [true, ["^@grafana/data.*"], ["^@grafana/ui.*"], ["^@grafana/runtime.*"]]
}
}