-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.ts
203 lines (183 loc) · 5.64 KB
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { Command, flags } from "@oclif/command";
import { camelCase } from "lodash";
import { resolve, extname } from "path";
import { writeJsonSync, readFileSync, writeFileSync } from "fs-extra";
import { greenBright } from "chalk";
import * as globby from "globby";
import { startSpinner, stopSpinner } from "../lib/spinner";
import { basicApi, defineYourOwnApi, todoApi } from "../lib/api/functions";
import { userInput } from "../lib/inquirer";
import {
checkEmptyDirectoy,
validateGraphqlSchemaFile,
} from "../lib/api/errorHandling";
import {
SAASTYPE,
Config,
DATABASE,
APITYPE,
CLOUDPROVIDER,
LANGUAGE,
NEPTUNEQUERYLANGUAGE,
RDBMSENGINE,
TEMPLATE,
} from "../utils/constants";
const prettier = require("prettier");
const exec = require("await-exec");
export default class Create extends Command {
static description = "Generates CDK code based on the given schema";
static flags = {
help: flags.help({ char: "h" }),
test: flags.boolean({ char: "t" }),
database: flags.enum({
char: "d",
options: [DATABASE.dynamoDB, DATABASE.neptuneDB, DATABASE.auroraDB],
}),
};
async run() {
const { flags } = this.parse(Create);
let templateDir: string;
let config: Config;
// Questions
const placeholder = process.argv[1];
if (
flags.test &&
!(
placeholder.includes("node_modules") ||
placeholder.includes("@panacloud") ||
placeholder.includes("npm")
)
) {
config = {
entityId: "",
api_token: "",
saasType: SAASTYPE.api,
api: {
template: TEMPLATE.defineApi,
nestedResolver: false,
language: LANGUAGE.typescript,
cloudprovider: CLOUDPROVIDER.aws,
apiName: "cliTesting",
schemaPath: "../test/test-schemas/todo.graphql",
apiType: APITYPE.graphql,
database: flags.database,
neptuneQueryLanguage:
flags.database === DATABASE.neptuneDB
? NEPTUNEQUERYLANGUAGE.gremlin
: undefined,
rdbmsEngine:
flags.database === DATABASE.neptuneDB
? RDBMSENGINE.mysql
: undefined,
},
};
} else {
let usrInput = await userInput();
config = {
// entityId: "",
// api_token: "",
saasType: SAASTYPE.api,
api: {
template: usrInput.template,
multitenancy: false,
nestedResolver: false,
language: LANGUAGE.typescript,
cloudprovider: CLOUDPROVIDER.aws,
apiName: camelCase(usrInput.api_name),
schemaPath: usrInput.schema_path,
apiType: APITYPE.graphql,
database: usrInput.database,
rdbmsEngine: usrInput.rdbmsEngine,
neptuneQueryLanguage: usrInput.neptuneQueryLanguage,
},
};
}
// Error handling
const validating = startSpinner("Validating Everything");
if (config!.saasType === SAASTYPE.api) {
templateDir = resolve(__dirname, "../lib/api/template");
checkEmptyDirectoy(validating);
if (config.api.template === TEMPLATE.defineApi) {
validateGraphqlSchemaFile(config!.api?.schemaPath, validating, "init");
}
}
if (config.api.template !== TEMPLATE.todoApi) {
writeJsonSync(`./codegenconfig.json`, {
...config,
api: {
...config.api,
schemaPath: "./editable_src/graphql/schema/schema.graphql",
},
});
}
stopSpinner(validating, "Everything's fine", false);
if (config.saasType === SAASTYPE.api) {
if (config.api.apiType === APITYPE.graphql) {
if (config.api.template === TEMPLATE.basicApi) {
await basicApi(config, templateDir!);
} else if (config.api.template === TEMPLATE.defineApi) {
await defineYourOwnApi(config, templateDir!);
} else {
await todoApi(config);
}
}
}
// const setUpForTest = startSpinner("Setup For Test");
// try {
// await exec(
// `npx gqlg --schemaFilePath ./editable_src/graphql/schema/schema.graphql --destDirPath ./tests/apiTests/graphql/`
// );
// } catch (error) {
// stopSpinner(setUpForTest, `Error: ${error}`, true);
// process.exit(1);
// }
// stopSpinner(setUpForTest, "Setup For Test", false);
if (
config.api.apiType === APITYPE.graphql &&
config.api.template === TEMPLATE.defineApi
) {
const generatingTypes = startSpinner("Generating Types");
try {
await exec(`npx graphql-codegen`);
} catch (error) {
stopSpinner(generatingTypes, `Error: ${error}`, true);
process.exit(1);
}
stopSpinner(generatingTypes, "Generated Types", false);
}
const formatting = startSpinner("Formatting Code");
// Formatting files.
const files = await globby(
[
"*",
"**/*.ts",
"!*.gql",
"!*.graphql",
"!?.",
"!*.md",
"!*.lock",
"!*.yaml",
"!*.yml",
"editable_src/panacloudconfig.json",
".panacloud/editable_src/panacloudconfig.json",
],
{
gitignore: true,
}
);
files.forEach(async (file: string) => {
const data = readFileSync(file, "utf8");
const nextData = prettier.format(data, {
parser: extname(file) === ".json" ? "json" : "typescript",
});
writeFileSync(file, nextData, "utf8");
});
stopSpinner(formatting, "Formatting Done", false);
this.log(
greenBright(
"Now Start Building Your Multi-Tenant Serverless Unicorn APIs"
)
);
this.log(greenBright("Your code goes inside editable_src directory"));
}
}