utils.js 3.3 KB
Newer Older
1
2
3
4
5
6
7
8
const execa = require('execa');
const fs = require('fs-extra');
const getPort = require('get-port');
const path = require('path');
const os = require('os');
const stripAnsi = require('strip-ansi');

async function bootstrap({ directory, template }) {
9
  const shouldInstallScripts = process.env.CI && process.env.CI !== 'false';
10
11
12
13
14
  await Promise.all(
    ['public/', 'src/', 'package.json'].map(async file =>
      fs.copy(path.join(template, file), path.join(directory, file))
    )
  );
15
16
17
18
19
20
21
  if (shouldInstallScripts) {
    const packageJson = fs.readJsonSync(path.join(directory, 'package.json'));
    packageJson.dependencies = Object.assign(packageJson.dependencies, {
      'react-scripts': 'latest',
    });
    fs.writeJsonSync(path.join(directory, 'package.json'), packageJson);
  }
22
  await execa('yarnpkg', ['install', '--mutex', 'network'], { cwd: directory });
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  if (!shouldInstallScripts) {
    fs.ensureSymlinkSync(
      path.resolve(
        path.join(
          __dirname,
          '..',
          'packages',
          'react-scripts',
          'bin',
          'react-scripts.js'
        )
      ),
      path.join(directory, 'node_modules', '.bin', 'react-scripts')
    );
    await execa('yarnpkg', ['link', 'react-scripts'], { cwd: directory });
  }
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
}

async function isSuccessfulDevelopment({ directory }) {
  const { stdout, stderr } = await execa(
    './node_modules/.bin/react-scripts',
    ['start', '--smoke-test'],
    {
      cwd: directory,
      env: { BROWSER: 'none', PORT: await getPort() },
    }
  );

  if (!/Compiled successfully/.test(stdout)) {
    throw new Error(`stdout: ${stdout}${os.EOL + os.EOL}stderr: ${stderr}`);
  }
}

async function isSuccessfulProduction({ directory }) {
  const { stdout, stderr } = await execa(
    './node_modules/.bin/react-scripts',
    ['build'],
    {
      cwd: directory,
    }
  );

  if (!/Compiled successfully/.test(stdout)) {
    throw new Error(`stdout: ${stdout}${os.EOL + os.EOL}stderr: ${stderr}`);
  }
}

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
async function getOutputDevelopment({ directory, env = {} }) {
  try {
    const { stdout, stderr } = await execa(
      './node_modules/.bin/react-scripts',
      ['start', '--smoke-test'],
      {
        cwd: directory,
        env: Object.assign(
          {},
          {
            BROWSER: 'none',
            PORT: await getPort(),
            CI: 'false',
            FORCE_COLOR: '0',
          },
          env
        ),
      }
    );
    return { stdout: stripAnsi(stdout), stderr: stripAnsi(stderr) };
  } catch (err) {
    return {
      stdout: '',
      stderr: stripAnsi(
        err.message
          .split(os.EOL)
          .slice(2)
          .join(os.EOL)
      ),
    };
  }
}

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
async function getOutputProduction({ directory, env = {} }) {
  try {
    const { stdout, stderr } = await execa(
      './node_modules/.bin/react-scripts',
      ['build'],
      {
        cwd: directory,
        env: Object.assign({}, { CI: 'false', FORCE_COLOR: '0' }, env),
      }
    );
    return { stdout: stripAnsi(stdout), stderr: stripAnsi(stderr) };
  } catch (err) {
    return {
      stdout: '',
      stderr: stripAnsi(
        err.message
          .split(os.EOL)
          .slice(2)
          .join(os.EOL)
      ),
    };
  }
}

module.exports = {
  bootstrap,
  isSuccessfulDevelopment,
  isSuccessfulProduction,
131
  getOutputDevelopment,
132
133
  getOutputProduction,
};