Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 999232e

Browse files
authored
refactor: get app directory from CLI (#459)
* refactor: get app directory from CLI * test: refactor to use NativeScriptCommonModule instead of NativeScriptModule * fix: return right value from shouldSnapshot * fix: get project data safely * refactor: get projectDir directly from projectData * refactor: export APP_DIR constant from projectHelpers * fix: default value for args for safeGet function * test: set single appium configuration file for all demo apps * tests: fix appium capabilities * chore: fix typo by e2e script
1 parent efaafc6 commit 999232e

26 files changed

+345
-133
lines changed

Diff for: demo/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
node_modules
22
platforms
3+
testapp
4+
*-report.*
5+
36
hooks
47
report
58

Diff for: demo/AngularApp/app/ninjas/details/ninja.module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { NativeScriptCommonModule } from "nativescript-angular/common";
12
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
2-
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
33
import { NativeScriptRouterModule } from "nativescript-angular/router";
44

55
import { NinjaComponent } from "./ninja.component";
@@ -8,9 +8,9 @@ import { routes } from "./ninja.routes";
88
@NgModule({
99
schemas: [NO_ERRORS_SCHEMA],
1010
imports: [
11-
NativeScriptModule,
1211
NativeScriptRouterModule,
13-
NativeScriptRouterModule.forChild(routes)
12+
NativeScriptRouterModule.forChild(routes),
13+
NativeScriptCommonModule,
1414
],
1515
declarations: [NinjaComponent]
1616
})

Diff for: demo/AngularApp/app/ninjas/ninjas.module.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { NativeScriptCommonModule } from "nativescript-angular/common";
12
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
2-
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
33
import { NativeScriptRouterModule } from "nativescript-angular/router";
44

55
import { NinjasComponent } from "./ninjas.component";
@@ -8,9 +8,9 @@ import { routes } from "./ninjas.routes";
88
@NgModule({
99
schemas: [NO_ERRORS_SCHEMA],
1010
imports: [
11-
NativeScriptModule,
1211
NativeScriptRouterModule,
13-
NativeScriptRouterModule.forChild(routes)
12+
NativeScriptRouterModule.forChild(routes),
13+
NativeScriptCommonModule,
1414
],
1515
declarations: [NinjasComponent]
1616
})

Diff for: demo/AngularApp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
6969
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
7070
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
71-
"e2e": "tsc -p e2e && mocha --opts ./e2e/config/mocha.opts",
71+
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",
7272
"compile-tests": "tsc -p e2e --watch"
7373
}
7474
}

Diff for: demo/JavaScriptApp/e2e/config/mocha.opts

-4
This file was deleted.

Diff for: demo/JavaScriptApp/package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"css-loader": "~0.28.7",
2525
"extract-text-webpack-plugin": "~3.0.0",
2626
"lazy": "1.0.11",
27+
"mocha": "~3.5.0",
28+
"mocha-junit-reporter": "^1.13.0",
29+
"mocha-multi": "^0.11.0",
2730
"nativescript-dev-appium": "next",
2831
"nativescript-dev-sass": "^1.3.5",
2932
"nativescript-dev-webpack": "file:../..",
@@ -32,9 +35,9 @@
3235
"raw-loader": "~0.5.1",
3336
"resolve-url-loader": "~2.1.0",
3437
"sass-loader": "^6.0.6",
35-
"webpack": "~3.8.1",
36-
"webpack-bundle-analyzer": "^2.8.2",
37-
"webpack-sources": "~1.0.1"
38+
"webpack": "~3.10.0",
39+
"webpack-bundle-analyzer": "^2.9.1",
40+
"webpack-sources": "^1.1.0"
3841
},
3942
"scripts": {
4043
"ns-bundle": "ns-bundle",
@@ -44,6 +47,6 @@
4447
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
4548
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
4649
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
47-
"e2e": "mocha --opts ./e2e/config/mocha.opts"
50+
"e2e": "mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json"
4851
}
49-
}
52+
}

Diff for: demo/TypeScriptApp/e2e/config/mocha.opts

-4
This file was deleted.

Diff for: demo/TypeScriptApp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
5454
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
5555
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
56-
"e2e": "tsc -p e2e && mocha --opts ./e2e/config/mocha.opts",
56+
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",
5757
"compile-tests": "tsc -p e2e --watch"
5858
}
5959
}

Diff for: demo/AngularApp/e2e/config/appium.capabilities.json renamed to demo/config/appium.capabilities.json

