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

refactor: get app directory from CLI #459

Merged
merged 10 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
node_modules
platforms
testapp
*-report.*

hooks
report

Expand Down
6 changes: 3 additions & 3 deletions demo/AngularApp/app/ninjas/details/ninja.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptRouterModule } from "nativescript-angular/router";

import { NinjaComponent } from "./ninja.component";
Expand All @@ -8,9 +8,9 @@ import { routes } from "./ninja.routes";
@NgModule({
schemas: [NO_ERRORS_SCHEMA],
imports: [
NativeScriptModule,
NativeScriptRouterModule,
NativeScriptRouterModule.forChild(routes)
NativeScriptRouterModule.forChild(routes),
NativeScriptCommonModule,
],
declarations: [NinjaComponent]
})
Expand Down
6 changes: 3 additions & 3 deletions demo/AngularApp/app/ninjas/ninjas.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeScriptCommonModule } from "nativescript-angular/common";
import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptRouterModule } from "nativescript-angular/router";

import { NinjasComponent } from "./ninjas.component";
Expand All @@ -8,9 +8,9 @@ import { routes } from "./ninjas.routes";
@NgModule({
schemas: [NO_ERRORS_SCHEMA],
imports: [
NativeScriptModule,
NativeScriptRouterModule,
NativeScriptRouterModule.forChild(routes)
NativeScriptRouterModule.forChild(routes),
NativeScriptCommonModule,
],
declarations: [NinjasComponent]
})
Expand Down
2 changes: 1 addition & 1 deletion demo/AngularApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
"e2e": "tsc -p e2e && mocha --opts ./e2e/config/mocha.opts",
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",
"compile-tests": "tsc -p e2e --watch"
}
}
4 changes: 0 additions & 4 deletions demo/JavaScriptApp/e2e/config/mocha.opts

This file was deleted.

13 changes: 8 additions & 5 deletions demo/JavaScriptApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"css-loader": "~0.28.7",
"extract-text-webpack-plugin": "~3.0.0",
"lazy": "1.0.11",
"mocha": "~3.5.0",
"mocha-junit-reporter": "^1.13.0",
"mocha-multi": "^0.11.0",
"nativescript-dev-appium": "next",
"nativescript-dev-sass": "^1.3.5",
"nativescript-dev-webpack": "file:../..",
Expand All @@ -32,9 +35,9 @@
"raw-loader": "~0.5.1",
"resolve-url-loader": "~2.1.0",
"sass-loader": "^6.0.6",
"webpack": "~3.8.1",
"webpack-bundle-analyzer": "^2.8.2",
"webpack-sources": "~1.0.1"
"webpack": "~3.10.0",
"webpack-bundle-analyzer": "^2.9.1",
"webpack-sources": "^1.1.0"
},
"scripts": {
"ns-bundle": "ns-bundle",
Expand All @@ -44,6 +47,6 @@
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
"e2e": "mocha --opts ./e2e/config/mocha.opts"
"e2e": "mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json"
}
}
}
4 changes: 0 additions & 4 deletions demo/TypeScriptApp/e2e/config/mocha.opts

This file was deleted.

