Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit bcdfdaf

Browse files
author
Anton Cherednikov
committed
Merge branch 'feature/PTF-863-generate-command' into 'develop'
PTF-863: generate command See merge request cs-platform/public/cli!14
2 parents 830fdd9 + 9c2fdd0 commit bcdfdaf

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"open": "^7.0.3",
3535
"ora": "^4.0.3",
3636
"os": "^0.1.1",
37+
"rimraf": "^3.0.2",
3738
"tabtab": "^3.0.2",
3839
"tree-node-cli": "^1.3.0",
3940
"ts-jest": "^25.3.1",

src/commands/service/generate.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Listr } from 'listr2';
2+
import { join } from 'path';
3+
import Command from '../../lib/command';
4+
import Aliases from '../../common/constants/aliases';
5+
import FileWorker from '../../common/file-worker';
6+
import PromisifiedFs from '../../common/promisified-fs';
7+
8+
export default class Generate extends Command {
9+
public static description = 'Generate entities and migrations';
10+
11+
public static aliases = [Aliases.GENERATE];
12+
13+
public async execute(): Promise<void> {
14+
await this.serviceWorker.validateSchema();
15+
16+
const tasks = new Listr<{ encodedZip: string; generated: string }>([
17+
{
18+
title: 'Validating schema',
19+
task: async (): Promise<void> => {
20+
await this.serviceWorker.validateSchema();
21+
},
22+
},
23+
{
24+
title: 'Preparing the service code for upload',
25+
task: async (ctx): Promise<void> => {
26+
ctx.encodedZip = await FileWorker.zipFolder();
27+
},
28+
},
29+
{
30+
title: 'Uploading service',
31+
task: async (ctx): Promise<void> => {
32+
const { encodedZip } = ctx;
33+
ctx.generated = await this.codestore.Service.generateEntities(encodedZip);
34+
},
35+
},
36+
{
37+
title: 'Saving generated code',
38+
task: async (ctx, task): Promise<void> => {
39+
const { generated } = ctx;
40+
const dataFolder = join(process.cwd(), 'src', 'data');
41+
await PromisifiedFs.rimraf(join(dataFolder, 'entities'));
42+
await FileWorker.saveZipFromB64(generated, dataFolder);
43+
44+
// eslint-disable-next-line no-param-reassign
45+
task.title = 'Generated code has been saved';
46+
},
47+
},
48+
]);
49+
50+
await tasks.run();
51+
}
52+
}

src/common/constants/aliases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ enum Aliases {
99
PULL = 'pull',
1010
PUSH = 'push',
1111
LOGS = 'logs',
12+
GENERATE = 'generate',
1213
}
1314

1415
export default Aliases;

src/common/promisified-fs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import {
22
writeFile, unlink, readdir, readFile, access, stat,
33
} from 'fs';
44
import { promisify } from 'util';
5+
import * as rimraf from 'rimraf';
56

67
export default class PromisifiedFs {
8+
public static rimraf = promisify(rimraf);
9+
710
public static unlink = promisify(unlink);
811

912
public static writeFile = promisify(writeFile);

src/lib/api-services/service/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApolloClient } from 'apollo-boost';
22
import {
3-
LIST_SERVICES, CREATE_SERVICE, DEPLOY_SERVICE, LIST_BUSINESS_DOMAINS, DELETE_SERVICE, DOWNLOAD_SERVICE, PUSH_SERVICE, SINGLE_SERVICE,
3+
LIST_SERVICES, CREATE_SERVICE, DEPLOY_SERVICE, LIST_BUSINESS_DOMAINS, DELETE_SERVICE, DOWNLOAD_SERVICE, PUSH_SERVICE, SINGLE_SERVICE, GENERATE_SERVICE_ENTITIES,
44
} from './queries';
55
import { IService, IServiceCreateResult, IServiceCreate } from '../../../interfaces/service.interface';
66
import ServiceStateEnum from '../../../common/constants/service-state.enum';
@@ -114,4 +114,15 @@ export default class Service {
114114
}, 5000);
115115
});
116116
}
117+
118+
public async generateEntities(encodedString: string): Promise<string> {
119+
const { data } = await this.apiClient.mutate({
120+
mutation: GENERATE_SERVICE_ENTITIES,
121+
variables: {
122+
base64Service: encodedString,
123+
},
124+
});
125+
126+
return data.generateServiceEntities.data;
127+
}
117128
}

src/lib/api-services/service/queries.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@ export const PUSH_SERVICE = gql`mutation pushService($base64Service: String!, $n
5959
success
6060
}
6161
}`;
62+
63+
export const GENERATE_SERVICE_ENTITIES = gql`mutation generateServiceEntities($base64Service: String!){
64+
generateServiceEntities(base64Service:$base64Service){
65+
data
66+
}
67+
}`;

0 commit comments

Comments
 (0)