UNPKG

10.7 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.publishAsync = publishAsync;
7function _config() {
8 const data = require("@expo/config");
9 _config = function () {
10 return data;
11 };
12 return data;
13}
14function _formData() {
15 const data = _interopRequireDefault(require("form-data"));
16 _formData = function () {
17 return data;
18 };
19 return data;
20}
21function _fsExtra() {
22 const data = _interopRequireDefault(require("fs-extra"));
23 _fsExtra = function () {
24 return data;
25 };
26 return data;
27}
28function _path() {
29 const data = _interopRequireDefault(require("path"));
30 _path = function () {
31 return data;
32 };
33 return data;
34}
35function _internal() {
36 const data = require("../internal");
37 _internal = function () {
38 return data;
39 };
40 return data;
41}
42function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43async function publishAsync(projectRoot, options = {}) {
44 var _options$target, _bundles$android$herm, _bundles$android, _bundles$android2, _bundles$ios$hermesBy, _bundles$ios, _bundles$ios2, _ref, _bundles$android$herm2, _bundles$android3, _bundles$android4, _ref2, _bundles$ios$hermesSo, _bundles$ios3, _bundles$ios4, _exp$ios, _exp$android, _options$releaseChann;
45 options.target = (_options$target = options.target) !== null && _options$target !== void 0 ? _options$target : (0, _config().getDefaultTarget)(projectRoot);
46 const target = options.target;
47 const user = await _internal().UserManager.ensureLoggedInAsync();
48 if (_internal().Env.isDebug()) {
49 console.log();
50 console.log('Publish Assets:');
51 console.log(`- Asset target: ${target}`);
52 console.log();
53 }
54 _internal().Analytics.logEvent('Publish', {
55 developerTool: _internal().Config.developerTool
56 });
57 const validationStatus = await _internal().Doctor.validateWithNetworkAsync(projectRoot);
58 if (validationStatus === _internal().Doctor.ERROR || validationStatus === _internal().Doctor.FATAL) {
59 throw new (_internal().XDLError)('PUBLISH_VALIDATION_ERROR', "Couldn't publish because errors were found. (See logs above.) Please fix the errors and try again.");
60 }
61
62 // Get project config
63 const {
64 exp,
65 pkg,
66 hooks
67 } = await (0, _internal().getPublishExpConfigAsync)(projectRoot, options);
68
69 // TODO: refactor this out to a function, throw error if length doesn't match
70 const validPostPublishHooks = (0, _internal().prepareHooks)(hooks, 'postPublish', projectRoot);
71 const bundles = await (0, _internal().createBundlesAsync)(projectRoot, options, {
72 platforms: ['ios', 'android'],
73 useDevServer: _internal().Env.shouldUseDevServer(exp)
74 });
75 (0, _internal().printBundleSizes)(bundles);
76 await _internal().ProjectAssets.publishAssetsAsync({
77 projectRoot,
78 exp,
79 bundles
80 });
81 const androidBundle = (_bundles$android$herm = (_bundles$android = bundles.android) === null || _bundles$android === void 0 ? void 0 : _bundles$android.hermesBytecodeBundle) !== null && _bundles$android$herm !== void 0 ? _bundles$android$herm : (_bundles$android2 = bundles.android) === null || _bundles$android2 === void 0 ? void 0 : _bundles$android2.code;
82 const iosBundle = (_bundles$ios$hermesBy = (_bundles$ios = bundles.ios) === null || _bundles$ios === void 0 ? void 0 : _bundles$ios.hermesBytecodeBundle) !== null && _bundles$ios$hermesBy !== void 0 ? _bundles$ios$hermesBy : (_bundles$ios2 = bundles.ios) === null || _bundles$ios2 === void 0 ? void 0 : _bundles$ios2.code;
83 const hasHooks = validPostPublishHooks.length > 0;
84 const androidSourceMap = hasHooks ? (_ref = (_bundles$android$herm2 = (_bundles$android3 = bundles.android) === null || _bundles$android3 === void 0 ? void 0 : _bundles$android3.hermesSourcemap) !== null && _bundles$android$herm2 !== void 0 ? _bundles$android$herm2 : (_bundles$android4 = bundles.android) === null || _bundles$android4 === void 0 ? void 0 : _bundles$android4.map) !== null && _ref !== void 0 ? _ref : null : null;
85 const iosSourceMap = hasHooks ? (_ref2 = (_bundles$ios$hermesSo = (_bundles$ios3 = bundles.ios) === null || _bundles$ios3 === void 0 ? void 0 : _bundles$ios3.hermesSourcemap) !== null && _bundles$ios$hermesSo !== void 0 ? _bundles$ios$hermesSo : (_bundles$ios4 = bundles.ios) === null || _bundles$ios4 === void 0 ? void 0 : _bundles$ios4.map) !== null && _ref2 !== void 0 ? _ref2 : null : null;
86 let response;
87 try {
88 response = await _uploadArtifactsAsync({
89 pkg,
90 exp,
91 iosBundle,
92 androidBundle,
93 options
94 });
95 } catch (e) {
96 if (e.serverError === 'SCHEMA_VALIDATION_ERROR') {
97 throw new Error(`There was an error validating your project schema. Check for any warnings about the contents of your app.json or app.config.js.`);
98 }
99 throw e;
100 }
101 let androidManifest = {};
102 let iosManifest = {};
103 const fullManifestUrl = response.url.replace('exp://', 'https://');
104 if (validPostPublishHooks.length || (_exp$ios = exp.ios) !== null && _exp$ios !== void 0 && _exp$ios.publishManifestPath || (_exp$android = exp.android) !== null && _exp$android !== void 0 && _exp$android.publishManifestPath || _internal().EmbeddedAssets.shouldEmbedAssetsForExpoUpdates(projectRoot, exp, pkg, target)) {
105 const sdkOrRuntimeVersion = exp.runtimeVersion ? {
106 'expo-runtime-version': exp.runtimeVersion
107 } : {
108 'expo-sdk-version': exp.sdkVersion
109 };
110 [androidManifest, iosManifest] = await Promise.all([_internal().ExponentTools.getManifestAsync(response.url, {
111 ...sdkOrRuntimeVersion,
112 'Exponent-Platform': 'android',
113 'Expo-Release-Channel': options.releaseChannel,
114 Accept: 'application/expo+json,application/json'
115 }), _internal().ExponentTools.getManifestAsync(response.url, {
116 ...sdkOrRuntimeVersion,
117 'Exponent-Platform': 'ios',
118 'Expo-Release-Channel': options.releaseChannel,
119 Accept: 'application/expo+json,application/json'
120 })]);
121 const hookOptions = {
122 url: response.url,
123 exp,
124 iosBundle,
125 iosSourceMap,
126 iosManifest,
127 iosManifestUrl: fullManifestUrl,
128 androidBundle,
129 androidSourceMap,
130 androidManifest,
131 androidManifestUrl: fullManifestUrl,
132 projectRoot,
133 log: msg => {
134 _internal().Logger.global.info({
135 quiet: true
136 }, msg);
137 }
138 };
139 for (const hook of validPostPublishHooks) {
140 _internal().Logger.global.info(`Running postPublish hook: ${hook.file}`);
141 try {
142 (0, _internal().runHook)(hook, hookOptions);
143 } catch (e) {
144 _internal().Logger.global.warn(`Warning: postPublish hook '${hook.file}' failed: ${e.stack}`);
145 }
146 }
147 }
148 await _internal().EmbeddedAssets.configureAsync({
149 projectRoot,
150 pkg,
151 exp,
152 releaseChannel: (_options$releaseChann = options.releaseChannel) !== null && _options$releaseChann !== void 0 ? _options$releaseChann : 'default',
153 iosManifestUrl: fullManifestUrl,
154 iosManifest,
155 iosBundle,
156 androidManifestUrl: fullManifestUrl,
157 androidManifest,
158 androidBundle,
159 target
160 });
161
162 // TODO: move to postPublish hook
163 if (exp.isKernel) {
164 await _handleKernelPublishedAsync({
165 user,
166 exp,
167 projectRoot,
168 url: response.url
169 });
170 }
171
172 // Create project manifest URL
173 const url = options.releaseChannel && options.releaseChannel !== 'default' ? `${response.url}?release-channel=${options.releaseChannel}` : response.url;
174 const projectPageUrl = createProjectPageURL({
175 projectPageUrl: response.projectPageUrl,
176 releaseChannel: options.releaseChannel
177 });
178 return {
179 ...response,
180 url,
181 projectPageUrl
182 };
183}
184async function _uploadArtifactsAsync({
185 exp,
186 iosBundle,
187 androidBundle,
188 options,
189 pkg
190}) {
191 _internal().Logger.global.info('');
192 _internal().Logger.global.info('Uploading JavaScript bundles');
193 const formData = new (_formData().default)();
194 formData.append('expJson', JSON.stringify(exp));
195 formData.append('packageJson', JSON.stringify(pkg));
196 formData.append('iosBundle', iosBundle, 'iosBundle');
197 formData.append('androidBundle', androidBundle, 'androidBundle');
198 formData.append('options', JSON.stringify(options));
199 const user = await _internal().UserManager.ensureLoggedInAsync();
200 const api = _internal().ApiV2.clientForUser(user);
201 return await api.uploadFormDataAsync('publish/new', formData);
202}
203async function _handleKernelPublishedAsync({
204 projectRoot,
205 user,
206 exp,
207 url
208}) {
209 var _exp$kernel, _exp$kernel2;
210 let owner = exp.owner;
211 if (!owner) {
212 if (user.kind !== 'user') {
213 throw new (_internal().XDLError)('ROBOT_ACCOUNT_ERROR', 'Kernel builds are not available for robot users when owner app.json field is not supplied');
214 }
215 owner = user.username;
216 }
217 let kernelBundleUrl = `${_internal().Config.api.scheme}://${_internal().Config.api.host}`;
218 if (_internal().Config.api.port) {
219 kernelBundleUrl = `${kernelBundleUrl}:${_internal().Config.api.port}`;
220 }
221 kernelBundleUrl = `${kernelBundleUrl}/@${owner}/${exp.slug}/bundle`;
222 const sdkOrRuntimeVersion = exp.runtimeVersion ? {
223 'expo-runtime-version': exp.runtimeVersion
224 } : {
225 'expo-sdk-version': exp.sdkVersion
226 };
227 if ((_exp$kernel = exp.kernel) !== null && _exp$kernel !== void 0 && _exp$kernel.androidManifestPath) {
228 const manifest = await _internal().ExponentTools.getManifestAsync(url, {
229 ...sdkOrRuntimeVersion,
230 'Exponent-Platform': 'android',
231 Accept: 'application/expo+json,application/json'
232 });
233 manifest.bundleUrl = kernelBundleUrl;
234 manifest.sdkVersion = 'UNVERSIONED';
235 await _fsExtra().default.writeFile(_path().default.resolve(projectRoot, exp.kernel.androidManifestPath), JSON.stringify(manifest));
236 }
237 if ((_exp$kernel2 = exp.kernel) !== null && _exp$kernel2 !== void 0 && _exp$kernel2.iosManifestPath) {
238 const manifest = await _internal().ExponentTools.getManifestAsync(url, {
239 ...sdkOrRuntimeVersion,
240 'Exponent-Platform': 'ios',
241 Accept: 'application/expo+json,application/json'
242 });
243 manifest.bundleUrl = kernelBundleUrl;
244 manifest.sdkVersion = 'UNVERSIONED';
245 await _fsExtra().default.writeFile(_path().default.resolve(projectRoot, exp.kernel.iosManifestPath), JSON.stringify(manifest));
246 }
247}
248function createProjectPageURL({
249 projectPageUrl,
250 releaseChannel
251}) {
252 if (!projectPageUrl) {
253 return null;
254 }
255 const formattedProjectUrl = `${projectPageUrl}?serviceType=classic&distribution=expo-go`;
256 if (releaseChannel && releaseChannel !== 'default') {
257 return `${formattedProjectUrl}&release-channel=${releaseChannel}`;
258 } else {
259 return formattedProjectUrl;
260 }
261}
262//# sourceMappingURL=publishAsync.js.map
\No newline at end of file