2 changes: 1 addition & 1 deletion demo/TypeScriptApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"build-ios-bundle": "npm run ns-bundle --ios --build-app",
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
"generate-android-snapshot": "generate-android-snapshot --targetArchs arm,arm64,ia32 --install",
"e2e": "tsc -p e2e && mocha --opts ./e2e/config/mocha.opts",
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",
"compile-tests": "tsc -p e2e --watch"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@
"fullReset": false,
"app": ""
},
"android27": {
"platformName": "Android",
"platformVersion": "27",
"deviceName": "Emulator-Api27-Google",
"avd": "Emulator-Api27-Google",
"lt": 60000,
"appActivity": "com.tns.NativeScriptActivity",
"newCommandTimeout": 720,
"noReset": true,
"fullReset": false,
"app": ""
},
"sim.iPhone7.iOS100": {
"platformName": "iOS",
"platformVersion": "10.0",
Expand All @@ -79,6 +91,14 @@
"fullReset": false,
"app": ""
},
"sim.iPhone7.iOS110": {
"platformName": "iOS",
"platformVersion": "11.2",
"deviceName": "iPhone 7 110",
"noReset": true,
"fullReset": false,
"app": ""
},
"sim.iPhone8.iOS110": {
"platformName": "iOS",
"platformVersion": "11.2",
Expand All @@ -90,17 +110,17 @@
"sim.iPhoneX.iOS110": {
"platformName": "iOS",
"platformVersion": "11.2",
"deviceName": "iPhone X",
"deviceName": "iPhone X 110",
"noReset": true,
"fullReset": false,
"app": ""
},
"sim.iPhoneX.iOS111": {
"platformName": "iOS",
"platformVersion": "11.1",
"deviceName": "iPhone X",
"deviceName": "iPhone X 111",
"noReset": true,
"fullReset": false,
"app": ""
}
}
}
59 changes: 40 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
const path = require("path");
const { existsSync } = require("fs");

const { getPackageJson, getProjectDir, isAngular, resolveAndroidAppPath } = require("./projectHelpers");
const {
APP_DIR,
getPackageJson,
getProjectDir,
isAngular,
isAndroid,
isIos,
resolveAndroidAppPath,
} = require("./projectHelpers");

const PROJECT_DIR = getProjectDir();
const APP_DIR = path.join(PROJECT_DIR, "app");
const APP_PATH = path.join(PROJECT_DIR, APP_DIR);

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

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

