Skip to content

Commit

Permalink
Conform ios inspector code with npm 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitar Kerezov committed May 30, 2017
1 parent 67fdb67 commit 69e8381
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ export const TYPESCRIPT_NAME = "typescript";
export const BUILD_OUTPUT_EVENT_NAME = "buildOutput";
export const CONNECTION_ERROR_EVENT_NAME = "connectionError";
export const VERSION_STRING = "version";
export const INSPECTOR_CACHE_DIRNAME = "ios-inspector";
33 changes: 23 additions & 10 deletions lib/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,40 @@ export class NpmInstallationManager implements INpmInstallationManager {
}

public async getInspectorFromCache(inspectorNpmPackageName: string, projectDir: string): Promise<string> {
let inspectorPath = path.join(projectDir, "node_modules", inspectorNpmPackageName);
let inspectorPath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME, inspectorNpmPackageName);

// local installation takes precedence over cache
if (!this.inspectorAlreadyInstalled(inspectorPath)) {
let cachepath = (await this.$childProcess.exec("npm get cache")).trim();
let version = await this.getLatestCompatibleVersion(inspectorNpmPackageName);
let pathToPackageInCache = path.join(cachepath, inspectorNpmPackageName, version);
let pathToUnzippedInspector = path.join(pathToPackageInCache, "package");
const cachePath = path.join(this.$options.profileDir, constants.INSPECTOR_CACHE_DIRNAME);
this.prepareCacheDir(cachePath);
const pathToPackageInCache = path.join(cachePath, constants.NODE_MODULES_FOLDER_NAME, inspectorNpmPackageName);

if (!this.$fs.exists(pathToPackageInCache)) {
await this.$childProcess.exec(`npm cache add ${inspectorNpmPackageName}@${version}`);
let inspectorTgzPathInCache = path.join(pathToPackageInCache, "package.tgz");
await this.$childProcess.exec(`tar -xf ${inspectorTgzPathInCache} -C ${pathToPackageInCache}`);
await this.$childProcess.exec(`npm install --prefix ${pathToUnzippedInspector}`);
const version = await this.getLatestCompatibleVersion(inspectorNpmPackageName);
await this.$childProcess.exec(`npm install ${inspectorNpmPackageName}@${version} --prefix ${cachePath}`);
}

this.$logger.out("Using inspector from cache.");
return pathToUnzippedInspector;
return pathToPackageInCache;
}

return inspectorPath;
}

private prepareCacheDir(cacheDirName: string): void {
if (!this.$fs.exists(cacheDirName)) {
this.$fs.createDirectory(cacheDirName);
}

const cacheDirPackageJsonLocation = path.join(cacheDirName, constants.PACKAGE_JSON_FILE_NAME);
if (!this.$fs.exists(cacheDirPackageJsonLocation)) {
this.$fs.writeJson(cacheDirPackageJsonLocation, {
name: constants.INSPECTOR_CACHE_DIRNAME,
version: "0.1.0"
});
}
}

private inspectorAlreadyInstalled(pathToInspector: string): Boolean {
if (this.$fs.exists(pathToInspector)) {
return true;
Expand Down

0 comments on commit 69e8381

Please sign in to comment.