# All files / src/deployTools now.js

22.58% Statements 14/62
0% Branches 0/18
0% Functions 0/15
20.41% Lines 10/49

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
    794x
    4x
    4x
    4x
    4x

    4x

    4x

    4x

    4x

    4x

     import chalk from 'chalk';
    import path from 'path';
    import fs from 'fs';
    import { executeAsync } from '../systemTools/exec';
    import {
        logInfo,
        getAppFolder
    } from '../common';
    import { askQuestion, generateOptions, finishQuestion } from '../systemTools/prompt';

    const _runDeploymentTask = (c, nowConfigPath) => new Promise((resolve, reject) => {
        require('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 = (configFilePath, envConfigPath, nowParamsExists = false, envContent = '') => new Promise((resolve, reject) => {
        if (!fs.existsSync(configFilePath)) {
            const content = { public: true, version: 2 };
            logInfo(`${chalk.white('now.json')} file does not exist. Creating one for you`);
            askQuestion('What is your project name?')
                .then((v) => {
                    finishQuestion();
                    content.name = v;
                    if (!nowParamsExists) {
                        askQuestion('Do you have now token? If no leave empty and you will be asked to create one')
                            .then((v) => {
                                finishQuestion();
                                if (v) {
                                    envContent += `NOW_TOKEN=${v}\n`;
                                    fs.writeFileSync(envConfigPath, envContent);
                                }
                                fs.writeFileSync(configFilePath, JSON.stringify(content, null, 2));
                                resolve();
                            });
                        return;
                    }
                    fs.writeFileSync(configFilePath, JSON.stringify(content, null, 2));
                    resolve();
                });
            return;
        }
        resolve();
    });

    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 };