+23-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@
7171
"fullReset": false,
7272
"app": ""
7373
},
74+
"android27": {
75+
"platformName": "Android",
76+
"platformVersion": "27",
77+
"deviceName": "Emulator-Api27-Google",
78+
"avd": "Emulator-Api27-Google",
79+
"lt": 60000,
80+
"appActivity": "com.tns.NativeScriptActivity",
81+
"newCommandTimeout": 720,
82+
"noReset": true,
83+
"fullReset": false,
84+
"app": ""
85+
},
7486
"sim.iPhone7.iOS100": {
7587
"platformName": "iOS",
7688
"platformVersion": "10.0",
@@ -79,6 +91,14 @@
7991
"fullReset": false,
8092
"app": ""
8193
},
94+
"sim.iPhone7.iOS110": {
95+
"platformName": "iOS",
96+
"platformVersion": "11.2",
97+
"deviceName": "iPhone 7 110",
98+
"noReset": true,
99+
"fullReset": false,
100+
"app": ""
101+
},
82102
"sim.iPhone8.iOS110": {
83103
"platformName": "iOS",
84104
"platformVersion": "11.2",
@@ -90,17 +110,17 @@
90110
"sim.iPhoneX.iOS110": {
91111
"platformName": "iOS",
92112
"platformVersion": "11.2",
93-
"deviceName": "iPhone X",
113+
"deviceName": "iPhone X 110",
94114
"noReset": true,
95115
"fullReset": false,
96116
"app": ""
97117
},
98118
"sim.iPhoneX.iOS111": {
99119
"platformName": "iOS",
100120
"platformVersion": "11.1",
101-
"deviceName": "iPhone X",
121+
"deviceName": "iPhone X 111",
102122
"noReset": true,
103123
"fullReset": false,
104124
"app": ""
105125
}
106-
}
126+
}
File renamed without changes.

Diff for: index.js

+40-19
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,58 @@
11
const path = require("path");
22
const { existsSync } = require("fs");
33

4-
const { getPackageJson, getProjectDir, isAngular, resolveAndroidAppPath } = require("./projectHelpers");
4+
const {
5+
APP_DIR,
6+
getPackageJson,
7+
getProjectDir,
8+
isAngular,
9+
isAndroid,
10+
isIos,
11+
resolveAndroidAppPath,
12+
} = require("./projectHelpers");
513

614
const PROJECT_DIR = getProjectDir();
7-
const APP_DIR = path.join(PROJECT_DIR, "app");
15+
const APP_PATH = path.join(PROJECT_DIR, APP_DIR);
816

917
Object.assign(exports, require('./plugins'));
1018

1119
if (isAngular({ projectDir: PROJECT_DIR })) {
1220
Object.assign(exports, require('./plugins/angular'));
1321
}
1422

