Code coverage report for lib/project-data.js

Statements: 93.65% (59 / 63)      Branches: 77.78% (14 / 18)      Functions: 100% (10 / 10)      Lines: 93.65% (59 / 63)      Ignored: none     

All files » lib/ » project-data.js
1 2 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    1 1 1 1 27 27 27 27 27 27 27   1 27 27 27 27 24 24 24 24 24 24 24               24 24 24               3       1 1   1 3 3 3 3 3 2     1       1 2 2 2 2 2 2 2 2 2 2           2 2     1 26 26 26 26   1 1   1 1  
///<reference path=".d.ts"/>
"use strict";
var path = require("path");
var os = require("os");
var ProjectData = (function () {
    function ProjectData($fs, $errors, $logger, $projectHelper, $staticConfig, $options) {
        this.$fs = $fs;
        this.$errors = $errors;
        this.$logger = $logger;
        this.$projectHelper = $projectHelper;
        this.$staticConfig = $staticConfig;
        this.$options = $options;
        this.initializeProjectData().wait();
    }
    ProjectData.prototype.initializeProjectData = function () {
        var _this = this;
        return (function () {
            var projectDir = _this.$projectHelper.projectDir;
            if (projectDir) {
                _this.initializeProjectDataCore(projectDir);
                var data = null;
                Eif (_this.$fs.exists(_this.projectFilePath).wait()) {
                    var fileContent = null;
                    try {
                        fileContent = _this.$fs.readJson(_this.projectFilePath).wait();
                        data = fileContent[_this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
                    }
                    catch (err) {
                        _this.$errors.fail({ formatStr: "The project file %s is corrupted." + os.EOL +
                                "Consider restoring an earlier version from your source control or backup." + os.EOL +
                                "Additional technical info: %s",
                            suppressCommandHelp: true }, _this.projectFilePath, err.toString());
                    }
                    Eif (data) {
                        _this.projectId = data.id;
                        _this.dependencies = fileContent.dependencies;
                    }
                    else {
                        _this.tryToUpgradeProject().wait();
                    }
                }
            }
            else {
                _this.tryToUpgradeProject().wait();
            }
        }).future()();
    };
    ProjectData.prototype.throwNoProjectFoundError = function () {
        this.$errors.fail("No project found at or above '%s' and neither was a --path specified.", this.$options.path || path.resolve("."));
    };
    ProjectData.prototype.tryToUpgradeProject = function () {
        var _this = this;
        return (function () {
            var projectDir = _this.projectDir || path.resolve(_this.$options.path || ".");
            var oldProjectFilePath = path.join(projectDir, ProjectData.OLD_PROJECT_FILE_NAME);
            if (_this.$fs.exists(oldProjectFilePath).wait()) {
                _this.upgrade(projectDir, oldProjectFilePath).wait();
            }
            else {
                _this.throwNoProjectFoundError();
            }
        }).future()();
    };
    ProjectData.prototype.upgrade = function (projectDir, oldProjectFilePath) {
        var _this = this;
        return (function () {
            try {
                var oldProjectData = _this.$fs.readJson(oldProjectFilePath).wait();
                var newProjectFilePath = _this.projectFilePath || path.join(projectDir, _this.$staticConfig.PROJECT_FILE_NAME);
                var newProjectData = _this.$fs.exists(newProjectFilePath).wait() ? _this.$fs.readJson(newProjectFilePath).wait() : {};
                newProjectData[_this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = oldProjectData;
                _this.$fs.writeJson(newProjectFilePath, newProjectData).wait();
                _this.projectId = newProjectData[_this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE].id;
                _this.$fs.deleteFile(oldProjectFilePath).wait();
            }
            catch (err) {
                _this.$logger.out("An error occurred while upgrading your project.");
                throw err;
            }
            _this.initializeProjectDataCore(projectDir);
            _this.$logger.out("Successfully upgraded your project file.");
        }).future()();
    };
    ProjectData.prototype.initializeProjectDataCore = function (projectDir) {
        this.projectDir = projectDir;
        this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
        this.platformsDir = path.join(projectDir, "platforms");
        this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
    };
    ProjectData.OLD_PROJECT_FILE_NAME = ".tnsproject";
    return ProjectData;
})();
exports.ProjectData = ProjectData;
$injector.register("projectData", ProjectData);