UNPKG

5.72 kBJavaScriptView Raw
1/**
2 * Main namespace for the windowslib.
3 *
4 * @copyright
5 * Copyright (c) 2014-2016 by Appcelerator, Inc. All Rights Reserved.
6 *
7 * @license
8 * Licensed under the terms of the Apache Public License.
9 * Please see the LICENSE included with this distribution for details.
10 */
11
12const
13 appc = require('node-appc'),
14 async = require('async'),
15 EventEmitter = require('events').EventEmitter,
16 magik = require('./lib/utilities').magik,
17 mix = require('./lib/utilities').mix,
18 __ = appc.i18n(__dirname).__,
19
20 packageJson = require('./package.json'),
21
22 assemblies = exports.assemblies = require('./lib/assemblies'),
23 device = exports.device = require('./lib/device'),
24 emulator = exports.emulator = require('./lib/emulator'),
25 env = exports.env = require('./lib/env'),
26 windowsphone = exports.windowsphone = require('./lib/windowsphone'),
27 wptool = exports.wptool = require('./lib/wptool'),
28 winstore = exports.winstore = require('./lib/winstore'),
29 visualstudio = exports.visualstudio = require('./lib/visualstudio');
30
31var cache;
32
33exports.certs = require('./lib/certs');
34exports.detect = detect;
35exports.install = install;
36exports.LogRelay = require('./lib/logrelay');
37exports.process = require('./lib/process');
38exports.version = packageJson.version;
39
40/**
41 * Detects the entire Windows phone environment information.
42 *
43 * @param {Object} [options] - An object containing various settings.
44 * @param {Boolean} [options.bypassCache=false] - When true, re-detects the all Windows phone information.
45 * @param {Boolean} [options.skipWpTool=false] - When true, skips detection of Windows phone.
46 * @param {String} [options.assemblyPath=%WINDIR%\Microsoft.NET\assembly\GAC_MSIL] - Path to .NET global assembly cache.
47 * @param {String} [options.powershell] - Path to the <code>powershell</code> executable.
48 * @param {String} [options.preferredWindowsPhoneSDK] - The preferred version of the Windows Phone SDK to use by default. Example "8.0".
49 * @param {String} [options.preferredVisualStudio] - The preferred version of Visual Studio to use by default. Example: "13".
50 * @param {Object} [options.requiredAssemblies] - An object containing assemblies to check for in addition to the required windowslib dependencies.
51 * @param {String} [options.supportedMSBuildVersions] - A string with a version number or range to check if a MSBuild version is supported.
52 * @param {String} [options.supportedVisualStudioVersions] - A string with a version number or range to check if a Visual Studio install is supported.
53 * @param {Function} [callback(err, info)] - A function to call when all detection tasks have completed.
54 *
55 * @returns {EventEmitter}
56 */
57function detect(options, callback) {
58 return magik(options, callback, function (emitter, options, callback) {
59 if (cache && !options.bypassCache) {
60 emitter.emit('detected', cache);
61 return callback(null, cache);
62 }
63
64 var results = {
65 detectVersion: '3.0',
66 issues: []
67 },
68 libs = [
69 env,
70 visualstudio,
71 windowsphone,
72 assemblies,
73 winstore
74 ];
75
76 if (!options.skipWpTool) {
77 libs.push(wptool);
78 }
79 async.each(libs, function (lib, next) {
80 lib.detect(options, function (err, result) {
81 err || mix(result, results);
82 next(err);
83 });
84 }, function (err) {
85 if (err) {
86 emitter.emit('error', err);
87 callback(err);
88 } else {
89 cache = results;
90 emitter.emit('detected', results);
91 callback(null, results);
92 }
93 });
94 });
95}
96
97/**
98 * Installs the specified app to an Windows Phone emulator. If the emulator is not running, it will launch it.
99 *
100 * @param {String} udid - The UDID of the emulator to install the app to or null if you want windowslib to pick one.
101 * @param {String} appPath - The path to the Windows Phone app to install.
102 * @param {Object} [options] - An object containing various settings.
103 * @param {Boolean} [options.bypassCache=false] - When true, re-detects the environment configuration.
104 * @param {Number} [options.timeout] - Number of milliseconds to wait before timing out.
105 * @param {Function} [callback(err)] - A function to call when the simulator has launched.
106 *
107 * @emits module:windowslib#error
108 * @emits module:windowslib#installed
109 * @emits module:windowslib#launched
110 *
111 * @returns {EventEmitter}
112 */
113function install(udid, appPath, options, callback) {
114 return magik(options, callback, function (emitter, options, callback) {
115 detect(options, function (err, results) {
116 if (err) {
117 emitter.emit('error', err);
118 return callback(err);
119 }
120 var type;
121
122 // determine if this is a device or emulator udid
123 if (results.devices.some(function (d) { return d.udid === udid; })) {
124 // it's a device!
125 type = device;
126 } else {
127 Object.keys(results.emulators).some(function (wpsdk) {
128 return results.emulators[wpsdk].some(function (e) {
129 if (e.udid === udid) {
130 type = emulator;
131 return true;
132 }
133 });
134 });
135 }
136
137 if (!type) {
138 // oh no
139 var ex = new Error(__('Invalid device id: %s', udid));
140 emitter.emit('error', ex);
141 return callback(ex);
142 }
143
144 var installEmitter = type.install(udid, appPath, options, callback),
145 originalEmitter = installEmitter.emit;
146
147 // make sure we have at least one 'error' handler to keep longjohn from complaining
148 installEmitter.on('error', function () {});
149
150 installEmitter.emit = function () {
151 originalEmitter.apply(installEmitter, arguments);
152 emitter.emit.apply(emitter, arguments);
153 };
154 });
155 });
156}