<commit_before>﻿var validations = require('./common/validations'),
    fs = require('fs'),
    path = require('path'),
    exec = require('child_process').exec,
    log = require('loglevel');

var runApp = function (platform, callback) {
  if (!validations.platformToRunValid(platform)) {
    return (callback(new Error('Invalid platform specified.')));
  }
  
  if (platform.toUpperCase() === "WINDOWS") {
    if (!isWindows()) {
      return callback(new Error('Visual Studio projects can only be executed in Windows environments.'));
    }

    var windowsManifest = "appxmanifest.xml";
    searchFile(process.cwd(), windowsManifest, function (err, results) {
      if (err) {
        log.debug(err);
        return callback(new Error('Could not find the Windows app manifest.'));
      }
      
      getWindowsVersion(function (error, version) {
        if (err) {
          return callback(err);
        }

        if (results && results.length > 0 && /^10/.test(version)) {
          // If there is a windows app manifest and the OS is Windows 10, install the windows 10 app
          installWindows10App(results[0], function (err) {
            callback(err);
          });
        } else {
          // Run the windows cordova app
          runCordovaApp(platform, function (err) {
            callback(err);
          });
        }
      });
    });
  } else {
    // Run the non-windows cordova app
    runCordovaApp(platform, function (err) {
      callback(err);
    });
  }
};

var installWindows10App = function (manifestPath, callback) {
  log.info('Installing Windows 10 app...');
  var cmdLine = 'powershell Add-AppxPackage -Register ' + manifestPath;
  log.debug('    ' + cmdLine);
  exec(cmdLine, function (err, stdout, stderr) {
    log.debug(stdout);
    if (err) {
      log.debug(err);
      return callback(new Error('Failed to install the Windows 10 app.'));
    } else if (stderr.length) {
      log.error(stderr.trim());
    }
    
    callback();
  });
}

var runCordovaApp = function (platform, callback) {
  log.info('Running cordova app for ' + platform + ' platform...');
  
  var cordovaDir = path.join(process.cwd(), 'cordova');
  try {
    process.chdir(cordovaDir);
  }
  catch (err) {
    log.error('ERROR: Failed to change the working directory to ' + cordovaDir);
    log.debug(err);
    return callback(new Error('Failed to run the Cordova app.'));
  }
  
  // path to cordova shell command
  var cordovaPath = path.resolve(__dirname, '..', 'node_modules', 'cordova', 'bin', 'cordova');
  
  var cmdLine = cordovaPath + ' run ' + platform;
  log.debug('    ' + cmdLine);
  exec(cmdLine, function (err, stdout, stderr) {
    log.debug(stdout);
    if (err) {
      log.debug(err);
      return callback(new Error('Failed to run the app for ' + platform + ' platform.'));
    } else if (stderr.length) {
      log.error(stderr.trim());
    }
    
    callback();
  });
}

var isWindows = function () {
  return isWindows = /^win/.test(process.platform);
}

var getWindowsVersion = function (callback) {
  var cmdLine = 'powershell (Get-WmiObject win32_operatingsystem).version';
  log.debug('    ' + cmdLine);
  exec(cmdLine, function (err, stdout, stderr) {
    log.debug(stdout);
    if (err) {
      log.debug(err);
      return callback(new Error('Failed to run the app for ' + platform + ' platform.'));
    } else if (stderr.length) {
      log.error(stderr.trim());
    }
    
    callback(undefined, stdout.trim());
  });
}

var searchFile = function (dir, fileName, done) {
  var results = [];
  fs.readdir(dir, function (err, list) {
    if (err) {
      return done(err);
    }

    var pending = list.length;
    if (!pending) {
      return done(null, results);
    }

    list.forEach(function (file) {
      file = path.resolve(dir, file);
      fs.stat(file, function (err, stat) {
        if (stat && stat.isDirectory()) {
          searchFile(file, fileName, function (err, res) {
            results = results.concat(res);
            if (!--pending){
              done(null, results);
            }
          });
        } else {
          if (path.basename(file) === fileName) {
            results.push(file);
          }

          if (!--pending) {
            done(null, results);
          }
        }
      });
    });
  });
};

var openVisualStudioSolution = function (callback) {
  if (!isWindows()) {
    return callback(new Error('Visual Studio solutions can only be opened in Windows environments.'));
  }

  var solutionFileName = 'CordovaApp.sln';
  searchFile(process.cwd(), solutionFileName, function (err, results) {
    if (err) {
      log.debug(err);
      return callback(new Error('Could not find the Visual Studio solution "' + solutionFileName + '"'));
    }

    if (!results || results.length === 0) {
      return callback(new Error('Could not find the Visual Studio solution "' + solutionFileName + '"'));
    }

    var solutionFilePath = results[0];

    log.info('Opening the Visual Studio solution "' + solutionFilePath + '"...');

    var cmdLine = 'start ' + solutionFilePath;
    exec(cmdLine, function (err, stdout, stderr) {
      log.debug(stdout);
      if (err) {
        log.debug(err);
        return callback(new Error('Failed to open the Visual Studio solution "' + solutionFilePath + '".'));
      } else if (stderr.length) {
        log.error(stderr.trim());
      }

      callback();
    });
  });
};

module.exports = {
  runApp: runApp,
  openVisualStudioSolution: openVisualStudioSolution
};
<commit_msg>Minor change in CLI log messages<commit_after>﻿var validations = require('./common/validations'),
    fs = require('fs'),
    path = require('path'),
    exec = require('child_process').exec,
    log = require('loglevel');

