Skip to content

Commit aaee65e

Browse files
committed
fix: plugin service tests and config reading
1 parent 95b79eb commit aaee65e

10 files changed

+258
-217
lines changed

lib/common/project-helper.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as path from "path";
22
import * as _ from 'lodash';
3-
import { IProjectHelper, IFileSystem, IErrors } from "./declarations";
3+
import { IProjectHelper, IFileSystem } from "./declarations";
44
import { IOptions } from "../declarations";
55
import { injector } from "./yok";
66

77
export class ProjectHelper implements IProjectHelper {
88
constructor(private $logger: ILogger,
99
private $fs: IFileSystem,
1010
private $staticConfig: Config.IStaticConfig,
11-
private $errors: IErrors,
11+
// private $errors: IErrors,
1212
private $options: IOptions) { }
1313

1414
private cachedProjectDir = "";
@@ -60,15 +60,17 @@ export class ProjectHelper implements IProjectHelper {
6060
}
6161

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

7375
return true;
7476
}

lib/project-data.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ export class ProjectData implements IProjectData {
8383
private $projectHelper: IProjectHelper,
8484
private $staticConfig: IStaticConfig,
8585
private $options: IOptions,
86-
private $logger: ILogger,
87-
private $injector: IInjector,
86+
private $logger: ILogger,
87+
private $injector: IInjector,
8888
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
89-
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
90-
91-
get projectConfig(): IProjectConfigService {
92-
return this.$injector.resolve('projectConfigService');
93-
}
89+
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }
90+
91+
get projectConfig(): IProjectConfigService {
92+
return this.$injector.resolve('projectConfigService');
93+
}
9494

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

121121
try {
@@ -219,16 +219,16 @@ export class ProjectData implements IProjectData {
219219
}
220220

221221
private initializeProjectIdentifiers(config: INsConfig): Mobile.IProjectIdentifier {
222-
let identifier: Mobile.IProjectIdentifier = {
223-
ios: '',
224-
android: ''
225-
};
222+
const identifier: Mobile.IProjectIdentifier = {
223+
ios: '',
224+
android: ''
225+
};
226226
if (config.ios) {
227-
identifier.ios = config.ios.id || config.id;
228-
}
229-
if (config.android) {
230-
identifier.android = config.android.id || config.id;
231-
}
227+
identifier.ios = config.ios.id || config.id;
228+
}
229+
if (config.android) {
230+
identifier.android = config.android.id || config.id;
231+
}
232232

233233
return identifier;
234234
}

lib/services/plugins-service.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class PluginsService implements IPluginsService {
6262
const pluginData = this.convertToPluginData(realNpmPackageJson, projectData.projectDir);
6363

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

@@ -443,13 +443,14 @@ This framework comes from ${dependencyName} plugin, which is installed multiple
443443
}
444444
}
445445

