@powerhousedao/codegen 0.0.4 → 0.0.6
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/cli.d.ts +2 -0
- package/dist/cli.js +40 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/actions.esm.t +16 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/creators.esm.t +8 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/customUtils.esm.t +5 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/documentModel.esm.t +7 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/documentModelTest.esm.t +25 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/index.d.ts +27 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/index.esm.t +9 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/index.js +59 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/lib.esm.t +9 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/lib.inject_export.esm.t +7 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/object.esm.t +49 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/reducer.esm.t +35 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/rootIndex.esm.t +37 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/schema.esm.t +6 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/types.esm.t +19 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/utils.esm.t +43 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/actions.esm.t +22 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/creators.esm.t +34 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/customReducers.esm.t +20 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/customTest.esm.t +38 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/index.d.ts +21 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/index.js +24 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/object.esm.t +37 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/operations.esm.t +17 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/editor.esm.t +15 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/index.d.ts +19 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/index.esm.t +16 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/index.js +15 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/lib.esm.t +9 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/lib.inject_export.esm.t +7 -0
- package/dist/codegen/.hygen/templates/powerhouse/generate-editor/story.esm.t +31 -0
- package/dist/codegen/graphql.d.ts +8 -0
- package/dist/codegen/graphql.js +94 -0
- package/dist/codegen/hygen.d.ts +12 -0
- package/dist/codegen/hygen.js +101 -0
- package/dist/codegen/index.d.ts +5 -0
- package/dist/codegen/index.js +88 -0
- package/dist/codegen/utils.d.ts +2 -0
- package/dist/codegen/utils.js +37 -0
- package/dist/create-lib/index.d.ts +2 -0
- package/dist/create-lib/index.js +127 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/utils.d.ts +27 -0
- package/dist/utils.js +93 -0
- package/package.json +2 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= name %>/editor.tsx"
|
|
3
|
+
unless_exists: true
|
|
4
|
+
---
|
|
5
|
+
<% if(!documentTypes.length){ %>import { Action } from 'document-model/document';<% } %>
|
|
6
|
+
import { EditorProps } from 'document-model-editors';
|
|
7
|
+
<% documentTypes.forEach(type => { _%>
|
|
8
|
+
import { <%= documentTypesMap[type] %>State, <%= documentTypesMap[type] %>Action } from "../.<%= documentModelsDir %>/<%= h.changeCase.param(documentTypesMap[type]) %>";
|
|
9
|
+
%><% }); _%>
|
|
10
|
+
|
|
11
|
+
export type IProps = <% if(!documentTypes.length){ %>EditorProps<unknown, Action><% } else { %><% documentTypes.forEach((type, index) => { _%>EditorProps<<%= documentTypesMap[type] %>State, <%= documentTypesMap[type] %>Action%>%>><% if(index < documentTypes.length - 1){ %> | <% }%><% }); _%> <% } %>;
|
|
12
|
+
|
|
13
|
+
export default function Editor(props: IProps) {
|
|
14
|
+
return <></>;
|
|
15
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type Args = {
|
|
2
|
+
name: string;
|
|
3
|
+
rootDir: string;
|
|
4
|
+
documentModelsDir: string;
|
|
5
|
+
documentTypes: string;
|
|
6
|
+
documentTypesMap: string;
|
|
7
|
+
};
|
|
8
|
+
declare const _default: {
|
|
9
|
+
params: ({ args }: {
|
|
10
|
+
args: Args;
|
|
11
|
+
}) => {
|
|
12
|
+
rootDir: string;
|
|
13
|
+
documentModelsDir: string;
|
|
14
|
+
name: string;
|
|
15
|
+
documentTypes: string[];
|
|
16
|
+
documentTypesMap: unknown;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export default _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= name %>/index.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { type Editor as EditorModule<% if(!documentTypes.length){ %>, Action<% } %> } from 'document-model/document';
|
|
6
|
+
import Editor from './editor';
|
|
7
|
+
<% documentTypes.forEach(type => { _%>
|
|
8
|
+
import { <%= documentTypesMap[type] %>State, <%= documentTypesMap[type] %>Action } from "../.<%= documentModelsDir %>/<%= h.changeCase.param(documentTypesMap[type]) %>";
|
|
9
|
+
%><% }); _%>
|
|
10
|
+
|
|
11
|
+
export const module: <% if(!documentTypes.length){ %>EditorModule<unknown, Action><% } else { %><% documentTypes.forEach((type, index) => { _%>EditorModule<<%= documentTypesMap[type] %>State, <%= documentTypesMap[type] %>Action%>%>> <% if(index < documentTypes.length - 1){ %>| <% }%><% }); _%> <% } %>= {
|
|
12
|
+
Component: Editor,
|
|
13
|
+
documentTypes: [<% if(!documentTypes.length){ %>'*'<% } else { %><% documentTypes.forEach(type => { _%>"<%= type %>", %><% }); _%> <% } %>],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default module;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = {
|
|
4
|
+
params: ({ args }) => {
|
|
5
|
+
return {
|
|
6
|
+
rootDir: args.rootDir,
|
|
7
|
+
documentModelsDir: args.documentModelsDir,
|
|
8
|
+
name: args.name,
|
|
9
|
+
documentTypes: args.documentTypes
|
|
10
|
+
.split(',')
|
|
11
|
+
.filter(type => type !== ''),
|
|
12
|
+
documentTypesMap: JSON.parse(args.documentTypesMap),
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= name %>/<%= name %>.stories.tsx"
|
|
3
|
+
unless_exists: true
|
|
4
|
+
---
|
|
5
|
+
import Editor from './editor';
|
|
6
|
+
import { createDocumentStory } from 'document-model-editors';
|
|
7
|
+
<% if(!documentTypes.length){ %>import { baseReducer, utils } from 'document-model/document';<% } %>
|
|
8
|
+
<% documentTypes.forEach(type => { _%>
|
|
9
|
+
import * as <%= documentTypesMap[type] %>Module from "../.<%= documentModelsDir %>/<%= h.changeCase.param(documentTypesMap[type]) %>";
|
|
10
|
+
%><% }); _%>
|
|
11
|
+
|
|
12
|
+
<% if(!documentTypes.length){ %>
|
|
13
|
+
const { meta, CreateDocumentStory: <%= h.changeCase.pascal(name) %> } = createDocumentStory(
|
|
14
|
+
Editor,
|
|
15
|
+
(...args) => baseReducer(...args, document => document),
|
|
16
|
+
utils.createExtendedState(),
|
|
17
|
+
);
|
|
18
|
+
export { <%= h.changeCase.pascal(name) %> };
|
|
19
|
+
<% } %>
|
|
20
|
+
|
|
21
|
+
<% documentTypes.forEach((type, index) => { _%>
|
|
22
|
+
const { <% if(index === 0){ %>meta, <% } %>CreateDocumentStory: <%= documentTypesMap[type] %> } = createDocumentStory(
|
|
23
|
+
Editor,
|
|
24
|
+
<%= documentTypesMap[type] %>Module.reducer,
|
|
25
|
+
<%= documentTypesMap[type] %>Module.utils.createDocument(),
|
|
26
|
+
);
|
|
27
|
+
export { <%= documentTypesMap[type] %> }
|
|
28
|
+
|
|
29
|
+
%><% }); _%>
|
|
30
|
+
|
|
31
|
+
export default { ...meta, title: '<%= h.changeCase.title(name) %>' };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const generateSchema: (model: string, dir: string, { watch, format }?: {
|
|
2
|
+
watch?: boolean | undefined;
|
|
3
|
+
format?: boolean | undefined;
|
|
4
|
+
}) => Promise<any>;
|
|
5
|
+
export declare const generateSchemas: (dir: string, { watch, format }?: {
|
|
6
|
+
watch?: boolean | undefined;
|
|
7
|
+
format?: boolean | undefined;
|
|
8
|
+
}) => Promise<any>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateSchemas = exports.generateSchema = void 0;
|
|
4
|
+
const cli_1 = require("@graphql-codegen/cli");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const getDirectories = (source) => (0, fs_1.readdirSync)(source, { withFileTypes: true })
|
|
7
|
+
.filter(dirent => dirent.isDirectory())
|
|
8
|
+
.map(dirent => dirent.name);
|
|
9
|
+
const tsConfig = {
|
|
10
|
+
strict: true,
|
|
11
|
+
strictScalars: true,
|
|
12
|
+
scalars: {
|
|
13
|
+
Unknown: 'unknown',
|
|
14
|
+
DateTime: 'string',
|
|
15
|
+
Attachment: 'string',
|
|
16
|
+
Address: '`${string}:0x${string}`',
|
|
17
|
+
},
|
|
18
|
+
enumsAsTypes: true,
|
|
19
|
+
allowEnumStringTypes: true,
|
|
20
|
+
avoidOptionals: {
|
|
21
|
+
field: true,
|
|
22
|
+
},
|
|
23
|
+
useIndexSignature: true,
|
|
24
|
+
noSchemaStitching: true,
|
|
25
|
+
skipTypename: true,
|
|
26
|
+
// maybeValue: "T | null | undefined",
|
|
27
|
+
inputMaybeValue: 'T | null | undefined',
|
|
28
|
+
};
|
|
29
|
+
function schemaConfig(name, dir) {
|
|
30
|
+
return {
|
|
31
|
+
[`${dir}/${name}/gen/schema/types.ts`]: {
|
|
32
|
+
schema: [
|
|
33
|
+
{
|
|
34
|
+
[`${dir}/${name}/schema.graphql`]: {
|
|
35
|
+
skipGraphQLImport: false,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
plugins: ['typescript'],
|
|
40
|
+
config: tsConfig,
|
|
41
|
+
},
|
|
42
|
+
[`${dir}/${name}/gen/schema/zod.ts`]: {
|
|
43
|
+
schema: `${dir}/${name}/schema.graphql`,
|
|
44
|
+
plugins: ['@acaldas/graphql-codegen-typescript-validation-schema'],
|
|
45
|
+
config: {
|
|
46
|
+
importFrom: `./types`,
|
|
47
|
+
schema: 'zod',
|
|
48
|
+
...tsConfig,
|
|
49
|
+
scalarSchemas: {
|
|
50
|
+
Unknown: 'z.unknown()',
|
|
51
|
+
DateTime: 'z.string().datetime()',
|
|
52
|
+
Attachment: 'z.string()',
|
|
53
|
+
Address: 'z.custom<`${string}:0x${string}`>((val) => /^[a-zA-Z0-9]+:0x[a-fA-F0-9]{40}$/.test(val as string))',
|
|
54
|
+
},
|
|
55
|
+
directives: {
|
|
56
|
+
equals: {
|
|
57
|
+
value: ['regex', '/^$1$/'],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
withObjectType: true,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const generateSchema = (model, dir, { watch = false, format = false } = {}) => {
|
|
66
|
+
const documentModelConfig = schemaConfig(model, dir);
|
|
67
|
+
const config = {
|
|
68
|
+
overwrite: true,
|
|
69
|
+
generates: documentModelConfig,
|
|
70
|
+
watch,
|
|
71
|
+
hooks: {
|
|
72
|
+
afterOneFileWrite: format ? ['prettier --ignore-path --write'] : [],
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
return (0, cli_1.generate)(config, true);
|
|
76
|
+
};
|
|
77
|
+
exports.generateSchema = generateSchema;
|
|
78
|
+
const generateSchemas = (dir, { watch = false, format = false } = {}) => {
|
|
79
|
+
const documentModels = getDirectories(dir);
|
|
80
|
+
const documentModelConfigs = documentModels.reduce((obj, model) => ({
|
|
81
|
+
...obj,
|
|
82
|
+
...schemaConfig(model, dir),
|
|
83
|
+
}), {});
|
|
84
|
+
const config = {
|
|
85
|
+
overwrite: true,
|
|
86
|
+
generates: documentModelConfigs,
|
|
87
|
+
watch,
|
|
88
|
+
hooks: {
|
|
89
|
+
afterOneFileWrite: format ? ['prettier --ignore-path --write'] : [],
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
return (0, cli_1.generate)(config, true);
|
|
93
|
+
};
|
|
94
|
+
exports.generateSchemas = generateSchemas;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DocumentModel } from 'document-model';
|
|
2
|
+
export declare function generateAll(dir: string, { watch, format }?: {
|
|
3
|
+
watch?: boolean | undefined;
|
|
4
|
+
format?: boolean | undefined;
|
|
5
|
+
}): Promise<void>;
|
|
6
|
+
export declare function generateDocumentModel(documentModel: DocumentModel.DocumentModelState, dir: string, { watch, format }?: {
|
|
7
|
+
watch?: boolean | undefined;
|
|
8
|
+
format?: boolean | undefined;
|
|
9
|
+
}): Promise<void>;
|
|
10
|
+
export declare function generateEditor(name: string, documentTypes: string[], documentTypesMap: Record<string, string>, dir: string, documentModelsDir: string, { format }?: {
|
|
11
|
+
format?: boolean | undefined;
|
|
12
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.generateEditor = exports.generateDocumentModel = exports.generateAll = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const hygen_1 = require("hygen");
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const utils_1 = require("./utils");
|
|
11
|
+
const logger = new hygen_1.Logger(console.log.bind(console));
|
|
12
|
+
const defaultTemplates = path_1.default.join(__dirname, '.hygen', 'templates');
|
|
13
|
+
async function run(args, { watch = false, format = false } = {}) {
|
|
14
|
+
const result = await (0, hygen_1.runner)(args, {
|
|
15
|
+
templates: defaultTemplates,
|
|
16
|
+
cwd: process.cwd(),
|
|
17
|
+
logger,
|
|
18
|
+
createPrompter: () => {
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
20
|
+
return require('enquirer');
|
|
21
|
+
},
|
|
22
|
+
exec: (action, body) => {
|
|
23
|
+
const opts = body && body.length > 0 ? { input: body } : {};
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
|
|
25
|
+
return require('execa').shell(action, opts);
|
|
26
|
+
},
|
|
27
|
+
debug: !!process.env.DEBUG,
|
|
28
|
+
});
|
|
29
|
+
if (format) {
|
|
30
|
+
const execa = await import('execa');
|
|
31
|
+
result.actions
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
|
|
33
|
+
.filter(action => ['added', 'inject'].includes(action.status))
|
|
34
|
+
.forEach(action => {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
36
|
+
execa.$ `prettier --ignore-path --write ${action.subject.replace('.', process.cwd())}`;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
async function generateAll(dir, { watch = false, format = false } = {}) {
|
|
42
|
+
const files = fs_1.default.readdirSync(dir, { withFileTypes: true });
|
|
43
|
+
for (const directory of files.filter(f => f.isDirectory())) {
|
|
44
|
+
const documentModelPath = path_1.default.join(dir, directory.name, `${directory.name}.json`);
|
|
45
|
+
if (!fs_1.default.existsSync(documentModelPath)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const documentModel = await (0, utils_1.loadDocumentModel)(documentModelPath);
|
|
50
|
+
await generateDocumentModel(documentModel, dir, { watch, format });
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.error(directory.name, error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.generateAll = generateAll;
|
|
58
|
+
async function generateDocumentModel(documentModel, dir, { watch = false, format = false } = {}) {
|
|
59
|
+
// Generate the singular files for the document model logic
|
|
60
|
+
await run([
|
|
61
|
+
'powerhouse',
|
|
62
|
+
'generate-document-model',
|
|
63
|
+
'--document-model',
|
|
64
|
+
JSON.stringify(documentModel),
|
|
65
|
+
'--root-dir',
|
|
66
|
+
dir,
|
|
67
|
+
], { watch, format });
|
|
68
|
+
// Generate the module-specific files for the document model logic
|
|
69
|
+
const latestSpec = documentModel.specifications[documentModel.specifications.length - 1];
|
|
70
|
+
for (const module of latestSpec.modules) {
|
|
71
|
+
await run([
|
|
72
|
+
'powerhouse',
|
|
73
|
+
'generate-document-model-module',
|
|
74
|
+
'--document-model',
|
|
75
|
+
JSON.stringify(documentModel),
|
|
76
|
+
'--root-dir',
|
|
77
|
+
dir,
|
|
78
|
+
'--module',
|
|
79
|
+
module.name,
|
|
80
|
+
], { watch, format });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.generateDocumentModel = generateDocumentModel;
|
|
84
|
+
async function generateEditor(name, documentTypes, documentTypesMap, dir, documentModelsDir, { format = false } = {}) {
|
|
85
|
+
// Generate the singular files for the document model logic
|
|
86
|
+
await run([
|
|
87
|
+
'powerhouse',
|
|
88
|
+
'generate-editor',
|
|
89
|
+
'--name',
|
|
90
|
+
name,
|
|
91
|
+
'--root-dir',
|
|
92
|
+
dir,
|
|
93
|
+
'--document-types',
|
|
94
|
+
documentTypes.join(','),
|
|
95
|
+
'--document-types-map',
|
|
96
|
+
JSON.stringify(documentTypesMap),
|
|
97
|
+
'--document-models-dir',
|
|
98
|
+
documentModelsDir,
|
|
99
|
+
], { format });
|
|
100
|
+
}
|
|
101
|
+
exports.generateEditor = generateEditor;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
import type { PowerhouseConfig } from '../utils';
|
|
3
|
+
export declare function generate(config: PowerhouseConfig): Promise<void>;
|
|
4
|
+
export declare function generateFromFile(path: string, config: PowerhouseConfig): Promise<void>;
|
|
5
|
+
export declare function generateEditor(name: string, documentTypes: string[], config: PowerhouseConfig): Promise<void>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.generateEditor = exports.generateFromFile = exports.generate = void 0;
|
|
8
|
+
const hygen_1 = require("./hygen");
|
|
9
|
+
const graphql_1 = require("./graphql");
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
11
|
+
const path_1 = require("path");
|
|
12
|
+
const change_case_1 = require("change-case");
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
function generateGraphqlSchema(documentModel) {
|
|
15
|
+
const spec = documentModel.specifications[documentModel.specifications.length - 1];
|
|
16
|
+
if (!spec) {
|
|
17
|
+
console.log(`No spec found for ${documentModel.id}`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const { modules, state: { global, local }, } = spec;
|
|
21
|
+
const schemas = [
|
|
22
|
+
global.schema,
|
|
23
|
+
local.schema,
|
|
24
|
+
...modules
|
|
25
|
+
.map(module => [
|
|
26
|
+
`# ${module.name}`,
|
|
27
|
+
...module.operations.map(op => op.schema),
|
|
28
|
+
])
|
|
29
|
+
.flat()
|
|
30
|
+
.filter(schema => schema && schema.length > 0),
|
|
31
|
+
];
|
|
32
|
+
return schemas.join('\n\n');
|
|
33
|
+
}
|
|
34
|
+
function getDocumentTypesMap(dir) {
|
|
35
|
+
const documentTypesMap = {};
|
|
36
|
+
fs_1.default.readdirSync(dir, { withFileTypes: true })
|
|
37
|
+
.filter(dirent => dirent.isDirectory())
|
|
38
|
+
.map(dirent => dirent.name)
|
|
39
|
+
.forEach(name => {
|
|
40
|
+
const specPath = (0, path_1.resolve)(dir, name, `${name}.json`);
|
|
41
|
+
if (!fs_1.default.existsSync(specPath)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const specRaw = fs_1.default.readFileSync(specPath, 'utf-8');
|
|
45
|
+
try {
|
|
46
|
+
const spec = JSON.parse(specRaw);
|
|
47
|
+
if (spec.id) {
|
|
48
|
+
documentTypesMap[spec.id] = (0, change_case_1.pascalCase)(name);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
console.error(`Failed to parse ${specPath}`);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return documentTypesMap;
|
|
56
|
+
}
|
|
57
|
+
async function generate(config) {
|
|
58
|
+
const { format, watch } = config;
|
|
59
|
+
await (0, graphql_1.generateSchemas)(config.documentModelsDir, { format, watch });
|
|
60
|
+
await (0, hygen_1.generateAll)(config.documentModelsDir, { format, watch });
|
|
61
|
+
}
|
|
62
|
+
exports.generate = generate;
|
|
63
|
+
async function generateFromFile(path, config) {
|
|
64
|
+
// load document model spec from file
|
|
65
|
+
const documentModel = await (0, utils_1.loadDocumentModel)(path);
|
|
66
|
+
const name = (0, change_case_1.paramCase)(documentModel.name);
|
|
67
|
+
// create document model folder and spec as json
|
|
68
|
+
fs_1.default.mkdirSync((0, path_1.join)(config.documentModelsDir, name), { recursive: true });
|
|
69
|
+
fs_1.default.writeFileSync((0, path_1.join)(config.documentModelsDir, name, `${name}.json`), JSON.stringify(documentModel, null, 4));
|
|
70
|
+
// bundle graphql schemas together
|
|
71
|
+
const schemaStr = generateGraphqlSchema(documentModel);
|
|
72
|
+
if (schemaStr) {
|
|
73
|
+
fs_1.default.writeFileSync((0, path_1.join)(config.documentModelsDir, name, `schema.graphql`), schemaStr);
|
|
74
|
+
}
|
|
75
|
+
await (0, graphql_1.generateSchema)(name, config.documentModelsDir, config);
|
|
76
|
+
await (0, hygen_1.generateDocumentModel)(documentModel, config.documentModelsDir, config);
|
|
77
|
+
}
|
|
78
|
+
exports.generateFromFile = generateFromFile;
|
|
79
|
+
async function generateEditor(name, documentTypes, config) {
|
|
80
|
+
const { documentModelsDir, format } = config;
|
|
81
|
+
const docummentTypesMap = getDocumentTypesMap(documentModelsDir);
|
|
82
|
+
const invalidType = documentTypes.find(type => !Object.keys(docummentTypesMap).includes(type));
|
|
83
|
+
if (invalidType) {
|
|
84
|
+
throw new Error(`Document model for ${invalidType} not found`);
|
|
85
|
+
}
|
|
86
|
+
return (0, hygen_1.generateEditor)(name, documentTypes, docummentTypesMap, config.editorsDir, config.documentModelsDir, { format });
|
|
87
|
+
}
|
|
88
|
+
exports.generateEditor = generateEditor;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.loadDocumentModel = void 0;
|
|
7
|
+
const document_model_1 = require("document-model/document-model");
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
async function loadDocumentModel(path) {
|
|
10
|
+
let documentModel;
|
|
11
|
+
try {
|
|
12
|
+
if (!path) {
|
|
13
|
+
throw new Error('Document model file not specified');
|
|
14
|
+
}
|
|
15
|
+
else if (path.endsWith('.zip')) {
|
|
16
|
+
const file = await document_model_1.utils.loadFromFile(path);
|
|
17
|
+
documentModel = file.state.global;
|
|
18
|
+
}
|
|
19
|
+
else if (path.endsWith('.json')) {
|
|
20
|
+
const data = fs_1.default.readFileSync(path, 'utf-8');
|
|
21
|
+
const document = JSON.parse(data);
|
|
22
|
+
// z.DocumentModelStateSchema().parse(document);
|
|
23
|
+
documentModel = document;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
throw new Error('File type not supported. Must be zip or json.');
|
|
27
|
+
}
|
|
28
|
+
return documentModel;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// @ts-expect-error
|
|
32
|
+
throw error.code === 'MODULE_NOT_FOUND'
|
|
33
|
+
? new Error(`Document model not found.`)
|
|
34
|
+
: error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.loadDocumentModel = loadDocumentModel;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const child_process_1 = require("child_process");
|
|
8
|
+
const enquirer_1 = require("enquirer");
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const util_1 = __importDefault(require("util"));
|
|
12
|
+
const utils_1 = require("../utils");
|
|
13
|
+
const exec = util_1.default.promisify(child_process_1.exec);
|
|
14
|
+
const BOILERPLATE_REPO = 'https://github.com/powerhouse-inc/document-model-boilerplate.git';
|
|
15
|
+
function isUsingYarn() {
|
|
16
|
+
return (process.env.npm_config_user_agent || '').startsWith('yarn');
|
|
17
|
+
}
|
|
18
|
+
function buildPackageJson(appPath, projectName) {
|
|
19
|
+
const packageJson = JSON.parse(fs_1.default.readFileSync(path_1.default.join(appPath, 'package.json'), 'utf-8'));
|
|
20
|
+
const newPackage = {
|
|
21
|
+
...packageJson,
|
|
22
|
+
name: projectName,
|
|
23
|
+
version: '1.0.0',
|
|
24
|
+
description: '',
|
|
25
|
+
};
|
|
26
|
+
fs_1.default.writeFileSync(path_1.default.join(appPath, 'package.json'), JSON.stringify(newPackage, null, 2), 'utf8');
|
|
27
|
+
}
|
|
28
|
+
function buildPowerhouseConfig(appPath, documentModelsDir, editorsDir) {
|
|
29
|
+
const filePath = path_1.default.join(appPath, 'powerhouse.config.json');
|
|
30
|
+
const packageJson = JSON.parse(fs_1.default.readFileSync(filePath, 'utf-8'));
|
|
31
|
+
const newPackage = {
|
|
32
|
+
...packageJson,
|
|
33
|
+
documentModelsDir,
|
|
34
|
+
editorsDir,
|
|
35
|
+
};
|
|
36
|
+
fs_1.default.writeFileSync(filePath, JSON.stringify(newPackage, null, 2), 'utf8');
|
|
37
|
+
}
|
|
38
|
+
function buildIndex(appPath, documentModelsDir, editorsDir) {
|
|
39
|
+
fs_1.default.writeFileSync(path_1.default.join(appPath, 'index.ts'), `import * as documentModelsExports from '${documentModelsDir}';
|
|
40
|
+
import * as editorsExports from '${editorsDir}';
|
|
41
|
+
|
|
42
|
+
export const documentModels = Object.values(documentModelsExports);
|
|
43
|
+
export const editors = Object.values(editorsExports);`, 'utf8');
|
|
44
|
+
}
|
|
45
|
+
async function runCmd(command) {
|
|
46
|
+
try {
|
|
47
|
+
const { stdout, stderr } = await exec(command);
|
|
48
|
+
console.log(stdout);
|
|
49
|
+
console.log(stderr);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.log('\x1b[31m', error, '\x1b[0m');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function init() {
|
|
56
|
+
const args = (0, utils_1.parseArgs)(process.argv.slice(2), utils_1.configSpec);
|
|
57
|
+
// checks if a project name was provided
|
|
58
|
+
let projectName = args._.shift();
|
|
59
|
+
if (!projectName) {
|
|
60
|
+
const result = await (0, enquirer_1.prompt)([
|
|
61
|
+
{
|
|
62
|
+
type: 'input',
|
|
63
|
+
name: 'projectName',
|
|
64
|
+
message: 'What is the project name?',
|
|
65
|
+
required: true,
|
|
66
|
+
},
|
|
67
|
+
]);
|
|
68
|
+
if (!result.projectName) {
|
|
69
|
+
console.log('\x1b[31m', 'You have to provide name to your app.');
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
projectName = result.projectName;
|
|
73
|
+
}
|
|
74
|
+
const { documentModelsDir, editorsDir } = args['--interactive']
|
|
75
|
+
? await (0, utils_1.promptDirectories)()
|
|
76
|
+
: utils_1.DEFAULT_CONFIG;
|
|
77
|
+
const appPath = path_1.default.join(process.cwd(), projectName);
|
|
78
|
+
try {
|
|
79
|
+
fs_1.default.mkdirSync(appPath);
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
if (err.code === 'EEXIST') {
|
|
83
|
+
console.log('\x1b[31m', `The folder ${projectName} already exists in the current directory, please give it another name.`, '\x1b[0m');
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
console.log(err);
|
|
87
|
+
}
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
createProject(projectName, documentModelsDir, editorsDir);
|
|
91
|
+
}
|
|
92
|
+
async function createProject(projectName, documentModelsDir, editorsDir) {
|
|
93
|
+
try {
|
|
94
|
+
const useYarn = isUsingYarn();
|
|
95
|
+
console.log('\x1b[33m', 'Downloading the project structure...', '\x1b[0m');
|
|
96
|
+
await runCmd(`git clone --depth 1 ${BOILERPLATE_REPO} ${projectName}`);
|
|
97
|
+
const appPath = path_1.default.join(process.cwd(), projectName);
|
|
98
|
+
process.chdir(appPath);
|
|
99
|
+
console.log('\x1b[34m', 'Installing dependencies...', '\x1b[0m');
|
|
100
|
+
await runCmd(useYarn ? 'yarn install' : 'npm install');
|
|
101
|
+
console.log();
|
|
102
|
+
fs_1.default.rmSync(path_1.default.join(appPath, './.git'), { recursive: true });
|
|
103
|
+
await runCmd('git init');
|
|
104
|
+
try {
|
|
105
|
+
fs_1.default.mkdirSync(path_1.default.join(appPath, documentModelsDir));
|
|
106
|
+
fs_1.default.mkdirSync(path_1.default.join(appPath, editorsDir));
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (!error.message.includes('EEXIST')) {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
buildPackageJson(appPath, projectName);
|
|
114
|
+
buildPowerhouseConfig(appPath, documentModelsDir, editorsDir);
|
|
115
|
+
buildIndex(appPath, documentModelsDir, editorsDir);
|
|
116
|
+
console.log('\x1b[32m', 'The installation is done!', '\x1b[0m');
|
|
117
|
+
console.log();
|
|
118
|
+
console.log('\x1b[34m', 'You can start by typing:');
|
|
119
|
+
console.log(` cd ${projectName}`);
|
|
120
|
+
console.log(useYarn ? ' yarn generate' : ' npm run generate', '\x1b[0m');
|
|
121
|
+
console.log();
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.log(error);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
init();
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./codegen"), exports);
|
|
18
|
+
__exportStar(require("./utils"), exports);
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { generateMock as zodGenerateMock } from '@anatine/zod-mock';
|
|
2
|
+
import arg from 'arg';
|
|
3
|
+
export type PowerhouseConfig = {
|
|
4
|
+
documentModelsDir: string;
|
|
5
|
+
editorsDir: string;
|
|
6
|
+
interactive?: boolean;
|
|
7
|
+
format?: boolean;
|
|
8
|
+
watch?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare const DEFAULT_CONFIG: PowerhouseConfig;
|
|
11
|
+
export declare const configSpec: {
|
|
12
|
+
readonly '--document-models': StringConstructor;
|
|
13
|
+
readonly '--editors': StringConstructor;
|
|
14
|
+
readonly '--interactive': BooleanConstructor;
|
|
15
|
+
readonly '--format': BooleanConstructor;
|
|
16
|
+
readonly '--watch': BooleanConstructor;
|
|
17
|
+
readonly '-i': "--interactive";
|
|
18
|
+
readonly '-f': "--format";
|
|
19
|
+
readonly '-w': "--watch";
|
|
20
|
+
};
|
|
21
|
+
export declare function getConfig(): PowerhouseConfig;
|
|
22
|
+
export declare function writeConfig(config: PowerhouseConfig): void;
|
|
23
|
+
export declare function parseArgs<T extends arg.Spec>(argv: string[], spec: T): arg.Result<T>;
|
|
24
|
+
export declare function parseConfig(argv: string[]): Partial<PowerhouseConfig>;
|
|
25
|
+
export declare function promptDirectories(config?: PowerhouseConfig): Promise<Pick<PowerhouseConfig, "documentModelsDir" | "editorsDir">>;
|
|
26
|
+
export type generateMockTypeFn = typeof zodGenerateMock;
|
|
27
|
+
export declare const generateMock: generateMockTypeFn;
|