Skip to content

Commit 4c1556c

Browse files
committed
fix: better heuristics for js plugins and ones without a nativescript section in package.json
1 parent 94c0d02 commit 4c1556c

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

lib/project.service.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export namespace ProjectService {
2424
export async function prepareProject(plugin: MarketplaceService.PluginModel) {
2525
try {
2626
await _copyTestProject(testProject);
27-
await _installPlugin(plugin.name, testProject, _isDev(plugin.name));
27+
await _installPlugin(plugin, testProject);
2828
} catch (errExec) {
2929
Logger.error(JSON.stringify(errExec));
3030
}
@@ -53,20 +53,20 @@ export namespace ProjectService {
5353

5454
async function testPlugin(plugin: MarketplaceService.PluginModel, options: string) {
5555
const result = { android: false, ios: false };
56-
let hasPlatform = false;
56+
let skipBuild = false;
5757
try {
58-
if (plugin.badges.androidVersion) {
58+
skipBuild = !plugin.badges.androidVersion && plugin.badges.iosVersion;
59+
if (!skipBuild) {
5960
result.android = !!(await _buildProject(testProject, 'android', options));
60-
hasPlatform = true;
61+
} else {
62+
Logger.error('Skipping android build! Plugin only has ios support.');
6163
}
6264

63-
if (plugin.badges.iosVersion) {
65+
skipBuild = !plugin.badges.iosVersion && plugin.badges.androidVersion;
66+
if (!skipBuild) {
6467
result.ios = !!(await _buildProject(testProject, 'ios', options));
65-
hasPlatform = true;
66-
}
67-
68-
if (!hasPlatform) {
69-
Logger.error('plugin has no platform');
68+
} else {
69+
Logger.error('Skipping ios build! Plugin only has android support.');
7070
}
7171
} catch (errExec) {
7272
Logger.error(JSON.stringify(errExec));
@@ -81,24 +81,35 @@ export namespace ProjectService {
8181
return result;
8282
}
8383

84-
function _isDev(name: string): boolean {
85-
return name && name.indexOf('-dev-') !== -1;
86-
}
87-
88-
async function _installPlugin(name: string, projectName: string, isDev: boolean) {
84+
async function _installPlugin(plugin: MarketplaceService.PluginModel, projectName: string) {
85+
const name = plugin.name;
86+
const isDev = name && name.indexOf('-dev-') !== -1;
8987
Logger.debug(`installing ${name} plugin ...`);
9088
const cwd = path.join(testDirectory, projectName);
91-
const command = isDev ? `npm i ${name} --save-dev` : `tns plugin add ${name}`;
89+
let command = `tns plugin add ${name}`;
90+
if (isDev) {
91+
// dev plugin (e.g. nativescript-dev-typescript)
92+
command = `npm i ${name} --save-dev`;
93+
} else if (!plugin.badges.androidVersion && !plugin.badges.iosVersion) {
94+
// regular (not nativescript specific) plugin
95+
command = `npm i ${name} --save`;
96+
}
97+
9298
await execPromise(cwd, command);
9399
if (!isDev) {
94-
_modifyProject(cwd, name);
100+
_modifyProject(cwd, plugin);
95101
}
96102
}
97103

98-
function _modifyProject(appRoot: string, name: string) {
104+
function _modifyProject(appRoot: string, plugin: MarketplaceService.PluginModel) {
105+
const name = plugin.name;
99106
const mainTsPath = path.join(appRoot, 'app', 'main-view-model.ts');
100107
let mainTs = readFileSync(mainTsPath, 'utf8');
101-
mainTs = `import * as testPlugin from '${name}';\n` + mainTs;
108+
if (plugin.badges.typings) {
109+
mainTs = `import * as testPlugin from '${name}';\n` + mainTs;
110+
} else {
111+
mainTs = `const testPlugin = require('${name}');\n` + mainTs;
112+
}
102113
mainTs = mainTs.replace('public onTap() {', 'public onTap() {\nfor (let testExport in testPlugin) {console.log(testExport);}\n');
103114
if (mainTs.indexOf('testExport') === -1) {
104115
throw new Error('Template content has changed! Plugin test script needs to be updated.');

0 commit comments

Comments
 (0)