@bufbuild/protoplugin 0.0.9

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.
Files changed (39) hide show
  1. package/README.md +38 -0
  2. package/dist/cjs/create-es-plugin.js +117 -0
  3. package/dist/cjs/ecmascript/gencommon.js +338 -0
  4. package/dist/cjs/ecmascript/generated-file.js +300 -0
  5. package/dist/cjs/ecmascript/import-symbol.js +34 -0
  6. package/dist/cjs/ecmascript/index.js +30 -0
  7. package/dist/cjs/ecmascript/runtime-imports.js +46 -0
  8. package/dist/cjs/ecmascript/schema.js +84 -0
  9. package/dist/cjs/ecmascript/target.js +15 -0
  10. package/dist/cjs/error.js +34 -0
  11. package/dist/cjs/index.js +22 -0
  12. package/dist/cjs/package.json +1 -0
  13. package/dist/cjs/plugin.js +15 -0
  14. package/dist/cjs/run-node.js +84 -0
  15. package/dist/esm/create-es-plugin.js +113 -0
  16. package/dist/esm/ecmascript/gencommon.js +327 -0
  17. package/dist/esm/ecmascript/generated-file.js +296 -0
  18. package/dist/esm/ecmascript/import-symbol.js +30 -0
  19. package/dist/esm/ecmascript/index.js +21 -0
  20. package/dist/esm/ecmascript/runtime-imports.js +42 -0
  21. package/dist/esm/ecmascript/schema.js +80 -0
  22. package/dist/esm/ecmascript/target.js +14 -0
  23. package/dist/esm/error.js +29 -0
  24. package/dist/esm/index.js +17 -0
  25. package/dist/esm/plugin.js +14 -0
  26. package/dist/esm/run-node.js +80 -0
  27. package/dist/types/create-es-plugin.d.ts +21 -0
  28. package/dist/types/ecmascript/gencommon.d.ts +23 -0
  29. package/dist/types/ecmascript/generated-file.d.ts +63 -0
  30. package/dist/types/ecmascript/import-symbol.d.ts +39 -0
  31. package/dist/types/ecmascript/index.d.ts +7 -0
  32. package/dist/types/ecmascript/runtime-imports.d.ts +21 -0
  33. package/dist/types/ecmascript/schema.d.ts +41 -0
  34. package/dist/types/ecmascript/target.d.ts +4 -0
  35. package/dist/types/error.d.ts +4 -0
  36. package/dist/types/index.d.ts +4 -0
  37. package/dist/types/plugin.d.ts +18 -0
  38. package/dist/types/run-node.d.ts +12 -0
  39. package/package.json +48 -0
