Code coverage report for lib/services/project-service.js

Statements: 82.76% (72 / 87)      Branches: 55.88% (19 / 34)      Functions: 100% (9 / 9)      Lines: 82.76% (72 / 87)      Ignored: none     

All files » lib/services/ » project-service.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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119    1 1 1 1 1 1 2 2 2 2 2 2 2 2 2   1 2 2 2     2 2 2 2 2 2 2 2     2 2       2     2 2 2 2 2 2 2 2 2       2 2               2 2           2     1 2 2 2 2 2 2       2   2     1 2 2 2 2 2 2 2 2     2     1 2 2 2     2       2   1   1 1  
///<reference path="../.d.ts"/>
"use strict";
var constants = require("../constants");
var osenv = require("osenv");
var path = require("path");
var shell = require("shelljs");
var ProjectService = (function () {
    function ProjectService($npm, $errors, $fs, $logger, $projectDataService, $projectHelper, $projectNameValidator, $projectTemplatesService, $options) {
        this.$npm = $npm;
        this.$errors = $errors;
        this.$fs = $fs;
        this.$logger = $logger;
        this.$projectDataService = $projectDataService;
        this.$projectHelper = $projectHelper;
        this.$projectNameValidator = $projectNameValidator;
        this.$projectTemplatesService = $projectTemplatesService;
        this.$options = $options;
    }
    ProjectService.prototype.createProject = function (projectName) {
        var _this = this;
        return (function () {
            Iif (!projectName) {
                _this.$errors.fail("You must specify <App name> when creating a new project.");
            }
            _this.$projectNameValidator.validate(projectName);
            var projectId = _this.$options.appid || _this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX);
            var projectDir = path.join(path.resolve(_this.$options.path || "."), projectName);
            _this.$fs.createDirectory(projectDir).wait();
            var customAppPath = _this.getCustomAppPath();
            Eif (customAppPath) {
                customAppPath = path.resolve(customAppPath);
                Iif (!_this.$fs.exists(customAppPath).wait()) {
                    _this.$errors.failWithoutHelp("The specified path \"" + customAppPath + "\" doesn't exist. Check that you specified the path correctly and try again.");
                }
                var customAppContents = _this.$fs.enumerateFilesInDirectorySync(customAppPath);
                Iif (customAppContents.length === 0) {
                    _this.$errors.failWithoutHelp("The specified path \"" + customAppPath + "\" is empty directory.");
                }
            }
            Iif (_this.$fs.exists(projectDir).wait() && !_this.$fs.isEmptyDir(projectDir).wait()) {
                _this.$errors.fail("Path already exists and is not empty %s", projectDir);
            }
            _this.$logger.trace("Creating a new NativeScript project with name %s and id %s at location %s", projectName, projectId, projectDir);
            var appDirectory = path.join(projectDir, constants.APP_FOLDER_NAME);
            var appPath = null;
            Eif (customAppPath) {
                _this.$logger.trace("Using custom app from %s", customAppPath);
                var relativePathFromSourceToTarget = path.relative(customAppPath, appDirectory);
                Eif (relativePathFromSourceToTarget !== appDirectory) {
                    var doesRelativePathGoUpAtLeastOneDir = relativePathFromSourceToTarget.split(path.sep)[0] === "..";
                    Iif (!doesRelativePathGoUpAtLeastOneDir) {
                        _this.$errors.fail("Project dir %s must not be created at/inside the template used to create the project %s.", projectDir, customAppPath);
                    }
                }
                _this.$logger.trace("Copying custom app into %s", appDirectory);
                appPath = customAppPath;
            }
            else {
                _this.$logger.trace("Using NativeScript hello world application");
                var defaultTemplatePath = _this.$projectTemplatesService.defaultTemplatePath.wait();
                _this.$logger.trace("Copying NativeScript hello world application into %s", appDirectory);
                appPath = defaultTemplatePath;
            }
            try {
                _this.createProjectCore(projectDir, appPath, projectId).wait();
            }
            catch (err) {
                _this.$fs.deleteDirectory(projectDir).wait();
                throw err;
            }
            _this.$logger.out("Project %s was successfully created", projectName);
        }).future()();
    };
    ProjectService.prototype.createProjectCore = function (projectDir, appSourcePath, projectId) {
        var _this = this;
        return (function () {
            _this.$fs.ensureDirectoryExists(projectDir).wait();
            var appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME);
            _this.$fs.createDirectory(appDestinationPath).wait();
            Iif (_this.$options.symlink) {
                _this.$fs.symlink(appSourcePath, appDestinationPath).wait();
            }
            else {
                shell.cp('-R', path.join(appSourcePath, "*"), appDestinationPath);
            }
            _this.createBasicProjectStructure(projectDir, projectId).wait();
        }).future()();
    };
    ProjectService.prototype.createBasicProjectStructure = function (projectDir, projectId) {
        var _this = this;
        return (function () {
            _this.$fs.createDirectory(path.join(projectDir, "platforms")).wait();
            _this.$projectDataService.initialize(projectDir);
            _this.$projectDataService.setValue("id", projectId).wait();
            var tnsModulesVersion = _this.$options.tnsModulesVersion;
            var packageName = constants.TNS_CORE_MODULES_NAME;
            Iif (tnsModulesVersion) {
                packageName = packageName + "@" + tnsModulesVersion;
            }
            _this.$npm.executeNpmCommand("npm install " + packageName + " --save --save-exact", projectDir).wait();
        }).future()();
    };
    ProjectService.prototype.getCustomAppPath = function () {
        var customAppPath = this.$options.copyFrom || this.$options.linkTo;
        Eif (customAppPath) {
            Iif (customAppPath.indexOf("http://") === 0) {
                this.$errors.fail("Only local paths for custom app are supported.");
            }
            Iif (customAppPath.substr(0, 1) === '~') {
                customAppPath = path.join(osenv.home(), customAppPath.substr(1));
            }
        }
        return customAppPath;
    };
    return ProjectService;
})();
exports.ProjectService = ProjectService;
$injector.register("projectService", ProjectService);