1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | function 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 |
|
18 | function _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 |
|
20 | const fsPath = require('path');
|
21 |
|
22 | const JSZip = require('jszip');
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | class TemplateSaver {
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
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);
|
49 |
|
50 | zip.file('grammar/', null, Object.assign({}, options, {
|
51 | dir: true
|
52 | }));
|
53 |
|
54 | if (template.getParserManager().getTemplatizedGrammar()) {
|
55 | zip.file('grammar/template.tem', template.getParserManager().getTemplatizedGrammar(), options);
|
56 | }
|
57 |
|
58 |
|
59 | if (metadata.getREADME()) {
|
60 | zip.file('README.md', metadata.getREADME(), options);
|
61 | }
|
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 = 'sample.txt';
|
72 | } else {
|
73 | fileName = "sample_".concat(locale, ".txt");
|
74 | }
|
75 |
|
76 | zip.file(fileName, sampleFiles[locale], options);
|
77 | });
|
78 | }
|
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('models/', null, Object.assign({}, options, {
|
88 | dir: true
|
89 | }));
|
90 | modelFiles.forEach(function (file) {
|
91 | zip.file('models/' + file.name, file.content, options);
|
92 | });
|
93 | zip.file('lib/', 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('lib/' + 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 |
|
114 | module.exports = TemplateSaver; |
\ | No newline at end of file |