@@ -0,0 +1,63 @@
1
+ import type { DescEnum, DescFile, DescMessage } from "@bufbuild/protobuf";
2
+ import { CodeGeneratorResponse } from "@bufbuild/protobuf";
3
+ import type { ImportSymbol } from "./import-symbol.js";
4
+ import type { RuntimeImports } from "./runtime-imports";
5
+ /**
6
+ * All types that can be passed to GeneratedFile.print()
7
+ */
8
+ declare type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | DescMessage | DescEnum | Printable[];
9
+ /**
10
+ * Represents a JavaScript, TypeScript, or TypeScript declaration file.
11
+ */
12
+ export interface GeneratedFile {
13
+ /**
14
+ * Create a standard preamble the includes comments at the top of the
15
+ * protobuf source file (like a license header), as well as information
16
+ * about the code generator and its version.
17
+ *
18
+ * The preamble is always placed at the very top of the generated file,
19
+ * above import statements.
20
+ */
21
+ preamble(file: DescFile): void;
22
+ /**
23
+ * Add a line of code to the file.
24
+ *
25
+ * - string: Prints the string verbatim.
26
+ * - number or boolean: Prints a literal.
27
+ * - bigint: Prints an expression using protoInt64.parse().
28
+ * - Uint8Array: Prints an expression that re-created the array.
29
+ * - ImportSymbol: Adds an import statement and prints the name of the symbol.
30
+ * - DescMessage or DescEnum: Imports the type if necessary, and prints the name.
31
+ */
32
+ print(...any: Printable[]): void;
33
+ /**
34
+ * Reserves an identifier in this file.
35
+ */
36
+ export(name: string): ImportSymbol;
37
+ /**
38
+ * Import a message or enumeration generated by protoc-gen-es.
39
+ */
40
+ import(type: DescMessage | DescEnum): ImportSymbol;
41
+ /**
42
+ * Import any symbol from a file or package.
43
+ *
44
+ * The import path can point to a package, for example `@foo/bar/baz.js`, or
45
+ * to a file, for example `./bar/baz.js`.
46
+ *
47
+ * Note that while paths to a file begin with a `./`, they must be
48
+ * relative to the project root. The import path is automatically made
49
+ * relative to the current file.
50
+ */
51
+ import(name: string, from: string): ImportSymbol;
52
+ }
53
+ export interface GenerateFileToResponse {
54
+ toResponse(res: CodeGeneratorResponse): void;
55
+ }
56
+ declare type CreateTypeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;
57
+ export declare function createGeneratedFile(name: string, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, preambleSettings: {
58
+ pluginName: string;
59
+ pluginVersion: string;
60
+ parameter: string | undefined;
61
+ tsNocheck: boolean;
62
+ }): GeneratedFile & GenerateFileToResponse;
63
+ export {};
@@ -0,0 +1,39 @@
1
+ /**
2
+ * An import symbol represents an ECMAScript import.
3
+ */
4
+ export declare type ImportSymbol = {
5
+ readonly kind: "es_symbol";
6
+ /**
7
+ * The name to import.
8
+ */
9
+ readonly name: string;
10
+ /**
11
+ * The import path.
12
+ *
13
+ * The path can point to a package, for example `@foo/bar/baz.js`, or
14
+ * to a file, for example `./bar/baz.js`.
15
+ *
16
+ * Note that while paths to a file begin with a `./`, they must be
17
+ * relative to the project root.
18
+ */
19
+ readonly from: string;
20
+ /**
21
+ * Whether this is a type-only import - an import that only exists in
22
+ * TypeScript.
23
+ */
24
+ readonly typeOnly: boolean;
25
+ /**
26
+ * Create a copy of this import, and make it type-only for TypeScript.
27
+ */
28
+ toTypeOnly(): ImportSymbol;
29
+ /**
30
+ * The unique ID based on name and from, disregarding typeOnly.
31
+ */
32
+ readonly id: EsSymbolId;
33
+ };
34
+ /**
35
+ * Create a new import symbol.
36
+ */
37
+ export declare function createImportSymbol(name: string, from: string, typeOnly?: boolean): ImportSymbol;
38
+ declare type EsSymbolId = string;
39
+ export {};
@@ -0,0 +1,7 @@
1
+ export { Target } from "./target.js";
2
+ export { Schema } from "./schema.js";
3
+ export { RuntimeImports } from "./runtime-imports.js";
4
+ export { GeneratedFile } from "./generated-file.js";
5
+ export { ImportSymbol } from "./import-symbol.js";
6
+ export declare const localName: typeof import("@bufbuild/protobuf/dist/types/private/names.js").localName;
7
+ export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
@@ -0,0 +1,21 @@
1
+ import type { ImportSymbol } from "./import-symbol.js";
2
+ export interface RuntimeImports {
3
+ proto2: ImportSymbol;
4
+ proto3: ImportSymbol;
5
+ Message: ImportSymbol;
6
+ PartialMessage: ImportSymbol;
7
+ PlainMessage: ImportSymbol;
8
+ FieldList: ImportSymbol;
9
+ MessageType: ImportSymbol;
10
+ BinaryReadOptions: ImportSymbol;
11
+ BinaryWriteOptions: ImportSymbol;
12
+ JsonReadOptions: ImportSymbol;
13
+ JsonWriteOptions: ImportSymbol;
14
+ JsonValue: ImportSymbol;
15
+ JsonObject: ImportSymbol;
16
+ protoInt64: ImportSymbol;
17
+ ScalarType: ImportSymbol;
18
+ MethodKind: ImportSymbol;
19
+ MethodIdempotency: ImportSymbol;
20
+ }
21
+ export declare function createRuntimeImports(bootstrapWkt: boolean): RuntimeImports;
@@ -0,0 +1,41 @@
1
+ import type { CodeGeneratorRequest, DescFile } from "@bufbuild/protobuf";
2
+ import { CodeGeneratorResponse } from "@bufbuild/protobuf";
3
+ import type { GeneratedFile } from "./generated-file.js";
4
+ import { RuntimeImports } from "./runtime-imports.js";
5
+ import type { Target } from "./target";
6
+ /**
7
+ * Schema describes the files and types that the plugin is requested to
8
+ * generate.
9
+ */
10
+ export interface Schema {
11
+ /**
12
+ * The files we are asked to generate.
13
+ */
14
+ readonly files: readonly DescFile[];
15
+ /**
16
+ * All files contained in the code generator request.
17
+ */
18
+ readonly allFiles: readonly DescFile[];
19
+ /**
20
+ * The plugin option `target`. A code generator should support all targets.
21
+ */
22
+ readonly targets: readonly Target[];
23
+ /**
24
+ * Provides some symbols from the runtime library @bufbuild/protobuf.
25
+ */
26
+ readonly runtime: RuntimeImports;
27
+ /**
28
+ * Generate a new file with the given name.
29
+ */
30
+ generateFile(name: string): GeneratedFile;
31
+ /**
32
+ * The original google.protobuf.compiler.CodeGeneratorRequest.
33
+ */
34
+ readonly proto: CodeGeneratorRequest;
35
+ }
36
+ interface SchemaController {
37
+ schema: Schema;
38
+ toResponse: (res: CodeGeneratorResponse) => void;
39
+ }
40
+ export declare function createSchema(request: CodeGeneratorRequest, targets: Target[], pluginName: string, pluginVersion: string, tsNocheck: boolean, bootstrapWkt: boolean): SchemaController;
41
+ export {};
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Represents possible values of the plugin option `target`.
3
+ */
4
+ export declare type Target = "js" | "ts" | "dts";
@@ -0,0 +1,4 @@
1
+ export declare class PluginOptionError extends Error {
2
+ constructor(option: string, reason?: unknown);
3
+ }
4
+ export declare function reasonToString(reason: unknown): string;
@@ -0,0 +1,4 @@
1
+ export { Plugin } from "./plugin.js";
2
+ export { Schema } from "./ecmascript/schema.js";
3
+ export { runNodeJs } from "./run-node.js";
4
+ export { createEcmaScriptPlugin } from "./create-es-plugin.js";
@@ -0,0 +1,18 @@
1
+ import type { CodeGeneratorRequest, CodeGeneratorResponse } from "@bufbuild/protobuf";
2
+ /**
3
+ * Represents any code generator plugin.
4
+ */
5
+ export interface Plugin {
6
+ /**
7
+ * Name of this code generator plugin.
8
+ */
9
+ name: string;
10
+ /**
11
+ * Version of this code generator plugin.
12
+ */
13
+ version: string;
14
+ /**
15
+ * Run this plugin for the given request.
16
+ */
17
+ run(request: CodeGeneratorRequest): CodeGeneratorResponse;
18
+ }
@@ -0,0 +1,12 @@
1
+ import type { Plugin } from "./plugin.js";
2
+ /**
3
+ * Run a plugin with Node.js.
4
+ *
5
+ * ```
6
+ * #!/usr/bin/env node
7
+ * const {runNodeJs} = require("@bufbuild/protoplugin");
8
+ * const {myPlugin} = require("./protoc-gen-x-plugin.js");
9
+ * runNodeJs(myPlugin);
10
+ * ```
11
+ */
12
+ export declare function runNodeJs(plugin: Plugin): void;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@bufbuild/protoplugin",
3
+ "version": "0.0.9",
4
+ "license": "(Apache-2.0 AND BSD-3-Clause)",
5
+ "description": "Helps to create your own Protocol Buffers code generators.",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/bufbuild/protobuf-es.git",
9
+ "directory": "packages/protoplugin"
10
+ },
11
+ "sideEffects": false,
12
+ "scripts": {
13
+ "clean": "rm -rf ./dist/cjs/* ./dist/esm/* ./dist/types/*",
14
+ "build": "npm run build:cjs && npm run build:esm+types",
15
+ "build:cjs": "npx tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
16
+ "build:esm+types": "npx tsc --project tsconfig.json --module ES2015 --outDir ./dist/esm --declaration --declarationDir ./dist/types"
17
+ },
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/esm/index.js",
22
+ "require": "./dist/cjs/index.js",
23
+ "default": "./dist/esm/index.js"
24
+ },
25
+ "./ecmascript": {
26
+ "import": "./dist/esm/ecmascript/index.js",
27
+ "require": "./dist/cjs/ecmascript/index.js",
28
+ "default": "./dist/esm/ecmascript/index.js"
29
+ }
30
+ },
31
+ "types": "./dist/types/index.d.ts",
32
+ "typesVersions": {
33
+ "*": {
34
+ "ecmascript": [
35
+ "./dist/types/ecmascript/index.d.ts"
36
+ ]
37
+ }
38
+ },
39
+ "dependencies": {
40
+ "@bufbuild/protobuf": "^0.0.9"
41
+ },
42
+ "devDependencies": {
43
+ "typescript": "^4.7.4"
44
+ },
45
+ "files": [
46
+ "dist/**/"
47
+ ]
48
+ }