var runApp = function (platform, callback) {
  if (!validations.platformToRunValid(platform)) {
    return (callback(new Error('Invalid platform specified.')));
  }
  
  if (platform.toUpperCase() === "WINDOWS") {
    if (!isWindows()) {
      return callback(new Error('Visual Studio projects can only be executed in Windows environments.'));
    }

    var windowsManifest = "appxmanifest.xml";
    searchFile(process.cwd(), windowsManifest, function (err, results) {
      if (err) {
        log.debug(err);
        return callback(new Error('Could not find the Windows app manifest.'));
      }
      
      getWindowsVersion(function (error, version) {
        if (err) {
          return callback(err);
        }

        if (results && results.length > 0 && /^10/.test(version)) {
          // If there is a windows app manifest and the OS is Windows 10, install the windows 10 app
          installWindows10App(results[0], function (err) {
            callback(err);
          });
        } else {
          // Run the windows cordova app
          runCordovaApp(platform, function (err) {
            callback(err);
          });
        }
      });
    });
  } else {
    // Run the non-windows cordova app
    runCordovaApp(platform, function (err) {
      callback(err);
    });
  }
};

var installWindows10App = function (manifestPath, callback) {
  log.info('Installing Windows 10 app...');
  var cmdLine = 'powershell Add-AppxPackage -Register ' + manifestPath;
  log.debug('    ' + cmdLine);
  exec(cmdLine, function (err, stdout, stderr) {
    log.debug(stdout);
    if (err) {
      log.debug(err);
      return callback(new Error('Failed to install the Windows 10 app.'));
    } else if (stderr.length) {
      log.error(stderr.trim());
    }
    
    callback();
  });
}

var runCordovaApp = function (platform, callback) {
  log.info('Running cordova app for ' + platform + ' platform...');
  
  var cordovaDir = path.join(process.cwd(), 'cordova');
  try {
    process.chdir(cordovaDir);
  }
  catch (err) {
    log.error('ERROR: Failed to change the working directory to ' + cordovaDir);
    log.debug(err);
    return callback(new Error('Failed to run the Cordova app.'));
  }
  
  // path to cordova shell command
  var cordovaPath = path.resolve(__dirname, '..', 'node_modules', 'cordova', 'bin', 'cordova');
  
  var cmdLine = cordovaPath + ' run ' + platform;
  log.debug('    ' + cmdLine);
  exec(cmdLine, function (err, stdout, stderr) {
    log.debug(stdout);
    if (err) {
      log.debug(err);
      return callback(new Error('Failed to run the app for ' + platform + ' platform.'));
    } else if (stderr.length) {
      log.error(stderr.trim());
    }
    
    callback();
  });
}

var isWindows = function () {
  return isWindows = /^win/.test(process.platform);
}

var getWindowsVersion = function (callback) {
  log.info('Obtaining Windows version...');
  var cmdLine = 'powershell (Get-WmiObject win32_operatingsystem).version';
  log.debug('    ' + cmdLine);
  exec(cmdLine, function (err, stdout, stderr) {
    log.debug(stdout);
    if (err) {
      log.debug(err);
      return callback(new Error('Failed to run the app for ' + platform + ' platform.'));
    } else if (stderr.length) {
      log.error(stderr.trim());
    }
    
    callback(undefined, stdout.trim());
  });
}

var searchFile = function (dir, fileName, done) {
  var results = [];
  fs.readdir(dir, function (err, list) {
    if (err) {
      return done(err);
    }

    var pending = list.length;
    if (!pending) {
      return done(null, results);
    }

    list.forEach(function (file) {
      file = path.resolve(dir, file);
      fs.stat(file, function (err, stat) {
        if (stat && stat.isDirectory()) {
          searchFile(file, fileName, function (err, res) {
            results = results.concat(res);
            if (!--pending){
              done(null, results);
            }
          });
        } else {
          if (path.basename(file) === fileName) {
            results.push(file);
          }

          if (!--pending) {
            done(null, results);
          }
        }
      });
    });
  });
};

var openVisualStudioSolution = function (callback) {
  if (!isWindows()) {
    return callback(new Error('Visual Studio solutions can only be opened in Windows environments.'));
  }

  var solutionFileName = 'CordovaApp.sln';
  searchFile(process.cwd(), solutionFileName, function (err, results) {
    if (err) {
      log.debug(err);
      return callback(new Error('Could not find the Visual Studio solution "' + solutionFileName + '"'));
    }

    if (!results || results.length === 0) {
      return callback(new Error('Could not find the Visual Studio solution "' + solutionFileName + '"'));
    }

    var solutionFilePath = results[0];

    log.info('Opening the Visual Studio solution "' + solutionFilePath + '"...');

    var cmdLine = 'start ' + solutionFilePath;
    exec(cmdLine, function (err, stdout, stderr) {
      log.debug(stdout);
      if (err) {
        log.debug(err);
        return callback(new Error('Failed to open the Visual Studio solution "' + solutionFilePath + '".'));
      } else if (stderr.length) {
        log.error(stderr.trim());
      }

      callback();
    });
  });
};

module.exports = {
  runApp: runApp,
  openVisualStudioSolution: openVisualStudioSolution
};

{'fraction_non_alphanumeric': 0.1407039553929256, 'fraction_numerical': 0.003484927687750479, 'mean_word_length': 2.289111747851003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6917097', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3288, 'n_words': 862}