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 | var schema = scheme_loader_1.loadSchema(transformedOptions.introspection);
|
25 | var documents = transformedOptions.documents;
|
26 | var flattenInnerTypes = templateConfig.flattenInnerTypes;
|
27 | var codegen = codegen_1.prepareCodegen(schema, document_loader_1.loadDocumentsSources(documents), transformedOptions.template.config.primitives, flattenInnerTypes);
|
28 | var strategy = templateConfig.strategy || 'SINGLE_FILE';
|
29 | var baseOutPath = path.basename(transformedOptions.outPath);
|
30 | if (strategy === 'SINGLE_FILE') {
|
31 | if (baseOutPath.indexOf('.') === -1) {
|
32 | throw "Generator '" + transformedOptions.template.language + "' uses single-file strategy! Please specify a filename using --out flag!";
|
33 | }
|
34 | var templatePath = path.resolve(__dirname, transformedOptions.template.config.basePath, transformedOptions.template.config.template);
|
35 | var outPath = path.resolve(transformedOptions.outPath);
|
36 | var outDir = path.dirname(outPath);
|
37 | mkdirp.sync(outDir);
|
38 | return [{
|
39 | isDev: transformedOptions.isDev,
|
40 | content: template_loader_1.compileTemplate(codegen, templatePath),
|
41 | path: outPath
|
42 | }];
|
43 | }
|
44 | else if (strategy === 'MULTIPLE_FILES') {
|
45 | if (baseOutPath.indexOf('.') > -1) {
|
46 | throw "Generator '" + transformedOptions.template.language + "' uses multiple-files strategy! Please specify a directory using --out flag!";
|
47 | }
|
48 | var resultsArr_1 = [];
|
49 | var filesExtension_1 = transformedOptions.template.config.filesExtension;
|
50 | var templates_1 = transformedOptions.template.config.templates;
|
51 | var outPath = path.resolve(transformedOptions.outPath);
|
52 | mkdirp.sync(outPath);
|
53 | Object.keys(templates_1).forEach(function (templateName) {
|
54 | var templatePath = path.resolve(__dirname, transformedOptions.template.config.basePath, templates_1[templateName]);
|
55 | if (templateName === 'model') {
|
56 | codegen.models.forEach(function (model) {
|
57 | resultsArr_1.push({
|
58 | isDev: transformedOptions.isDev,
|
59 | content: template_loader_1.compileTemplate(model, templatePath),
|
60 | path: path.resolve(transformedOptions.outPath, model.name + '.model.' + filesExtension_1)
|
61 | });
|
62 | });
|
63 | }
|
64 | if (templateName === 'document') {
|
65 | codegen.documents.forEach(function (document) {
|
66 | resultsArr_1.push({
|
67 | isDev: transformedOptions.isDev,
|
68 | content: template_loader_1.compileTemplate(document, templatePath),
|
69 | path: path.resolve(transformedOptions.outPath, document.name + '.document.' + filesExtension_1)
|
70 | });
|
71 | });
|
72 | }
|
73 | });
|
74 | resultsArr_1 = resultsArr_1.filter(function (item) { return item.content.length > 0; });
|
75 | if (templates_1['index']) {
|
76 | var directoryPath_1 = path.resolve(transformedOptions.outPath);
|
77 | var indexOutPath = path.resolve(directoryPath_1, 'index.' + filesExtension_1);
|
78 | var templatePath = path.resolve(transformedOptions.template.config.basePath, templates_1['index']);
|
79 | resultsArr_1.push({
|
80 | isDev: transformedOptions.isDev,
|
81 | content: template_loader_1.compileTemplate({ files: resultsArr_1.map(function (item) {
|
82 | return {
|
83 | fileName: path.basename(item.path, '.' + filesExtension_1),
|
84 | fullPath: item.path,
|
85 | extension: filesExtension_1,
|
86 | directory: directoryPath_1
|
87 | };
|
88 | }) }, templatePath),
|
89 | path: indexOutPath
|
90 | });
|
91 | }
|
92 | return resultsArr_1;
|
93 | }
|
94 | else {
|
95 | throw "Unknown strategy (" + strategy + ") specified in language template " + transformedOptions.template.language;
|
96 | }
|
97 | })
|
98 | .then(function (generationResult) {
|
99 | generationResult.forEach(function (file) {
|
100 | if (file.isDev) {
|
101 | console.log("================== " + file.path + " ==================");
|
102 | console.log(file.content);
|
103 | }
|
104 | else {
|
105 | fs.writeFileSync(file.path, file.content);
|
106 | console.log("Generated file written to " + file.path);
|
107 | }
|
108 | });
|
109 | })
|
110 | .catch(cli_1.cliError);
|
111 |
|
\ | No newline at end of file |