UNPKG

3.82 kBJavaScriptView Raw
1/**
2 * Detects general Windows environment information such as PowerShell permissions.
3 *
4 * @module env
5 *
6 * @copyright
7 * Copyright (c) 2014 by Appcelerator, Inc. All Rights Reserved.
8 *
9 * @license
10 * Licensed under the terms of the Apache Public License.
11 * Please see the LICENSE included with this distribution for details.
12 */
13
14const
15 appc = require('node-appc'),
16 async = require('async'),
17 fs = require('fs'),
18 magik = require('./utilities').magik,
19 os = require('os'),
20 path = require('path'),
21 __ = appc.i18n(__dirname).__;
22
23var cache;
24
25exports.detect = detect;
26
27/**
28 * Detects general Windows environment information such as PowerShell permissions.
29 *
30 * @param {Object} [options] - An object containing various settings.
31 * @param {Boolean} [options.bypassCache=false] - When true, re-detects Windows environment information.
32 * @param {String} [options.powershell] - Path to the <code>powershell</code> executable.
33 * @param {Function} [callback(err, results)] - A function to call with the Windows environment information.
34 *
35 * @emits module:env#detected
36 * @emits module:env#error
37 *
38 * @returns {EventEmitter}
39 */
40function detect(options, callback) {
41 return magik(options, callback, function (emitter, options, callback) {
42 if (cache && !options.bypassCache) {
43 emitter.emit('detected', cache);
44 return callback(null, cache);
45 }
46
47 var results = cache = {
48 os: {
49 name: 'Windows',
50 version: os.release()
51 },
52 powershell: {
53 enabled: null
54 },
55 issues: []
56 };
57
58 if (appc.version.lt(os.release(), '6.2.0')) {
59 results.issues.push({
60 id: 'WINDOWS_STORE_APPS_NOT_SUPPORTED',
61 type: 'warning',
62 message: __('Windows Store apps are not supported on this version of Windows.') + '\n' +
63 __('You must use Windows 8 or newer to create Windows Store apps.')
64 });
65 emitter.emit('detected', results);
66 return callback(null, results);
67 }
68
69 async.series([
70 function osInfo(next) {
71 appc.subprocess.run('wmic', ['os', 'get', 'Caption,Version'], function (code, out, err) {
72 if (code) return next(code);
73
74 var s = out.split('\n')[1].split(/ {2,}/);
75 s.length > 0 && (results.os.name = s[0].trim());
76 s.length > 1 && (results.os.version = s[1].trim());
77
78 next();
79 });
80 },
81
82 function powershell(next) {
83 if (!results.os.version) return next();
84
85 appc.subprocess.getRealName(path.resolve(__dirname, '..', 'bin', 'test_permissions.ps1'), function (err, psScript) {
86 if (err) {
87 return next(err);
88 }
89
90 appc.subprocess.run(options.powershell || 'powershell', [
91 '-ExecutionPolicy', 'Bypass', '-NoLogo', '-NonInteractive', '-NoProfile', '-File', psScript
92 ], function (code, out, err) {
93 if (!code && /success/i.test(out.trim().split('\n').shift())) {
94 results.powershell.enabled = true;
95 } else {
96 results.powershell.enabled = false;
97 results.issues.push({
98 id: 'WINDOWS_POWERSHELL_SCRIPTS_DISABLED',
99 type: 'error',
100 message: __('Executing PowerShell scripts is disabled.') + '\n' +
101 __('In order to build Windows Hybrid apps for the Windows Store (winstore), you must change the execution policy to allow PowerShell scripts.') + '\n' +
102 __('To enable PowerShell scripts, search __PowerShell__ in the __Start__ menu, right click the icon, select __Run as administrator__, then run:') + '\n' +
103 ' __Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser__'
104 });
105 }
106 next();
107 });
108 });
109 }
110 ], function (err) {
111 if (err) {
112 emitter.emit('error', err);
113 callback(err);
114 } else {
115 emitter.emit('detected', results);
116 callback(null, results);
117 }
118 });
119 });
120};