summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/tasks/utils.js
blob: ac18bc99806255669816679b15ed900b8df451cc (plain)
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
const childProcess = require('child_process');
const fs = require('fs/promises');
const path = require('path');
const { BUILD_DIR, GEO_DIR, LOCALES_DIR, IMAGES_DIR, ICONS_DIR } = require('./constants');

async function copyAssetsToBuildDirectory() {
  await Promise.all([
    copyToBuildFolder(IMAGES_DIR, 'assets/images'),
    copyToBuildFolder(ICONS_DIR, 'assets/icons'),
    copyToBuildFolder(GEO_DIR, 'assets/geo', '.gl'),
    copyToBuildFolder(LOCALES_DIR, 'locales', '.po'),
  ]);
}

async function copyRecursively(sourcePath, destinationPath, filter) {
  await fs.mkdir(destinationPath, {
    recursive: true,
  });

  const sourceFiles = await fs.readdir(sourcePath, {
    recursive: true,
  });

  for (const sourceFile of sourceFiles) {
    const sourceFileAbsolute = path.join(sourcePath, sourceFile);
    const destinationPathAbsolute = path.join(destinationPath, sourceFile);

    await fs.cp(sourceFileAbsolute, destinationPathAbsolute, {
      filter,
      recursive: true,
    });
  }
}

async function copyToBuildFolder(sourcePath, folderName, extension) {
  const destinationPath = path.join(BUILD_DIR, folderName);
  const copyExtensionFilter = extension ? getCopyExtensionFilter(extension) : undefined;

  await copyRecursively(sourcePath, destinationPath, copyExtensionFilter);
}

function getCopyExtensionFilter(extension) {
  const copyExtensionFilter = (filePath) => {
    const fileExtension = path.extname(filePath);

    return fileExtension === extension;
  };

  return copyExtensionFilter;
}

async function removeRecursively(path) {
  await fs.rm(path, {
    recursive: true,
    force: true,
  });
}

async function runNpmScript(scriptName) {
  const command = `npm run ${scriptName}`;

  try {
    await runCommand(command);
  } catch (errors) {
    if (Array.isArray(errors)) {
      // Remove first error as it will always
      // bubble up and be printed to the console.
      errors.slice(1).forEach((error) => {
        console.error(new Error(error));
      });
    }
    process.exit(1);
  }
}

async function runCommand(command) {
  return new Promise((resolve, reject) => {
    childProcess.exec(command, (error, stdout, stderr) => {
      if (error) {
        return reject([error, stdout, stderr]);
      }

      return resolve([stdout, stderr]);
    });
  });
}

function setNodeEnvironment(environment) {
  process.env.NODE_ENV = environment;
}

exports.copyAssetsToBuildDirectory = copyAssetsToBuildDirectory;
exports.copyRecursively = copyRecursively;
exports.copyToBuildFolder = copyToBuildFolder;
exports.getCopyExtensionFilter = getCopyExtensionFilter;
exports.removeRecursively = removeRecursively;
exports.runCommand = runCommand;
exports.runNpmScript = runNpmScript;
exports.setNodeEnvironment = setNodeEnvironment;