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