UNPKG

4.21 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14'use strict';
15
16function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
17
18function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
19
20const fsPath = require('path');
21
22const JSZip = require('jszip');
23/**
24 * A utility to persist templates to data sources.
25 * @class
26 * @abstract
27 * @private
28 */
29
30
31class TemplateSaver {
32 /**
33 * Persists this template to a Cicero Template Archive (cta) file.
34 * @param {Template} template - the template to persist
35 * @param {string} [language] - target language for the archive (should be 'ergo')
36 * @param {Object} [options] - JSZip options
37 * @return {Promise<Buffer>} the zlib buffer
38 */
39 static toArchive(template, language, options) {
40 return _asyncToGenerator(function* () {
41 if (!language || typeof language !== 'string') {
42 throw new Error('language is required and must be a string');
43 }
44
45 const metadata = template.getMetadata().createTargetMetadata(language);
46 let zip = new JSZip();
47 let packageFileContents = JSON.stringify(metadata.getPackageJson());
48 zip.file('package.json', packageFileContents, options); // save the grammar
49
50 zip.file('text/', null, Object.assign({}, options, {
51 dir: true
52 }));
53
54 if (template.getParserManager().getTemplatizedGrammar()) {
55 zip.file('text/grammar.tem.md', template.getParserManager().getTemplatizedGrammar(), options);
56 } // save the README.md if present
57
58
59 if (metadata.getREADME()) {
60 zip.file('README.md', metadata.getREADME(), options);
61 } // Save the sample files
62
63
64 const sampleFiles = metadata.getSamples();
65
66 if (sampleFiles) {
67 Object.keys(sampleFiles).forEach(function (locale) {
68 let fileName;
69
70 if (locale === 'default') {
71 fileName = 'text/sample.md';
72 } else {
73 fileName = "text/sample_".concat(locale, ".md");
74 }
75
76 zip.file(fileName, sampleFiles[locale], options);
77 });
78 } // save the request.json if present & not text-only
79
80
81 if (metadata.getRequest()) {
82 let requestFileContents = JSON.stringify(metadata.getRequest());
83 zip.file('request.json', requestFileContents, options);
84 }
85
86 let modelFiles = template.getModelManager().getModels();
87 zip.file('model/', null, Object.assign({}, options, {
88 dir: true
89 }));
90 modelFiles.forEach(function (file) {
91 zip.file('model/' + file.name, file.content, options);
92 });
93 zip.file('logic/', null, Object.assign({}, options, {
94 dir: true
95 }));
96 const scriptFiles = template.getScriptManager().getScriptsForTarget(language);
97 scriptFiles.forEach(function (file) {
98 let fileIdentifier = file.getIdentifier();
99 let fileName = fsPath.basename(fileIdentifier);
100 zip.file('logic/' + fileName, file.contents, options);
101 });
102 return zip.generateAsync({
103 type: 'nodebuffer'
104 }).then(something => {
105 return Promise.resolve(something).then(result => {
106 return result;
107 });
108 });
109 })();
110 }
111
112}
113
114module.exports = TemplateSaver;
\No newline at end of file