Skip to content
This repository was archived by the owner on Sep 23, 2022. It is now read-only.

Commit ef386e3

Browse files
tcdavisexpbot
authored andcommitted
cleanup code for versions not supporting project dependencies
fbshipit-source-id: e4047b5
1 parent 91f92cd commit ef386e3

File tree

12 files changed

+43
-1043
lines changed

12 files changed

+43
-1043
lines changed

example/src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class App extends Component {
5151
sessionId: Math.random()
5252
.toString(36)
5353
.substr(2, 8),
54-
sdkVersion: '24.0.0',
54+
sdkVersion: '25.0.0',
5555
});
5656

5757
this._logSubscription = this._snack.addLogListener(this._onLog);

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"license": "MIT",
2424
"dependencies": {
2525
"babel-runtime": "^6.23.0",
26-
"babylon": "^6.17.2",
2726
"diff": "^3.2.0",
2827
"lodash": "^4.16.1",
2928
"platform": "^1.3.4",

src/SnackSession.js

Lines changed: 42 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ import constructExperienceURL from './utils/constructExperienceURL';
2424
import sendFileUtils from './utils/sendFileUtils';
2525
import isModulePreloaded from './utils/isModulePreloaded';
2626
import { convertDependencyFormat } from './utils/projectDependencies';
27-
import {
28-
findModuleDependencies,
29-
writeModuleVersions,
30-
removeModuleVersions,
31-
} from './utils/moduleUtils';
3227

3328
let platform = null;
3429
// + and - are used as delimiters in the uri, ensure they do not appear in the channel itself
@@ -169,11 +164,6 @@ export default class SnackSession {
169164
throw new Error('Please use a channel id with more entropy');
170165
}
171166

172-
this._handleFindDependenciesAsync();
173-
if (this.supportsFeature('PROJECT_DEPENDENCIES')) {
174-
this._removeModuleVersionPins();
175-
}
176-
177167
this.pubnub = new PubNub({
178168
publishKey: 'pub-c-2a7fd67b-333d-40db-ad2d-3255f8835f70',
179169
subscribeKey: 'sub-c-0b655000-d784-11e6-b950-02ee2ddab7fe',
@@ -352,18 +342,10 @@ export default class SnackSession {
352342
// TODO: error when changing SDK to an unsupported version
353343
setSdkVersion = (sdkVersion: SDKVersion): void => {
354344
if (this.sdkVersion !== sdkVersion) {
355-
if (
356-
sdkSupportsFeature(sdkVersion, 'PROJECT_DEPENDENCIES') &&
357-
!this.supportsFeature('PROJECT_DEPENDENCIES')
358-
) {
359-
this._removeModuleVersionPins();
360-
}
361-
362345
this.sdkVersion = sdkVersion;
363346

364347
this._sendStateEvent();
365348
this._updateDevSession();
366-
this._handleFindDependenciesAsync();
367349
}
368350
};
369351

@@ -492,7 +474,7 @@ export default class SnackSession {
492474
const payload = {
493475
manifest,
494476
code: this.files,
495-
dependencies: this.supportsFeature('PROJECT_DEPENDENCIES') ? this.dependencies : null,
477+
dependencies: this.dependencies,
496478
isDraft: options.isDraft,
497479
};
498480

@@ -583,27 +565,23 @@ export default class SnackSession {
583565
* @function
584566
*/
585567
addModuleAsync = async (name: string, version?: string): Promise<void> => {
586-
if (this.supportsFeature('PROJECT_DEPENDENCIES')) {
587-
const install = async () => {
588-
try {
589-
this.dependencies = await this._addModuleAsync(name, version, this.dependencies);
590-
} finally {
591-
this._sendStateEvent();
592-
this._publish();
593-
}
594-
};
568+
const install = async () => {
569+
try {
570+
this.dependencies = await this._addModuleAsync(name, version, this.dependencies);
571+
} finally {
572+
this._sendStateEvent();
573+
this._publish();
574+
}
575+
};
595576

596-
return (this._lastInstall = this._lastInstall.then(install, install));
597-
}
577+
return (this._lastInstall = this._lastInstall.then(install, install));
598578
};
599579

600580
removeModuleAsync = async (name: string): Promise<void> => {
601-
if (this.supportsFeature('PROJECT_DEPENDENCIES')) {
602-
/* $FlowFixMe */
603-
this.dependencies = pickBy(this.dependencies, (value, key: string) => key !== name);
604-
this._sendStateEvent();
605-
this._publish();
606-
}
581+
/* $FlowFixMe */
582+
this.dependencies = pickBy(this.dependencies, (value, key: string) => key !== name);
583+
this._sendStateEvent();
584+
this._publish();
607585
};
608586

609587
/**
@@ -616,37 +594,35 @@ export default class SnackSession {
616594
modules: { [name: string]: ?string },
617595
onError: (name: string, e: Error) => mixed
618596
): Promise<void> => {
619-
if (this.supportsFeature('PROJECT_DEPENDENCIES')) {
620-
const sync = async () => {
621-
/* $FlowFixMe */
622-
let dependencies = pickBy(this.dependencies, (value, name: string) =>
623-
// Only keep dependencies in the modules list
624-
modules.hasOwnProperty(name)
625-
);
597+
const sync = async () => {
598+
/* $FlowFixMe */
599+
let dependencies = pickBy(this.dependencies, (value, name: string) =>
600+
// Only keep dependencies in the modules list
601+
modules.hasOwnProperty(name)
602+
);
626603

627-
// Install any new dependencies in series
628-
for (const name of Object.keys(modules)) {
629-
try {
630-
dependencies = await this._addModuleAsync(name, modules[name], dependencies);
631-
} catch (e) {
632-
onError(name, e);
633-
}
604+
// Install any new dependencies in series
605+
for (const name of Object.keys(modules)) {
606+
try {
607+
dependencies = await this._addModuleAsync(name, modules[name], dependencies);
608+
} catch (e) {
609+
onError(name, e);
634610
}
611+
}
635612

636-
// Don't update state if nothing changed
637-
if (!isEqual(dependencies, this.dependencies)) {
638-
// Update dependencies list
639-
this.dependencies = dependencies;
613+
// Don't update state if nothing changed
614+
if (!isEqual(dependencies, this.dependencies)) {
615+
// Update dependencies list
616+
this.dependencies = dependencies;
640617

641-
// Notify listeners
642-
this._sendStateEvent();
643-
this._publish();
644-
}
645-
};
618+
// Notify listeners
619+
this._sendStateEvent();
620+
this._publish();
621+
}
622+
};
646623

647-
// Queue multiple syncs
648-
return (this._lastModuleSync = this._lastModuleSync.then(sync, sync));
649-
}
624+
// Queue multiple syncs
625+
return (this._lastModuleSync = this._lastModuleSync.then(sync, sync));
650626
};
651627

652628
// Private methods and properties
@@ -849,8 +825,6 @@ export default class SnackSession {
849825
};
850826

851827
_publishNotDebouncedAsync = async () => {
852-
await this._handleFindDependenciesAsync();
853-
854828
if (this.loadingMessage) {
855829
this._sendLoadingEvent();
856830
} else {
@@ -869,16 +843,6 @@ export default class SnackSession {
869843
dependencies: this.dependencies,
870844
metadata,
871845
};
872-
if (!this.supportsFeature('PROJECT_DEPENDENCIES')) {
873-
if (message.diff.hasOwnProperty('App.js')) {
874-
message.diff['app.js'] = message.diff['App.js'];
875-
delete message.diff['App.js'];
876-
}
877-
if (message.s3url.hasOwnProperty('App.js')) {
878-
message.s3url['app.js'] = message.s3url['App.js'];
879-
delete message.s3url['App.js'];
880-
}
881-
}
882846

883847
this.pubnub.publish({ channel: this.channel, message }, (status, response) => {
884848
if (status.error) {
@@ -989,9 +953,9 @@ export default class SnackSession {
989953
count++;
990954

991955
this._log(
992-
`Requesting dependency: ${this.snackagerUrl}/bundle/${name}${version
993-
? `@${version}`
994-
: ''}?platforms=ios,android`
956+
`Requesting dependency: ${this.snackagerUrl}/bundle/${name}${
957+
version ? `@${version}` : ''
958+
}?platforms=ios,android`
995959
);
996960
const res = await fetch(
997961
`${this.snackagerUrl}/bundle/${name}${version ? `@${version}` : ''}?platforms=ios,android`
@@ -1023,7 +987,7 @@ export default class SnackSession {
1023987
const match = /^(?:@([^/?]+)\/)?([^@/?]+)(?:\/([^@]+))?/.exec(name);
1024988

1025989
if (!match) {
1026-
return Promise.reject(new Error(`Failed to parse the package name: '${name}'`))
990+
return Promise.reject(new Error(`Failed to parse the package name: '${name}'`));
1027991
}
1028992

1029993
const fullName = (match[1] ? `@${match[1]}/` : '') + match[2];
@@ -1066,179 +1030,6 @@ export default class SnackSession {
10661030
return results.every(result => result);
10671031
};
10681032

1069-
_handleFindDependenciesAsync = async () => {
1070-
if (this.supportsFeature('PROJECT_DEPENDENCIES')) {
1071-
return;
1072-
}
1073-
1074-
const files = this.files;
1075-
1076-
if (this.isResolving) {
1077-
return;
1078-
}
1079-
1080-
this.isResolving = true;
1081-
1082-
try {
1083-
await Promise.all(
1084-
Object.keys(files).map(async key => {
1085-
if (key.endsWith('.js')) {
1086-
const codeAtStartOfFindDependencies = files[key].contents;
1087-
const codeWithVersions = await this._findDependenciesOnceAsync(files[key].contents);
1088-
if (files[key].contents === codeAtStartOfFindDependencies) {
1089-
// can be null if no changes need to be made
1090-
if (codeWithVersions) {
1091-
files[key].contents = codeWithVersions;
1092-
this._sendStateEvent();
1093-
}
1094-
}
1095-
}
1096-
})
1097-
);
1098-
} catch (e) {
1099-
console.error(e);
1100-
} finally {
1101-
this.loadingMessage = null;
1102-
this.isResolving = false;
1103-
this._sendStateEvent();
1104-
}
1105-
};
1106-
1107-
_findDependenciesOnceAsync = async (file: string): Promise<?string> => {
1108-
let modules: { [string]: string };
1109-
1110-
try {
1111-
// Find all module imports in the code
1112-
// This will skip local imports and reserved ones
1113-
modules = pickBy(
1114-
/* $FlowFixMe */
1115-
findModuleDependencies(file),
1116-
(version: string, module: string) =>
1117-
!module.startsWith('.') && !isModulePreloaded(module, this.sdkVersion)
1118-
);
1119-
} catch (e) {
1120-
// Likely a parse error
1121-
this._error(`Couldn't find dependencies: ${e.message}`);
1122-
return null;
1123-
}
1124-
1125-
// Check if the dependencies already exist
1126-
const changedModules = Object.keys(modules).filter(moduleName => {
1127-
return (
1128-
!this.dependencies.hasOwnProperty(moduleName) ||
1129-
modules[moduleName] !== this.dependencies[moduleName].version
1130-
);
1131-
});
1132-
if (!Object.keys(modules).length || !changedModules.length) {
1133-
this._log(`All dependencies are already loaded: ${JSON.stringify(modules)}`);
1134-
return null;
1135-
}
1136-
1137-
this._sendStateEvent();
1138-
this.loadingMessage = `Resolving dependencies`;
1139-
this._sendLoadingEvent();
1140-
1141-
// TODO: only run the following code after a delay to ensure the user has finished typing
1142-
try {
1143-
// Fetch the dependencies
1144-
// This will also trigger bundling
1145-
this._log(`Fetching dependencies: ${JSON.stringify(modules)}`);
1146-
const results = await Promise.all(
1147-
Object.keys(modules).map(name => this._maybeFetchDependencyAsync(name, modules[name]))
1148-
);
1149-
this._log(`Got dependencies: ${JSON.stringify(results)}`);
1150-
// results will have an error key if they failed
1151-
1152-
let peerDependencies = {};
1153-
let peerDependencyResolutions = {};
1154-
1155-
// Some items might have peer dependencies
1156-
// We need to collect them and install them
1157-
results.map(it => {
1158-
if (it.dependencies) {
1159-
Object.keys(it.dependencies).forEach(name => {
1160-
if (!isModulePreloaded(name, this.sdkVersion)) {
1161-
// $FlowFixMe we already confirmed it.dependencies exists
1162-
peerDependencies[name] = { version: it.dependencies[name], isUserSpecified: false };
1163-
}
1164-
});
1165-
}
1166-
});
1167-
1168-
// Set dependencies to the updated list
1169-
// TODO: Understand if / why this line is this neccesary
1170-
this.dependencies = { ...this.dependencies, ...peerDependencies };
1171-
this._sendStateEvent();
1172-
1173-
// Fetch the peer dependencies
1174-
this._log(`Fetching peer dependencies: ${JSON.stringify(peerDependencies)}`);
1175-
peerDependencyResolutions = await Promise.all(
1176-
Object.keys(peerDependencies).map(name =>
1177-
this._maybeFetchDependencyAsync(name, peerDependencies[name].version)
1178-
)
1179-
);
1180-
1181-
// Collect all dependency and peer dependency names and version
1182-
const dependencies = {};
1183-
1184-
// do peerDeps first to make sure we prioritize the non-peerDeps versions
1185-
peerDependencyResolutions.forEach(it => {
1186-
dependencies[it.name] = { version: it.version, isUserSpecified: false };
1187-
});
1188-
1189-
results.forEach(it => {
1190-
dependencies[it.name] = { version: it.version, isUserSpecified: true };
1191-
});
1192-
1193-
let code = file;
1194-
1195-
// We need to insert peer dependencies in code when found
1196-
if (peerDependencyResolutions.length && !this.supportsFeature('PROJECT_DEPENDENCIES')) {
1197-
this._log(`Adding imports for peer dependencies: ${JSON.stringify(peerDependencies)}`);
1198-
1199-
const { default: insertImports } = await import('./utils/insertImports');
1200-
1201-
code = insertImports(
1202-
code,
1203-
peerDependencyResolutions.map(it => ({
1204-
// Insert an import statement for the module
1205-
// This will skip the import if already present
1206-
from: it.name,
1207-
}))
1208-
);
1209-
}
1210-
1211-
// TODO: this system will not remove old dependencies that are no longer needed!
1212-
Object.assign(this.dependencies, dependencies);
1213-
1214-
if (!this.supportsFeature('PROJECT_DEPENDENCIES')) {
1215-
this._log('Writing module versions');
1216-
code = writeModuleVersions(code, dependencies);
1217-
}
1218-
1219-
this._sendStateEvent();
1220-
1221-
return code;
1222-
} catch (e) {
1223-
// TODO: Show user that there is an error getting dependencies
1224-
this._error(`Error in _findDependenciesOnceAsync: ${e.message}`);
1225-
} finally {
1226-
this._sendStateEvent();
1227-
}
1228-
};
1229-
1230-
_removeModuleVersionPins = () => {
1231-
Object.keys(this.files).map(key => {
1232-
if (key.endsWith('.js')) {
1233-
try {
1234-
this.files[key].contents = removeModuleVersions(this.files[key].contents);
1235-
} catch (e) {
1236-
// Do nothing
1237-
}
1238-
}
1239-
});
1240-
};
1241-
12421033
_addModuleAsync = async (name: string, version: ?string, previous: ExpoDependencyV2) => {
12431034
if (isModulePreloaded(name, this.sdkVersion)) {
12441035
throw new Error(`Module is already preloaded: ${name}`);

0 commit comments

Comments
 (0)