446-
private getInstalledFrameworkVersion(platform: string, projectData: IProjectData): string {
447-
const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
448-
const frameworkData = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
449-
return frameworkData.version;
446+
private getInstalledFrameworkVersion(platform: constants.PlatformTypes, projectData: IProjectData): string {
447+
const runtimePackage = this.$projectDataService.getRuntimePackage(projectData.projectDir, platform);
448+
// const platformData = this.$platformsDataService.getPlatformData(platform, projectData);
449+
// const frameworkData = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
450+
return runtimePackage.version;
450451
}
451452

452-
private isPluginDataValidForPlatform(pluginData: IPluginData, platform: string, projectData: IProjectData): boolean {
453+
private isPluginDataValidForPlatform(pluginData: IPluginData, platform: constants.PlatformTypes, projectData: IProjectData): boolean {
453454
let isValid = true;
454455

455456
const installedFrameworkVersion = this.getInstalledFrameworkVersion(platform, projectData);

lib/services/project-config-service.ts

+53-40
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,76 @@ import { CONFIG_FILE_NAME_JS, CONFIG_FILE_NAME_TS, CONFIG_FILE_NAME_DISPLAY } fr
22
import * as path from "path";
33
import * as _ from 'lodash';
44
import * as ts from 'typescript';
5-
import { IFileSystem } from "../common/declarations";
5+
import { IFileSystem, IProjectHelper } from "../common/declarations";
66
import { injector } from "../common/yok";
7-
import { IProjectData, INsConfig, IProjectConfigService } from "../definitions/project";
7+
import { INsConfig, IProjectConfigService } from "../definitions/project";
88
import { IInjector } from "../common/definitions/yok";
99

1010
export class ProjectConfigService implements IProjectConfigService {
11-
private _config: INsConfig;
11+
private _config: INsConfig;
1212

1313
constructor(
1414
private $fs: IFileSystem,
15-
private $logger: ILogger,
16-
private $injector: IInjector
15+
private $logger: ILogger,
16+
private $injector: IInjector
1717
) {
1818

19-
}
19+
}
2020

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

25-
public readConfig(projectDir?: string): INsConfig {
26-
const configJSFilePath = path.join(projectDir || this.projectData.projectDir, CONFIG_FILE_NAME_JS);
27-
const configTSFilePath = path.join(projectDir || this.projectData.projectDir, CONFIG_FILE_NAME_TS);
29+
get projectHelper(): IProjectHelper {
30+
return this.$injector.resolve('projectHelper');
31+
}
2832

29-
const hasTS = this.$fs.exists(configTSFilePath);
30-
const hasJS = this.$fs.exists(configJSFilePath);
33+
public readConfig(projectDir?: string): INsConfig {
34+
const configJSFilePath = path.join(projectDir || this.projectHelper.projectDir, CONFIG_FILE_NAME_JS);
35+
const configTSFilePath = path.join(projectDir || this.projectHelper.projectDir, CONFIG_FILE_NAME_TS);
3136

32-
if (!hasTS && !hasJS) {
33-
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.`);
34-
}
37+
const hasTS = this.$fs.exists(configTSFilePath);
38+
const hasJS = this.$fs.exists(configJSFilePath);
3539

36-
if (hasTS && hasJS) {
37-
this.$logger.warn(`You have both a ${CONFIG_FILE_NAME_JS} and ${CONFIG_FILE_NAME_TS} file. Defaulting to ${CONFIG_FILE_NAME_TS}.`);
38-
}
40+
if (!hasTS && !hasJS) {
41+
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.`);
42+
}
43+
44+
if (hasTS && hasJS) {
45+
this.$logger.warn(`You have both a ${CONFIG_FILE_NAME_JS} and ${CONFIG_FILE_NAME_TS} file. Defaulting to ${CONFIG_FILE_NAME_TS}.`);
46+
}
47+
48+
let config: INsConfig;
3949

40-
let config: INsConfig;
41-
4250
if (hasTS) {
43-
const rawSource = this.$fs.readText(configTSFilePath);
44-
const transpiledSource = ts.transpileModule(rawSource, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
45-
// console.log('transpiledSource.outputText:', transpiledSource.outputText)
46-
config = eval(transpiledSource.outputText);
47-
} else {
48-
const rawSource = this.$fs.readText(configJSFilePath);
49-
// console.log('rawSource:', rawSource)
50-
config = eval(rawSource);
51-
}
51+
const rawSource = this.$fs.readText(configTSFilePath);
52+
const transpiledSource = ts.transpileModule(rawSource, { compilerOptions: { module: ts.ModuleKind.CommonJS } });
53+
const result: any = this.requireFromString(transpiledSource.outputText, configTSFilePath);
54+
config = result['default'] ? result['default'] : result;
55+
// console.log('transpiledSource.outputText:', transpiledSource.outputText)
56+
// config = eval(transpiledSource.outputText);
57+
} else {
58+
const rawSource = this.$fs.readText(configJSFilePath);
59+
// console.log('rawSource:', rawSource)
60+
// config = eval(rawSource);
61+
config = this.requireFromString(rawSource, configJSFilePath);
62+
}
63+
64+
// console.log('config: ', config);
65+
66+
return config;
67+
}
5268

53-
return config;
54-
}
55-
56-
public getValue(key: string): any {
57-
if (!this._config) {
58-
this._config = this.readConfig();
59-
}
60-
return _.get(this._config, key);
61-
}
69+
public getValue(key: string): any {
70+
if (!this._config) {
71+
this._config = this.readConfig();
72+
}
73+
return _.get(this._config, key);
74+
}
6275
}
6376

6477
injector.register('projectConfigService', ProjectConfigService);

lib/services/project-data-service.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class ProjectDataService implements IProjectDataService {
3434
private $staticConfig: IStaticConfig,
3535
private $logger: ILogger,
3636
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
37-
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
37+
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
3838
private $injector: IInjector) {
3939
try {
4040
// add the ProjectData of the default projectDir to the projectData cache
@@ -45,11 +45,11 @@ export class ProjectDataService implements IProjectDataService {
4545
} catch (e) {
4646
// the CLI is required as a lib from a non-project folder
4747
}
48-
}
49-
50-
get $pluginsService(): IPluginsService {
51-
return this.$injector.resolve("pluginsService");
52-
}
48+
}
49+
50+
get $pluginsService(): IPluginsService {
51+
return this.$injector.resolve("pluginsService");
52+
}
5353

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

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

411414
return config;
412-
}
413-
414-
public getRuntimePackage(projectDir: string, platform: constants.SupportedPlatform): IBasePluginData {
415-
return this.$pluginsService.getDependenciesFromPackageJson(projectDir).devDependencies.find(d => {
416-
if (platform === constants.PlatformTypes.ios) {
417-
return [constants.SCOPED_IOS_RUNTIME_NAME, constants.TNS_IOS_RUNTIME_NAME].includes(d.name);
418-
} else if (platform === constants.PlatformTypes.android) {
419-
return [constants.SCOPED_ANDROID_RUNTIME_NAME, d.name === constants.TNS_ANDROID_RUNTIME_NAME].includes(d.name);
420-
}
421-
});
422-
}
415+
}
416+
417+
public getRuntimePackage(projectDir: string, platform: constants.SupportedPlatform): IBasePluginData {
418+
return this.$pluginsService.getDependenciesFromPackageJson(projectDir).devDependencies.find(d => {
419+
if (platform === constants.PlatformTypes.ios) {
420+
return [constants.SCOPED_IOS_RUNTIME_NAME, constants.TNS_IOS_RUNTIME_NAME].includes(d.name);
421+
} else if (platform === constants.PlatformTypes.android) {
422+
return [constants.SCOPED_ANDROID_RUNTIME_NAME, d.name === constants.TNS_ANDROID_RUNTIME_NAME].includes(d.name);
423+
}
424+
});
425+
}
423426

424427
@exported("projectDataService")
425428
public getNsConfigDefaultContent(data?: Object): string {

npm-shrinkwrap.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)