@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
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const index_1 = require("./codegen/index");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
async function parseCommand(argv) {
|
|
7
|
+
const args = (0, utils_1.parseArgs)(argv, {
|
|
8
|
+
'--editor': String,
|
|
9
|
+
'-e': '--editor',
|
|
10
|
+
'--document-types': String,
|
|
11
|
+
});
|
|
12
|
+
const editorName = args['--editor'];
|
|
13
|
+
return {
|
|
14
|
+
editor: !!editorName,
|
|
15
|
+
editorName,
|
|
16
|
+
documentTypes: args['--document-types'],
|
|
17
|
+
arg: args._,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async function main() {
|
|
21
|
+
const argv = process.argv.slice(2);
|
|
22
|
+
const baseConfig = (0, utils_1.getConfig)();
|
|
23
|
+
const argsConfig = (0, utils_1.parseConfig)(argv);
|
|
24
|
+
const config = { ...baseConfig, ...argsConfig };
|
|
25
|
+
if (config.interactive) {
|
|
26
|
+
const result = await (0, utils_1.promptDirectories)(config);
|
|
27
|
+
Object.assign(config, result);
|
|
28
|
+
}
|
|
29
|
+
const command = await parseCommand(argv);
|
|
30
|
+
if (command.editor) {
|
|
31
|
+
await (0, index_1.generateEditor)(command.editorName, command.documentTypes?.split(/[|,;]/g) ?? [], config);
|
|
32
|
+
}
|
|
33
|
+
else if (command.arg.length === 2) {
|
|
34
|
+
await (0, index_1.generateFromFile)(command.arg[1], config);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
await (0, index_1.generate)(config);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
main();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/actions.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
<% modules.forEach(module => { _%>
|
|
6
|
+
import { <%= h.changeCase.pascal(documentType) %><%= h.changeCase.pascal(module.name) %>Action } from './<%= module.name %>/actions';
|
|
7
|
+
<% }); _%>
|
|
8
|
+
|
|
9
|
+
<% modules.forEach(module => { _%>
|
|
10
|
+
export * from './<%= module.name %>/actions';
|
|
11
|
+
<% }); _%>
|
|
12
|
+
|
|
13
|
+
export type <%= h.changeCase.pascal(documentType) %>Action =
|
|
14
|
+
<% modules.forEach(module => { _%>
|
|
15
|
+
| <%= h.changeCase.pascal(documentType) %><%= h.changeCase.pascal(module.name) %>Action
|
|
16
|
+
<% }); _%>;
|
package/dist/codegen/.hygen/templates/powerhouse/generate-document-model/documentModelTest.esm.t
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/src/tests/document-model.test.ts"
|
|
3
|
+
unless_exists: true
|
|
4
|
+
---
|
|
5
|
+
/**
|
|
6
|
+
* This is a scaffold file meant for customization:
|
|
7
|
+
* - change it by adding new tests or modifying the existing ones
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import utils, { initialGlobalState, initialLocalState } from '../../gen/utils';
|
|
11
|
+
|
|
12
|
+
describe('<%= h.changeCase.title(documentType) %> Document Model', () => {
|
|
13
|
+
it('should create a new <%= h.changeCase.title(documentType) %> document', () => {
|
|
14
|
+
const document = utils.createDocument();
|
|
15
|
+
|
|
16
|
+
expect(document).toBeDefined();
|
|
17
|
+
expect(document.documentType).toBe('<%- documentTypeId %>');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should create a new <%= h.changeCase.title(documentType) %> document with a valid initial state', () => {
|
|
21
|
+
const document = utils.createDocument();
|
|
22
|
+
expect(document.state.global).toStrictEqual(initialGlobalState);
|
|
23
|
+
expect(document.state.local).toStrictEqual(initialLocalState);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type Args = {
|
|
2
|
+
documentModel: string;
|
|
3
|
+
rootDir: string;
|
|
4
|
+
};
|
|
5
|
+
declare const _default: {
|
|
6
|
+
params: ({ args }: {
|
|
7
|
+
args: Args;
|
|
8
|
+
}) => {
|
|
9
|
+
initialGlobalState: string;
|
|
10
|
+
initialLocalState: string;
|
|
11
|
+
rootDir: string;
|
|
12
|
+
documentModel: string;
|
|
13
|
+
documentTypeId: string;
|
|
14
|
+
documentType: string;
|
|
15
|
+
extension: string;
|
|
16
|
+
modules: {
|
|
17
|
+
name: string;
|
|
18
|
+
__typename?: "Module" | undefined;
|
|
19
|
+
description: import("document-model/document-model").Maybe<string>;
|
|
20
|
+
id: string;
|
|
21
|
+
operations: import("document-model/document-model").Operation[];
|
|
22
|
+
}[];
|
|
23
|
+
fileExtension: string;
|
|
24
|
+
hasLocalSchema: boolean;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export default _default;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const change_case_1 = require("change-case");
|
|
4
|
+
function documentModelToString(documentModel) {
|
|
5
|
+
return JSON.stringify({
|
|
6
|
+
...documentModel,
|
|
7
|
+
specifications: documentModel.specifications.map(s => ({
|
|
8
|
+
...s,
|
|
9
|
+
state: Object.keys(s.state).reduce((values, scope) => {
|
|
10
|
+
const state = s.state[scope];
|
|
11
|
+
return {
|
|
12
|
+
...values,
|
|
13
|
+
[scope]: {
|
|
14
|
+
...state,
|
|
15
|
+
// initial value has to be stringified twice
|
|
16
|
+
// as it is expected to be a string
|
|
17
|
+
initialValue: JSON.stringify(state.initialValue),
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}, {}),
|
|
21
|
+
})),
|
|
22
|
+
}, null, 4);
|
|
23
|
+
}
|
|
24
|
+
exports.default = {
|
|
25
|
+
params: ({ args }) => {
|
|
26
|
+
const documentModel = JSON.parse(args.documentModel);
|
|
27
|
+
const latestSpec = documentModel.specifications[documentModel.specifications.length - 1];
|
|
28
|
+
return {
|
|
29
|
+
rootDir: args.rootDir,
|
|
30
|
+
documentModel: documentModelToString(documentModel),
|
|
31
|
+
documentTypeId: documentModel.id,
|
|
32
|
+
documentType: documentModel.name,
|
|
33
|
+
extension: documentModel.extension,
|
|
34
|
+
modules: latestSpec.modules.map(m => ({
|
|
35
|
+
...m,
|
|
36
|
+
name: (0, change_case_1.paramCase)(m.name),
|
|
37
|
+
})),
|
|
38
|
+
fileExtension: documentModel.extension,
|
|
39
|
+
hasLocalSchema: latestSpec.state.local.schema !== '',
|
|
40
|
+
...getInitialStates(latestSpec.state),
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
function getInitialStates(scopeState) {
|
|
45
|
+
const { global, local } = scopeState;
|
|
46
|
+
const scopes = { global, local };
|
|
47
|
+
Object.entries(scopes).forEach(([scope, state]) => {
|
|
48
|
+
if (state.schema !== '' && state.initialValue === '') {
|
|
49
|
+
throw new Error(`${scope.charAt(0).toLocaleUpperCase() + scope.slice(1)} scope has a defined schema but is missing an initial value.`);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
initialGlobalState: handleEmptyState(global.initialValue),
|
|
54
|
+
initialLocalState: handleEmptyState(local.initialValue),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function handleEmptyState(state) {
|
|
58
|
+
return state === '' ? '{}' : state;
|
|
59
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/object.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { BaseDocument, ExtendedState, PartialState, applyMixins, SignalDispatch } from 'document-model/document';
|
|
6
|
+
import { <%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState } from './types';
|
|
7
|
+
import { <%= h.changeCase.pascal(documentType) %>Action } from './actions';
|
|
8
|
+
import { reducer } from './reducer';
|
|
9
|
+
import utils from './utils';
|
|
10
|
+
<% modules.forEach(module => { _%>
|
|
11
|
+
import <%= h.changeCase.pascal(documentType) %>_<%= h.changeCase.pascal(module.name) %> from './<%= module.name %>/object';
|
|
12
|
+
<% }); _%>
|
|
13
|
+
|
|
14
|
+
<% modules.forEach(module => { _%>
|
|
15
|
+
export * from './<%= module.name %>/object';
|
|
16
|
+
<% }); _%>
|
|
17
|
+
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
19
|
+
interface <%= h.changeCase.pascal(documentType) %> extends
|
|
20
|
+
<%= modules.map(m => ' ' + h.changeCase.pascal(documentType) + '_' + h.changeCase.pascal(m.name)).join(',\n') %> {}
|
|
21
|
+
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
23
|
+
class <%= h.changeCase.pascal(documentType) %> extends BaseDocument<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>Action, <%= h.changeCase.pascal(documentType) %>LocalState> {
|
|
24
|
+
static fileExtension = '<%= extension %>';
|
|
25
|
+
|
|
26
|
+
constructor(initialState?: Partial<ExtendedState<PartialState<<%= h.changeCase.pascal(documentType) %>State>, PartialState<<%= h.changeCase.pascal(documentType) %>LocalState>>>, dispatch?: SignalDispatch) {
|
|
27
|
+
super(reducer, utils.createDocument(initialState), dispatch);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public saveToFile(path: string, name?: string) {
|
|
31
|
+
return super.saveToFile(path, <%= h.changeCase.pascal(documentType) %>.fileExtension, name);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public loadFromFile(path: string) {
|
|
35
|
+
return super.loadFromFile(path);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static async fromFile(path: string) {
|
|
39
|
+
const document = new this();
|
|
40
|
+
await document.loadFromFile(path);
|
|
41
|
+
return document;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
applyMixins(<%= h.changeCase.pascal(documentType) %>, [
|
|
46
|
+
<%= modules.map(m => ' ' + h.changeCase.pascal(documentType) + '_' + h.changeCase.pascal(m.name)).join(',\n') %>
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
export { <%= h.changeCase.pascal(documentType) %> };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/reducer.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { ImmutableStateReducer, utils } from "document-model/document";
|
|
6
|
+
import { <%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState, z } from './schema';
|
|
7
|
+
import { <%= h.changeCase.pascal(documentType) %>Action } from './actions';
|
|
8
|
+
|
|
9
|
+
<% modules.forEach(m => { _%>
|
|
10
|
+
import { reducer as <%= h.changeCase.pascal(m.name) %>Reducer } from '../src/reducers/<%= h.changeCase.param(m.name) %>';
|
|
11
|
+
<%_ }); %>
|
|
12
|
+
|
|
13
|
+
const stateReducer: ImmutableStateReducer<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>Action, <%= h.changeCase.pascal(documentType) %>LocalState> =
|
|
14
|
+
(state, action, dispatch) => {
|
|
15
|
+
if (utils.isBaseAction(action)) {
|
|
16
|
+
return state;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
switch (action.type) {
|
|
20
|
+
<%-
|
|
21
|
+
modules.map(m => m.operations.map(o =>
|
|
22
|
+
' case "' + h.changeCase.constant(o.name) + '":\n' +
|
|
23
|
+
' ' + (o.schema !== null ?
|
|
24
|
+
'z.' + h.changeCase.pascalCase(o.name) + 'InputSchema().parse(action.input);\n' :
|
|
25
|
+
'if (Object.keys(action.input).length > 0) throw new Error("Expected empty input for action ' + h.changeCase.constant(o.name) + '");\n') +
|
|
26
|
+
' ' + h.changeCase.pascal(m.name) + 'Reducer.' + h.changeCase.camel(o.name) + 'Operation(state[action.scope], action, dispatch);\n' +
|
|
27
|
+
' break;\n'
|
|
28
|
+
).join('\n')).join('\n')
|
|
29
|
+
%>
|
|
30
|
+
default:
|
|
31
|
+
return state;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const reducer = utils.createReducer<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>Action, <%= h.changeCase.pascal(documentType) %>LocalState>(stateReducer);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/index.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
/**
|
|
6
|
+
* This is a scaffold file meant for customization.
|
|
7
|
+
* Delete the file and run the code generator again to have it reset
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { actions as BaseActions, DocumentModel } from 'document-model/document';
|
|
11
|
+
import { actions as <%= h.changeCase.pascal(documentType) %>Actions, <%= h.changeCase.pascal(documentType) %> } from './gen';
|
|
12
|
+
import { reducer } from './gen/reducer';
|
|
13
|
+
import { documentModel } from './gen/document-model';
|
|
14
|
+
import genUtils from './gen/utils';
|
|
15
|
+
import * as customUtils from './src/utils';
|
|
16
|
+
import { <%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>Action } from './gen/types';
|
|
17
|
+
|
|
18
|
+
const Document = <%= h.changeCase.pascal(documentType) %>;
|
|
19
|
+
const utils = { ...genUtils, ...customUtils };
|
|
20
|
+
const actions = { ...BaseActions, ...<%= h.changeCase.pascal(documentType) %>Actions };
|
|
21
|
+
|
|
22
|
+
export const module: DocumentModel<
|
|
23
|
+
<%= h.changeCase.pascal(documentType) %>State,
|
|
24
|
+
<%= h.changeCase.pascal(documentType) %>Action,
|
|
25
|
+
<%= h.changeCase.pascal(documentType) %>
|
|
26
|
+
> = {
|
|
27
|
+
Document,
|
|
28
|
+
reducer,
|
|
29
|
+
actions,
|
|
30
|
+
utils,
|
|
31
|
+
documentModel,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { <%= h.changeCase.pascal(documentType) %>, Document, reducer, actions, utils, documentModel }
|
|
35
|
+
|
|
36
|
+
export * from './gen/types';
|
|
37
|
+
export * from './src/utils';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/types.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import type { Document, ExtendedState } from 'document-model/document';
|
|
6
|
+
import type { <%= h.changeCase.pascal(documentType) %>State } from './schema/types';
|
|
7
|
+
<% if(hasLocalSchema) { -%>
|
|
8
|
+
import type { <%= h.changeCase.pascal(documentType) %>LocalState } from './schema/types';
|
|
9
|
+
<%} -%>
|
|
10
|
+
import type { <%= h.changeCase.pascal(documentType) %>Action } from './actions';
|
|
11
|
+
|
|
12
|
+
export { z } from './schema';
|
|
13
|
+
export type * from './schema/types';
|
|
14
|
+
<% if(!hasLocalSchema) { -%>
|
|
15
|
+
type <%= h.changeCase.pascal(documentType) %>LocalState = Record<PropertyKey, never>;
|
|
16
|
+
<%} -%>
|
|
17
|
+
export type Extended<%= h.changeCase.pascal(documentType) %>State = ExtendedState<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState>;
|
|
18
|
+
export type <%= h.changeCase.pascal(documentType) %>Document = Document<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>Action, <%= h.changeCase.pascal(documentType) %>LocalState>;
|
|
19
|
+
export { <%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState, <%= h.changeCase.pascal(documentType) %>Action };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/utils.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { DocumentModelUtils, utils as base } from 'document-model/document';
|
|
6
|
+
import { <%= h.changeCase.pascal(documentType) %>Action, <%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState } from './types';
|
|
7
|
+
import { reducer } from './reducer';
|
|
8
|
+
|
|
9
|
+
export const initialGlobalState: <%= h.changeCase.pascal(documentType) %>State = <%- initialGlobalState %>;
|
|
10
|
+
export const initialLocalState: <%= h.changeCase.pascal(documentType) %>LocalState = <%- initialLocalState %>;
|
|
11
|
+
|
|
12
|
+
const utils: DocumentModelUtils<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>Action, <%= h.changeCase.pascal(documentType) %>LocalState> = {
|
|
13
|
+
fileExtension: '<%- fileExtension %>',
|
|
14
|
+
createState(state) {
|
|
15
|
+
return { global: { ...initialGlobalState, ...state?.global }, local: { ...initialLocalState, ...state?.local } };
|
|
16
|
+
},
|
|
17
|
+
createExtendedState(extendedState) {
|
|
18
|
+
return base.createExtendedState(
|
|
19
|
+
{ ...extendedState, documentType: '<%- documentTypeId %>' },
|
|
20
|
+
utils.createState
|
|
21
|
+
);
|
|
22
|
+
},
|
|
23
|
+
createDocument(state) {
|
|
24
|
+
return base.createDocument(
|
|
25
|
+
utils.createExtendedState(state),
|
|
26
|
+
utils.createState
|
|
27
|
+
);
|
|
28
|
+
},
|
|
29
|
+
saveToFile(document, path, name) {
|
|
30
|
+
return base.saveToFile(document, path, '<%- fileExtension %>', name);
|
|
31
|
+
},
|
|
32
|
+
saveToFileHandle(document, input) {
|
|
33
|
+
return base.saveToFileHandle(document, input);
|
|
34
|
+
},
|
|
35
|
+
loadFromFile(path) {
|
|
36
|
+
return base.loadFromFile(path, reducer);
|
|
37
|
+
},
|
|
38
|
+
loadFromInput(input) {
|
|
39
|
+
return base.loadFromInput(input, reducer);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export default utils;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/<%= module %>/actions.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { Action<% if (actions.find(a => a.hasAttachment)) {%>, ActionWithAttachment<%}%> } from 'document-model/document';
|
|
6
|
+
import {
|
|
7
|
+
<% actions.filter(a => a.hasInput).forEach(action => { _%>
|
|
8
|
+
<%= h.changeCase.pascal(action.name) %>Input,
|
|
9
|
+
<% }); _%>
|
|
10
|
+
} from '../types';
|
|
11
|
+
|
|
12
|
+
<% actions.filter(a => a.hasInput).forEach(actionType => { _%>
|
|
13
|
+
export type <%= h.changeCase.pascal(actionType.name) %>Action = Action<%if(actionType.hasAttachment){ %>WithAttachment<% } %><'<%= h.changeCase.constantCase(actionType.name) %>', <%= h.changeCase.pascal(actionType.name) %>Input, '<%= actionType.scope %>'>;
|
|
14
|
+
<% }); _%>
|
|
15
|
+
<% actions.filter(a => !a.hasInput).forEach(actionType => { _%>
|
|
16
|
+
export type <%= h.changeCase.pascal(actionType.name) %>Action = Action<'<%= h.changeCase.constantCase(actionType.name) %>', never, '<%= actionType.scope %>'>;
|
|
17
|
+
<% }); _%>
|
|
18
|
+
|
|
19
|
+
export type <%= h.changeCase.pascal(documentType) %><%= h.changeCase.pascal(module) %>Action =
|
|
20
|
+
<% actions.forEach(actionType => { _%>
|
|
21
|
+
| <%= h.changeCase.pascal(actionType.name) %>Action
|
|
22
|
+
<% }); _%>;
|
package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/creators.esm.t
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/<%= module %>/creators.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { utils<% if (actions.find(a => a.hasAttachment)) {%>, AttachmentInput<%}%> } from 'document-model/document';
|
|
6
|
+
import { z,
|
|
7
|
+
<% actions.filter(a => a.hasInput).forEach(action => { _%>
|
|
8
|
+
<%= h.changeCase.pascal(action.name) %>Input,
|
|
9
|
+
<% }); _%>
|
|
10
|
+
} from '../types';
|
|
11
|
+
import {
|
|
12
|
+
<% actions.forEach(action => { _%>
|
|
13
|
+
<%= h.changeCase.pascal(action.name) %>Action,
|
|
14
|
+
<% }); _%>
|
|
15
|
+
} from './actions';
|
|
16
|
+
|
|
17
|
+
const { createAction } = utils;
|
|
18
|
+
|
|
19
|
+
<% actions.filter(a => a.hasInput).forEach(action => { _%>
|
|
20
|
+
export const <%= h.changeCase.camel(action.name) %> = (input: <%= h.changeCase.pascal(action.name) %>Input<%if(action.hasAttachment){ %>, attachments: AttachmentInput[] <% } %>) =>
|
|
21
|
+
createAction<<%= h.changeCase.pascal(action.name) %>Action>(
|
|
22
|
+
'<%= h.changeCase.constantCase(action.name) %>',
|
|
23
|
+
{...input},
|
|
24
|
+
<%if(action.hasAttachment){ %>attachments<% } else { %>undefined<% } %>,
|
|
25
|
+
z.<%= h.changeCase.pascalCase(action.name) %>InputSchema,
|
|
26
|
+
'<%= action.scope %>'
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
<% }); _%>
|
|
30
|
+
|
|
31
|
+
<% actions.filter(a => !a.hasInput).forEach(action => { _%>
|
|
32
|
+
export const <%= h.changeCase.camel(action.name) %> = () =>
|
|
33
|
+
createAction<<%= h.changeCase.pascal(action.name) %>Action>('<%= h.changeCase.constantCase(action.name) %>');
|
|
34
|
+
<% }); _%>
|
package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/customReducers.esm.t
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/src/reducers/<%= module %>.ts"
|
|
3
|
+
unless_exists: true
|
|
4
|
+
---
|
|
5
|
+
/**
|
|
6
|
+
* This is a scaffold file meant for customization:
|
|
7
|
+
* - modify it by implementing the reducer functions
|
|
8
|
+
* - delete the file and run the code generator again to have it reset
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { <%= h.changeCase.pascal(documentType) %><%= h.changeCase.pascal(module) %>Operations } from '../../gen/<%= module %>/operations';
|
|
12
|
+
|
|
13
|
+
export const reducer: <%= h.changeCase.pascal(documentType) %><%= h.changeCase.pascal(module) %>Operations = {
|
|
14
|
+
<% actions.forEach(action => { _%>
|
|
15
|
+
<%= h.changeCase.camel(action.name) %>Operation(state, action, dispatch) {
|
|
16
|
+
// TODO: Implement "<%= h.changeCase.camel(action.name) %>Operation" reducer
|
|
17
|
+
throw new Error('Reducer "<%= h.changeCase.camel(action.name) %>Operation" not yet implemented');
|
|
18
|
+
},
|
|
19
|
+
<% }); _%>
|
|
20
|
+
}
|
package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/customTest.esm.t
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/src/tests/<%= module %>.test.ts"
|
|
3
|
+
unless_exists: true
|
|
4
|
+
---
|
|
5
|
+
/**
|
|
6
|
+
* This is a scaffold file meant for customization:
|
|
7
|
+
* - change it by adding new tests or modifying the existing ones
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { generateMock } from '@acaldas/powerhouse';
|
|
11
|
+
|
|
12
|
+
import utils from '../../gen/utils';
|
|
13
|
+
import { z } from '../../gen/schema';
|
|
14
|
+
import { reducer } from '../../gen/reducer';
|
|
15
|
+
import * as creators from '../../gen/<%= module %>/creators';
|
|
16
|
+
import { <%= h.changeCase.pascal(documentType) %>Document } from '../../gen/types';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
describe('<%= h.changeCase.pascal(module) %> Operations', () => {
|
|
20
|
+
let document: <%= h.changeCase.pascal(documentType) %>Document;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
document = utils.createDocument();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
<% actions.forEach(action => { _%>
|
|
27
|
+
it('should handle <%= h.changeCase.camel(action.name) %> operation', () => {
|
|
28
|
+
const input = generateMock(z.<%= h.changeCase.pascal(action.name) %>InputSchema());
|
|
29
|
+
const updatedDocument = reducer(document, creators.<%= h.changeCase.camel(action.name) %>(input));
|
|
30
|
+
|
|
31
|
+
expect(updatedDocument.operations.<%= action.scope %>).toHaveLength(1);
|
|
32
|
+
expect(updatedDocument.operations.<%= action.scope %>[0].type).toBe('<%= h.changeCase.constant(action.name) %>');
|
|
33
|
+
expect(updatedDocument.operations.<%= action.scope %>[0].input).toStrictEqual(input);
|
|
34
|
+
expect(updatedDocument.operations.<%= action.scope %>[0].index).toEqual(0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
<% }); _%>
|
|
38
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Args } from '../generate-document-model';
|
|
2
|
+
type ModuleArgs = Args & {
|
|
3
|
+
module: string;
|
|
4
|
+
};
|
|
5
|
+
declare const _default: {
|
|
6
|
+
params: ({ args }: {
|
|
7
|
+
args: ModuleArgs;
|
|
8
|
+
}) => {
|
|
9
|
+
rootDir: string;
|
|
10
|
+
documentType: string;
|
|
11
|
+
module: string;
|
|
12
|
+
actions: {
|
|
13
|
+
name: import("document-model/document-model").Maybe<string>;
|
|
14
|
+
hasInput: boolean;
|
|
15
|
+
hasAttachment: boolean | undefined;
|
|
16
|
+
scope: import("document-model/document").OperationScope;
|
|
17
|
+
state: string;
|
|
18
|
+
}[];
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export default _default;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const change_case_1 = require("change-case");
|
|
4
|
+
exports.default = {
|
|
5
|
+
params: ({ args }) => {
|
|
6
|
+
const documentModel = JSON.parse(args.documentModel);
|
|
7
|
+
const latestSpec = documentModel.specifications[documentModel.specifications.length - 1];
|
|
8
|
+
const filteredModules = latestSpec.modules.filter(m => m.name === args.module);
|
|
9
|
+
return {
|
|
10
|
+
rootDir: args.rootDir,
|
|
11
|
+
documentType: documentModel.name,
|
|
12
|
+
module: (0, change_case_1.paramCase)(args.module),
|
|
13
|
+
actions: filteredModules.length > 0
|
|
14
|
+
? filteredModules[0].operations.map(a => ({
|
|
15
|
+
name: a.name,
|
|
16
|
+
hasInput: a.schema !== null,
|
|
17
|
+
hasAttachment: a.schema?.includes(': Attachment'),
|
|
18
|
+
scope: a.scope || 'global',
|
|
19
|
+
state: a.scope === 'global' ? '' : a.scope, // the state this action affects
|
|
20
|
+
}))
|
|
21
|
+
: [],
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/<%= module %>/object.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { BaseDocument<% if (actions.find(a => a.hasAttachment)) {%>, AttachmentInput<%}%> } from 'document-model/document';
|
|
6
|
+
import {
|
|
7
|
+
<% actions.filter(action => action.hasInput).forEach(action => { _%>
|
|
8
|
+
<%= h.changeCase.pascal(action.name) %>Input,
|
|
9
|
+
<% }); _%>
|
|
10
|
+
<%= h.changeCase.pascal(documentType) %>State,
|
|
11
|
+
<%= h.changeCase.pascal(documentType) %>LocalState
|
|
12
|
+
} from '../types';
|
|
13
|
+
import {
|
|
14
|
+
<% actions.forEach(action => { _%>
|
|
15
|
+
<%= h.changeCase.camel(action.name) %>,
|
|
16
|
+
<% }); _%>
|
|
17
|
+
} from './creators';
|
|
18
|
+
import { <%= h.changeCase.pascal(documentType) %>Action } from '../actions';
|
|
19
|
+
|
|
20
|
+
export default class <%= h.changeCase.pascal(documentType) %>_<%= h.changeCase.pascal(module) %> extends BaseDocument<
|
|
21
|
+
<%= h.changeCase.pascal(documentType) %>State,
|
|
22
|
+
<%= h.changeCase.pascal(documentType) %>Action,
|
|
23
|
+
<%= h.changeCase.pascal(documentType) %>LocalState
|
|
24
|
+
> {
|
|
25
|
+
<% actions.filter(action => action.hasInput).forEach(action => { _%>
|
|
26
|
+
public <%= h.changeCase.camel(action.name) %>(input: <%= h.changeCase.pascal(action.name) %>Input<%if(action.hasAttachment){ %>, attachments: AttachmentInput[] <% } %>) {
|
|
27
|
+
return this.dispatch(<%= h.changeCase.camel(action.name) %>(input<%if(action.hasAttachment){ %>, attachments<% } %>));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
<% }); _%>
|
|
31
|
+
<% actions.filter(action => !action.hasInput).forEach(action => { _%>
|
|
32
|
+
public <%= h.changeCase.camel(action.name) %>() {
|
|
33
|
+
return this.dispatch(<%= h.changeCase.camel(action.name) %>());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
<% }); _%>
|
|
37
|
+
}
|
package/dist/codegen/.hygen/templates/powerhouse/generate-document-model-module/operations.esm.t
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/<%= module %>/operations.ts"
|
|
3
|
+
force: true
|
|
4
|
+
---
|
|
5
|
+
import { SignalDispatch } from 'document-model/document';
|
|
6
|
+
import {
|
|
7
|
+
<% actions.forEach(action => { _%>
|
|
8
|
+
<%= h.changeCase.pascal(action.name) %>Action,
|
|
9
|
+
<% }); _%>
|
|
10
|
+
} from './actions';
|
|
11
|
+
import { <%= actions.map(action => h.changeCase.pascal(h.changeCase.pascal(documentType + '_' + action.state + '_State'))).filter((value, index, self) => self.indexOf(value) === index).join(', ') %> } from '../types';
|
|
12
|
+
|
|
13
|
+
export interface <%= h.changeCase.pascal(documentType) %><%= h.changeCase.pascal(module) %>Operations {
|
|
14
|
+
<% actions.forEach(action => { _%>
|
|
15
|
+
<%= h.changeCase.camel(action.name) %>Operation: (state: <%= h.changeCase.pascal(documentType + '_' + action.state + '_State') %>, action: <%= h.changeCase.pascal(action.name) %>Action, dispatch?: SignalDispatch) => void,
|
|
16
|
+
<% }); _%>
|
|
17
|
+
}
|