exports.getEntryModule = function () {
const maybePackageJsonEntry = getPackageJsonEntry();
if (!maybePackageJsonEntry) {
throw new Error("app/package.json must contain a `main` attribute.");
exports.getAotEntryModule = function (appDirectory = APP_PATH) {
const entry = getPackageJsonEntry(appDirectory);
const aotEntry = `${entry}.aot.ts`;

const aotEntryPath = path.resolve(appDirectory, aotEntry);
if (!existsSync(aotEntryPath)) {
throw new Error(`For ahead-of-time compilation you need to have an entry module ` +
`at ${aotEntryPath} that bootstraps the app with a static platform instead of dynamic one!`)
}

const maybeAotEntry = getAotEntry(maybePackageJsonEntry);
return maybeAotEntry || maybePackageJsonEntry;
return aotEntry;
}

exports.getEntryModule = function (appDirectory = APP_PATH) {
const entry = getPackageJsonEntry(appDirectory);

const tsEntryPath = path.resolve(appDirectory, `${entry}.ts`);
const jsEntryPath = path.resolve(appDirectory, `${entry}.js`);
if (!existsSync(tsEntryPath) && !existsSync(jsEntryPath)) {
throw new Error(`The entry module ${entry} specified in ` +
`${appDirectory}/package.json doesn't exist!`)
}

return entry;
};

exports.getAppPath = platform => {
if (/ios/i.test(platform)) {
if (isIos(platform)) {
const appName = path.basename(PROJECT_DIR);
const sanitizedName = sanitize(appName);

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

function getPackageJsonEntry() {
const packageJsonSource = getPackageJson(APP_DIR);
function getPackageJsonEntry(appDirectory) {
const packageJsonSource = getPackageJson(appDirectory);
const entry = packageJsonSource.main;

return entry ? entry.replace(/\.js$/i, "") : null;
}

function getAotEntry(entry) {
const aotEntry = `${entry}.aot.ts`;
const aotEntryPath = path.join(APP_DIR, aotEntry);
if (!entry) {
throw new Error(`${appDirectory}/package.json must contain a 'main' attribute!`);
}

return existsSync(aotEntryPath) ? aotEntry : null;
return entry.replace(/\.js$/i, "");
}
19 changes: 9 additions & 10 deletions installer.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
const path = require("path");
const fs = require("fs");

const helpers = require("./projectHelpers");
const projectFilesManager = require("./projectFilesManager");
const dependencyManager = require("./dependencyManager");

const PROJECT_DIR = helpers.getProjectDir();
const APP_DIR = path.resolve(PROJECT_DIR, "app");

function install() {
const packageJson = helpers.getPackageJson(PROJECT_DIR);
const projectDir = helpers.getProjectDir();
const appPath = helpers.getAppPath();
const packageJson = helpers.getPackageJson(projectDir);

projectFilesManager.addProjectFiles(PROJECT_DIR, APP_DIR);
projectFilesManager.addProjectFiles(projectDir, appPath);

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

helpers.writePackageJson(packageJson, PROJECT_DIR);
helpers.writePackageJson(packageJson, projectDir);

dependencyManager.showHelperMessages(postinstallOptions);
}

function uninstall() {
projectFilesManager.removeProjectFiles(PROJECT_DIR, APP_DIR);
const projectDir = helpers.getProjectDir();
const appPath = helpers.getAppPath();
projectFilesManager.removeProjectFiles(projectDir, appPath);

console.log("NativeScript Webpack removed!");
}

Expand Down
10 changes: 5 additions & 5 deletions lib/after-prepare.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const snapshotGenerator = require("../snapshot/android/project-snapshot-generator");
const utils = require("./utils");
const { installSnapshotArtefacts } = require("../snapshot/android/project-snapshot-generator");
const { shouldSnapshot } = require("./utils");

module.exports = function ($mobileHelper, $projectData, hookArgs) {
module.exports = function ($projectData, hookArgs) {
const env = hookArgs.env || {};
const shouldSnapshotOptions = {
platform: hookArgs.platform,
bundle: hookArgs.appFilesUpdaterOptions.bundle,
release: hookArgs.appFilesUpdaterOptions.release
};

if (env.snapshot && utils.shouldSnapshot($mobileHelper, shouldSnapshotOptions)) {
snapshotGenerator.installSnapshotArtefacts($projectData.projectDir);
if (env.snapshot && shouldSnapshot(shouldSnapshotOptions)) {
installSnapshotArtefacts($projectData.projectDir);
}
}
12 changes: 7 additions & 5 deletions lib/before-cleanApp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const ProjectSnapshotGenerator = require("../snapshot/android/project-snapshot-generator");
module.exports = function ($mobileHelper, hookArgs) {
if ($mobileHelper.isAndroidPlatform(hookArgs.platformInfo.platform)) {
ProjectSnapshotGenerator.cleanSnapshotArtefacts(hookArgs.platformInfo.projectData.projectDir);
}
const { cleanSnapshotArtefacts } = require("../snapshot/android/project-snapshot-generator");
const { isAndroid } = require("../projectHelpers");

module.exports = function (hookArgs) {
if (isAndroid(hookArgs.platformInfo.platform)) {
cleanSnapshotArtefacts(hookArgs.platformInfo.projectData.projectDir);
}
}
4 changes: 2 additions & 2 deletions lib/before-prepareJS.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { runWebpackCompiler } = require("./compiler");

module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
module.exports = function ($projectData, $logger, hookArgs) {
const env = hookArgs.config.env || {};
const platform = hookArgs.config.platform;
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
Expand All @@ -10,6 +10,6 @@ module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
bundle: appFilesUpdaterOptions.bundle,
release: appFilesUpdaterOptions.release,
};
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $mobileHelper, $projectData, $logger, hookArgs);
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $projectData, $logger, hookArgs);
return result;
}
4 changes: 2 additions & 2 deletions lib/before-watch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { runWebpackCompiler } = require("./compiler");

module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
module.exports = function ($projectData, $logger, hookArgs) {
if (hookArgs.config) {
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
if (appFilesUpdaterOptions.bundle) {
Expand All @@ -15,7 +15,7 @@ module.exports = function ($mobileHelper, $projectData, $logger, hookArgs) {
watch: true
};

return runWebpackCompiler(config, $mobileHelper, $projectData, $logger, hookArgs);
return runWebpackCompiler(config, $projectData, $logger, hookArgs);
}));
}
}
Expand Down
Loading