@grafana/plugin-types-bundler 0.3.1-canary.1688.14351851013.0 → 0.3.1-canary.1688.14359239827.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/dist/args.js ADDED
@@ -0,0 +1,24 @@
1
+ import parseArgs from 'minimist';
2
+ import { join, resolve } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const args = process.argv.slice(2);
6
+ const __dirname = fileURLToPath(new URL(".", import.meta.url));
7
+ const parsedArgs = parseArgs(args, {
8
+ alias: {
9
+ entryPoint: "entry-point",
10
+ tsConfig: "ts-config",
11
+ outDir: "out-dir"
12
+ },
13
+ string: ["entryPoint", "tsConfig", "outDir"],
14
+ unknown: (arg) => {
15
+ console.error(`Unknown argument: ${arg}`);
16
+ process.exit(1);
17
+ }
18
+ });
19
+ const DEFAULT_ARGS = {
20
+ tsConfig: resolve(__dirname, "../tsconfig", "tsconfig.json"),
21
+ outDir: join(process.cwd(), "dist")
22
+ };
23
+
24
+ export { DEFAULT_ARGS, parsedArgs };
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync } from 'fs';
3
+ import { parsedArgs } from '../args.js';
4
+ import { generateTypes } from '../bundleTypes.js';
5
+ import { debug } from '../debug.js';
6
+
7
+ let { entryPoint, tsConfig, outDir } = parsedArgs;
8
+ if (entryPoint === void 0) {
9
+ console.error("Please provide the path for the entry types file as an argument.");
10
+ console.error('(E.g. "npx @grafana/plugin-types-bundler ./src/types/index.ts")');
11
+ process.exit(1);
12
+ }
13
+ if (!existsSync(entryPoint)) {
14
+ console.error(`File not found: ${entryPoint}`);
15
+ process.exit(1);
16
+ }
17
+ const startTime = Date.now().valueOf();
18
+ try {
19
+ console.log("\u26A1\uFE0F Starting to bundle types for plugin...");
20
+ debug({ entryPoint, tsConfig, outDir });
21
+ generateTypes({ entryPoint, tsConfig, outDir });
22
+ } catch (error) {
23
+ console.error("Error while bundling types:", error);
24
+ process.exit(1);
25
+ }
26
+ const endTime = Date.now().valueOf();
27
+ console.log(`\u{1F4E6} Types bundled successfully (${endTime - startTime}ms)`);
@@ -0,0 +1,63 @@
1
+ import { getImportsInfo } from '@grafana/levitate';
2
+ import { generateDtsBundle } from 'jackw-dts-bundle-gen-test';
3
+ import { mkdirSync, writeFileSync, existsSync } from 'node:fs';
4
+ import { join, extname } from 'node:path';
5
+ import { DEFAULT_ARGS } from './args.js';
6
+ import { debug } from './debug.js';
7
+
8
+ const generateTypes = (args) => {
9
+ const { entryPoint, tsConfig, outDir } = handleArgs(args);
10
+ const entryPoints = [
11
+ {
12
+ filePath: entryPoint,
13
+ libraries: {
14
+ inlinedLibraries: getImportedPackages(entryPoint)
15
+ }
16
+ }
17
+ ];
18
+ const options = {
19
+ preferredConfigPath: tsConfig
20
+ };
21
+ debug({ ...entryPoints[0], ...options });
22
+ const dts = generateDtsBundle(entryPoints, options);
23
+ const cleanedDts = cleanDTS(dts);
24
+ mkdirSync(outDir, { recursive: true });
25
+ writeFileSync(join(outDir, "index.d.ts"), cleanedDts);
26
+ };
27
+ function getImportedPackages(entryPoint) {
28
+ const { imports } = getImportsInfo(entryPoint);
29
+ const npmPackages = imports.map((importInfo) => importInfo.packageName).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
+ }
42
+ function handleArgs(args) {
43
+ let { entryPoint, tsConfig, outDir } = args;
44
+ if (entryPoint === void 0) {
45
+ throw new Error(
46
+ 'Please provide the path for the entry types file as an argument. (E.g. "npx @grafana/plugin-types-bundler --entry-point ./src/types/index.ts")'
47
+ );
48
+ }
49
+ if (!existsSync(entryPoint)) {
50
+ throw new Error(`Entry point not found: ${entryPoint}`);
51
+ }
52
+ if (!Boolean(tsConfig)) {
53
+ debug("No tsconfig provided, using default tsconfig.json ('../tsconfig/tsconfig.json')");
54
+ tsConfig = DEFAULT_ARGS.tsConfig;
55
+ }
56
+ if (!Boolean(outDir)) {
57
+ debug("No outDir provided, using default dist ('./dist')");
58
+ outDir = DEFAULT_ARGS.outDir;
59
+ }
60
+ return { entryPoint, tsConfig, outDir };
61
+ }
62
+
63
+ export { generateTypes };
package/dist/debug.js ADDED
@@ -0,0 +1,5 @@
1
+ import createDebug from 'debug';
2
+
3
+ const debug = createDebug("plugin-types-bundler");
4
+
5
+ export { debug };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/plugin-types-bundler",
3
- "version": "0.3.1-canary.1688.14351851013.0",
3
+ "version": "0.3.1-canary.1688.14359239827.0",
4
4
  "description": "Bundle grafana plugin typescript types for sharing with other plugins",
5
5
  "bin": "./dist/bin/run.js",
6
6
  "type": "module",
@@ -55,5 +55,5 @@
55
55
  "jackw-dts-bundle-gen-test": "^9.5.1",
56
56
  "minimist": "^1.2.8"
57
57
  },
58
- "gitHead": "d12e0b3b91fd84644f6a17c5c6a62bc29f0b8bca"
58
+ "gitHead": "f40be3335f11422cc5a8fc587d1d24765e7d7ccd"
59
59
  }