15-
exports.getEntryModule = function () {
16-
const maybePackageJsonEntry = getPackageJsonEntry();
17-
if (!maybePackageJsonEntry) {
18-
throw new Error("app/package.json must contain a `main` attribute.");
23+
exports.getAotEntryModule = function (appDirectory = APP_PATH) {
24+
const entry = getPackageJsonEntry(appDirectory);
25+
const aotEntry = `${entry}.aot.ts`;
26+
27+
const aotEntryPath = path.resolve(appDirectory, aotEntry);
28+
if (!existsSync(aotEntryPath)) {
29+
throw new Error(`For ahead-of-time compilation you need to have an entry module ` +
30+
`at ${aotEntryPath} that bootstraps the app with a static platform instead of dynamic one!`)
1931
}
2032

21-
const maybeAotEntry = getAotEntry(maybePackageJsonEntry);
22-
return maybeAotEntry || maybePackageJsonEntry;
33+
return aotEntry;
34+
}
35+
36+
exports.getEntryModule = function (appDirectory = APP_PATH) {
37+
const entry = getPackageJsonEntry(appDirectory);
38+
39+
const tsEntryPath = path.resolve(appDirectory, `${entry}.ts`);
40+
const jsEntryPath = path.resolve(appDirectory, `${entry}.js`);
41+
if (!existsSync(tsEntryPath) && !existsSync(jsEntryPath)) {
42+
throw new Error(`The entry module ${entry} specified in ` +
43+
`${appDirectory}/package.json doesn't exist!`)
44+
}
45+
46+
return entry;
2347
};
2448

2549
exports.getAppPath = platform => {
26-
if (/ios/i.test(platform)) {
50+
if (isIos(platform)) {
2751
const appName = path.basename(PROJECT_DIR);
2852
const sanitizedName = sanitize(appName);
2953

3054
return `platforms/ios/${sanitizedName}/app`;
31-
} else if (/android/i.test(platform)) {
55+
} else if (isAndroid(platform)) {
3256
return resolveAndroidAppPath(PROJECT_DIR);
3357
} else {
3458
throw new Error(`Invalid platform: ${platform}`);
@@ -40,16 +64,13 @@ const sanitize = name => name
4064
.filter(char => /[a-zA-Z0-9]/.test(char))
4165
.join("");
4266

43-
function getPackageJsonEntry() {
44-
const packageJsonSource = getPackageJson(APP_DIR);
67+
function getPackageJsonEntry(appDirectory) {
68+
const packageJsonSource = getPackageJson(appDirectory);
4569
const entry = packageJsonSource.main;
4670

47-
return entry ? entry.replace(/\.js$/i, "") : null;
48-
}
49-
50-
function getAotEntry(entry) {
51-
const aotEntry = `${entry}.aot.ts`;
52-
const aotEntryPath = path.join(APP_DIR, aotEntry);
71+
if (!entry) {
72+
throw new Error(`${appDirectory}/package.json must contain a 'main' attribute!`);
73+
}
5374

54-
return existsSync(aotEntryPath) ? aotEntry : null;
75+
return entry.replace(/\.js$/i, "");
5576
}

Diff for: installer.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
const path = require("path");
2-
const fs = require("fs");
3-
41
const helpers = require("./projectHelpers");
52
const projectFilesManager = require("./projectFilesManager");
63
const dependencyManager = require("./dependencyManager");
74

8-
const PROJECT_DIR = helpers.getProjectDir();
9-
const APP_DIR = path.resolve(PROJECT_DIR, "app");
10-
115
function install() {
12-
const packageJson = helpers.getPackageJson(PROJECT_DIR);
6+
const projectDir = helpers.getProjectDir();
7+
const appPath = helpers.getAppPath();
8+
const packageJson = helpers.getPackageJson(projectDir);
139

14-
projectFilesManager.addProjectFiles(PROJECT_DIR, APP_DIR);
10+
projectFilesManager.addProjectFiles(projectDir, appPath);
1511

1612
const postinstallOptions = dependencyManager.addProjectDeps(packageJson);
1713
packageJson.devDependencies = postinstallOptions.deps;
1814

19-
helpers.writePackageJson(packageJson, PROJECT_DIR);
15+
helpers.writePackageJson(packageJson, projectDir);
2016

2117
dependencyManager.showHelperMessages(postinstallOptions);
2218
}
2319

2420
function uninstall() {
25-
projectFilesManager.removeProjectFiles(PROJECT_DIR, APP_DIR);
21+
const projectDir = helpers.getProjectDir();
22+
const appPath = helpers.getAppPath();
23+
projectFilesManager.removeProjectFiles(projectDir, appPath);
24+
2625
console.log("NativeScript Webpack removed!");
2726
}
2827

Diff for: lib/after-prepare.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const snapshotGenerator = require("../snapshot/android/project-snapshot-generator");
2-
const utils = require("./utils");
1+
const { installSnapshotArtefacts } = require("../snapshot/android/project-snapshot-generator");
2+
const { shouldSnapshot } = require("./utils");
33

4-
module.exports = function ($mobileHelper, $projectData, hookArgs) {
4+
module.exports = function ($projectData, hookArgs) {
55
const env = hookArgs.env || {};
66
const shouldSnapshotOptions = {
77
platform: hookArgs.platform,
88
bundle: hookArgs.appFilesUpdaterOptions.bundle,
99
release: hookArgs.appFilesUpdaterOptions.release
1010
};
1111

12-
if (env.snapshot && utils.shouldSnapshot($mobileHelper, shouldSnapshotOptions)) {
13-
snapshotGenerator.installSnapshotArtefacts($projectData.projectDir);
12+
if (env.snapshot && shouldSnapshot(shouldSnapshotOptions)) {
13+
installSnapshotArtefacts($projectData.projectDir);
1414
}
1515
}

Diff for: lib/before-cleanApp.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const ProjectSnapshotGenerator = require("../snapshot/android/project-snapshot-generator");
2-
module.exports = function ($mobileHelper, hookArgs) {
3-
if ($mobileHelper.isAndroidPlatform(hookArgs.platformInfo.platform)) {
4-
ProjectSnapshotGenerator.cleanSnapshotArtefacts(hookArgs.platformInfo.projectData.projectDir);
5-
}
1+
const { cleanSnapshotArtefacts } = require("../snapshot/android/project-snapshot-generator");
2+
const { isAndroid } = require("../projectHelpers");
3+
4+
module.exports = function (hookArgs) {
5+
if (isAndroid(hookArgs.platformInfo.platform)) {
6+
cleanSnapshotArtefacts(hookArgs.platformInfo.projectData.projectDir);
7+
}
68
}

Diff for: lib/before-prepareJS.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { runWebpackCompiler } = require("./compiler");
22

3-
module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
3+
module.exports = function ($projectData, $logger, hookArgs) {
44
const env = hookArgs.config.env || {};
55
const platform = hookArgs.config.platform;
66
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
@@ -10,6 +10,6 @@ module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
1010
bundle: appFilesUpdaterOptions.bundle,
1111
release: appFilesUpdaterOptions.release,
1212
};
13-
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $mobileHelper, $projectData, $logger, hookArgs);
13+
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $projectData, $logger, hookArgs);
1414
return result;
1515
}

Diff for: lib/before-watch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { runWebpackCompiler } = require("./compiler");
22

3-
module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
3+
module.exports = function ($projectData, $logger, hookArgs) {
44
if (hookArgs.config) {
55
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
66
if (appFilesUpdaterOptions.bundle) {
@@ -15,7 +15,7 @@ module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
1515
watch: true
1616
};
1717

18-
return runWebpackCompiler(config, $mobileHelper, $projectData, $logger, hookArgs);
18+
return runWebpackCompiler(config, $projectData, $logger, hookArgs);
1919
}));
2020
}
2121
}

0 commit comments

Comments
 (0)