All files / lib command-remove.js

100% Statements 28/28
62.5% Branches 5/8
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          1x 1x 1x 1x   1x 1x 1x 1x 1x     1x 1x 1x 1x               1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x           1x 1x     1x    
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
"use strict";
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const inquirer = require('inquirer')
 
const projectInfoManager = require('./project-info-manager.js')
const awsmobilejsConstant = require('./utils/awsmobilejs-constant.js')
const pathManager = require('./utils/awsmobilejs-path-manager.js')
const gitManager = require('./utils/git-manager.js')
const mobileExportJSFileManager = require('./aws-operations/mobile-exportjs-file-manager.js')
 
function removeAWSMobileJS() {
    let projectInfo = projectInfoManager.getProjectInfo()
    Eif(projectInfo){
        console.log(chalk.bgYellow.bold('Warning:') + ' This will remove the awsmobilejs folder and all the backup folders created by awsmobile-cli')
        inquirer.prompt([
            {
                type: 'confirm',
                name: 'ok',
                message: 'Remove awsmobilejs features from the current project?',
                default: false
            }
        ]).then(function (answers) {
            Eif(answers.ok){
                mobileExportJSFileManager.onClearBackend(projectInfo)
                gitManager.removeAwsmobilejs(projectInfo.ProjectPath)
                removeBackupAWSMobileJSDirs(projectInfo.ProjectPath)
                removeAWSMobileJSDir(projectInfo.ProjectPath)
                console.log('awsmobilejs is removed from the current project')
            }
        })
    }
}
 
function removeBackupAWSMobileJSDirs(projectPath){
    let dirs = fs.readdirSync(projectPath)
    let reg = new RegExp('^' + awsmobilejsConstant.AWSMobileJSBackUpDirName)
	for(let i = 0; i < dirs.length; i++) {
		let stat = fs.lstatSync(path.join(projectPath, dirs[i]))
		Eif(stat.isDirectory() && reg.test(dirs[i])) {
            fs.removeSync(dirs[i])
        }
    }
}
 
function removeAWSMobileJSDir(projectPath){
    let awsmobilejsPath = pathManager.getAWSMobileJSDirPath(projectPath)
    fs.removeSync(awsmobilejsPath)
}
 
module.exports = {
    removeAWSMobileJS
}