1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | var cli_1 = require("./cli");
|
4 | var scheme_loader_1 = require("./scheme-loader");
|
5 | var codegen_1 = require("./codegen");
|
6 | var document_loader_1 = require("./document-loader");
|
7 | var template_loader_1 = require("./template-loader");
|
8 | var fs = require("fs");
|
9 | var path = require("path");
|
10 | var mkdirp = require("mkdirp");
|
11 | var handlebars_helpers_1 = require("./handlebars-helpers");
|
12 | var options = cli_1.initCLI(process.argv);
|
13 | cli_1.validateCliOptions(options);
|
14 | cli_1.transformOptions(options)
|
15 | .then(function (transformedOptions) {
|
16 | var templateConfig = transformedOptions.template.config;
|
17 | handlebars_helpers_1.initHelpers();
|
18 | handlebars_helpers_1.initPartials((templateConfig.partials || []).map(function (partialPath) {
|
19 | return {
|
20 | content: template_loader_1.loadFromPath(path.resolve(__dirname, transformedOptions.template.config.basePath, partialPath)),
|
21 | name: path.basename(partialPath, path.extname(partialPath))
|
22 | };
|
23 | }));
|
24 | handlebars_helpers_1.initTemplateHelpers((templateConfig.helpers || []).map(function (helperPath) {
|
25 | return {
|
26 | func: require(path.resolve(__dirname, transformedOptions.template.config.basePath, helperPath)),
|
27 | name: path.basename(helperPath, path.extname(helperPath))
|
28 | };
|
29 | }));
|
30 | var schema = scheme_loader_1.loadSchema(transformedOptions.introspection);
|
31 | var documents = transformedOptions.documents;
|
32 | var codegen = codegen_1.prepareCodegen(schema, document_loader_1.loadDocumentsSources(documents), transformedOptions.template.config.primitives, {
|
33 | flattenInnerTypes: templateConfig.flattenInnerTypes,
|
34 | noSchema: transformedOptions.noSchema,
|
35 | noDocuments: transformedOptions.noDocuments
|
36 | });
|
37 | var strategy = templateConfig.strategy || 'SINGLE_FILE';
|
38 | var baseOutPath = path.basename(transformedOptions.outPath);
|
39 | if (strategy === 'SINGLE_FILE') {
|
40 | if (baseOutPath.indexOf('.') === -1) {
|
41 | throw "Generator '" + transformedOptions.template.language + "' uses single-file strategy! Please specify a filename using --out flag!";
|
42 | }
|
43 | var templatePath = path.resolve(__dirname, transformedOptions.template.config.basePath, transformedOptions.template.config.template);
|
44 | var outPath = path.resolve(transformedOptions.outPath);
|
45 | var outDir = path.dirname(outPath);
|
46 | mkdirp.sync(outDir);
|
47 | return [{
|
48 | isDev: transformedOptions.isDev,
|
49 | content: template_loader_1.compileTemplate(codegen, templatePath),
|
50 | path: outPath
|
51 | }];
|
52 | }
|
53 | else if (strategy === 'MULTIPLE_FILES') {
|
54 | if (baseOutPath.indexOf('.') > -1) {
|
55 | throw "Generator '" + transformedOptions.template.language + "' uses multiple-files strategy! Please specify a directory using --out flag!";
|
56 | }
|
57 | var resultsArr_1 = [];
|
58 | var filesExtension_1 = transformedOptions.template.config.filesExtension;
|
59 | var templates_1 = transformedOptions.template.config.templates;
|
60 | var outPath = path.resolve(transformedOptions.outPath);
|
61 | mkdirp.sync(outPath);
|
62 | Object.keys(templates_1).forEach(function (templateName) {
|
63 | var templatePath = path.resolve(__dirname, transformedOptions.template.config.basePath, templates_1[templateName]);
|
64 | if (templateName === 'model') {
|
65 | codegen.models.forEach(function (model) {
|
66 | resultsArr_1.push({
|
67 | isDev: transformedOptions.isDev,
|
68 | content: template_loader_1.compileTemplate(model, templatePath),
|
69 | path: path.resolve(transformedOptions.outPath, model.name + '.model.' + filesExtension_1)
|
70 | });
|
71 | });
|
72 | }
|
73 | if (templateName === 'document') {
|
74 | codegen.documents.forEach(function (document) {
|
75 | resultsArr_1.push({
|
76 | isDev: transformedOptions.isDev,
|
77 | content: template_loader_1.compileTemplate(document, templatePath),
|
78 | path: path.resolve(transformedOptions.outPath, document.name + '.document.' + filesExtension_1)
|
79 | });
|
80 | });
|
81 | }
|
82 | });
|
83 | resultsArr_1 = resultsArr_1.filter(function (item) { return item.content.length > 0; });
|
84 | if (templates_1['index']) {
|
85 | var directoryPath_1 = path.resolve(transformedOptions.outPath);
|
86 | var indexOutPath = path.resolve(directoryPath_1, 'index.' + filesExtension_1);
|
87 | var templatePath = path.resolve(transformedOptions.template.config.basePath, templates_1['index']);
|
88 | resultsArr_1.push({
|
89 | isDev: transformedOptions.isDev,
|
90 | content: template_loader_1.compileTemplate({ files: resultsArr_1.map(function (item) {
|
91 | return {
|
92 | fileName: path.basename(item.path, '.' + filesExtension_1),
|
93 | fullPath: item.path,
|
94 | extension: filesExtension_1,
|
95 | directory: directoryPath_1
|
96 | };
|
97 | }) }, templatePath),
|
98 | path: indexOutPath
|
99 | });
|
100 | }
|
101 | return resultsArr_1;
|
102 | }
|
103 | else {
|
104 | throw "Unknown strategy (" + strategy + ") specified in language template " + transformedOptions.template.language;
|
105 | }
|
106 | })
|
107 | .then(function (generationResult) {
|
108 | generationResult.forEach(function (file) {
|
109 | if (file.isDev) {
|
110 | console.log("================== " + file.path + " ==================");
|
111 | console.log(file.content);
|
112 | }
|
113 | else {
|
114 | fs.writeFileSync(file.path, file.content);
|
115 | console.log("Generated file written to " + file.path);
|
116 | }
|
117 | });
|
118 | })
|
119 | .catch(cli_1.cliError);
|
120 |
|
\ | No newline at end of file |