Skip to content

Commit

Permalink
fix: plugin service tests and config reading
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 committed Sep 4, 2020
1 parent 95b79eb commit aaee65e
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 217 deletions.
24 changes: 13 additions & 11 deletions lib/common/project-helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as path from "path";
import * as _ from 'lodash';
import { IProjectHelper, IFileSystem, IErrors } from "./declarations";
import { IProjectHelper, IFileSystem } from "./declarations";
import { IOptions } from "../declarations";
import { injector } from "./yok";

export class ProjectHelper implements IProjectHelper {
constructor(private $logger: ILogger,
private $fs: IFileSystem,
private $staticConfig: Config.IStaticConfig,
private $errors: IErrors,
// private $errors: IErrors,
private $options: IOptions) { }

private cachedProjectDir = "";
Expand Down Expand Up @@ -60,15 +60,17 @@ export class ProjectHelper implements IProjectHelper {
}

private isProjectFileCorrect(projectFilePath: string): boolean {
if (this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE) {
try {
const fileContent = this.$fs.readJson(projectFilePath);
const clientSpecificData = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] && fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE].id;
return !!clientSpecificData;
} catch (err) {
this.$errors.fail("The project file is corrupted. Additional technical information: %s", err);
}
}
// this is no longer correct, since we have moved the "nativescript" key out from package.json

// if (this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE) {
// try {
// const fileContent = this.$fs.readJson(projectFilePath);
// const clientSpecificData = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] && fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE].id;
// return !!clientSpecificData;
// } catch (err) {
// this.$errors.fail("The project file is corrupted. Additional technical information: %s", err);
// }
// }

return true;
}
Expand Down
36 changes: 18 additions & 18 deletions lib/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export class ProjectData implements IProjectData {
private $projectHelper: IProjectHelper,
private $staticConfig: IStaticConfig,
private $options: IOptions,
private $logger: ILogger,
private $injector: IInjector,
private $logger: ILogger,
private $injector: IInjector,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
get projectConfig(): IProjectConfigService {
return this.$injector.resolve('projectConfigService');
}
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }

get projectConfig(): IProjectConfigService {
return this.$injector.resolve('projectConfigService');
}

