# All files / src/deployTools now.js

22.22% Statements 16/72
0% Branches 0/26
0% Functions 0/14
24.44% Lines 11/45

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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    862x
    2x
    2x
    2x
    2x

    2x
    2x

    2x

    2x

    2x

    2x

     import chalk from 'chalk';
    import path from 'path';
    import fs from 'fs';
    import inquirer from 'inquirer';
    import dotenv from 'dotenv';

    import { executeAsync } from '../systemTools/exec';
    import {
        logInfo,
        getAppFolder
    } from '../common';

    const _runDeploymentTask = (c, nowConfigPath) => new Promise((resolve, reject) => {
        dotenv.config();
        const defaultBuildFolder = path.join(getAppFolder(c, 'web'), 'public');
        const params = [defaultBuildFolder, '-A', nowConfigPath];
        if (process.env.NOW_TOKEN) params.push('-t', process.env.NOW_TOKEN);
        executeAsync(c, `now ${params.join(' ')}`)
            .then(() => resolve())
            .catch(error => reject(error));
    });

    const _createConfigFiles = async (configFilePath, envConfigPath, nowParamsExists = false, envContent = '') => {
        if (!fs.existsSync(configFilePath)) {
            const content = { public: true, version: 2 };
            logInfo(`${chalk.white('now.json')} file does not exist. Creating one for you`);

            const { name } = await inquirer.prompt([{
                type: 'input',
                name: 'name',
                message: 'What is your project name?',
                validate: i => !!i || 'Please enter a name'
            }, {
                type: 'input',
                name: 'token',
                message: 'Do you have now token? If no leave empty and you will be asked to create one'
            }]);

            content.name = name;

            if (!nowParamsExists) {
                const { token } = await inquirer.prompt({
                    type: 'input',
                    name: 'token',
                    message: 'Do you have now token? If no leave empty and you will be asked to create one'
                });
                if (token) {
                    envContent += `NOW_TOKEN=${token}\n`;
                    fs.writeFileSync(envConfigPath, envContent);
                }
                return fs.writeFileSync(configFilePath, JSON.stringify(content, null, 2));
            }
            return fs.writeFileSync(configFilePath, JSON.stringify(content, null, 2));
        }
    };

    const deployToNow = c => new Promise((resolve, reject) => {
        const nowConfigPath = path.resolve(c.paths.project.dir, 'now.json');
        const envConfigPath = path.resolve(c.paths.project.dir, '.env');

        let envContent;
        try {
            envContent = fs.readFileSync(envConfigPath).toString();
        } catch (err) {
            envContent = '';
        }

        let matched = false;
        envContent.split('\n').map(line => line.split('=')).forEach(([key]) => {
            if (['NOW_TOKEN'].indexOf(key) > -1) {
                matched = true;
            }
        });

        _createConfigFiles(nowConfigPath, envConfigPath, matched, envContent)
            .then(() => {
                _runDeploymentTask(c, nowConfigPath)
                    .then(() => {
                        resolve();
                    })
                    .catch(err => reject(err));
            });
    });

    export { deployToNow };