1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __rest = (this && this.__rest) || function (s, e) {
|
12 | var t = {};
|
13 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
14 | t[p] = s[p];
|
15 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
16 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
17 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
18 | t[p[i]] = s[p[i]];
|
19 | }
|
20 | return t;
|
21 | };
|
22 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
23 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | const path_1 = __importDefault(require("path"));
|
27 | const fs_1 = __importDefault(require("fs"));
|
28 | const ern_core_1 = require("ern-core");
|
29 | const index_1 = require("./index");
|
30 | const Constants_1 = require("./Constants");
|
31 | exports.GENERATE = [
|
32 | ['android', 'ERNAndroid'],
|
33 | ['javascript', 'ERNES6'],
|
34 | ['IOS', 'ERNSwift'],
|
35 | ];
|
36 | function generateSwagger(_a, outFolder) {
|
37 | var { apiSchemaPath = Constants_1.MODEL_FILE, name, namespace = '' } = _a, optional = __rest(_a, ["apiSchemaPath", "name", "namespace"]);
|
38 | return __awaiter(this, void 0, void 0, function* () {
|
39 | const inputSpec = path_1.default.resolve(outFolder, apiSchemaPath);
|
40 | const shared = Object.assign({ apiPackage: `${namespace}.api`, description: optional.apiDescription, groupId: namespace, inputSpec, modelPackage: `${namespace}.model`, projectVersion: optional.apiVersion, version: optional.apiVersion }, optional);
|
41 | for (const [projectName, lang] of exports.GENERATE) {
|
42 | const cc = new index_1.CodegenConfigurator(Object.assign(Object.assign({}, shared), { lang, outputDir: outFolder + '/' + projectName, projectName }));
|
43 | const opts = yield cc.toClientOptInput();
|
44 | new index_1.DefaultGenerator().opts(opts).generate();
|
45 | }
|
46 | });
|
47 | }
|
48 | exports.generateSwagger = generateSwagger;
|
49 | function generatePackageJson(_a) {
|
50 | var { npmScope, reactNativeVersion, apiVersion = '1.0.0', apiDescription, apiAuthor, apiLicense, bridgeVersion = '', packageName } = _a, conf = __rest(_a, ["npmScope", "reactNativeVersion", "apiVersion", "apiDescription", "apiAuthor", "apiLicense", "bridgeVersion", "packageName"]);
|
51 |
|
52 |
|
53 | const options = Object.assign(Object.assign({}, conf), { apiSchemaPath: Constants_1.MODEL_FILE });
|
54 | return JSON.stringify({
|
55 | author: apiAuthor,
|
56 | dependencies: {
|
57 | 'react-native-electrode-bridge': `${bridgeVersion.split('.')[0]}.${bridgeVersion.split('.')[1]}.x`,
|
58 | },
|
59 | description: apiDescription,
|
60 | devDependencies: {
|
61 | 'flow-bin': Constants_1.FLOW_BIN_VERSION,
|
62 | },
|
63 | ern: {
|
64 | message: options,
|
65 | moduleType: `${ern_core_1.ModuleTypes.API}`,
|
66 | },
|
67 | keywords: [`${ern_core_1.ModuleTypes.API}`],
|
68 | license: apiLicense,
|
69 | main: 'javascript/src/index.js',
|
70 | name: npmScope ? `@${npmScope}/${packageName}` : packageName,
|
71 | scripts: {
|
72 | flow: 'flow',
|
73 | },
|
74 | version: apiVersion,
|
75 | }, null, 2);
|
76 | }
|
77 | exports.generatePackageJson = generatePackageJson;
|
78 | function generateInitialSchema({ namespace, apiSchemaPath, }) {
|
79 | return __awaiter(this, void 0, void 0, function* () {
|
80 | return apiSchemaPath && fs_1.default.existsSync(apiSchemaPath)
|
81 | ? ern_core_1.fileUtils.readFile(apiSchemaPath)
|
82 | : `
|
83 | {
|
84 | "swagger": "2.0",
|
85 | "info": {
|
86 | "description": "Walmart Item Module",
|
87 | "title": "WalmartItem",
|
88 | "contact": {
|
89 | "name": "ERN Mobile Platform Team"
|
90 | }
|
91 | },
|
92 | "paths": {
|
93 | "/items": {
|
94 | "get": {
|
95 | "tags": [
|
96 | "WalmartItem"
|
97 | ],
|
98 | "description": "Returns all items from the system that the user has access to",
|
99 | "operationId": "findItems",
|
100 | "parameters": [{
|
101 | "name": "limit",
|
102 | "in": "query",
|
103 | "description": "maximum number of results to return",
|
104 | "required": false,
|
105 | "type": "integer",
|
106 | "format": "int32"
|
107 | }],
|
108 | "responses": {
|
109 | "200": {
|
110 | "description": "Item response",
|
111 | "schema": {
|
112 | "type": "array",
|
113 | "items": {
|
114 | "$ref": "#/definitions/Item"
|
115 | }
|
116 | }
|
117 | }
|
118 | }
|
119 | },
|
120 | "post": {
|
121 | "tags": [
|
122 | "WalmartItem"
|
123 | ],
|
124 | "description": "Creates a Item in the store.",
|
125 | "operationId": "addItem",
|
126 | "parameters": [{
|
127 | "name": "item",
|
128 | "in": "body",
|
129 | "description": "Item to add",
|
130 | "required": true,
|
131 | "schema": {
|
132 | "$ref": "#/definitions/Item"
|
133 | }
|
134 | }],
|
135 | "responses": {
|
136 | "200": {
|
137 | "schema": {
|
138 | "type": "boolean"
|
139 | }
|
140 | }
|
141 | }
|
142 | }
|
143 | },
|
144 | "event/itemAdded": {
|
145 | "event": {
|
146 | "tags": [
|
147 | "WalmartItem"
|
148 | ],
|
149 | "operationId": "itemAdded",
|
150 | "parameters": [{
|
151 | "name": "itemId",
|
152 | "in": "path",
|
153 | "description": "Event to notify new item added",
|
154 | "required": true,
|
155 | "type": "string"
|
156 | }]
|
157 | }
|
158 | }
|
159 | },
|
160 | "definitions": {
|
161 | "Item": {
|
162 | "type": "object",
|
163 | "required": [
|
164 | "name",
|
165 | "id"
|
166 | ],
|
167 | "properties": {
|
168 | "id": {
|
169 | "type": "integer",
|
170 | "format": "int64"
|
171 | },
|
172 | "name": {
|
173 | "type": "string"
|
174 | },
|
175 | "desc": {
|
176 | "type": "string"
|
177 | }
|
178 | }
|
179 | }
|
180 | }
|
181 | }
|
182 | `;
|
183 | });
|
184 | }
|
185 | exports.generateInitialSchema = generateInitialSchema;
|
186 | function generateFlowConfig() {
|
187 | return `[ignore]
|
188 |
|
189 | [include]
|
190 |
|
191 | [libs]
|
192 |
|
193 | [lints]
|
194 |
|
195 | [options]
|
196 | `;
|
197 | }
|
198 | exports.generateFlowConfig = generateFlowConfig;
|
199 | function generateProject(config = {}, outFolder) {
|
200 | return __awaiter(this, void 0, void 0, function* () {
|
201 | yield ern_core_1.fileUtils.writeFile(path_1.default.join(outFolder, Constants_1.PKG_FILE), generatePackageJson(config));
|
202 | yield ern_core_1.fileUtils.writeFile(path_1.default.join(outFolder, Constants_1.MODEL_FILE), yield generateInitialSchema(config));
|
203 | yield ern_core_1.fileUtils.writeFile(path_1.default.join(outFolder, Constants_1.FLOW_CONFIG_FILE), generateFlowConfig());
|
204 | yield generateSwagger(config, outFolder);
|
205 | });
|
206 | }
|
207 | exports.default = generateProject;
|
208 |
|
\ | No newline at end of file |