@baeta/generator-sdk 0.0.2

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 ADDED
@@ -0,0 +1,10 @@
1
+ # @baeta/generator-sdk
2
+
3
+ ## 0.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#25](https://github.com/andreisergiu98/baeta/pull/25) [`c2c5875`](https://github.com/andreisergiu98/baeta/commit/c2c5875f8356e166f34a20b3e4384f9bce093e61) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - refactor cli and plugins
8
+
9
+ - Updated dependencies [[`c2c5875`](https://github.com/andreisergiu98/baeta/commit/c2c5875f8356e166f34a20b3e4384f9bce093e61)]:
10
+ - @baeta/plugin@0.0.15
package/dist/index.cjs ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class; var _class2;// lib/file.ts
2
+ var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises);
3
+ var _path = require('path');
4
+ var File = (_class = class {
5
+ constructor(filename, content, tag) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);
6
+ this.filename = filename;
7
+ this.content = content;
8
+ this.tag = tag;
9
+ }
10
+ __init() {this.persisted = false}
11
+ __init2() {this.write = async () => {
12
+ if (this.persisted) {
13
+ return;
14
+ }
15
+ this.persisted = true;
16
+ const dir = _path.dirname.call(void 0, this.filename);
17
+ await _promises2.default.mkdir(dir, { recursive: true });
18
+ return _promises2.default.writeFile(this.filename, this.buildContent(), "utf-8");
19
+ }}
20
+ __init3() {this.unlink = async () => {
21
+ this.persisted = false;
22
+ return _promises2.default.unlink(this.filename);
23
+ }}
24
+ buildContent() {
25
+ return this.buildHeader() + this.content;
26
+ }
27
+ buildHeader() {
28
+ const ext = _path.extname.call(void 0, this.filename);
29
+ const header = [
30
+ this.createComment("This file was generated by baeta. Do not edit it directly.", ext) + "\n",
31
+ this.createComment("eslint-disable", ext),
32
+ this.createComment("prettier-ignore", ext)
33
+ ].join("\n");
34
+ return header + "\n\n";
35
+ }
36
+ createComment(comment, extension) {
37
+ if ([".gql", ".graphql"].includes(extension)) {
38
+ return `# ${comment}`;
39
+ }
40
+ return `/* ${comment} */`;
41
+ }
42
+ }, _class);
43
+
44
+ // lib/file-manager.ts
45
+ var FileManager = (_class2 = class {constructor() { _class2.prototype.__init4.call(this); }
46
+ __init4() {this.files = []}
47
+ add(...file) {
48
+ this.files.push(...file);
49
+ }
50
+ get(filename) {
51
+ return this.files.find((file) => file.filename === filename);
52
+ }
53
+ getAll() {
54
+ return this.files;
55
+ }
56
+ getByTag(tag) {
57
+ return this.files.filter((file) => file.tag === tag);
58
+ }
59
+ remove(filename) {
60
+ const index = this.files.findIndex((file) => file.filename === filename);
61
+ this.files.splice(index, 1);
62
+ }
63
+ removeAll() {
64
+ this.files = [];
65
+ }
66
+ removeByTag(tag) {
67
+ this.files = this.files.filter((file) => file.tag !== tag);
68
+ }
69
+ writeAll() {
70
+ return Promise.all(this.files.map((file) => file.write()));
71
+ }
72
+ writeByTag(tag) {
73
+ const files = this.getByTag(tag);
74
+ return Promise.all(files.map((file) => file.write()));
75
+ }
76
+ unlinkAll() {
77
+ return Promise.all(this.files.map((file) => file.unlink())).then(() => {
78
+ });
79
+ }
80
+ }, _class2);
81
+
82
+ // lib/plugin.ts
83
+ var _plugin = require('@baeta/plugin');
84
+ var GeneratorPluginVersion = /* @__PURE__ */ ((GeneratorPluginVersion2) => {
85
+ GeneratorPluginVersion2[GeneratorPluginVersion2["V1"] = 0] = "V1";
86
+ return GeneratorPluginVersion2;
87
+ })(GeneratorPluginVersion || {});
88
+ var defaultPluginFn = async (params) => {
89
+ return params.next();
90
+ };
91
+ var defaultWatchFn = () => ({ include: [], ignore: [] });
92
+ function createPluginFactoryV1(options) {
93
+ return (config) => ({
94
+ name: options.name,
95
+ config,
96
+ version: 0 /* V1 */,
97
+ type: _plugin.PluginType.Generator,
98
+ end: _nullishCoalesce(options.end, () => ( defaultPluginFn)),
99
+ generate: _nullishCoalesce(options.generate, () => ( defaultPluginFn)),
100
+ setup: _nullishCoalesce(options.setup, () => ( defaultPluginFn)),
101
+ watch: _nullishCoalesce(options.watch, () => ( defaultWatchFn))
102
+ });
103
+ }
104
+ function isGeneratorPlugin(plugin) {
105
+ return plugin.type === _plugin.PluginType.Generator;
106
+ }
107
+ function getGeneratorPlugins(plugins) {
108
+ if (!plugins) {
109
+ return [];
110
+ }
111
+ return plugins.filter(isGeneratorPlugin);
112
+ }
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+ exports.File = File; exports.FileManager = FileManager; exports.GeneratorPluginVersion = GeneratorPluginVersion; exports.createPluginFactoryV1 = createPluginFactoryV1; exports.getGeneratorPlugins = getGeneratorPlugins; exports.isGeneratorPlugin = isGeneratorPlugin;
@@ -0,0 +1,89 @@
1
+ import { PluginType } from '@baeta/plugin';
2
+
3
+ interface GeneratorOptions {
4
+ schemas: string[];
5
+ modulesDir?: string;
6
+ moduleDefinitionName?: string;
7
+ baseTypesPath?: string;
8
+ contextType?: string;
9
+ extensions?: string;
10
+ scalars?: Record<string, string>;
11
+ }
12
+
13
+ declare class File {
14
+ filename: string;
15
+ content: string;
16
+ tag: string;
17
+ persisted: boolean;
18
+ constructor(filename: string, content: string, tag: string);
19
+ write: () => Promise<void>;
20
+ unlink: () => Promise<void>;
21
+ private buildContent;
22
+ protected buildHeader(): string;
23
+ protected createComment(comment: string, extension: string): string;
24
+ }
25
+
26
+ declare class FileManager {
27
+ files: File[];
28
+ add(...file: File[]): void;
29
+ get(filename: string): File | undefined;
30
+ getAll(): File[];
31
+ getByTag(tag: string): File[];
32
+ remove(filename: string): void;
33
+ removeAll(): void;
34
+ removeByTag(tag: string): void;
35
+ writeAll(): Promise<void[]>;
36
+ writeByTag(tag: string): Promise<void[]>;
37
+ unlinkAll(): Promise<void>;
38
+ }
39
+
40
+ type Ctx<T = Record<string, unknown>> = {
41
+ fileManager: FileManager;
42
+ didSetup: string[];
43
+ didGenerate: string[];
44
+ didEnd: string[];
45
+ pluginNames: string[];
46
+ generatorOptions: GeneratorOptions;
47
+ } & T;
48
+
49
+ type MatchPattern = string | RegExp;
50
+ type Matcher = MatchPattern | MatchPattern[];
51
+ declare enum GeneratorPluginVersion {
52
+ V1 = 0
53
+ }
54
+ interface GeneratorPluginV1Params<Config, Store> {
55
+ config: Config;
56
+ ctx: Ctx<Store>;
57
+ next: () => Promise<void>;
58
+ }
59
+ type GeneratorPluginV1Fn<Config, Store> = (params: GeneratorPluginV1Params<Config, Store>) => Promise<void>;
60
+ type GeneratorPluginV1WatchOptions<Config> = (generatorOptions: GeneratorOptions, pluginConfig: Config) => {
61
+ include: string | string[];
62
+ ignore: Matcher;
63
+ };
64
+ type GeneratorPluginV1Factory<Config, Store> = {
65
+ name: string;
66
+ setup?: GeneratorPluginV1Fn<Config, Store>;
67
+ generate?: GeneratorPluginV1Fn<Config, Store>;
68
+ end?: GeneratorPluginV1Fn<Config, Store>;
69
+ watch?: GeneratorPluginV1WatchOptions<Config>;
70
+ };
71
+ interface GeneratorPluginV1<Config, Store> {
72
+ name: string;
73
+ version: GeneratorPluginVersion.V1;
74
+ type: PluginType.Generator;
75
+ config: Config;
76
+ setup: GeneratorPluginV1Fn<Config, Store>;
77
+ generate: GeneratorPluginV1Fn<Config, Store>;
78
+ end: GeneratorPluginV1Fn<Config, Store>;
79
+ watch: GeneratorPluginV1WatchOptions<Config>;
80
+ }
81
+ declare function createPluginFactoryV1<Config, Store = {}>(options: GeneratorPluginV1Factory<Config, Store>): (config: Config) => GeneratorPluginV1<Config, Store>;
82
+ declare function isGeneratorPlugin(plugin: {
83
+ type: PluginType;
84
+ }): plugin is GeneratorPluginV1<unknown, unknown>;
85
+ declare function getGeneratorPlugins(plugins?: Array<{
86
+ type: PluginType;
87
+ }>): GeneratorPluginV1<unknown, unknown>[];
88
+
89
+ export { Ctx, File, FileManager, GeneratorOptions, GeneratorPluginV1, GeneratorPluginV1Factory, GeneratorPluginV1Fn, GeneratorPluginV1Params, GeneratorPluginV1WatchOptions, GeneratorPluginVersion, createPluginFactoryV1, getGeneratorPlugins, isGeneratorPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ // lib/file.ts
2
+ import fs from "fs/promises";
3
+ import { dirname, extname } from "path";
4
+ var File = class {
5
+ constructor(filename, content, tag) {
6
+ this.filename = filename;
7
+ this.content = content;
8
+ this.tag = tag;
9
+ }
10
+ persisted = false;
11
+ write = async () => {
12
+ if (this.persisted) {
13
+ return;
14
+ }
15
+ this.persisted = true;
16
+ const dir = dirname(this.filename);
17
+ await fs.mkdir(dir, { recursive: true });
18
+ return fs.writeFile(this.filename, this.buildContent(), "utf-8");
19
+ };
20
+ unlink = async () => {
21
+ this.persisted = false;
22
+ return fs.unlink(this.filename);
23
+ };
24
+ buildContent() {
25
+ return this.buildHeader() + this.content;
26
+ }
27
+ buildHeader() {
28
+ const ext = extname(this.filename);
29
+ const header = [
30
+ this.createComment("This file was generated by baeta. Do not edit it directly.", ext) + "\n",
31
+ this.createComment("eslint-disable", ext),
32
+ this.createComment("prettier-ignore", ext)
33
+ ].join("\n");
34
+ return header + "\n\n";
35
+ }
36
+ createComment(comment, extension) {
37
+ if ([".gql", ".graphql"].includes(extension)) {
38
+ return `# ${comment}`;
39
+ }
40
+ return `/* ${comment} */`;
41
+ }
42
+ };
43
+
44
+ // lib/file-manager.ts
45
+ var FileManager = class {
46
+ files = [];
47
+ add(...file) {
48
+ this.files.push(...file);
49
+ }
50
+ get(filename) {
51
+ return this.files.find((file) => file.filename === filename);
52
+ }
53
+ getAll() {
54
+ return this.files;
55
+ }
56
+ getByTag(tag) {
57
+ return this.files.filter((file) => file.tag === tag);
58
+ }
59
+ remove(filename) {
60
+ const index = this.files.findIndex((file) => file.filename === filename);
61
+ this.files.splice(index, 1);
62
+ }
63
+ removeAll() {
64
+ this.files = [];
65
+ }
66
+ removeByTag(tag) {
67
+ this.files = this.files.filter((file) => file.tag !== tag);
68
+ }
69
+ writeAll() {
70
+ return Promise.all(this.files.map((file) => file.write()));
71
+ }
72
+ writeByTag(tag) {
73
+ const files = this.getByTag(tag);
74
+ return Promise.all(files.map((file) => file.write()));
75
+ }
76
+ unlinkAll() {
77
+ return Promise.all(this.files.map((file) => file.unlink())).then(() => {
78
+ });
79
+ }
80
+ };
81
+
82
+ // lib/plugin.ts
83
+ import { PluginType } from "@baeta/plugin";
84
+ var GeneratorPluginVersion = /* @__PURE__ */ ((GeneratorPluginVersion2) => {
85
+ GeneratorPluginVersion2[GeneratorPluginVersion2["V1"] = 0] = "V1";
86
+ return GeneratorPluginVersion2;
87
+ })(GeneratorPluginVersion || {});
88
+ var defaultPluginFn = async (params) => {
89
+ return params.next();
90
+ };
91
+ var defaultWatchFn = () => ({ include: [], ignore: [] });
92
+ function createPluginFactoryV1(options) {
93
+ return (config) => ({
94
+ name: options.name,
95
+ config,
96
+ version: 0 /* V1 */,
97
+ type: PluginType.Generator,
98
+ end: options.end ?? defaultPluginFn,
99
+ generate: options.generate ?? defaultPluginFn,
100
+ setup: options.setup ?? defaultPluginFn,
101
+ watch: options.watch ?? defaultWatchFn
102
+ });
103
+ }
104
+ function isGeneratorPlugin(plugin) {
105
+ return plugin.type === PluginType.Generator;
106
+ }
107
+ function getGeneratorPlugins(plugins) {
108
+ if (!plugins) {
109
+ return [];
110
+ }
111
+ return plugins.filter(isGeneratorPlugin);
112
+ }
113
+ export {
114
+ File,
115
+ FileManager,
116
+ GeneratorPluginVersion,
117
+ createPluginFactoryV1,
118
+ getGeneratorPlugins,
119
+ isGeneratorPlugin
120
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@baeta/generator-sdk",
3
+ "version": "0.0.2",
4
+ "homepage": "https://github.com/andreisergiu98/baeta#readme",
5
+ "bugs": {
6
+ "url": "https://github.com/andreisergiu98/baeta/issues"
7
+ },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/andreisergiu98/baeta.git",
11
+ "directory": "packages/generator-sdk"
12
+ },
13
+ "license": "MIT",
14
+ "author": "Andrei Pampu <pampu.andrei@pm.me>",
15
+ "type": "module",
16
+ "files": [
17
+ "dist",
18
+ "package.json"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "prepack": "prep",
23
+ "postpack": "prep --clean",
24
+ "types": "tsc --noEmit"
25
+ },
26
+ "dependencies": {
27
+ "@baeta/plugin": "^0.0.15",
28
+ "chokidar": "^3.5.3"
29
+ },
30
+ "devDependencies": {
31
+ "@baeta/build-tools": "^0.0.0",
32
+ "@types/node": "^18.11.18",
33
+ "typescript": "^4.9.4"
34
+ },
35
+ "engines": {
36
+ "node": ">=18.0.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "exports": {
41
+ ".": {
42
+ "import": "./dist/index.js",
43
+ "require": "./dist/index.cjs",
44
+ "types": "./dist/index.d.ts"
45
+ }
46
+ },
47
+ "types": "dist/index.d.ts"
48
+ },
49
+ "exports": {
50
+ ".": {
51
+ "import": "./dist/index.js",
52
+ "require": "./dist/index.cjs",
53
+ "types": "./dist/index.d.ts"
54
+ }
55
+ },
56
+ "types": "dist/index.d.ts"
57
+ }