Skip to content

Commit

Permalink
Fixed error while stop expo packager (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
itoys authored Jul 23, 2018
1 parent c9e236d commit 77b86bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 41 deletions.
58 changes: 41 additions & 17 deletions src/common/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,29 @@ export class Packager {
args = args.concat("--resetCache");
}

// Arguments below using for Expo apps
args.push("--root", path.relative(this.projectPath, path.resolve(this.workspacePath, ".vscode")));
let helper = new ExponentHelper(this.workspacePath, this.projectPath);
return helper.getExpPackagerOptions()
.then((options: ExpConfigPackager) => {
Object.keys(options).forEach(key => {
args = args.concat([`--${key}`, options[key]]);
});

return helper.isExpoApp(false)
.then((isExpo) => {
if (isExpo) {
// Arguments below using for Expo apps
args.push("--root", path.relative(this.projectPath, path.resolve(this.workspacePath, ".vscode")));
return helper.getExpPackagerOptions()
.then((options: ExpConfigPackager) => {
Object.keys(options).forEach(key => {
args = args.concat([`--${key}`, options[key]]);
});

return args;
})
.catch(() => {
this.logger.warning("Couldn't read packager's options from exp.json, continue...");
return args;
});
} else {
return args;
})
.catch(() => {
this.logger.warning("Couldn't read packager's options from exp.json, continue...");
return args;
});
}
});
})
.then((args) => {
const projectRoot = SettingsHelper.getReactNativeProjectRoot(this.workspacePath);
Expand Down Expand Up @@ -293,11 +301,27 @@ export class Packager {
this.logger.info("Stopping Packager");
return new CommandExecutor(this.projectPath, this.logger).killReactPackager(this.packagerProcess).then(() => {
this.packagerProcess = undefined;
this.logger.debug("Stopping Exponent");
return XDL.stopAll(this.projectPath)
.then(() =>
this.logger.debug("Exponent Stopped")
);

let helper = new ExponentHelper(this.workspacePath, this.projectPath);

return helper.isExpoApp(false)
.then((isExpo) => {
if (isExpo) {
this.logger.debug("Stopping Exponent");
return XDL.stopAll(this.projectPath)
.then(() => {
this.logger.debug("Exponent Stopped");
})
.catch((err) => {
if (err.code === "NOT_LOGGED_IN") {
return void(0);
}
throw err;
});
} else {
return void(0);
}
});
});
}

Expand Down
47 changes: 23 additions & 24 deletions src/extension/exponent/exponentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const DBL_SLASHES = /\\/g;
export class ExponentHelper {
private workspaceRootPath: string;
private projectRootPath: string;
private fs: FileSystem;
private fs: FileSystem = new FileSystem();
private hasInitialized: boolean;
private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel();

Expand All @@ -42,6 +42,7 @@ export class ExponentHelper {
public configureExponentEnvironment(): Q.Promise<void> {
this.lazilyInitialize();
this.logger.info("Making sure your project uses the correct dependencies for exponent. This may take a while...");
this.logger.logStream("Checking if this is Expo app.");
return this.isExpoApp(true)
.then(isExpo => {
this.logger.logStream(".\n");
Expand Down Expand Up @@ -85,6 +86,27 @@ export class ExponentHelper {
.then(opts => opts || {});
}

public isExpoApp(showProgress: boolean = false): Q.Promise<boolean> {
if (showProgress) {
this.logger.logStream("...");
}

const packageJsonPath = this.pathToFileInWorkspace("package.json");
return this.fs.readFile(packageJsonPath)
.then(content => {
const packageJson = JSON.parse(content);
const isExp = packageJson.dependencies && !!packageJson.dependencies.expo || false;
if (showProgress) this.logger.logStream(".");
return isExp;
}).catch(() => {
if (showProgress) {
this.logger.logStream(".");
}
// Not in a react-native project
return false;
});
}

/**
* Path to a given file inside the .vscode directory
*/
Expand Down Expand Up @@ -258,35 +280,12 @@ AppRegistry.registerRunnable('main', function(appParameters) {
return path.join(this.projectRootPath, filename).replace(DBL_SLASHES, "/");
}

private isExpoApp(showProgress: boolean = false): Q.Promise<boolean> {
this.logger.logStream("Checking if this is Expo app.");
if (showProgress) {
this.logger.logStream("...");
}

const packageJsonPath = this.pathToFileInWorkspace("package.json");
return this.fs.readFile(packageJsonPath)
.then(content => {
const packageJson = JSON.parse(content);
const isExp = packageJson.dependencies && !!packageJson.dependencies.expo || false;
if (showProgress) this.logger.logStream(".");
return isExp;
}).catch(() => {
if (showProgress) {
this.logger.logStream(".");
}
// Not in a react-native project
return false;
});
}

/**
* Works as a constructor but only initiliazes when it's actually needed.
*/
private lazilyInitialize(): void {
if (!this.hasInitialized) {
this.hasInitialized = true;
this.fs = new FileSystem();

XDL.configReactNativeVersionWargnings();
XDL.attachLoggerStream(this.projectRootPath, {
Expand Down

0 comments on commit 77b86bf

Please sign in to comment.