public initializeProjectData(projectDir?: string): void {
projectDir = projectDir || this.$projectHelper.projectDir;
Expand All @@ -114,8 +114,8 @@ export class ProjectData implements IProjectData {
public initializeProjectDataFromContent(packageJsonContent: string, projectDir?: string): void {
projectDir = projectDir || this.$projectHelper.projectDir || "";
const projectFilePath = this.getProjectFilePath(projectDir);
// If no project found, projectDir should be null
let nsConfig: INsConfig = this.projectConfig.readConfig(projectDir);
// If no project found, projectDir should be null
const nsConfig: INsConfig = this.projectConfig.readConfig(projectDir);
let packageJsonData = null;

try {
Expand Down Expand Up @@ -219,16 +219,16 @@ export class ProjectData implements IProjectData {
}

private initializeProjectIdentifiers(config: INsConfig): Mobile.IProjectIdentifier {
let identifier: Mobile.IProjectIdentifier = {
ios: '',
android: ''
};
const identifier: Mobile.IProjectIdentifier = {
ios: '',
android: ''
};
if (config.ios) {
identifier.ios = config.ios.id || config.id;
}
if (config.android) {
identifier.android = config.android.id || config.id;
}
identifier.ios = config.ios.id || config.id;
}
if (config.android) {
identifier.android = config.android.id || config.id;
}

return identifier;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/services/plugins-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class PluginsService implements IPluginsService {
const pluginData = this.convertToPluginData(realNpmPackageJson, projectData.projectDir);

// Validate
const action = async (pluginDestinationPath: string, platform: string, platformData: IPlatformData): Promise<void> => {
const action = async (pluginDestinationPath: string, platform: constants.PlatformTypes, platformData: IPlatformData): Promise<void> => {
this.isPluginDataValidForPlatform(pluginData, platform, projectData);
};

Expand Down Expand Up @@ -443,13 +443,14 @@ This framework comes from ${dependencyName} plugin, which is installed multiple
}
}

private getInstalledFrameworkVersion(platform: string, projectData: IProjectData): string {
const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
const frameworkData = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
return frameworkData.version;
private getInstalledFrameworkVersion(platform: constants.PlatformTypes, projectData: IProjectData): string {
const runtimePackage = this.$projectDataService.getRuntimePackage(projectData.projectDir, platform);
// const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
// const frameworkData = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
return runtimePackage.version;
}

private isPluginDataValidForPlatform(pluginData: IPluginData, platform: string, projectData: IProjectData): boolean {
private isPluginDataValidForPlatform(pluginData: IPluginData, platform: constants.PlatformTypes, projectData: IProjectData): boolean {
let isValid = true;

const installedFrameworkVersion = this.getInstalledFrameworkVersion(platform, projectData);
Expand Down
93 changes: 53 additions & 40 deletions lib/services/project-config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,76 @@ import { CONFIG_FILE_NAME_JS, CONFIG_FILE_NAME_TS, CONFIG_FILE_NAME_DISPLAY } fr
import * as path from "path";
import * as _ from 'lodash';
import * as ts from 'typescript';
import { IFileSystem } from "../common/declarations";
import { IFileSystem, IProjectHelper } from "../common/declarations";
import { injector } from "../common/yok";
import { IProjectData, INsConfig, IProjectConfigService } from "../definitions/project";
import { INsConfig, IProjectConfigService } from "../definitions/project";
import { IInjector } from "../common/definitions/yok";

export class ProjectConfigService implements IProjectConfigService {
private _config: INsConfig;
private _config: INsConfig;

constructor(
private $fs: IFileSystem,
private $logger: ILogger,
private $injector: IInjector
private $logger: ILogger,
private $injector: IInjector
) {

}
}

get projectData(): IProjectData {
return this.$injector.resolve('projectData');
}
private requireFromString(src: string, filename: string): NodeModule {
// @ts-ignore
const m = new module.constructor();
m.paths = module.paths;
m._compile(src, filename);
return m.exports;
}

public readConfig(projectDir?: string): INsConfig {
const configJSFilePath = path.join(projectDir || this.projectData.projectDir, CONFIG_FILE_NAME_JS);
const configTSFilePath = path.join(projectDir || this.projectData.projectDir, CONFIG_FILE_NAME_TS);
get projectHelper(): IProjectHelper {
return this.$injector.resolve('projectHelper');
}

const hasTS = this.$fs.exists(configTSFilePath);
const hasJS = this.$fs.exists(configJSFilePath);
public readConfig(projectDir?: string): INsConfig {
const configJSFilePath = path.join(projectDir || this.projectHelper.projectDir, CONFIG_FILE_NAME_JS);
const configTSFilePath = path.join(projectDir || this.projectHelper.projectDir, CONFIG_FILE_NAME_TS);

if (!hasTS && !hasJS) {
throw new Error(`You do not appear to have a ${CONFIG_FILE_NAME_DISPLAY} file. Please install NativeScript 7+ "npm i -g nativescript". You can also try running "ns migrate" after you have the latest installed. Exiting for now.`);
}
const hasTS = this.$fs.exists(configTSFilePath);
const hasJS = this.$fs.exists(configJSFilePath);

if (hasTS && hasJS) {
this.$logger.warn(`You have both a ${CONFIG_FILE_NAME_JS} and ${CONFIG_FILE_NAME_TS} file. Defaulting to ${CONFIG_FILE_NAME_TS}.`);
}
if (!hasTS && !hasJS) {
throw new Error(`You do not appear to have a ${CONFIG_FILE_NAME_DISPLAY} file. Please install NativeScript 7+ "npm i -g nativescript". You can also try running "ns migrate" after you have the latest installed. Exiting for now.`);
}

if (hasTS && hasJS) {
this.$logger.warn(`You have both a ${CONFIG_FILE_NAME_JS} and ${CONFIG_FILE_NAME_TS} file. Defaulting to ${CONFIG_FILE_NAME_TS}.`);
}

let config: INsConfig;

let config: INsConfig;

if (hasTS) {
const rawSource = this.$fs.readText(configTSFilePath);
const transpiledSource = ts.transpileModule(rawSource, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
// console.log('transpiledSource.outputText:', transpiledSource.outputText)
config = eval(transpiledSource.outputText);
} else {
const rawSource = this.$fs.readText(configJSFilePath);
// console.log('rawSource:', rawSource)
config = eval(rawSource);
}
const rawSource = this.$fs.readText(configTSFilePath);
const transpiledSource = ts.transpileModule(rawSource, { compilerOptions: { module: ts.ModuleKind.CommonJS } });
const result: any = this.requireFromString(transpiledSource.outputText, configTSFilePath);
config = result['default'] ? result['default'] : result;
// console.log('transpiledSource.outputText:', transpiledSource.outputText)
// config = eval(transpiledSource.outputText);
} else {
const rawSource = this.$fs.readText(configJSFilePath);
// console.log('rawSource:', rawSource)
// config = eval(rawSource);
config = this.requireFromString(rawSource, configJSFilePath);
}

// console.log('config: ', config);

return config;
}

return config;
}

public getValue(key: string): any {
if (!this._config) {
this._config = this.readConfig();
}
return _.get(this._config, key);
}
public getValue(key: string): any {
if (!this._config) {
this._config = this.readConfig();
}
return _.get(this._config, key);
}
}

injector.register('projectConfigService', ProjectConfigService);
37 changes: 20 additions & 17 deletions lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ProjectDataService implements IProjectDataService {
private $staticConfig: IStaticConfig,
private $logger: ILogger,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
private $injector: IInjector) {
try {
// add the ProjectData of the default projectDir to the projectData cache
Expand All @@ -45,11 +45,11 @@ export class ProjectDataService implements IProjectDataService {
} catch (e) {
// the CLI is required as a lib from a non-project folder
}
}
get $pluginsService(): IPluginsService {
return this.$injector.resolve("pluginsService");
}
}

get $pluginsService(): IPluginsService {
return this.$injector.resolve("pluginsService");
}

public getNSValue(projectDir: string, propertyName: string): any {
return this.getValue(projectDir, this.getNativeScriptPropertyName(propertyName));
Expand Down Expand Up @@ -206,6 +206,9 @@ export class ProjectDataService implements IProjectDataService {
}

private updateNsConfigValue(projectDir: string, updateObject?: INsConfig, propertiesToRemove?: string[]): void {
// todo: figure out a way to update js/ts configs
// most likely needs an ast parser/writer
// should be delegated to the config service
const nsConfigPath = path.join(projectDir, constants.CONFIG_FILE_NAME_JS);
const currentNsConfig = this.getNsConfig(nsConfigPath);
let newNsConfig = currentNsConfig;
Expand Down Expand Up @@ -409,17 +412,17 @@ export class ProjectDataService implements IProjectDataService {
Object.assign(config, data);

return config;
}
public getRuntimePackage(projectDir: string, platform: constants.SupportedPlatform): IBasePluginData {
return this.$pluginsService.getDependenciesFromPackageJson(projectDir).devDependencies.find(d => {
if (platform === constants.PlatformTypes.ios) {
return [constants.SCOPED_IOS_RUNTIME_NAME, constants.TNS_IOS_RUNTIME_NAME].includes(d.name);
} else if (platform === constants.PlatformTypes.android) {
return [constants.SCOPED_ANDROID_RUNTIME_NAME, d.name === constants.TNS_ANDROID_RUNTIME_NAME].includes(d.name);
}
});
}
}

public getRuntimePackage(projectDir: string, platform: constants.SupportedPlatform): IBasePluginData {
return this.$pluginsService.getDependenciesFromPackageJson(projectDir).devDependencies.find(d => {
if (platform === constants.PlatformTypes.ios) {
return [constants.SCOPED_IOS_RUNTIME_NAME, constants.TNS_IOS_RUNTIME_NAME].includes(d.name);
} else if (platform === constants.PlatformTypes.android) {
return [constants.SCOPED_ANDROID_RUNTIME_NAME, d.name === constants.TNS_ANDROID_RUNTIME_NAME].includes(d.name);
}
});
}

@exported("projectDataService")
public getNsConfigDefaultContent(data?: Object): string {
Expand Down
3 changes: 1 addition & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aaee65e

Please sign in to comment.