/ script / config.js
config.js
  1  // This module exports paths, names, and other metadata that is referenced
  2  // throughout the build.
  3  
  4  'use strict';
  5  
  6  const fs = require('fs');
  7  const path = require('path');
  8  const spawnSync = require('./lib/spawn-sync');
  9  
 10  const repositoryRootPath = path.resolve(__dirname, '..');
 11  const apmRootPath = path.join(repositoryRootPath, 'apm');
 12  const scriptRootPath = path.join(repositoryRootPath, 'script');
 13  const buildOutputPath = path.join(repositoryRootPath, 'out');
 14  const docsOutputPath = path.join(repositoryRootPath, 'docs', 'output');
 15  const intermediateAppPath = path.join(buildOutputPath, 'app');
 16  const symbolsPath = path.join(buildOutputPath, 'symbols');
 17  const electronDownloadPath = path.join(repositoryRootPath, 'electron');
 18  const homeDirPath = process.env.HOME || process.env.USERPROFILE;
 19  const atomHomeDirPath =
 20    process.env.ATOM_HOME || path.join(homeDirPath, '.atom');
 21  
 22  const appMetadata = require(path.join(repositoryRootPath, 'package.json'));
 23  const apmMetadata = require(path.join(apmRootPath, 'package.json'));
 24  const computedAppVersion = computeAppVersion(
 25    process.env.ATOM_RELEASE_VERSION || appMetadata.version
 26  );
 27  const channel = getChannel(computedAppVersion);
 28  const appName = getAppName(channel);
 29  const executableName = getExecutableName(channel, appName);
 30  const channelName = getChannelName(channel);
 31  
 32  module.exports = {
 33    appMetadata,
 34    apmMetadata,
 35    channel,
 36    channelName,
 37    appName,
 38    executableName,
 39    computedAppVersion,
 40    repositoryRootPath,
 41    apmRootPath,
 42    scriptRootPath,
 43    buildOutputPath,
 44    docsOutputPath,
 45    intermediateAppPath,
 46    symbolsPath,
 47    electronDownloadPath,
 48    atomHomeDirPath,
 49    homeDirPath,
 50    getApmBinPath,
 51    getNpmBinPath,
 52    snapshotAuxiliaryData: {}
 53  };
 54  
 55  function getChannelName(channel) {
 56    return channel === 'stable' ? 'atom' : `atom-${channel}`;
 57  }
 58  
 59  function getChannel(version) {
 60    const match = version.match(/\d+\.\d+\.\d+(-([a-z]+)(\d+|-\w{4,})?)?$/);
 61    if (!match) {
 62      throw new Error(`Found incorrectly formatted Atom version ${version}`);
 63    } else if (match[2]) {
 64      return match[2];
 65    }
 66  
 67    return 'stable';
 68  }
 69  
 70  function getAppName(channel) {
 71    return channel === 'stable'
 72      ? 'Atom'
 73      : `Atom ${process.env.ATOM_CHANNEL_DISPLAY_NAME ||
 74          channel.charAt(0).toUpperCase() + channel.slice(1)}`;
 75  }
 76  
 77  function getExecutableName(channel, appName) {
 78    if (process.platform === 'darwin') {
 79      return appName;
 80    } else if (process.platform === 'win32') {
 81      return channel === 'stable' ? 'atom.exe' : `atom-${channel}.exe`;
 82    } else {
 83      return 'atom';
 84    }
 85  }
 86  
 87  function computeAppVersion(version) {
 88    if (version.match(/-dev$/)) {
 89      const result = spawnSync('git', ['rev-parse', '--short', 'HEAD'], {
 90        cwd: repositoryRootPath
 91      });
 92      const commitHash = result.stdout.toString().trim();
 93      version += '-' + commitHash;
 94    }
 95    return version;
 96  }
 97  
 98  function getApmBinPath() {
 99    const apmBinName = process.platform === 'win32' ? 'apm.cmd' : 'apm';
100    return path.join(
101      apmRootPath,
102      'node_modules',
103      'atom-package-manager',
104      'bin',
105      apmBinName
106    );
107  }
108  
109  function getNpmBinPath(external = false) {
110    if (process.env.NPM_BIN_PATH) return process.env.NPM_BIN_PATH;
111  
112    const npmBinName = process.platform === 'win32' ? 'npm.cmd' : 'npm';
113    const localNpmBinPath = path.resolve(
114      repositoryRootPath,
115      'script',
116      'node_modules',
117      '.bin',
118      npmBinName
119    );
120    return !external && fs.existsSync(localNpmBinPath)
121      ? localNpmBinPath
122      : npmBinName;
123  }