├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── liveedit.android.js ├── liveedit.ios.js ├── package.json ├── platforms └── android │ └── nativescript.aar └── support ├── .jshintrc ├── getHashVersion.js ├── postinstall.js ├── testFramework.app.js ├── tslint.json ├── watcher.entities └── watcher.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | jni/ 3 | *.tar 4 | *.tgz 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | demo/ 2 | support/getHashVersion.js 3 | livesync.* 4 | jni/ 5 | .idea/ 6 | *.tgz 7 | *.tar 8 | .npmignore 9 | jni/ 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Nathanael Anderson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![npm](https://img.shields.io/npm/v/nativescript-liveedit.svg)](https://www.npmjs.com/package/nativescript-liveedit) 2 | [![npm](https://img.shields.io/npm/l/nativescript-liveedit.svg)](https://www.npmjs.com/package/nativescript-liveedit) 3 | [![npm](https://img.shields.io/npm/dt/nativescript-liveedit.svg?label=npm%20d%2fls)](https://www.npmjs.com/package/nativescript-liveedit) 4 | 5 | # NativeScript Real Time LiveEdit Ability 6 | 7 | A NativeScript module providing real time development for Android. This version is for v2.3.x of the Android Runtimes. 8 | Please note this project USED to be called NativeScript-LiveSync, but to eliminate the confusion between the Telerik LiveSync and my LiveSync, I decided to rename my project. 9 | 10 | ## License 11 | 12 | All this code is (c)2015-2016 Master Technology. This is released under the MIT License, meaning you are free to include this in any type of program -- However for entities that need a support contract, changes, enhancements and/or a commercial license please contact me at [http://nativescript.tools](http://nativescript.tools). 13 | 14 | I also do contract work; so if you have a module you want built for NativeScript (or any other software projects) feel free to contact me [nathan@master-technology.com](mailto://nathan@master-technology.com). 15 | 16 | [![Donate](https://img.shields.io/badge/Donate-PayPal-brightgreen.svg?style=plastic)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=HN8DDMWVGBNQL&lc=US&item_name=Nathanael%20Anderson&item_number=nativescript%2dliveedit&no_note=1&no_shipping=1¤cy_code=USD&bn=PP%2dDonationsBF%3ax%3aNonHosted) 17 | [![Patreon](https://img.shields.io/badge/Pledge-Patreon-brightgreen.svg?style=plastic)](https://www.patreon.com/NathanaelA) 18 | 19 | 20 | ## Differences between Telerik LiveSync & Master Technology LiveEdit/Pro 21 | 22 | Master Technology released the LiveEdit project in v1.00 of NativeScript; In the version v1.2.0 of the NativeScript command line tools; Telerik has now released a *limited* LiveSync (or what I consider a DeadSync command. :grinning: ). The differences from my LiveEdit and Telerik's LiveSync is substantial enough that I will continue to use and maintain my version for the foreseeable future. 23 | The good news is they are catching up, they have fixed several major issues in each release and as they release newer versions they get a lot closer in feature parity. 24 | 25 | #### Pros of Telerik's LiveSync: 26 | * No extra code added to your application! 27 | * Works on iOS Devices & iOS Simulator 28 | * Support Angular2 projects 29 | * Supports syncing multiple devices in sequence 30 | 31 | #### Cons of Telerik's LiveSync: 32 | * Not really Live. It syncs the files; but then has to restart the application from scratch when changing anything but a CSS or XML file. 33 | * Delays while it detects any changes and then deploys the changes. 34 | * Delays while it is re-launching Application. 35 | * Loss of all application state since it reloads the app on every change. 36 | * If you navigated three screens deep, and make a JS file change; you will need to re-navigate to that screen again to see it. 37 | * Incredibly slow LiveSync startup time. 38 | * Reset of the Application even if you change a file that isn't even being used. 39 | * Easy to crash your application as the JavaScript and XML are not checked before being sent to the application. 40 | * Doesn't apparently work on some Android devices... 41 | 42 | #### Con's of Master Technology's LiveEdit: 43 | * You have to use the included patched runtime. (Please vote up the [issue](https://github.com/NativeScript/android-runtime/pull/92)) 44 | * tns clean & add/remove platforms will reset the runtime, so you need to de-install/re-install the plugin 45 | * Small amount of JavaScript added code to your project. 46 | * Only works on the Android platform, no iOS support yet. 47 | * Does not support Angular2 yet - This will be available to [Patreon](https://www.patreon.com/NathanaelA) members only. 48 | 49 | #### Pro's of Master Technology's LiveEdit: 50 | * Live, You see the app change almost exactly when your editor saves the files. 51 | * New files are detected and synced instantly. 52 | * Application state is almost always fully maintained. 53 | * The screen you are working on only reloads ONLY if it is the code you just changed. 54 | * Built in ability to detect errors in XML and JS before pushing to device to eliminate crashing the app on the device. 55 | * Ability to only reload application on files that are singletons or other files that you would rather have the app reloaded for. 56 | * Ability to restart application by touching or creating a "restart.livesync" or "restart.liveedit" file. 57 | * Ability to sync fonts from the app/fonts folder 58 | * Ability to sync standalone images png/jpg files 59 | * Ability to run Tests instantly! 60 | * Ability to auto-launch application if it crashed and is no longer running on device/emulator. 61 | 62 | The iOS side is currently just a simple DUMMY WRAPPER so that any usage you use on the Android side will not cause any issues when you deploy to your iOS devices/emulator. 63 | 64 | ## Real Time LiveEdit Demo 65 | 66 | [![1st Video Showing off Real Time LiveEdit Development Ability](http://img.youtube.com/vi/cCiyJZexSOQ/0.jpg)](http://www.youtube.com/watch?v=cCiyJZexSOQ) 67 | [![2nd Video Showing off Real Time LiveEdit Development Ability](http://img.youtube.com/vi/1p_4n9xBWZ0/0.jpg)](http://www.youtube.com/watch?v=1p_4n9xBWZ0) 68 | 69 | 70 | 71 | ## VERY IMPORTANT NOTES 72 | 73 | This plugin includes the latest release runtimes WITH the liveedit patch included into it. 74 | 75 | If you want to compile the runtimes your self; you can clone the latest runtime; switch to the release branch, and then manually patch it with my above patch (pull request 92), and then install the runtime following the latest documentation. [http://docs.nativescript.org/running-latest](http://docs.nativescript.org/running-latest) 76 | 77 | You can also run the latest nightly runtimes from [http://nativescript.rocks](http://nativescript.rocks), and the LiveEdit patch is auto-applied to the nightly master before it builds it. 78 | 79 | Please note the watcher specifically does NOT watch the **App_Resources** folders, mainly because this folder must be built, as these are compiled resources. 80 | In addition the device code itself does not have any code to start watching any new folders when they are added; restarting the app will allow it to start watching it. I have a billion other things on my list that affects me more. So this is a very low priority to actually code it up, I would gladly take pull requests that fixes this, if you find this oversight too annoying. 81 | 82 | I realize XMLLint is a pain to install on windows, so I have included all the needed files on the NativeScript.rocks site. 83 | 84 | If you use the new tns clean, or tns platform add/remove to reset your platform folder; this deletes resets the entire platform folder, meaning you loose the custom liveedit runtime. You will have to de-install and re-install the plugin to continue using it. 85 | 86 | ## Installation 87 | 88 | Run `tns plugin add nativescript-liveedit` 89 | 90 | ### Upgrading 91 | You need to de-install the prior version of LiveEdit, then install the new version. 92 | Run `tns plugin remove nativescript-livesedit` then run `tns plugin add nativescript-liveedit` 93 | 94 | ### Prerequisites: 95 | Run `npm install jshint -g` 96 | 97 | If you are using TypeScript; you should install tslint; run `npm install tslint -g` 98 | 99 | If you don't have xmllint already on your machine; you will need to install it. (Windows users: http://xmlsoft.org/sources/win32/) 100 | 101 | 102 | ## Usage & Running 103 | 104 | On your development machine you need to open a command prompt to your main application folder; and type **node watcher** which will start the utility that handles verification and pushing new files to the devices or emulators. 105 | 106 | To use the liveedit module you must first `require()` it in your application. 107 | 108 | ```js 109 | var liveedit = require("nativescript-liveedit" ); 110 | ``` 111 | 112 | You should as a minimum put this in your **app.js** like so: 113 | ```js 114 | var application = require("application"); 115 | 116 | // ---- ADD THIS LINE ---- 117 | require('nativescript-liveedit'); 118 | // ----------------------- 119 | 120 | application.start({ moduleName: "main-page" }); 121 | ``` 122 | 123 | Then this will activate at the start of the application and work for the entire time, also notice the removal of the "./" in the cssFile. I'm not sure why Telerik put a ./ for the app.css as it is unneeded. 124 | 125 | ## Magic Restart Files 126 | Changes in these files will automatically cause the application to restart on the device or emulator. 127 | * app.js 128 | * restart.livesync 129 | * restart.liveedit 130 | * Any other file you add via the **restartFile** command described below. 131 | 132 | 133 | ## Get the LiveEdit object 134 | ```var liveedit = require('nativescript-liveedit');``` 135 | 136 | ### Methods 137 | 138 | #### addModelPageLink(Page, Model) 139 | ##### Parameters 140 | * Page - this is the page that the model is related too. 141 | * Model - this is the model that relates to the page 142 | 143 | #### ignoreFile(Page) 144 | ##### Parameters 145 | * Page - this is the file to totally ignored for sending as updates. 146 | You can call this multiple times and it will just add it to a list of files to ignore. 147 | 148 | #### restartFile(Page) 149 | ##### Parameters 150 | * Page - this is the file to cause the app on the client to restart. 151 | You can call this multiple times and it will just add it to a list of files to restart on. 152 | By default, app.js, restart.liveedit and restart.livesync are in this list. 153 | 154 | #### enabled(value) 155 | ##### Parameters 156 | * (no parameter) will return if it is enabled 157 | * (value) - set it to be enabled (true) or disabled (false) 158 | 159 | #### debugMode(value) 160 | ##### Parameters 161 | * (no Parameter) will return if it is running in debugMode 162 | * (value) - set it to be forced into or out of debugMode, rather than letting it use the detection method. 163 | 164 | #### getAppName() 165 | This will return the package name in the from the AndroidManifest 166 | 167 | #### getAppVersion() 168 | This will return the VersionName from inside the AndroidManifest 169 | 170 | #### restart() 171 | This will fully restart the application - 172 | 173 | #### checkForEmulator() 174 | This will check to see if the app is running on a emulator 175 | 176 | #### checkForDebugMode() 177 | This will check to see if the app was signed with a debug key (i.e. debug mode) 178 | 179 | #### reloadPage() 180 | This will reload the current page 181 | 182 | #### isSuspended() 183 | This will tell you if the application is suspended. (i.e. some other app has focus) 184 | 185 | #### currentAppPath() 186 | This will return the current application path. 187 | -------------------------------------------------------------------------------- /liveedit.android.js: -------------------------------------------------------------------------------- 1 | /********************************************************************************** 2 | * (c) 2015, 2016 Master Technology 3 | * Licensed under the MIT license or contact me for a Support or Commercial License 4 | * 5 | * I do contract work in most languages, so let me solve your problems! 6 | * 7 | * Any questions please feel free to email me or put a issue up on the github repo 8 | * Version 0.1.4 Nathan@master-technology.com 9 | *********************************************************************************/ 10 | "use strict"; 11 | 12 | /* jshint node: true, browser: true, unused: true, undef: true */ 13 | /* global android, com, java, javax, __clearRequireCachedItem, unescape, __runtimeVersion */ 14 | 15 | // -------------------------------------------- 16 | var fs = require("file-system"); 17 | var fsa = require("file-system/file-system-access").FileSystemAccess; 18 | var application = require('application'); 19 | var frameCommon = require('ui/frame/frame-common'); 20 | var styleScope = require("ui/styling/style-scope"); 21 | var fileResolver = require("file-system/file-name-resolver"); 22 | var page = require('ui/page').Page; 23 | // -------------------------------------------- 24 | var FSA = new fsa(); 25 | // -------------------------------------------- 26 | 27 | var LiveEditSingleton = null; 28 | 29 | var LiveEdit = function() { 30 | if (LiveEditSingleton) { 31 | return LiveEditSingleton; 32 | } 33 | LiveEditSingleton = this; 34 | 35 | this._modelLink = {}; 36 | this._appName = null; 37 | this._appVersion = null; 38 | this._currentVersion = null; 39 | this._observers = []; 40 | this._isDebugMode = null; 41 | this._updaterEnabled = true; 42 | this._ignoredPages = []; 43 | this._restartPages = ["app.js", "package.json", "restart.livesync", "restart.liveedit"]; 44 | this._supportFiles = {}; 45 | 46 | this._applicationResumedStatus = 0; 47 | this._suspendedNavigation = null; 48 | this._curAppPath = ''; 49 | this._tmpWatchPath = ''; 50 | 51 | this._startLiveEdit(); 52 | 53 | }; 54 | 55 | 56 | LiveEdit.prototype._startLiveEdit = function() { 57 | if (!application.android.context) { 58 | if (typeof __runtimeVersion === "undefined" || __runtimeVersion === "1.6.0") { 59 | var self = this; 60 | setTimeout(function () { 61 | self._startLiveEdit(); 62 | }, 500); 63 | return; 64 | } 65 | } 66 | this._curAppPath = fs.knownFolders.currentApp().path + "/"; 67 | 68 | if (this.checkForReleaseMode()) { 69 | // We don't do anything in a released application 70 | return; 71 | } 72 | 73 | // Some un-rooted Android devices have issues pushing into the /data/data folder, so use the 74 | // adb writable /data/local/tmp folder as a transfer mechanism. 75 | this._tmpWatchPath = "/data/local/tmp/" + this.getAppName(); 76 | try { 77 | var javaFile = new java.io.File(this._tmpWatchPath); 78 | if (!javaFile.exists()) { 79 | javaFile.mkdirs(); 80 | javaFile.setReadable(true, false); 81 | javaFile.setWritable(true, false); 82 | javaFile.setExecutable(true, false); 83 | } 84 | } catch (err) { 85 | console.log("LiveEdit: Error attempting to create tmpWatch folder", err); 86 | } 87 | 88 | 89 | this._hookFramework(); 90 | this._startObservers(); 91 | }; 92 | 93 | /** 94 | * Returns debugMode status, or allows you to set it. 95 | * @param value - true/false or no value. 96 | * @returns {*} - debug mode status. 97 | */ 98 | LiveEdit.prototype.debugMode = function(value) { 99 | if (arguments.length) { 100 | this._isDebugMode = !!value; 101 | } else if (this._isDebugMode === null) { 102 | return this.checkForDebugMode(); 103 | } 104 | return this._isDebugMode; 105 | }; 106 | 107 | /** 108 | * Allow you to enable/disable the instant update support 109 | * @param value - true or false 110 | * @returns {boolean} the current enabled status 111 | */ 112 | LiveEdit.prototype.enabled = function(value) { 113 | if (arguments.length) { 114 | this._updaterEnabled = !!value; 115 | } 116 | return this._updaterEnabled; 117 | }; 118 | 119 | LiveEdit.prototype.getContext = function() { 120 | if (application.android.context) { 121 | return (application.android.context); 122 | } 123 | var ctx = java.lang.Class.forName("android.app.AppGlobals").getMethod("getInitialApplication", null).invoke(null, null); 124 | if (ctx) return ctx; 125 | 126 | ctx = java.lang.Class.forName("android.app.ActivityThread").getMethod("currentApplication", null).invoke(null, null); 127 | return ctx; 128 | }; 129 | 130 | /** 131 | * Retrieves the App name from the AndroidManifest 132 | * @returns {string} - Name of App 133 | */ 134 | LiveEdit.prototype.getAppName = function() { 135 | if (!this._appName) { 136 | this._appName = this.getContext().getPackageName(); 137 | } 138 | return this._appName; 139 | }; 140 | 141 | /** 142 | * Retrieves the app Version from the AndroidManifest 143 | * @returns App Version 144 | */ 145 | LiveEdit.prototype.getAppVersion = function() { 146 | if (!this._appVersion) { 147 | var packageManager = this.getContext().getPackageManager(); 148 | //noinspection JSUnresolvedVariable 149 | this._appVersion = packageManager.getPackageInfo(this.getContext().getPackageName(), 0).versionName; 150 | } 151 | return this._appVersion; 152 | }; 153 | 154 | /** 155 | * Restart the application 156 | */ 157 | LiveEdit.prototype.restart = function() { 158 | var mStartActivity = new android.content.Intent(this.getContext(), com.tns.NativeScriptActivity.class); 159 | var mPendingIntentId = parseInt(Math.random()*100000,10); 160 | var mPendingIntent = android.app.PendingIntent.getActivity(this.getContext(), mPendingIntentId, mStartActivity, android.app.PendingIntent.FLAG_CANCEL_CURRENT); 161 | var mgr = this.getContext().getSystemService(android.content.Context.ALARM_SERVICE); 162 | mgr.set(android.app.AlarmManager.RTC, java.lang.System.currentTimeMillis() + 100, mPendingIntent); 163 | android.os.Process.killProcess(android.os.Process.myPid()); 164 | }; 165 | 166 | /** 167 | * Returns true if the application is running on a emulator 168 | * @returns {boolean} 169 | */ 170 | LiveEdit.prototype.checkForEmulator = function() { 171 | var res = android.os.Build.FINGERPRINT; 172 | return res.indexOf("generic") !== -1; 173 | }; 174 | 175 | /** 176 | * Returns a list of application signatures 177 | * @returns {Array} - signature array 178 | */ 179 | LiveEdit.prototype.getAppSignatures = function() { 180 | try { 181 | var packageManager = this.getContext().getPackageManager(); 182 | 183 | // GET_SIGNATURES = 64 184 | return packageManager.getPackageInfo(this.getContext().getPackageName(), 64).signatures; 185 | } catch (err) { 186 | return []; 187 | } 188 | }; 189 | 190 | /** 191 | * Returns true if the application was signed with a DebugKey meaning the app is in debug mode 192 | * @returns {boolean} - false if it is in release mode 193 | */ 194 | LiveEdit.prototype.checkForDebugMode = function() { 195 | var DEBUG_PRINCIPAL = new javax.security.auth.x500.X500Principal("CN=Android Debug,O=Android,C=US"); 196 | try 197 | { 198 | var signatures = this.getAppSignatures(); 199 | var cf = java.security.cert.CertificateFactory.getInstance("X.509"); 200 | for ( var i = 0; i < signatures.length;i++) 201 | { 202 | // Convert back into a Certificate 203 | var stream = new java.io.ByteArrayInputStream(signatures[i].toByteArray()); 204 | var cert = cf.generateCertificate(stream); 205 | 206 | // Get the Principal for the signing Signature 207 | var SigningPrincipal = cert.getSubjectX500Principal(); 208 | if (SigningPrincipal.equals(DEBUG_PRINCIPAL)) { 209 | this._isDebugMode = true; 210 | return true; 211 | } 212 | } 213 | this._isDebugMode = false; 214 | } 215 | catch (err) 216 | { 217 | } 218 | return false; 219 | }; 220 | 221 | /** 222 | * Returns true if this application is signed by a release key 223 | * @returns {boolean} - false if the app is in debug mode 224 | */ 225 | LiveEdit.prototype.checkForReleaseMode = function() { 226 | return !this.checkForDebugMode(); 227 | }; 228 | 229 | /** 230 | * Reload the current page 231 | * @param p - optional page name to reload 232 | */ 233 | LiveEdit.prototype.reloadPage = function(p) { 234 | reloadPage(p); 235 | }; 236 | 237 | /** 238 | * Is the application suspended 239 | * @returns {boolean} - true/false 240 | */ 241 | LiveEdit.prototype.isSuspended = function() { 242 | return this._applicationResumedStatus === 0; 243 | }; 244 | 245 | /** 246 | * This allows you to link a type of file to a page; to force it to reload if the support file changes... 247 | * @param page 248 | * @param fileName 249 | */ 250 | LiveEdit.prototype.addSupportReloads = function(page, fileName) { 251 | if (fileName === undefined || fileName === null || fileName.length === 0) { return; } 252 | if (fileName[0] === '*' && fileName[1] === '.') { 253 | fileName = fileName.substr(1); 254 | } 255 | 256 | if (page.endsWith('.js')) { 257 | page = page.substring(0, page.length-3); 258 | } 259 | 260 | // We only allow a file or and extension to be assigned to ONE page 261 | this._supportFiles[fileName] = page; 262 | }; 263 | /** 264 | * This allows you to link model(s) to a specific page 265 | * @param page - the page that uses this model 266 | * @param model - the model that is linked to the page 267 | */ 268 | LiveEdit.prototype.addModelPageLink = function(page, model) { 269 | if (!model.endsWith('.js')) { 270 | model = model + ".js"; 271 | } 272 | if (page.endsWith('.js')) { 273 | page = page.substring(0, page.length-3); 274 | } 275 | if (typeof this._modelLink[model] === "undefined") { 276 | this._modelLink[model] = page; 277 | return; 278 | } else if (!Array.isArray(this._modelLink[model])) { 279 | if (this._modelLink[model] === page) { 280 | return; 281 | } 282 | // Convert to an array if it is already assigned a page 283 | this._modelLink[model] = [this._modelLink[model]]; 284 | } 285 | 286 | var found = false; 287 | for (var i=0;i= 3) { 367 | var callback = arguments[2]; 368 | args[2] = function() { 369 | if (!callback) { return; } 370 | if (!modalInfo.liveClose) { 371 | callback.apply(undefined,arguments); 372 | } 373 | }; 374 | } 375 | this.__showModal.apply(this, args); 376 | if (!pageName && frameCommon.topmost().currentEntry) { 377 | pageName = frameCommon.topmost().currentEntry.entry.moduleName; 378 | } 379 | modalInfo.pageName = pageName; 380 | modalInfo.params = Array.prototype.slice.call(arguments); 381 | modalInfo.liveClose = false; 382 | 383 | 384 | }; 385 | 386 | // TODO: This doesn't work properly in v1.10 and before -- test to see if this will work in v1.20 of runtimes; 387 | // TODO: If this still doesn't work in v1.20 we might need to make a patch. :-) 388 | /* application.on(application.uncaughtErrorEvent, function (args) { 389 | if (args.android) { 390 | // For Android applications, args.android is an NativeScriptError. 391 | console.log("!------------- NativeScriptError: " + args.android); 392 | } else if (args.ios) { 393 | // For iOS applications, args.ios is NativeScriptError. 394 | console.log("NativeScriptError: " + args.ios); 395 | } 396 | else { 397 | console.log("!------------- NSE:", args); 398 | } 399 | }); */ 400 | 401 | 402 | }; 403 | 404 | /** 405 | * Used to track any pages for navigation while the app is suspended 406 | * @param value - page name 407 | * @returns {string} - page name 408 | * @private 409 | */ 410 | LiveEdit.prototype._suspendedNavigate = function(value, isModal) { 411 | if (arguments.length) { 412 | this._suspendedNavigation = [value, isModal]; 413 | } 414 | return this._suspendedNavigation; 415 | }; 416 | 417 | /** 418 | * Used to track if the app is suspended 419 | * @private 420 | */ 421 | LiveEdit.prototype._applicationSuspended = function() { 422 | this._applicationResumedStatus--; 423 | }; 424 | 425 | /** 426 | * Used to track when the app is resumed 427 | * @private 428 | */ 429 | LiveEdit.prototype._applicationResumed = function() { 430 | this._applicationResumedStatus++; 431 | if (this._suspendedNavigation) { 432 | reloadPage(this._suspendedNavigation[0], this._suspendedNavigation[1]); 433 | this._suspendedNavigation = null; 434 | } 435 | }; 436 | 437 | /** 438 | * Checks if the modified file is related to the currently loaded page 439 | * @param v 440 | * @private 441 | */ 442 | LiveEdit.prototype._checkCurrentPage = function(v) { 443 | var f = frameCommon.topmost(), i; 444 | var CE, CEjs, CExml, CEcss, isModal=false; 445 | if (f.currentEntry) { 446 | if (f.currentEntry.moduleName) { 447 | CE = f.currentEntry.moduleName; 448 | } else if (f.currentEntry.entry) { 449 | CE = f.currentEntry.entry.moduleName; 450 | } 451 | } 452 | 453 | 454 | for (i=0;i= 0) { 524 | if (this._supportFiles[v.substr(idx)] === CE) { 525 | reloadPage(CE); 526 | } 527 | } 528 | } 529 | } 530 | } 531 | }; 532 | 533 | 534 | 535 | 536 | /** 537 | * This Starts the Observers for the Update Folders 538 | * @private 539 | */ 540 | LiveEdit.prototype._startObservers = function() { 541 | var self = this; 542 | var FO = android.os.FileObserver.extend({ 543 | FOPath: "", 544 | LastPage: "", 545 | LastTime: 0, 546 | onEvent: function (event, path) { 547 | var curTime = Date.now(); 548 | if (this.LastPage === path) { 549 | if (this.LastTime + 500 > curTime) { 550 | this.LastTime = curTime; 551 | return; 552 | } 553 | } 554 | this.LastPage = path; 555 | this.LastTime = curTime; 556 | 557 | if (self._updaterEnabled) { 558 | var curFile; 559 | if (this.FOPath.indexOf("/data/local/tmp/") === 0) { 560 | curFile = new java.io.File(this.FOPath + path); 561 | } else { 562 | curFile = new java.io.File(self._curAppPath + this.FOPath + path); 563 | } 564 | var curLength = curFile.length(); 565 | //console.log("!----- Event", event, this.FOPath + path, curFile.exists()); 566 | var selfFO = this; 567 | 568 | var checkFunction = function() { 569 | setTimeout(function () { 570 | var newLength = curFile.length(); 571 | if (newLength !== curLength) { 572 | curLength = newLength; 573 | checkFunction(); 574 | return; 575 | } 576 | if (selfFO.FOPath.indexOf("/data/local/tmp/") === 0) { 577 | self._moveSecondaryFile(selfFO.FOPath, path); 578 | return; 579 | } 580 | 581 | self._checkCurrentPage(selfFO.FOPath + path); 582 | }, 250); 583 | }; 584 | checkFunction(); 585 | } 586 | } 587 | }); 588 | 589 | this._startDirectoryObservers(this._curAppPath, "", FO); 590 | this._startDirectoryObservers("", this._tmpWatchPath + "/", FO); 591 | }; 592 | 593 | LiveEdit.prototype._moveFile = function(src, dest) { 594 | 595 | var path = dest.substr(0, dest.lastIndexOf('/') + 1); 596 | var javaFile; 597 | 598 | try { 599 | javaFile = new java.io.File(path); 600 | if (!javaFile.exists()) { 601 | javaFile.mkdirs(); 602 | javaFile.setReadable(true); 603 | javaFile.setWritable(true); 604 | } 605 | } 606 | catch (err) { 607 | console.info("LiveEdit - MoveFile - Creating File Folder Error", err); 608 | } 609 | 610 | var srcFile = new java.io.File(src); 611 | var destFile = new java.io.File(dest); 612 | if (srcFile.renameTo(destFile)) { 613 | // Move was successful 614 | return true; 615 | } 616 | 617 | 618 | var myInput = new java.io.FileInputStream(src); 619 | var myOutput = new java.io.FileOutputStream(dest); 620 | 621 | 622 | var success = true; 623 | try { 624 | //transfer bytes from the inputfile to the outputfile 625 | var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.class.getField("TYPE").get(null), 1024); 626 | var length; 627 | while ((length = myInput.read(buffer)) > 0) { 628 | myOutput.write(buffer, 0, length); 629 | } 630 | } 631 | catch (err) { 632 | success = false; 633 | } 634 | 635 | //Close the streams 636 | myOutput.flush(); 637 | myOutput.close(); 638 | myOutput = null; 639 | myInput.close(); 640 | myInput = null; 641 | 642 | if (srcFile.exists()) { 643 | srcFile.delete(); 644 | } 645 | return success; 646 | }; 647 | 648 | LiveEdit.prototype._moveSecondaryFile = function(srcPath, filePath) { 649 | var newFile = unescape(filePath); 650 | var javaFile = new java.io.File(srcPath+filePath); 651 | if (!javaFile.exists()) { 652 | return; 653 | } 654 | if (newFile.endsWith(".css") || newFile.endsWith(".js") || newFile.endsWith(".xml") || newFile.endsWith(".ttf") || newFile.endsWith(".livesync") || newFile.endsWith('.json') || newFile.endsWith('.png') || newFile.endsWith('.jpg') || newFile.endsWith(".liveedit")) { 655 | this._moveFile(srcPath+filePath, this._curAppPath+newFile); 656 | } else { 657 | // We don't keep any tmp/ non related files. 658 | javaFile.delete(); 659 | } 660 | }; 661 | 662 | /** 663 | * Since the Android Observer is broken, we have to traverse each folder ourselves and setup its own Observer 664 | * @param basePath 665 | * @param relPath 666 | * @param FileObserver 667 | * @private 668 | */ 669 | LiveEdit.prototype._startDirectoryObservers = function(basePath, relPath, FileObserver) { 670 | 671 | var fullPath = basePath + relPath; 672 | 673 | var flags = 386 + 8; // CREATE = 256, MODIFY = 2, MOVED_TO = 128 = (256 | 2 | 128) = 386 674 | var observer = new FileObserver(fullPath, flags); 675 | observer.FOPath = relPath; 676 | observer.startWatching(); 677 | this._observers.push(observer); 678 | 679 | var javaFile = new java.io.File(fullPath); 680 | 681 | if (!javaFile.canRead()) { // Security issue 682 | return; 683 | } 684 | 685 | var filesList = javaFile.listFiles(); 686 | if (!filesList || !filesList.length || basePath.length === 0) { // Probably Security Issue, but lets not crash if we don't get a filesList 687 | return; 688 | } 689 | 690 | for (var i=0;itns platform update android\n Then you need to redo the installation of this plugin\n >tns plugin add nativescript-liveedit", 60 | "If upgrading is not a options, download this plugin tagged to your current runtimes. using the tag @ns" + version + " to specify the specific version.", 61 | "\n---------------------------------------------------------------------------------------------------\n"); 62 | cleanupFiles(); 63 | process.exit(0); 64 | } 65 | 66 | var cnt = 0; 67 | function checkHash(v) { 68 | cnt++; 69 | 70 | if (!aarFileHashes[v]) { 71 | console.error("---------------------------------------------------------------------------------------------------\n", 72 | "This version of LiveEdit does not support the version of the Android runtimes you have.\n This is probably because you have updated to a newer version of the NativeScript Android runtimes.\n","A new version of NativeScript-LiveEdit should be released shortly.", 73 | "\n---------------------------------------------------------------------------------------------------\n"); 74 | cleanupFiles(); 75 | process.exit(0); 76 | } 77 | var convert = false, liveSync = false; 78 | if (aarFileHashes[v]) { 79 | if (aarFileHashes[v].upgrade) { 80 | displayUpgrade(aarFileHashes[v].version); 81 | } 82 | convert = !!aarFileHashes[v].convert; 83 | liveSync = !!aarFileHashes[v].liveSync; 84 | } 85 | 86 | if (cnt >= 1) { 87 | if (liveSync) { 88 | console.error("---------------------------------------------------------------------------------------------------\n", 89 | "You are already running the current LiveEdit runtimes. Updating just the LiveEdit Javascript...", 90 | "\n---------------------------------------------------------------------------------------------------\n"); 91 | } else if (convert) { 92 | console.error("---------------------------------------------------------------------------------------------------\n", 93 | "Installing the LiveEdit version of the runtimes....", 94 | "\n---------------------------------------------------------------------------------------------------\n"); 95 | } 96 | 97 | copyFiles(convert); 98 | } 99 | } 100 | 101 | function copyFile(src, dest, forceOverWrite) { 102 | if (!forceOverWrite && fs.existsSync(dest)) return; 103 | var buffer = fs.readFileSync(src); 104 | fs.writeFileSync(dest, buffer); 105 | } 106 | 107 | function copyFiles(convert) { 108 | copyFile("./support/watcher.js","../../watcher.js", true); 109 | copyFile("./support/watcher.entities","../../watcher.entities", false); 110 | copyFile("./support/.jshintrc","../../.jshintrc", false); 111 | copyFile("./support/tslint.json", "../../tslint.json", false); 112 | if (convert) { 113 | copyFile("./platforms/android/nativescript.aar", "../../platforms/android/libs/runtime-libs/nativescript.aar", true); 114 | } 115 | cleanupFiles(); 116 | 117 | process.exit(0); 118 | } 119 | 120 | function cleanupFiles() { 121 | // Delete these files so that they don't end up in the compiled project, 122 | // the tns plugin command is simple stupid in that it copies everything to the platforms/.../tns_modules/nativescript-liveedit folder 123 | // However, later it cleans up after itself; but this is more of a precaution so that they files never end up in that folder. 124 | // In addition this eliminates TNS from attempting to make a include.gradle file automatically for no reason 125 | fs.unlinkSync("./platforms/android/nativescript.aar"); 126 | fs.unlinkSync("./support/watcher.entities"); 127 | fs.unlinkSync("./support/watcher.js"); 128 | fs.unlinkSync("./support/.jshintrc"); 129 | fs.unlinkSync("./support/tslint.json"); 130 | fs.unlinkSync("./support/postinstall.js"); 131 | fs.rmdirSync("./platforms/android"); 132 | fs.rmdirSync("./platforms"); 133 | } 134 | 135 | 136 | function getFileSha(filename, callback) { 137 | var shaSum = crypto.createHash('sha1'); 138 | if (!fs.existsSync(filename)) { 139 | console.error("---------------------------------------------------------------------------------------------------\n", 140 | "Unable to find the Android Runtimes. Please make sure that you have done a\n >tns platform add android\n","Then re-run the adding of this plugin.", 141 | "\n---------------------------------------------------------------------------------------------------\n"); 142 | cleanupFiles(); 143 | process.exit(0); 144 | } 145 | var readStream = fs.createReadStream(filename); 146 | readStream.on('data', function(d) { 147 | shaSum.update(d); 148 | }); 149 | 150 | readStream.on('end', function() { 151 | var d = shaSum.digest('hex'); 152 | callback(d); 153 | }); 154 | } 155 | 156 | -------------------------------------------------------------------------------- /support/testFramework.app.js: -------------------------------------------------------------------------------- 1 | var application = require("application"); 2 | application.mainModule = "./tns_modules/nativescript-unit-test-runner/main-page"; 3 | application.cssFile = "./tns_modules/nativescript-unit-test-runner/app.css"; 4 | require ('nativescript-liveedit'); 5 | application.start(); 6 | //# sourceMappingURL=app.js.map -------------------------------------------------------------------------------- /support/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "class-name": true, 4 | "curly": true, 5 | "eofline": true, 6 | "indent": true, 7 | "interface-name": true, 8 | "jsdoc-format": true, 9 | "max-line-length": [false, 140], 10 | "no-consecutive-blank-lines": true, 11 | "no-construct": true, 12 | "no-debugger": true, 13 | "no-duplicate-key": true, 14 | "no-duplicate-variable": true, 15 | "no-shadowed-variable": true, 16 | "no-empty": true, 17 | "no-eval": true, 18 | "no-switch-case-fall-through": true, 19 | "no-trailing-whitespace": true, 20 | "no-unreachable": true, 21 | "no-unused-expression": true, 22 | "no-unused-variable": true, 23 | "no-use-before-declare": true, 24 | "no-var-keyword": true, 25 | "no-var-requires": false, 26 | "one-line": [ 27 | true, 28 | "check-open-brace", 29 | "check-catch", 30 | "check-else" 31 | ], 32 | "quotemark": [false, "double"], 33 | "semicolon": true, 34 | "switch-default": false, 35 | "triple-equals": [true, "allow-null-check"], 36 | "use-strict": true, 37 | "variable-name": [false, "allow-leading-underscore"], 38 | "whitespace": [ 39 | false, 40 | "check-branch", 41 | "check-decl", 42 | "check-operator", 43 | "check-module", 44 | "check-separator", 45 | "check-type", 46 | "check-typecast" 47 | ] 48 | } 49 | } -------------------------------------------------------------------------------- /support/watcher.entities: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 | 1281 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1310 | 1311 | 1312 | 1313 | 1314 | 1315 | 1316 | 1317 | 1318 | 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1338 | 1339 | 1340 | 1341 | 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1363 | 1364 | 1365 | 1366 | 1367 | 1368 | 1369 | 1370 | 1371 | 1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394 | 1395 | 1396 | 1397 | 1398 | 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | 1413 | 1414 | 1415 | 1416 | 1417 | 1418 | 1419 | 1420 | 1421 | 1422 | 1423 | 1424 | 1425 | 1426 | 1427 | 1428 | 1429 | 1430 | 1431 | 1432 | 1433 | 1434 | 1435 | 1436 | 1437 | 1438 | 1439 | 1440 | 1441 | 1442 | 1443 | 1444 | 1445 | 1446 | 1447 | 1448 | 1449 | 1450 | 1451 | 1452 | 1453 | 1454 | 1455 | 1456 | 1457 | 1458 | 1459 | 1460 | 1461 | 1462 | 1463 | 1464 | 1465 | 1466 | 1467 | 1468 | 1469 | 1470 | 1471 | 1472 | 1473 | 1474 | 1475 | 1476 | 1477 | 1478 | 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1485 | 1486 | 1487 | 1488 | 1489 | 1490 | 1491 | 1492 | 1493 | 1494 | 1495 | 1496 | 1497 | 1498 | 1499 | 1500 | 1501 | 1502 | 1503 | 1504 | 1505 | 1506 | 1507 | 1508 | 1509 | 1510 | 1511 | 1512 | 1513 | 1514 | 1515 | 1516 | 1517 | 1518 | 1519 | 1520 | 1521 | 1522 | 1523 | 1524 | 1525 | 1526 | 1527 | 1528 | 1529 | 1530 | 1531 | 1532 | 1533 | 1534 | 1535 | 1536 | 1537 | 1538 | 1539 | 1540 | 1541 | 1542 | 1543 | 1544 | 1545 | 1546 | 1547 | 1548 | 1549 | 1550 | 1551 | 1552 | 1553 | 1554 | 1555 | 1556 | 1557 | 1558 | 1559 | 1560 | 1561 | 1562 | 1563 | 1564 | 1565 | 1566 | 1567 | 1568 | 1569 | 1570 | 1571 | 1572 | 1573 | 1574 | 1575 | 1576 | 1577 | 1578 | 1579 | 1580 | 1581 | 1582 | 1583 | 1584 | 1585 | 1586 | 1587 | 1588 | 1589 | 1590 | 1591 | 1592 | 1593 | 1594 | 1595 | 1596 | 1597 | 1598 | 1599 | 1600 | 1601 | 1602 | 1603 | 1604 | 1605 | 1606 | 1607 | 1608 | 1609 | 1610 | 1611 | 1612 | 1613 | 1614 | 1615 | 1616 | 1617 | 1618 | 1619 | 1620 | 1621 | 1622 | 1623 | 1624 | 1625 | 1626 | 1627 | 1628 | 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 1636 | 1637 | 1638 | 1639 | 1640 | 1641 | 1642 | 1643 | 1644 | 1645 | 1646 | 1647 | 1648 | 1649 | 1650 | 1651 | 1652 | 1653 | 1654 | 1655 | 1656 | 1657 | 1658 | 1659 | 1660 | 1661 | 1662 | 1663 | 1664 | 1665 | 1666 | 1667 | 1668 | 1669 | 1670 | 1671 | 1672 | 1673 | 1674 | 1675 | 1676 | 1677 | 1678 | 1679 | 1680 | 1681 | 1682 | 1683 | 1684 | 1685 | 1686 | 1687 | 1688 | 1689 | 1690 | 1691 | 1692 | 1693 | 1694 | 1695 | 1696 | 1697 | 1698 | 1699 | 1700 | 1701 | 1702 | 1703 | 1704 | 1705 | 1706 | 1707 | 1708 | 1709 | 1710 | 1711 | 1712 | 1713 | 1714 | 1715 | 1716 | 1717 | 1718 | 1719 | 1720 | 1721 | 1722 | 1723 | 1724 | 1725 | 1726 | 1727 | 1728 | 1729 | 1730 | 1731 | 1732 | 1733 | 1734 | 1735 | 1736 | 1737 | 1738 | 1739 | 1740 | 1741 | 1742 | 1743 | 1744 | 1745 | 1746 | 1747 | 1748 | 1749 | 1750 | 1751 | 1752 | 1753 | 1754 | 1755 | 1756 | 1757 | 1758 | 1759 | 1760 | 1761 | 1762 | 1763 | 1764 | 1765 | 1766 | 1767 | 1768 | 1769 | 1770 | 1771 | 1772 | 1773 | 1774 | 1775 | 1776 | 1777 | 1778 | 1779 | 1780 | 1781 | 1782 | 1783 | 1784 | 1785 | 1786 | 1787 | 1788 | 1789 | 1790 | 1791 | 1792 | 1793 | 1794 | 1795 | 1796 | 1797 | 1798 | 1799 | 1800 | 1801 | 1802 | 1803 | 1804 | 1805 | 1806 | 1807 | 1808 | 1809 | 1810 | 1811 | 1812 | 1813 | 1814 | 1815 | 1816 | 1817 | 1818 | 1819 | 1820 | 1821 | 1822 | 1823 | 1824 | 1825 | 1826 | 1827 | 1828 | 1829 | 1830 | 1831 | 1832 | 1833 | 1834 | 1835 | 1836 | 1837 | 1838 | 1839 | 1840 | 1841 | 1842 | 1843 | 1844 | 1845 | 1846 | 1847 | 1848 | 1849 | 1850 | 1851 | 1852 | 1853 | 1854 | 1855 | 1856 | 1857 | 1858 | 1859 | 1860 | 1861 | 1862 | 1863 | 1864 | 1865 | 1866 | 1867 | 1868 | 1869 | 1870 | 1871 | 1872 | 1873 | 1874 | 1875 | 1876 | 1877 | 1878 | 1879 | 1880 | 1881 | 1882 | 1883 | 1884 | 1885 | 1886 | 1887 | 1888 | 1889 | 1890 | 1891 | 1892 | 1893 | 1894 | 1895 | 1896 | 1897 | 1898 | 1899 | 1900 | 1901 | 1902 | 1903 | 1904 | 1905 | 1906 | 1907 | 1908 | 1909 | 1910 | 1911 | 1912 | 1913 | 1914 | 1915 | 1916 | 1917 | 1918 | 1919 | 1920 | 1921 | 1922 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | 1930 | 1931 | 1932 | 1933 | 1934 | 1935 | 1936 | 1937 | 1938 | 1939 | 1940 | 1941 | 1942 | 1943 | 1944 | 1945 | 1946 | 1947 | 1948 | 1949 | 1950 | 1951 | 1952 | 1953 | 1954 | 1955 | 1956 | 1957 | 1958 | 1959 | 1960 | 1961 | 1962 | 1963 | 1964 | 1965 | 1966 | 1967 | 1968 | 1969 | 1970 | 1971 | 1972 | 1973 | 1974 | 1975 | 1976 | 1977 | 1978 | 1979 | 1980 | 1981 | 1982 | 1983 | 1984 | 1985 | 1986 | 1987 | 1988 | 1989 | 1990 | 1991 | 1992 | 1993 | 1994 | 1995 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2028 | 2029 | 2030 | 2031 | 2032 | 2033 | 2034 | ]> -------------------------------------------------------------------------------- /support/watcher.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | /********************************************************************************** 3 | * (c) 2015, 2016 Master Technology 4 | * Licensed under the MIT license or contact me for a Support or Commercial License 5 | * 6 | * I do contract work in most languages, so let me solve your problems! 7 | * 8 | * Any questions please feel free to email me or put a issue up on the github repo 9 | * Version 0.2.1 Nathan@master-technology.com 10 | *********************************************************************************/ 11 | "use strict"; 12 | 13 | /* global escape */ 14 | var TranspileTypeScript = true; 15 | 16 | // Used for Demo's 17 | var autoFocus = false; 18 | 19 | 20 | // What is the current Test Mode allowed 21 | var TM_AUTO = 0; // Automatically switch between Test most and normal mode depending on file saved 22 | var TM_ALWAYS = 1; // Always stay in Test mode 23 | var TM_NEVER = 2; // Never go into Test mode 24 | 25 | // What is the current set Test Mode 26 | var CM_UNKNOWN = 0; 27 | var CM_NO_DEVICE = 1; 28 | var CM_NORMAL = 2; 29 | var CM_TESTMODE = 3; 30 | 31 | // App Mode Constants 32 | var APPMODE_NORMAL = "app.js"; 33 | var APPMODE_TEST = "./tns_modules/nativescript-unit-test-runner/app.js"; 34 | var appProjectData = null; 35 | 36 | // DefaultActivity 37 | var defaultActivity = "com.tns.NativeScriptActivity"; 38 | 39 | // Load our Requires 40 | var fs = require('fs'); 41 | var cp = require('child_process'); 42 | var os = require('os'); 43 | var crypto = require('crypto'); 44 | 45 | // ------------------------------------------------------- 46 | // Handle Keystrokes 47 | 48 | var readline = require('readline'); 49 | readline.emitKeypressEvents(process.stdin); 50 | if (process.stdin.isTTY) { 51 | process.stdin.setRawMode(true); 52 | } 53 | process.stdin.on('keypress', handleKeyStrokes); 54 | 55 | 56 | // Configuration ---------------------------------------------- 57 | var watching = [".css", ".js", ".xml", ".ttf", ".otf", ".png", ".jpg"]; 58 | // ------------------------------------------------------------ 59 | 60 | console.log("\n------------------------------------------------------------"); 61 | console.log("NativeScript LiveEdit Watcher v0.21"); 62 | console.log("(c)2015, 2016, Master Technology. www.master-technology.com"); 63 | console.log("------------------------------------------------------------"); 64 | 65 | 66 | // Setup any missing Prototypes 67 | if (!String.prototype.endsWith) { 68 | String.prototype.endsWith = function(searchString, position) { 69 | var subjectString = this.toString(); 70 | if (position === undefined || position > subjectString.length) { 71 | position = subjectString.length; 72 | } 73 | position -= searchString.length; 74 | var lastIndex = subjectString.indexOf(searchString, position); 75 | return lastIndex !== -1 && lastIndex === position; 76 | }; 77 | } 78 | 79 | 80 | // Load the Project Information and output it 81 | /* ---------------------------------------------------------- */ 82 | var info, projectData = {nativescript: {id: ""}}, commandLine = {testMode: TM_AUTO}, currentMode; 83 | try { 84 | info = fs.readFileSync('package.json'); 85 | projectData = JSON.parse(info); 86 | } 87 | catch (err) { 88 | console.log("Unable to read your package.json file, the watcher.js MUST be in your root of your application's directory."); 89 | process.exit(1); 90 | return; 91 | } 92 | 93 | // The default project name is "org.nativescript." 94 | // however, if you know what you are doing you can replace this so in the future 95 | // when this is no longer hard coded; I want to make sure that it accepts them. 96 | 97 | if (!projectData || !projectData.nativescript || !projectData.nativescript.id || projectData.nativescript.id.length === 0 || projectData.nativescript.id.indexOf('.') < 3) { 98 | console.log("Your package.json file appears to be corrupt, the project that I am detecting is: ", projectData && projectData.nativescript && projectData.nativescript.id); 99 | process.exit(1); 100 | return; 101 | } 102 | 103 | if (!projectData.devDependencies || !projectData.devDependencies["nativescript-dev-typescript"]) { 104 | TranspileTypeScript = false; 105 | } 106 | 107 | getPackageActivity(); 108 | 109 | // Set the Global wide error handler 110 | setupErrorCatching(); 111 | 112 | console.log("Watching your project:", projectData.nativescript.id); 113 | 114 | // We will copy ourselves to the device, if it works then we have r/w access to the machine, if it fails we will use another method 115 | // Real non-rooted devices might have an issue with pushing to their directory; if we detect this; we will use an alternative method... 116 | /* ---------------------------------------------------------- */ 117 | var pushADB = normalPushADB; 118 | pushADB("watcher.js", {check: true}); 119 | 120 | 121 | // Check for jsHint & xmllint support 122 | /* ---------------------------------------------------------- */ 123 | var hasTSLint = false; 124 | var hasJSHint = false; 125 | var hasXMLLint = false; 126 | 127 | var _tsLintCallback = function(error) { 128 | if (!error || error.code === 0) { 129 | hasTSLint = true; 130 | } else { 131 | console.log("TSLINT has not been detected, disabled TSLINT support. (",error,")"); 132 | console.log("Without TSLINT support, changes to TS files might cause the phone app to crash."); 133 | console.log("To install, type 'npm install -g tslint'"); 134 | console.log("-------------------------------------------------------------------------------"); 135 | } 136 | }; 137 | 138 | var _jshintCallback = function(error) { 139 | if (!error || error.code === 0) { 140 | hasJSHint = true; 141 | } else { 142 | console.log("JSHINT has not been detected, disabled JSHINT support. (",error,")"); 143 | console.log("Without JSHINT support, changes to JS files might cause the phone app to crash."); 144 | console.log("To install, type 'npm install -g jshint'"); 145 | console.log("-------------------------------------------------------------------------------"); 146 | } 147 | }; 148 | 149 | var _xmllintCallback = function(error,a,b) { 150 | if (!error && b === '') { 151 | hasXMLLint = true; 152 | } else { 153 | console.log("XMLLINT has not been detected, disabled XMLLINT support. (",error,")"); 154 | console.log("Without XMLLINT support, malformed XML files will cause the phone app to crash."); 155 | if (os.type() === "Windows_NT") { 156 | console.log("You can download XMLLINT for windows from http://nativescript.rocks"); 157 | } 158 | console.log("--------------------------------------------------------------------------------"); 159 | } 160 | }; 161 | 162 | cp.exec("jshint watcher.js", {timeout: 3000}, _jshintCallback); 163 | 164 | if (TranspileTypeScript) { 165 | cp.exec("tslint --version" , {timeout: 3000}, _tsLintCallback); 166 | } 167 | 168 | if (os.type() === 'Windows_NT') { 169 | cp.exec("xmllint --noout .\\platforms\\android\\src\\main\\AndroidManifest.xml", {timeout: 3000}, _xmllintCallback); 170 | } else { 171 | cp.exec("xmllint --noout ./platforms/android/src/main/AndroidManifest.xml", {timeout: 3000}, _xmllintCallback); 172 | } 173 | 174 | // Globals 175 | var timeStamps = {}; 176 | var watchingFolders = {}; 177 | 178 | // Startup the Watchers... 179 | setupWatchers("./app"); 180 | 181 | handleCommandLine(); 182 | currentMode = getAppMode(); 183 | 184 | // TODO: Do we want to ignore all pre-compiled TSC files on startup? 185 | if (TranspileTypeScript) { 186 | launchTSC(); 187 | } 188 | 189 | // Start Karma if need be 190 | if (commandLine.testMode !== TM_NEVER) { 191 | if (fs.existsSync("./app/tests") && fs.existsSync("./node_modules/karma")) { 192 | launchKarma(); 193 | } 194 | } 195 | 196 | function launchTSC() { 197 | console.log("Starting TypeScript watcher..."); 198 | var child = cp.spawn('tsc.cmd',['-w'], {stdio: "inherit", detached: false, cwd: __dirname+"/app"}); 199 | } 200 | 201 | /** 202 | * isWatching - will respond true if watching this file type. 203 | * @param fileName 204 | * @returns {boolean} 205 | */ 206 | function isWatching(fileName) { 207 | for (var i=0;i 0) { pushADB = backupPushADB; console.log("Using backup method for updates!"); } 353 | if (serr.indexOf('Permission denied') > 0) { pushADB = backupPushADB; console.log("Using backup method for updates!"); } 354 | if (check !== true) { 355 | console.log("Failed to Push to Device: ", fileName); 356 | console.log(err); 357 | //console.log(sout); 358 | //console.log(serr); 359 | } 360 | } else if (check !== true && quiet === false ) { 361 | console.log("Pushed to Device: ", fileName); 362 | } 363 | if (callback) { 364 | callback(err); 365 | } 366 | }); 367 | } 368 | 369 | function pullADB(fileName, options) { 370 | var destFile = fileName; 371 | if (options && typeof options.destFile === "string") { 372 | destFile = options.destFile; 373 | } 374 | 375 | var quiet = false; 376 | if (options && options.quiet) { 377 | quiet = true; 378 | } 379 | 380 | var path = "/data/data/" + projectData.nativescript.id + "/files/" + fileName; 381 | try { 382 | var buffer = cp.execSync('adb pull "' + path + '" "' + destFile + '"', {timeout: 5000}); 383 | } catch (err) { 384 | if (err && err.stderr) { 385 | if (err.stderr.toString().indexOf("error: device not found") === 0) { return false;} 386 | console.log("Error:", err.stderr.toString(), err.stderr.length); 387 | } 388 | } 389 | return true; 390 | } 391 | 392 | function isTestFile(filename) { 393 | //console.log("Checking mode/name:",filename, currentMode); 394 | if (currentMode === CM_UNKNOWN) { return; } 395 | if (currentMode === CM_NO_DEVICE) { 396 | currentMode = getAppMode(); 397 | if (currentMode <= CM_NO_DEVICE) { return; } 398 | } 399 | 400 | if (filename.indexOf('./app/tests/') === 0) { 401 | if (currentMode === CM_NORMAL) { 402 | if (commandLine.testMode !== TM_NEVER) { 403 | setAppMode(CM_TESTMODE); 404 | launchApp({force: true}); 405 | } 406 | } else { 407 | launchApp({force: false}); 408 | } 409 | } else { 410 | if (currentMode === CM_TESTMODE) { 411 | if (commandLine.testMode !== TM_ALWAYS) { 412 | setAppMode(CM_NORMAL); 413 | launchApp({force: false}); 414 | } 415 | } else { 416 | launchApp(); 417 | } 418 | } 419 | } 420 | 421 | var isKarmaRunning = false; 422 | function launchKarma() { 423 | if (isKarmaRunning) { return; } 424 | isKarmaRunning = true; 425 | var KarmaServer = require('./node_modules/karma/lib/server'); 426 | 427 | var karmaConfig = { 428 | browsers: [], 429 | singleRun: false, 430 | frameworks: ['mocha', 'chai'], 431 | basePath: '', 432 | files: [ './app/tests/*.js' ], 433 | exclude: [], 434 | preprocessors: { }, 435 | port: 9876, 436 | colors: true, 437 | autoWatch: true, 438 | reporters: ['progress'] 439 | 440 | }; 441 | 442 | // Launch Karama 443 | console.log("Starting Karma..."); 444 | new KarmaServer(karmaConfig).start(); 445 | } 446 | 447 | var isLaunchScheduled = false; 448 | function futureAppLaunch() { 449 | if (isLaunchScheduled) { 450 | return; 451 | } 452 | isLaunchScheduled = setTimeout(function() { 453 | isLaunchScheduled = false; 454 | checkAppIsRunning(); 455 | }, 1000); 456 | } 457 | 458 | function restartApplication() { 459 | focusApp(); 460 | var cmd = "adb shell am force-stop "+projectData.nativescript.id; 461 | cp.exec(cmd, function(err, stdout) { 462 | doLaunch(); 463 | }); 464 | } 465 | 466 | function checkCrashedApp(autoStart) { 467 | var cmd = "adb shell dumpsys activity activities | grep cmp="+projectData.nativescript.id+"/com.tns.ErrorReportActivity"; 468 | if (os.type() === "Windows_NT") { 469 | cmd = "adb shell dumpsys activity activities ^| grep cmp="+projectData.nativescript.id+"/com.tns.ErrorReportActivity"; 470 | } 471 | 472 | cp.exec(cmd, function(err, stdout) { 473 | // Check to see if running 474 | if (stdout.length === 0) { 475 | if (autoStart === false) { return; } 476 | futureAppLaunch(); 477 | } else { 478 | restartApplication(); 479 | } 480 | }); 481 | } 482 | 483 | function checkAppIsRunning(autoStart) { 484 | var cmd = 'adb shell ps | grep '+projectData.nativescript.id; 485 | if (os.type() === "Windows_NT") { 486 | cmd = 'adb shell ps ^| grep ' + projectData.nativescript.id; 487 | } 488 | 489 | cp.exec(cmd, function(err, stdout) { 490 | // Check to see if running 491 | if (stdout.length === 0) { 492 | doLaunch(); 493 | } else { 494 | checkCrashedApp(autoStart); 495 | } 496 | }); 497 | } 498 | 499 | function doLaunch() { 500 | if (isLaunchScheduled) { 501 | clearTimeout(isLaunchScheduled); 502 | isLaunchScheduled = false; 503 | } 504 | focusApp(); 505 | console.log("Starting application..."); 506 | var child = cp.spawn('adb',['shell','am', 'start', '-S', projectData.nativescript.id + "/"+defaultActivity], {stdio: "ignore", detached: true}); 507 | child.unref(); 508 | } 509 | 510 | function launchApp(params) { 511 | if (params && params.force) { 512 | doLaunch(); 513 | return; 514 | } 515 | if (!params) { 516 | checkAppIsRunning(false); 517 | } else { 518 | checkAppIsRunning(); 519 | } 520 | } 521 | 522 | var cacheSHA = {}; 523 | function checkCache(fileName) { 524 | 525 | checkFileSha(fileName, function(d) { 526 | if (cacheSHA[fileName] === d) { return; } 527 | cacheSHA[fileName] = d; 528 | checkParsing(fileName); 529 | }); 530 | } 531 | 532 | /** 533 | * This runs the linters to verify file sanity before pushing to the device 534 | * @param fileName 535 | */ 536 | var lastFileName, lastFileStamp; 537 | function checkParsing(fileName) { 538 | console.log("\nChecking updated file: ", fileName); 539 | var isTS = false; 540 | 541 | var callback = function(err, stdout , stderr) { 542 | if (err && (err.code !== 0 || err.killed) ) { 543 | 544 | if (isTS) { 545 | // Clear the cache on this file; because a file can technically PASS TSC, but fail tsLINT; so we don't want to stop the 546 | // Next attempt; this is common with "let" vs "var" the "var" is not allowed in tslint, but "var" is the generated code from a "let" 547 | cacheSHA[fileName] = '-NONE-'; 548 | var tsFileName = fileName.substr(0,fileName.length-2)+"ts"; 549 | if (fs.existsSync(tsFileName)) { fileName = tsFile; } 550 | 551 | } 552 | console.log("---------------------------------------------------------------------------------------"); 553 | console.log("---- Failed Sanity Tests on", fileName); 554 | console.log("---------------------------------------------------------------------------------------"); 555 | if (stdout) { console.log("STDOut:", stdout); } 556 | if (stderr) { console.log("STDErr:", stderr); } 557 | console.log("---------------------------------------------------------------------------------------\n"); 558 | } else { 559 | pushADB(fileName, function (err) { 560 | if (!err) { 561 | isTestFile(fileName); 562 | } 563 | }); 564 | 565 | } 566 | }; 567 | 568 | if (fileName.endsWith(".js")) { 569 | // If this is from a TS File, check the TSFile 570 | var tsFile = fileName.substr(0,fileName.length-2)+"ts"; 571 | if (fs.existsSync(tsFile)) { 572 | // TODO: Check if TS file is good and then send 573 | isTS = true; 574 | if (hasTSLint){ 575 | cp.exec('tslint "' + tsFile + '"', {timeout: 5000}, callback); 576 | } else { 577 | callback(null, "", ""); 578 | } 579 | return; 580 | } 581 | if (hasJSHint) { 582 | cp.exec('jshint "' + fileName + '"', {timeout: 5000}, callback); 583 | } else { 584 | console.log("WARNING: JSHINT is not installed, no test performed on JS files."); 585 | callback(null, "", ""); 586 | } 587 | } else if (fileName.endsWith(".ts")) { 588 | if (hasTSLint) { 589 | isTS = true; 590 | cp.exec('tslint "' + fileName + '"', {timeout: 5000}, callback); 591 | } else { 592 | console.log("WARNING: TSLINT is not installed, no test performed on TS files."); 593 | callback(null, "", ""); 594 | } 595 | } else if (fileName.endsWith(".xml")) { 596 | if (hasXMLLint) { 597 | if (os.type() === 'Windows_NT') { 598 | cp.exec('type watcher.entities "' + fileName.replace(/\//g, '\\') + '" | xmllint --noout -', {timeout: 5000}, callback); 599 | } else { 600 | cp.exec('cat watcher.entities "' + fileName + '" | xmllint --noout -', {timeout: 5000}, callback); 601 | } 602 | } else { 603 | console.log("WARNING: XMLLINT is not installed, no test performed on XML files."); 604 | callback(null, "", ""); 605 | } 606 | } else { 607 | callback(null, "", ""); 608 | } 609 | } 610 | 611 | /** 612 | * This is the watcher callback to verify the file actually changed 613 | * @param dir 614 | * @returns {Function} 615 | */ 616 | function getWatcher(dir) { 617 | return function (event, fileName) { 618 | if (event === "rename") { 619 | verifyWatches(); 620 | if (fileName) { 621 | if (!fs.existsSync(dir + fileName)) { return; } 622 | var dirStat; 623 | try { 624 | dirStat = fs.statSync(dir + fileName); 625 | } catch (err) { 626 | // This means the File disappeared out from under me... 627 | return; 628 | } 629 | if (dirStat.isDirectory()) { 630 | if (!watchingFolders[dir + fileName]) { 631 | console.log("Adding new directory to watch: ", dir + fileName); 632 | setupWatchers(dir + fileName); 633 | } 634 | return; 635 | } 636 | } else { 637 | checkForChangedFolders(dir); 638 | } 639 | return; 640 | } 641 | 642 | if (!fileName) { 643 | fileName = checkForChangedFiles(dir); 644 | if (fileName) { 645 | checkCache(fileName); 646 | } 647 | } 648 | else { 649 | if (isWatching(fileName)) { 650 | if (!fs.existsSync(dir + fileName)) { 651 | return; 652 | } 653 | 654 | var stat; 655 | try { 656 | stat = fs.statSync(dir + fileName); 657 | } 658 | catch (e) { 659 | // This means the file disappeared between exists and stat... 660 | return; 661 | } 662 | if (stat.size === 0) { return; } 663 | if (timeStamps[dir + fileName] === undefined || timeStamps[dir + fileName] !== stat.mtime.getTime()) { 664 | //console.log("Found 2: ", event, dir+fileName, stat.mtime.getTime(), stat.mtime, stat.ctime.getTime(), stat.size); 665 | timeStamps[dir + fileName] = stat.mtime.getTime(); 666 | checkCache(dir + fileName); 667 | } 668 | 669 | } 670 | 671 | } 672 | }; 673 | } 674 | 675 | /** 676 | * This setups a watcher on a directory 677 | * @param path 678 | */ 679 | function setupWatchers(path) { 680 | // We want to track the watchers now and return if we are already watching this folder 681 | if (watchingFolders[path]) { return; } 682 | 683 | watchingFolders[path] = fs.watch(path, getWatcher(path + "/")); 684 | watchingFolders[path].on('error', function() { verifyWatches(); }); 685 | var fileList = fs.readdirSync(path); 686 | var stats; 687 | for (var i = 0; i < fileList.length; i++) { 688 | try { 689 | stats = fs.statSync(path + "/" + fileList[i]); 690 | } 691 | catch (e) { 692 | continue; 693 | } 694 | if (isWatching(fileList[i])) { 695 | timeStamps[path + "/" + fileList[i]] = stats.mtime.getTime(); 696 | } else { 697 | if (stats.isDirectory()) { 698 | if (fileList[i] === "node_modules") { 699 | watchingFolders[path + "/" + fileList[i]] = true; 700 | continue; 701 | } 702 | if (fileList[i] === "tns_modules") { 703 | watchingFolders[path + "/" + fileList[i]] = true; 704 | continue; 705 | } 706 | if (fileList[i] === "App_Resources") { 707 | watchingFolders[path + "/" + fileList[i]] = true; 708 | continue; 709 | } 710 | setupWatchers(path + "/" + fileList[i]); 711 | } 712 | } 713 | } 714 | } 715 | 716 | 717 | function checkFileSha(filename, callback) { 718 | var shaSum = crypto.createHash('sha1'); 719 | var readStream = fs.createReadStream(filename); 720 | readStream.on('data', function (d) { 721 | shaSum.update(d); 722 | }); 723 | 724 | readStream.on('end', function () { 725 | var d = shaSum.digest('hex'); 726 | callback(d); 727 | }); 728 | } 729 | 730 | 731 | function verifyWatches() { 732 | for (var key in watchingFolders) { 733 | if (watchingFolders.hasOwnProperty(key)) { 734 | if (watchingFolders[key] && !fs.existsSync(key)) { 735 | watchingFolders[key].close(); 736 | watchingFolders[key] = false; 737 | console.log("Removing", key, "from being watched."); 738 | } 739 | } 740 | } 741 | } 742 | 743 | function processYesNoAllow(value) { 744 | if (value) { 745 | value = value.toLowerCase(); 746 | if (value === "always" || value === "yes" || value === "true" || value === "t" || value === "y" || value === "a" || value === "1") { 747 | return true; 748 | } 749 | if (value === "never" || value === "no" || value === "false" || value === "f" || value === "n" || value === "0") { 750 | return false; 751 | } 752 | } 753 | return false; 754 | } 755 | 756 | function handleCommandLine() { 757 | var commandModeVar; 758 | for (var i=1;i -1) { 869 | endOffset = data.indexOf('>', offset); 870 | if (data[endOffset-1] !== '/') { 871 | endOffset = data.indexOf("", offset); 872 | } 873 | var stringData = data.substring(offset, endOffset); 874 | if (stringData.indexOf('android.intent.action.MAIN')) { 875 | found = true; 876 | offset = stringData.indexOf("android:name=") + 14; 877 | endOffset = stringData.indexOf('"', offset+1); 878 | var name = stringData.substring(offset, endOffset); 879 | if (name.length && defaultActivity !== name) { 880 | defaultActivity = name; 881 | console.log("Using custom activity:", name); 882 | } 883 | } 884 | 885 | start = offset+1; 886 | } 887 | } while (offset > -1 && !found); 888 | } 889 | 890 | function handleKeyStrokes(str, key) { 891 | if (str === "r") { 892 | restartApplication(); 893 | } else if (str === "m") { 894 | if (commandLine.testMode === TM_ALWAYS) { 895 | console.log("Setting to be Test mode: Auto"); 896 | commandLine.testMode = TM_AUTO; 897 | } else if (commandLine.testMode === TM_AUTO) { 898 | console.log("Setting to be Test mode: Never"); 899 | commandLine.testMode = TM_NEVER; 900 | } else if (commandLine.testMode === TM_NEVER) { 901 | console.log("Setting to be Test mode: Always"); 902 | commandLine.testMode = TM_ALWAYS; 903 | } 904 | } else if (str === "t") { 905 | setAppMode(CM_TESTMODE, true); 906 | restartApplication(); 907 | } else if (str === "n") { 908 | setAppMode(CM_NORMAL, true); 909 | restartApplication(); 910 | } else if (str === "a") { 911 | startAppium(); 912 | } else if (str === "d") { 913 | // Start Debugger 914 | } else if (str === "q") { 915 | process.exit(); 916 | } else if (key.ctrl === true && key.name === 'c') { 917 | process.exit(); 918 | } 919 | 920 | } 921 | 922 | function focusApp() { 923 | if (!autoFocus) { return; } 924 | // Focus.js = (new ActiveXObject("WScript.Shell")).AppActivate("Genymotion"); 925 | cp.execSync("cscript //nologo //E:jscript focus.js"); 926 | } 927 | 928 | function startAppium() { 929 | if (fs.existsSync("node_modules\\nativescript-dev-appium")) { 930 | setAppMode(CM_NORMAL, true); 931 | console.log("Starting NativeScript-Appium"); 932 | cp.spawn("node", ["node_modules\\nativescript-dev-appium\\test-runner.js"], {stdio: 'inherit'}); 933 | focusApp(); 934 | } 935 | } --------------------------------------------------------------------------------