@grafana/plugin-types-bundler 0.0.1 → 0.0.2-canary.1162.c54d7e1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +2 -0
- package/dist/bin/run.js +25 -0
- package/dist/bundleTypes.js +41 -0
- package/dist/utils.js +20 -0
- package/package.json +27 -6
- package/src/bin/run.ts +31 -0
- package/src/bundleTypes.ts +49 -0
- package/src/utils.ts +28 -0
- package/tsconfig.json +9 -0
- package/index.js +0 -1
package/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# v0.0.1 (Wed Sep 25 2024)
|
2
|
+
|
3
|
+
#### 🐛 Bug Fix
|
4
|
+
|
5
|
+
- Types Bundler: Add package [#1092](https://github.com/grafana/plugin-tools/pull/1092) ([@jackw](https://github.com/jackw) [@leventebalogh](https://github.com/leventebalogh))
|
6
|
+
|
7
|
+
#### Authors: 2
|
8
|
+
|
9
|
+
- Jack Westbrook ([@jackw](https://github.com/jackw))
|
10
|
+
- Levente Balogh ([@leventebalogh](https://github.com/leventebalogh))
|
package/README.md
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
|
6
6
|
`@grafana/plugin-types-bundler` is a cli tool that can be used to bundle types from the source code of a Grafana plugin. This single file will wrap all the types up into a single d.ts file which can then be shared with other Grafana plugins.
|
7
7
|
|
8
|
+
It is recommended that you create a separate `.ts` file that exports only the types you want to expose rather than run this tool against the plugins `module.{ts,tsx}` file.
|
9
|
+
|
8
10
|
## Install
|
9
11
|
|
10
12
|
```
|
package/dist/bin/run.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
import { existsSync } from 'fs';
|
3
|
+
import { generateTypes } from '../bundleTypes.js';
|
4
|
+
import { parsedArgs } from '../utils.js';
|
5
|
+
const { entryPoint, tsConfig, outDir } = parsedArgs;
|
6
|
+
if (entryPoint === undefined) {
|
7
|
+
console.error('Please provide the path for the entry types file as an argument.');
|
8
|
+
console.error('(E.g. "npx @grafana/plugin-types-bundler ./src/types/index.ts")');
|
9
|
+
process.exit(1);
|
10
|
+
}
|
11
|
+
if (!existsSync(entryPoint)) {
|
12
|
+
console.error(`File not found: ${entryPoint}`);
|
13
|
+
process.exit(1);
|
14
|
+
}
|
15
|
+
const startTime = Date.now().valueOf();
|
16
|
+
try {
|
17
|
+
console.log('⚡️ Starting to bundle types for plugin...');
|
18
|
+
generateTypes({ entryPoint, tsConfig, outDir });
|
19
|
+
}
|
20
|
+
catch (error) {
|
21
|
+
console.error('Error while bundling types:', error);
|
22
|
+
process.exit(1);
|
23
|
+
}
|
24
|
+
const endTime = Date.now().valueOf();
|
25
|
+
console.log(`📦 Types bundled successfully (${endTime - startTime}ms)`);
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import { getImportsInfo } from '@grafana/levitate';
|
2
|
+
import { generateDtsBundle } from 'jackw-dts-bundle-gen-test';
|
3
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
4
|
+
import { extname, join } from 'node:path';
|
5
|
+
export const generateTypes = ({ entryPoint, tsConfig, outDir }) => {
|
6
|
+
if (!entryPoint) {
|
7
|
+
throw new Error('Please provide the path for the entry types file as an argument.');
|
8
|
+
}
|
9
|
+
const entryPoints = [
|
10
|
+
{
|
11
|
+
filePath: entryPoint,
|
12
|
+
libraries: {
|
13
|
+
inlinedLibraries: getImportedPackages(entryPoint),
|
14
|
+
},
|
15
|
+
},
|
16
|
+
];
|
17
|
+
const options = {
|
18
|
+
preferredConfigPath: tsConfig,
|
19
|
+
};
|
20
|
+
const dts = generateDtsBundle(entryPoints, options);
|
21
|
+
const cleanedDts = cleanDTS(dts);
|
22
|
+
mkdirSync(outDir, { recursive: true });
|
23
|
+
writeFileSync(join(outDir, 'index.d.ts'), cleanedDts);
|
24
|
+
};
|
25
|
+
function getImportedPackages(entryPoint) {
|
26
|
+
const { imports } = getImportsInfo(entryPoint);
|
27
|
+
const npmPackages = imports
|
28
|
+
.map((importInfo) => importInfo.packageName)
|
29
|
+
.filter((packageName) => isBareSpecifier(packageName));
|
30
|
+
return Array.from(new Set(npmPackages));
|
31
|
+
}
|
32
|
+
function cleanDTS(dtsContent) {
|
33
|
+
let dtsString = dtsContent.join('\n');
|
34
|
+
dtsString = dtsString.replace('export {};', '');
|
35
|
+
return dtsString.trim() + '\n';
|
36
|
+
}
|
37
|
+
function isBareSpecifier(packageName) {
|
38
|
+
const isRelative = packageName.startsWith('./') || packageName.startsWith('../') || packageName.startsWith('/');
|
39
|
+
const hasExtension = extname(packageName) !== '';
|
40
|
+
return !isRelative && !hasExtension;
|
41
|
+
}
|
package/dist/utils.js
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
import parseArgs from 'minimist';
|
2
|
+
import { join } from 'node:path';
|
3
|
+
const args = process.argv.slice(2);
|
4
|
+
export const parsedArgs = parseArgs(args, {
|
5
|
+
alias: {
|
6
|
+
entryPoint: 'entry-point',
|
7
|
+
tsConfig: 'ts-config',
|
8
|
+
outDir: 'out-dir',
|
9
|
+
},
|
10
|
+
string: ['entryPoint', 'tsConfig', 'outDir'],
|
11
|
+
unknown: (arg) => {
|
12
|
+
console.error(`Unknown argument: ${arg}`);
|
13
|
+
process.exit(1);
|
14
|
+
},
|
15
|
+
default: {
|
16
|
+
entryPoint: undefined,
|
17
|
+
tsConfig: join(process.cwd(), 'tsconfig.json'),
|
18
|
+
outDir: join(process.cwd(), 'dist'),
|
19
|
+
},
|
20
|
+
});
|
package/package.json
CHANGED
@@ -1,18 +1,31 @@
|
|
1
1
|
{
|
2
2
|
"name": "@grafana/plugin-types-bundler",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.2-canary.1162.c54d7e1.0",
|
4
4
|
"description": "Bundle grafana plugin typescript types for sharing with other plugins",
|
5
|
-
"
|
5
|
+
"bin": "./dist/bin/run.js",
|
6
|
+
"type": "module",
|
7
|
+
"exports": {
|
8
|
+
"node": {
|
9
|
+
"import": "./dist/bundleTypes.js"
|
10
|
+
},
|
11
|
+
"default": "./dist/bundleTypes.js"
|
12
|
+
},
|
6
13
|
"scripts": {
|
7
|
-
"test": "
|
14
|
+
"test": "vitest --passWithNoTests",
|
15
|
+
"build": "tsc --project tsconfig.json && chmod +x ./dist/bin/run.js",
|
16
|
+
"dev": "tsc --project tsconfig.json --watch",
|
17
|
+
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx ./src",
|
18
|
+
"lint:fix": "npm run lint -- --fix",
|
19
|
+
"typecheck": "tsc --noEmit"
|
8
20
|
},
|
9
21
|
"publishConfig": {
|
10
22
|
"registry": "https://registry.npmjs.org/",
|
11
|
-
"access": "public"
|
23
|
+
"access": "public",
|
24
|
+
"provenance": true
|
12
25
|
},
|
13
26
|
"repository": {
|
14
27
|
"directory": "packages/plugin-types-bundler",
|
15
|
-
"url": "
|
28
|
+
"url": "https://github.com/grafana/plugin-tools"
|
16
29
|
},
|
17
30
|
"keywords": [
|
18
31
|
"grafana",
|
@@ -28,5 +41,13 @@
|
|
28
41
|
},
|
29
42
|
"engines": {
|
30
43
|
"node": ">=18.8.0"
|
31
|
-
}
|
44
|
+
},
|
45
|
+
"dependencies": {
|
46
|
+
"@grafana/levitate": "^0.14.0",
|
47
|
+
"@types/minimist": "^1.2.2",
|
48
|
+
"@types/node": "20.10.1",
|
49
|
+
"jackw-dts-bundle-gen-test": "^9.5.1",
|
50
|
+
"minimist": "^1.2.8"
|
51
|
+
},
|
52
|
+
"gitHead": "c54d7e126ed18fae732238d624be590298414276"
|
32
53
|
}
|
package/src/bin/run.ts
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
import { existsSync } from 'fs';
|
3
|
+
import { generateTypes } from '../bundleTypes.js';
|
4
|
+
import { parsedArgs } from '../utils.js';
|
5
|
+
|
6
|
+
const { entryPoint, tsConfig, outDir } = parsedArgs;
|
7
|
+
|
8
|
+
// Check if the first argument is present
|
9
|
+
if (entryPoint === undefined) {
|
10
|
+
console.error('Please provide the path for the entry types file as an argument.');
|
11
|
+
console.error('(E.g. "npx @grafana/plugin-types-bundler ./src/types/index.ts")');
|
12
|
+
process.exit(1);
|
13
|
+
}
|
14
|
+
|
15
|
+
// Check if the file exists
|
16
|
+
if (!existsSync(entryPoint)) {
|
17
|
+
console.error(`File not found: ${entryPoint}`);
|
18
|
+
process.exit(1);
|
19
|
+
}
|
20
|
+
|
21
|
+
const startTime = Date.now().valueOf();
|
22
|
+
try {
|
23
|
+
console.log('⚡️ Starting to bundle types for plugin...');
|
24
|
+
generateTypes({ entryPoint, tsConfig, outDir });
|
25
|
+
} catch (error) {
|
26
|
+
console.error('Error while bundling types:', error);
|
27
|
+
process.exit(1);
|
28
|
+
}
|
29
|
+
|
30
|
+
const endTime = Date.now().valueOf();
|
31
|
+
console.log(`📦 Types bundled successfully (${endTime - startTime}ms)`);
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import { getImportsInfo } from '@grafana/levitate';
|
2
|
+
import { EntryPointConfig, generateDtsBundle } from 'jackw-dts-bundle-gen-test';
|
3
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
4
|
+
import { extname, join } from 'node:path';
|
5
|
+
import type { ParsedArgs } from './utils.js';
|
6
|
+
|
7
|
+
export const generateTypes = ({ entryPoint, tsConfig, outDir }: ParsedArgs) => {
|
8
|
+
if (!entryPoint) {
|
9
|
+
throw new Error('Please provide the path for the entry types file as an argument.');
|
10
|
+
}
|
11
|
+
|
12
|
+
const entryPoints: EntryPointConfig[] = [
|
13
|
+
{
|
14
|
+
filePath: entryPoint,
|
15
|
+
libraries: {
|
16
|
+
inlinedLibraries: getImportedPackages(entryPoint),
|
17
|
+
},
|
18
|
+
},
|
19
|
+
];
|
20
|
+
const options = {
|
21
|
+
preferredConfigPath: tsConfig,
|
22
|
+
};
|
23
|
+
const dts = generateDtsBundle(entryPoints, options);
|
24
|
+
const cleanedDts = cleanDTS(dts);
|
25
|
+
|
26
|
+
mkdirSync(outDir, { recursive: true });
|
27
|
+
writeFileSync(join(outDir, 'index.d.ts'), cleanedDts);
|
28
|
+
};
|
29
|
+
|
30
|
+
function getImportedPackages(entryPoint: string) {
|
31
|
+
const { imports } = getImportsInfo(entryPoint);
|
32
|
+
const npmPackages = imports
|
33
|
+
.map((importInfo) => importInfo.packageName)
|
34
|
+
.filter((packageName) => isBareSpecifier(packageName));
|
35
|
+
// Remove duplicates
|
36
|
+
return Array.from(new Set(npmPackages));
|
37
|
+
}
|
38
|
+
|
39
|
+
function cleanDTS(dtsContent: string[]) {
|
40
|
+
let dtsString = dtsContent.join('\n');
|
41
|
+
dtsString = dtsString.replace('export {};', '');
|
42
|
+
return dtsString.trim() + '\n';
|
43
|
+
}
|
44
|
+
|
45
|
+
function isBareSpecifier(packageName: string) {
|
46
|
+
const isRelative = packageName.startsWith('./') || packageName.startsWith('../') || packageName.startsWith('/');
|
47
|
+
const hasExtension = extname(packageName) !== '';
|
48
|
+
return !isRelative && !hasExtension;
|
49
|
+
}
|
package/src/utils.ts
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
import parseArgs from 'minimist';
|
2
|
+
import { join } from 'node:path';
|
3
|
+
|
4
|
+
const args = process.argv.slice(2);
|
5
|
+
|
6
|
+
export type ParsedArgs = {
|
7
|
+
entryPoint?: string;
|
8
|
+
tsConfig: string;
|
9
|
+
outDir: string;
|
10
|
+
};
|
11
|
+
|
12
|
+
export const parsedArgs = parseArgs<ParsedArgs>(args, {
|
13
|
+
alias: {
|
14
|
+
entryPoint: 'entry-point',
|
15
|
+
tsConfig: 'ts-config',
|
16
|
+
outDir: 'out-dir',
|
17
|
+
},
|
18
|
+
string: ['entryPoint', 'tsConfig', 'outDir'],
|
19
|
+
unknown: (arg) => {
|
20
|
+
console.error(`Unknown argument: ${arg}`);
|
21
|
+
process.exit(1);
|
22
|
+
},
|
23
|
+
default: {
|
24
|
+
entryPoint: undefined,
|
25
|
+
tsConfig: join(process.cwd(), 'tsconfig.json'),
|
26
|
+
outDir: join(process.cwd(), 'dist'),
|
27
|
+
},
|
28
|
+
});
|
package/tsconfig.json
ADDED
package/index.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
console.log('@grafana/plugin-types-bundler');
|