1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Config = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var common_1 = require("./common");
|
6 | var manifest_1 = require("./manifest");
|
7 | var stripSlashes = common_1.util.stripSlashes;
|
8 | var DEFAULT = {
|
9 | FILE: 'ssr.yml',
|
10 | };
|
11 | var ERROR = {
|
12 | noFile: function (path) { return "An \"" + DEFAULT.FILE + "\" configuration file does not exist at: " + path; },
|
13 | };
|
14 | var Config = (function () {
|
15 | function Config(args) {
|
16 | this.def = args.def;
|
17 | }
|
18 | Object.defineProperty(Config.prototype, "secret", {
|
19 | get: function () {
|
20 | return toValue(this.def.secret);
|
21 | },
|
22 | enumerable: false,
|
23 | configurable: true
|
24 | });
|
25 | Object.defineProperty(Config.prototype, "builder", {
|
26 | get: function () {
|
27 | var builder = this.def.builder || {};
|
28 | builder.bundles = builder.bundles || 'bundles';
|
29 | builder.entries = builder.entries || '';
|
30 | return builder;
|
31 | },
|
32 | enumerable: false,
|
33 | configurable: true
|
34 | });
|
35 | Object.defineProperty(Config.prototype, "s3", {
|
36 | get: function () {
|
37 | var s3 = this.def.s3 || {};
|
38 | var path = s3.path || {};
|
39 | var endpoint = common_1.util.stripHttp(s3.endpoint || '');
|
40 | var cdn = common_1.util.stripHttp(s3.cdn || '');
|
41 | var accessKey = toValue(s3.accessKey);
|
42 | var secret = toValue(s3.secret);
|
43 | var bucket = s3.bucket || '';
|
44 | var api = {
|
45 | endpoint: endpoint,
|
46 | cdn: cdn,
|
47 | accessKey: accessKey,
|
48 | secret: secret,
|
49 | bucket: bucket,
|
50 | path: {
|
51 | base: stripSlashes(path.base || ''),
|
52 | manifest: stripSlashes(path.manifest || ''),
|
53 | bundles: stripSlashes(path.bundles || ''),
|
54 | },
|
55 | get config() {
|
56 | return { endpoint: endpoint, accessKey: accessKey, secret: secret };
|
57 | },
|
58 | get fs() {
|
59 | return common_1.fs.s3(api.config);
|
60 | },
|
61 | versions: function (options) {
|
62 | if (options === void 0) { options = {}; }
|
63 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
64 | var s3, prefix, list, dirs, versions;
|
65 | return tslib_1.__generator(this, function (_a) {
|
66 | switch (_a.label) {
|
67 | case 0:
|
68 | s3 = api.fs;
|
69 | prefix = api.path.base + "/" + api.path.bundles;
|
70 | list = s3.list({
|
71 | bucket: bucket,
|
72 | prefix: prefix,
|
73 | });
|
74 | return [4, list.dirs];
|
75 | case 1:
|
76 | dirs = (_a.sent()).items.map(function (_a) {
|
77 | var key = _a.key;
|
78 | return ({ key: key, version: common_1.fs.basename(key) });
|
79 | });
|
80 | versions = common_1.semver.sort(dirs.map(function (item) { return item.version; }));
|
81 | return [2, options.sort === 'DESC' ? versions.reverse() : versions];
|
82 | }
|
83 | });
|
84 | });
|
85 | },
|
86 | };
|
87 | return api;
|
88 | },
|
89 | enumerable: false,
|
90 | configurable: true
|
91 | });
|
92 | Object.defineProperty(Config.prototype, "baseUrl", {
|
93 | get: function () {
|
94 | var s3 = this.s3;
|
95 | return "https://" + (s3.cdn || s3.endpoint) + "/" + s3.path.base;
|
96 | },
|
97 | enumerable: false,
|
98 | configurable: true
|
99 | });
|
100 | Object.defineProperty(Config.prototype, "manifest", {
|
101 | get: function () {
|
102 | var filePath = common_1.fs.resolve(this.def.manifest || 'manifest.yml');
|
103 | var s3 = this.s3;
|
104 | var manifestUrl = "https://" + s3.endpoint + "/" + s3.bucket + "/" + s3.path.base + "/" + s3.path.manifest;
|
105 | var baseUrl = this.baseUrl;
|
106 | var config = this;
|
107 | var api = {
|
108 | local: {
|
109 | path: filePath,
|
110 | get exists() {
|
111 | return common_1.fs.pathExists(filePath);
|
112 | },
|
113 | load: function (args) {
|
114 | if (args === void 0) { args = {}; }
|
115 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
116 | var loadBundleManifest;
|
117 | return tslib_1.__generator(this, function (_a) {
|
118 | loadBundleManifest = args.loadBundleManifest;
|
119 | return [2, manifest_1.Manifest.fromFile({ path: filePath, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
|
120 | });
|
121 | });
|
122 | },
|
123 | ensureLatest: function (args) {
|
124 | if (args === void 0) { args = {}; }
|
125 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
126 | var remote, minimal;
|
127 | return tslib_1.__generator(this, function (_a) {
|
128 | switch (_a.label) {
|
129 | case 0: return [4, api.local.exists];
|
130 | case 1:
|
131 | if (!!(_a.sent())) return [3, 3];
|
132 | return [4, config.createFromTemplate()];
|
133 | case 2:
|
134 | _a.sent();
|
135 | _a.label = 3;
|
136 | case 3: return [4, api.s3.pull({ force: true })];
|
137 | case 4:
|
138 | remote = _a.sent();
|
139 | if (!remote.ok) return [3, 6];
|
140 | minimal = common_1.defaultValue(args.minimal, true);
|
141 | return [4, remote.save(filePath, { minimal: minimal })];
|
142 | case 5:
|
143 | _a.sent();
|
144 | _a.label = 6;
|
145 | case 6: return [2, api.local.load()];
|
146 | }
|
147 | });
|
148 | });
|
149 | },
|
150 | },
|
151 | s3: {
|
152 | url: manifestUrl,
|
153 | pull: function (args) {
|
154 | if (args === void 0) { args = {}; }
|
155 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
156 | return tslib_1.__generator(this, function (_a) {
|
157 | return [2, manifest_1.Manifest.get(tslib_1.__assign(tslib_1.__assign({}, args), { manifestUrl: manifestUrl, baseUrl: baseUrl }))];
|
158 | });
|
159 | });
|
160 | },
|
161 | },
|
162 | };
|
163 | return api;
|
164 | },
|
165 | enumerable: false,
|
166 | configurable: true
|
167 | });
|
168 | Config.prototype.createFromTemplate = function () {
|
169 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
170 | var source, target;
|
171 | return tslib_1.__generator(this, function (_a) {
|
172 | switch (_a.label) {
|
173 | case 0:
|
174 | source = common_1.fs.join(__dirname, '../tmpl/manifest.yml');
|
175 | target = this.manifest.local.path;
|
176 | return [4, common_1.fs.ensureDir(common_1.fs.dirname(target))];
|
177 | case 1:
|
178 | _a.sent();
|
179 | return [4, common_1.fs.copy(source, target)];
|
180 | case 2:
|
181 | _a.sent();
|
182 | return [2, { source: source, target: target }];
|
183 | }
|
184 | });
|
185 | });
|
186 | };
|
187 | Config.create = function (options) {
|
188 | if (options === void 0) { options = {}; }
|
189 | return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
190 | var path, def;
|
191 | return tslib_1.__generator(this, function (_a) {
|
192 | switch (_a.label) {
|
193 | case 0:
|
194 | path = common_1.fs.resolve(options.path || DEFAULT.FILE);
|
195 | return [4, common_1.fs.pathExists(path)];
|
196 | case 1:
|
197 | if (!!(_a.sent())) return [3, 2];
|
198 | throw new Error(ERROR.noFile(path));
|
199 | case 2: return [4, common_1.fs.file.loadAndParse(path)];
|
200 | case 3:
|
201 | def = _a.sent();
|
202 | return [2, new Config({ def: def })];
|
203 | }
|
204 | });
|
205 | });
|
206 | };
|
207 | Config.createSync = function (options) {
|
208 | if (options === void 0) { options = {}; }
|
209 | var path = common_1.fs.resolve(options.path || DEFAULT.FILE);
|
210 | if (!common_1.fs.pathExistsSync(path)) {
|
211 | throw new Error("An \"ssr.yml\" configuration file does not exist at: " + path);
|
212 | }
|
213 | else {
|
214 | var def = common_1.fs.file.loadAndParseSync(path);
|
215 | return new Config({ def: def });
|
216 | }
|
217 | };
|
218 | return Config;
|
219 | }());
|
220 | exports.Config = Config;
|
221 | var toValue = function (value) {
|
222 | value = value || '';
|
223 | value = process.env[value] ? process.env[value] : value;
|
224 | return value || '';
|
225 | };
|