mobiletto-orm-typedef-gen 2.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/README.md ADDED
@@ -0,0 +1,43 @@
1
+ mobiletto-orm-typedef-gen
2
+ =========================
3
+ This library provides tools to generate various files based on types declared using
4
+ [mobiletto-orm-typedef](https://github.com/cobbzilla/mobiletto-orm-typedef)
5
+
6
+ This is a separate library because it's almost always a dev dependency and not needed
7
+ at runtime.
8
+
9
+ ### Source
10
+ * [mobiletto-orm-typedef-gen on GitHub](https://github.com/cobbzilla/mobiletto-orm-typedef-gen)
11
+ * [mobiletto-orm-typedef-gen on npm](https://www.npmjs.com/package/mobiletto-orm-typedef-gen)
12
+
13
+ ## Support and Funding
14
+ I would be sincerely grateful for any [contribution via Patreon](https://www.patreon.com/cobbzilla)
15
+
16
+ ## Installation and usage
17
+ You can install `mobiletto-orm-typedef-gen` via npm or yarn or pnpm
18
+
19
+ ### npm package
20
+
21
+ # install with npm
22
+ npm i mobiletto-orm-typedef-gen
23
+
24
+ # install with yarn
25
+ yarn add mobiletto-orm-typedef-gen
26
+
27
+ # install with pnpm
28
+ pnpm add mobiletto-orm-typedef-gen
29
+
30
+ ### From source
31
+ To access the mobiletto-orm-typedef-gen source:
32
+
33
+ # Clone source and install dependencies
34
+ git clone https://github.com/cobbzilla/mobiletto-orm-typedef-gen.git
35
+ cd mobiletto-orm-typedef-gen
36
+ pnpm install
37
+
38
+ ### Using Mobiletto ORM Type Definitions
39
+ See the mobiletto-orm documentation on [Type Definitions](https://github.com/cobbzilla/mobiletto-orm#Type-Definitions)
40
+
41
+ You can also check out the [test suite](./test) for examples.
42
+
43
+ ### Generate TypeScript type for Mobiletto-ORM type
@@ -0,0 +1,14 @@
1
+ import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
2
+ export declare const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
3
+ export type TypeDeclaration = {
4
+ name: string;
5
+ fields: Record<string, unknown>;
6
+ root?: boolean;
7
+ };
8
+ export type GenerateOptions = {
9
+ outfile?: string;
10
+ name?: string;
11
+ disclaimer?: string;
12
+ prepareContext?: (typeDef: MobilettoOrmTypeDef, ctx: Record<string, unknown>) => Record<string, unknown>;
13
+ };
14
+ export declare const generate: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, templatePath: string, opts?: GenerateOptions) => string;
@@ -0,0 +1,99 @@
1
+ import { MobilettoOrmError, MobilettoOrmTypeDef, MobilettoOrmValidationError, } from "mobiletto-orm-typedef";
2
+ import * as fs from "fs";
3
+ import Handlebars from "handlebars";
4
+ import { dirname } from "path";
5
+ import { fileURLToPath } from "url";
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ export const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
8
+ const findDecls = (name, fields) => {
9
+ return _findDecls(name, fields, []);
10
+ };
11
+ const _findDecls = (name, fields, decls) => {
12
+ for (const fieldName of Object.keys(fields)) {
13
+ const field = fields[fieldName];
14
+ if (field.type === "object" && field.fields) {
15
+ const subName = name + "_" + field.name;
16
+ const subFields = field.fields;
17
+ decls.unshift({ name: subName, fields: subFields });
18
+ _findDecls(subName, subFields, decls);
19
+ }
20
+ }
21
+ return decls;
22
+ };
23
+ const defaultPrepareContext = (typeDef, ctx) => {
24
+ for (const fieldName of Object.keys(ctx.fields)) {
25
+ const field = ctx.fields[fieldName];
26
+ field.optional = !field.required || field.when;
27
+ field.typeIsBoolean = field.type === "boolean";
28
+ field.typeIsString = field.type === "string";
29
+ field.typeIsNumber = field.type === "number";
30
+ field.typeIsArray = field.type === "array";
31
+ field.typeIsObject = field.type === "object";
32
+ if (field.typeIsArray) {
33
+ field.valueType =
34
+ field.values && Array.isArray(field.values) && field.values.length > 0
35
+ ? typeof field.values[0]
36
+ : field.items &&
37
+ Array.isArray(field.items) &&
38
+ field.items.length > 0 &&
39
+ typeof field.items[0] === "object" &&
40
+ field.items[0].value
41
+ ? typeof field.items[0].value
42
+ : null;
43
+ if (field.valueType == null) {
44
+ const errors = {};
45
+ errors[field.name] = ["indeterminateArrayValueType"];
46
+ throw new MobilettoOrmValidationError(errors);
47
+ }
48
+ }
49
+ }
50
+ ctx.typeName = typeDef.typeName;
51
+ ctx.curlyOpen = "{";
52
+ ctx.curlyClose = "}";
53
+ return ctx;
54
+ };
55
+ export const generate = (typeDef, templatePath, opts) => {
56
+ if (!(typeDef instanceof MobilettoOrmTypeDef)) {
57
+ typeDef = new MobilettoOrmTypeDef(typeDef);
58
+ }
59
+ const disclaimer = (opts === null || opts === void 0 ? void 0 : opts.disclaimer) ? opts.disclaimer : STANDARD_AUTOGEN_FILE_DISCLAIMER;
60
+ const name = (opts === null || opts === void 0 ? void 0 : opts.name) ? opts.name : typeDef.typeName;
61
+ const decls = [...findDecls(name, typeDef.fields), { name, fields: typeDef.fields, root: true }];
62
+ let allData = "";
63
+ let first = true;
64
+ while (decls.length > 0) {
65
+ const decl = decls.pop();
66
+ if (!decl)
67
+ break;
68
+ const typeContext = defaultPrepareContext(typeDef, {
69
+ typeDef,
70
+ name: decl.name,
71
+ fields: decl.fields,
72
+ disclaimer: decls.length > 0 ? null : disclaimer,
73
+ first,
74
+ root: decl.root || false,
75
+ });
76
+ const preparedContext = (opts === null || opts === void 0 ? void 0 : opts.prepareContext) ? opts.prepareContext(typeDef, typeContext) : typeContext;
77
+ first = false;
78
+ typeContext.ctx = typeContext;
79
+ // sanity, scrub templatePath; we should only every read template files
80
+ const lastSlash = templatePath.lastIndexOf("/");
81
+ if (lastSlash === -1 || lastSlash === templatePath.length - 1) {
82
+ throw new MobilettoOrmError(`invalid template path: ${templatePath}`);
83
+ }
84
+ const endPath = templatePath.substring(0, lastSlash).replace(/^[A-Z0-9/_-]/g, "") + templatePath.substring(lastSlash);
85
+ if (endPath !== templatePath) {
86
+ throw new MobilettoOrmError(`invalid template path: ${templatePath}`);
87
+ }
88
+ if (!templatePath.endsWith(".ts.hbs")) {
89
+ throw new MobilettoOrmError(`invalid template path: ${templatePath}`);
90
+ }
91
+ const template = fs.readFileSync(__dirname + "/" + templatePath).toString("utf8");
92
+ const data = Handlebars.compile(template)(preparedContext);
93
+ allData = data + allData;
94
+ }
95
+ if (opts === null || opts === void 0 ? void 0 : opts.outfile) {
96
+ fs.writeFileSync(opts.outfile, allData);
97
+ }
98
+ return allData;
99
+ };
@@ -0,0 +1,3 @@
1
+ import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
2
+ import { GenerateOptions } from "../generate.js";
3
+ export declare const generateTypeScriptType: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, opts?: GenerateOptions) => string;
@@ -0,0 +1,4 @@
1
+ import { generate } from "../generate.js";
2
+ export const generateTypeScriptType = (typeDef, opts) => {
3
+ return generate(typeDef, "templates/tsType.ts.hbs", opts);
4
+ };
@@ -0,0 +1,3 @@
1
+ import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
2
+ import { GenerateOptions } from "../generate.js";
3
+ export declare const generateYup: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, opts?: GenerateOptions) => string;
@@ -0,0 +1,8 @@
1
+ import { generate } from "../generate.js";
2
+ export const generateYup = (typeDef, opts) => {
3
+ opts || (opts = {});
4
+ opts.prepareContext = (typeDef, ctx) => {
5
+ return ctx;
6
+ };
7
+ return generate(typeDef, "templates/yup.ts.hbs", opts);
8
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./generate.js";
2
+ export * from "./generators/tsType.js";
3
+ export * from "./generators/yup.js";
@@ -0,0 +1,3 @@
1
+ export * from "./generate.js";
2
+ export * from "./generators/tsType.js";
3
+ export * from "./generators/yup.js";
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "mobiletto-orm-typedef",
3
+ "type": "commonjs",
4
+ "main": "./index.js",
5
+ "homepage": "https://github.com/cobbzilla/mobiletto-orm-typedef",
6
+ "author": "Jonathan Cobb <udo9jqld@duck.com> (https://github.com/cobbzilla)",
7
+ "funding": {
8
+ "type": "patreon",
9
+ "url": "https://www.patreon.com/cobbzilla"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/cobbzilla/mobiletto-orm-typedef"
14
+ },
15
+ "license": "Apache-2.0"
16
+ }
@@ -0,0 +1,13 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}
2
+ {{/if}}export type {{ name }}Type = {{#if root}}{
3
+ _meta?: {
4
+ id: string;
5
+ version: string;
6
+ removed?: boolean;
7
+ ctime: number;
8
+ mtime: number;
9
+ };
10
+ {{else}}{
11
+ {{/if}}{{#each fields}} {{this.name}}{{#if this.optional}}?{{/if}}: {{#if this.typeIsObject}}{{ ../name }}_{{this.name}}Type{{else if this.typeIsArray}}{{this.valueType}}[]{{else}}{{this.type}}{{/if}};
12
+ {{/each}}
13
+ };
@@ -0,0 +1,5 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}
2
+ import * as yup from "yup";{{/if}}
3
+ export const {{name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
4
+ {{name}}: {{#if typeIsObject}}{{ ../name }}_{{this.name}}Schema{{else}}{{#if typeIsArray}}yup.array(yup.{{valueType}}),{{else}}yup.{{type}}(){{/if}}{{#if required}}.required('required'){{/if}}{{#if minValue}}.min({{minValue}}, 'minValue'){{/if}}{{#if maxValue}}.max({{maxValue}}, 'maxValue'){{/if}}{{#if min}}.min({{min}}, 'min'){{/if}}{{#if max}}.max({{max}}, 'max'){{/if}}{{#if regex}}.matches({{regex}}, 'regex'){{/if}}{{/if}},{{/each}}
5
+ });
@@ -0,0 +1,14 @@
1
+ import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
2
+ export declare const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
3
+ export type TypeDeclaration = {
4
+ name: string;
5
+ fields: Record<string, unknown>;
6
+ root?: boolean;
7
+ };
8
+ export type GenerateOptions = {
9
+ outfile?: string;
10
+ name?: string;
11
+ disclaimer?: string;
12
+ prepareContext?: (typeDef: MobilettoOrmTypeDef, ctx: Record<string, unknown>) => Record<string, unknown>;
13
+ };
14
+ export declare const generate: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, templatePath: string, opts?: GenerateOptions) => string;
@@ -0,0 +1,99 @@
1
+ import { MobilettoOrmError, MobilettoOrmTypeDef, MobilettoOrmValidationError, } from "mobiletto-orm-typedef";
2
+ import * as fs from "fs";
3
+ import Handlebars from "handlebars";
4
+ import { dirname } from "path";
5
+ import { fileURLToPath } from "url";
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ export const STANDARD_AUTOGEN_FILE_DISCLAIMER = "DO NOT EDIT THIS FILE. AUTO-GENERATED BY mobiletto-orm-typedef-gen";
8
+ const findDecls = (name, fields) => {
9
+ return _findDecls(name, fields, []);
10
+ };
11
+ const _findDecls = (name, fields, decls) => {
12
+ for (const fieldName of Object.keys(fields)) {
13
+ const field = fields[fieldName];
14
+ if (field.type === "object" && field.fields) {
15
+ const subName = name + "_" + field.name;
16
+ const subFields = field.fields;
17
+ decls.unshift({ name: subName, fields: subFields });
18
+ _findDecls(subName, subFields, decls);
19
+ }
20
+ }
21
+ return decls;
22
+ };
23
+ const defaultPrepareContext = (typeDef, ctx) => {
24
+ for (const fieldName of Object.keys(ctx.fields)) {
25
+ const field = ctx.fields[fieldName];
26
+ field.optional = !field.required || field.when;
27
+ field.typeIsBoolean = field.type === "boolean";
28
+ field.typeIsString = field.type === "string";
29
+ field.typeIsNumber = field.type === "number";
30
+ field.typeIsArray = field.type === "array";
31
+ field.typeIsObject = field.type === "object";
32
+ if (field.typeIsArray) {
33
+ field.valueType =
34
+ field.values && Array.isArray(field.values) && field.values.length > 0
35
+ ? typeof field.values[0]
36
+ : field.items &&
37
+ Array.isArray(field.items) &&
38
+ field.items.length > 0 &&
39
+ typeof field.items[0] === "object" &&
40
+ field.items[0].value
41
+ ? typeof field.items[0].value
42
+ : null;
43
+ if (field.valueType == null) {
44
+ const errors = {};
45
+ errors[field.name] = ["indeterminateArrayValueType"];
46
+ throw new MobilettoOrmValidationError(errors);
47
+ }
48
+ }
49
+ }
50
+ ctx.typeName = typeDef.typeName;
51
+ ctx.curlyOpen = "{";
52
+ ctx.curlyClose = "}";
53
+ return ctx;
54
+ };
55
+ export const generate = (typeDef, templatePath, opts) => {
56
+ if (!(typeDef instanceof MobilettoOrmTypeDef)) {
57
+ typeDef = new MobilettoOrmTypeDef(typeDef);
58
+ }
59
+ const disclaimer = (opts === null || opts === void 0 ? void 0 : opts.disclaimer) ? opts.disclaimer : STANDARD_AUTOGEN_FILE_DISCLAIMER;
60
+ const name = (opts === null || opts === void 0 ? void 0 : opts.name) ? opts.name : typeDef.typeName;
61
+ const decls = [...findDecls(name, typeDef.fields), { name, fields: typeDef.fields, root: true }];
62
+ let allData = "";
63
+ let first = true;
64
+ while (decls.length > 0) {
65
+ const decl = decls.pop();
66
+ if (!decl)
67
+ break;
68
+ const typeContext = defaultPrepareContext(typeDef, {
69
+ typeDef,
70
+ name: decl.name,
71
+ fields: decl.fields,
72
+ disclaimer: decls.length > 0 ? null : disclaimer,
73
+ first,
74
+ root: decl.root || false,
75
+ });
76
+ const preparedContext = (opts === null || opts === void 0 ? void 0 : opts.prepareContext) ? opts.prepareContext(typeDef, typeContext) : typeContext;
77
+ first = false;
78
+ typeContext.ctx = typeContext;
79
+ // sanity, scrub templatePath; we should only every read template files
80
+ const lastSlash = templatePath.lastIndexOf("/");
81
+ if (lastSlash === -1 || lastSlash === templatePath.length - 1) {
82
+ throw new MobilettoOrmError(`invalid template path: ${templatePath}`);
83
+ }
84
+ const endPath = templatePath.substring(0, lastSlash).replace(/^[A-Z0-9/_-]/g, "") + templatePath.substring(lastSlash);
85
+ if (endPath !== templatePath) {
86
+ throw new MobilettoOrmError(`invalid template path: ${templatePath}`);
87
+ }
88
+ if (!templatePath.endsWith(".ts.hbs")) {
89
+ throw new MobilettoOrmError(`invalid template path: ${templatePath}`);
90
+ }
91
+ const template = fs.readFileSync(__dirname + "/" + templatePath).toString("utf8");
92
+ const data = Handlebars.compile(template)(preparedContext);
93
+ allData = data + allData;
94
+ }
95
+ if (opts === null || opts === void 0 ? void 0 : opts.outfile) {
96
+ fs.writeFileSync(opts.outfile, allData);
97
+ }
98
+ return allData;
99
+ };
@@ -0,0 +1,3 @@
1
+ import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
2
+ import { GenerateOptions } from "../generate.js";
3
+ export declare const generateTypeScriptType: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, opts?: GenerateOptions) => string;
@@ -0,0 +1,4 @@
1
+ import { generate } from "../generate.js";
2
+ export const generateTypeScriptType = (typeDef, opts) => {
3
+ return generate(typeDef, "templates/tsType.ts.hbs", opts);
4
+ };
@@ -0,0 +1,3 @@
1
+ import { MobilettoOrmTypeDef, MobilettoOrmTypeDefConfig } from "mobiletto-orm-typedef";
2
+ import { GenerateOptions } from "../generate.js";
3
+ export declare const generateYup: (typeDef: MobilettoOrmTypeDef | MobilettoOrmTypeDefConfig, opts?: GenerateOptions) => string;
@@ -0,0 +1,8 @@
1
+ import { generate } from "../generate.js";
2
+ export const generateYup = (typeDef, opts) => {
3
+ opts || (opts = {});
4
+ opts.prepareContext = (typeDef, ctx) => {
5
+ return ctx;
6
+ };
7
+ return generate(typeDef, "templates/yup.ts.hbs", opts);
8
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./generate.js";
2
+ export * from "./generators/tsType.js";
3
+ export * from "./generators/yup.js";
@@ -0,0 +1,3 @@
1
+ export * from "./generate.js";
2
+ export * from "./generators/tsType.js";
3
+ export * from "./generators/yup.js";
@@ -0,0 +1,13 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}
2
+ {{/if}}export type {{ name }}Type = {{#if root}}{
3
+ _meta?: {
4
+ id: string;
5
+ version: string;
6
+ removed?: boolean;
7
+ ctime: number;
8
+ mtime: number;
9
+ };
10
+ {{else}}{
11
+ {{/if}}{{#each fields}} {{this.name}}{{#if this.optional}}?{{/if}}: {{#if this.typeIsObject}}{{ ../name }}_{{this.name}}Type{{else if this.typeIsArray}}{{this.valueType}}[]{{else}}{{this.type}}{{/if}};
12
+ {{/each}}
13
+ };
@@ -0,0 +1,5 @@
1
+ {{#if disclaimer}}// {{{ disclaimer }}}
2
+ import * as yup from "yup";{{/if}}
3
+ export const {{name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
4
+ {{name}}: {{#if typeIsObject}}{{ ../name }}_{{this.name}}Schema{{else}}{{#if typeIsArray}}yup.array(yup.{{valueType}}),{{else}}yup.{{type}}(){{/if}}{{#if required}}.required('required'){{/if}}{{#if minValue}}.min({{minValue}}, 'minValue'){{/if}}{{#if maxValue}}.max({{maxValue}}, 'maxValue'){{/if}}{{#if min}}.min({{min}}, 'min'){{/if}}{{#if max}}.max({{max}}, 'max'){{/if}}{{#if regex}}.matches({{regex}}, 'regex'){{/if}}{{/if}},{{/each}}
5
+ });
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "mobiletto-orm-typedef-gen",
3
+ "version": "2.0.2",
4
+ "type": "module",
5
+ "description": "generate various files based on mobiletto-orm-typedef definitions",
6
+ "keywords": [
7
+ "storage",
8
+ "s3",
9
+ "b2",
10
+ "simple",
11
+ "filesystem",
12
+ "orm",
13
+ "tool",
14
+ "mobiletto",
15
+ "mobiletto-orm"
16
+ ],
17
+ "homepage": "https://github.com/cobbzilla/mobiletto-orm-typedef-gen",
18
+ "author": "Jonathan Cobb <udo9jqld@duck.com> (https://github.com/cobbzilla)",
19
+ "funding": {
20
+ "type": "patreon",
21
+ "url": "https://www.patreon.com/cobbzilla"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/cobbzilla/mobiletto-orm-typedef-gen"
26
+ },
27
+ "license": "Apache-2.0",
28
+ "scripts": {
29
+ "test": "mocha ./test/*.spec.js",
30
+ "tsc": "tsc --module es2020 -p tsconfig.json && tsc --module es2020 -p tsconfig-cjs.json && copyfiles --flat --error src/templates/*.hbs lib/esm/templates/ && copyfiles --flat --error src/templates/*.hbs lib/cjs/templates/",
31
+ "lint": "npx eslint src test",
32
+ "lint:fix": "npm run lint -- --fix",
33
+ "prettier": "npx prettier src test --check",
34
+ "prettier:fix": "npm run prettier -- --write"
35
+ },
36
+ "main": "./lib/cjs/index.js",
37
+ "module": "./lib/esm/index.js",
38
+ "files": [
39
+ "lib/"
40
+ ],
41
+ "devDependencies": {
42
+ "@types/chai": "^4.3.5",
43
+ "@types/mocha": "^10.0.1",
44
+ "@types/node": "^20.3.3",
45
+ "@typescript-eslint/eslint-plugin": "^5.61.0",
46
+ "@typescript-eslint/parser": "^5.61.0",
47
+ "chai": "^4.3.6",
48
+ "copyfiles": "^2.4.1",
49
+ "eslint": "^8.44.0",
50
+ "eslint-config-prettier": "^8.8.0",
51
+ "handlebars": "^4.7.7",
52
+ "mobiletto-orm-typedef": "^2.1.8",
53
+ "mocha": "^10.2.0",
54
+ "prettier": "^2.8.8",
55
+ "tslint-config-prettier": "^1.18.0",
56
+ "typescript": "^5.1.6",
57
+ "yup": "^1.2.0"
58
+ }
59
+ }