packageMetadata.js 5.67 KB
Newer Older
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 100 101 102 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.readPackageJson = readPackageJson;
exports.checkMetadata = checkMetadata;

function _bluebirdLst() {
  const data = require("bluebird-lst");

  _bluebirdLst = function () {
    return data;
  };

  return data;
}

function _builderUtil() {
  const data = require("builder-util");

  _builderUtil = function () {
    return data;
  };

  return data;
}

function _fsExtraP() {
  const data = require("fs-extra-p");

  _fsExtraP = function () {
    return data;
  };

  return data;
}

var path = _interopRequireWildcard(require("path"));

function semver() {
  const data = _interopRequireWildcard(require("semver"));

  semver = function () {
    return data;
  };

  return data;
}

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

const normalizeData = require("normalize-package-data");
/** @internal */


function readPackageJson(_x) {
  return _readPackageJson.apply(this, arguments);
}

function _readPackageJson() {
  _readPackageJson = (0, _bluebirdLst().coroutine)(function* (file) {
    const data = yield (0, _fsExtraP().readJson)(file);
    yield authors(file, data);
    normalizeData(data); // remove not required fields because can be used for remote build

    delete data.scripts;
    delete data.readme;
    return data;
  });
  return _readPackageJson.apply(this, arguments);
}

function authors(_x2, _x3) {
  return _authors.apply(this, arguments);
}
/** @internal */


function _authors() {
  _authors = (0, _bluebirdLst().coroutine)(function* (file, data) {
    if (data.contributors != null) {
      return;
    }

    let authorData;

    try {
      authorData = yield (0, _fsExtraP().readFile)(path.resolve(path.dirname(file), "AUTHORS"), "utf8");
    } catch (ignored) {
      return;
    }

    data.contributors = authorData.split(/\r?\n/g).map(it => it.replace(/^\s*#.*$/, "").trim());
  });
  return _authors.apply(this, arguments);
}

function checkMetadata(metadata, devMetadata, appPackageFile, devAppPackageFile) {
  const errors = [];

  const reportError = missedFieldName => {
    errors.push(`Please specify '${missedFieldName}' in the package.json (${appPackageFile})`);
  };

  const checkNotEmpty = (name, value) => {
    if ((0, _builderUtil().isEmptyOrSpaces)(value)) {
      reportError(name);
    }
  };

  if (metadata.directories != null) {
    errors.push(`"directories" in the root is deprecated, please specify in the "build"`);
  }

  checkNotEmpty("name", metadata.name);

  if ((0, _builderUtil().isEmptyOrSpaces)(metadata.description)) {
    _builderUtil().log.warn({
      appPackageFile
    }, `description is missed in the package.json`);
  }

  if (metadata.author == null) {
    _builderUtil().log.warn({
      appPackageFile
    }, `author is missed in the package.json`);
  }

  checkNotEmpty("version", metadata.version);

  if (metadata != null) {
    checkDependencies(metadata.dependencies, errors);
  }

  if (metadata !== devMetadata) {
    if (metadata.build != null) {
      errors.push(`'build' in the application package.json (${appPackageFile}) is not supported since 3.0 anymore. Please move 'build' into the development package.json (${devAppPackageFile})`);
    }
  }

  const devDependencies = metadata.devDependencies;

  if (devDependencies != null && "electron-rebuild" in devDependencies) {
    _builderUtil().log.info('electron-rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies\n\nTo ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`');
  }

  if (errors.length > 0) {
    throw new (_builderUtil().InvalidConfigurationError)(errors.join("\n"));
  }
}

function versionSatisfies(version, range, loose) {
  if (version == null) {
    return false;
  }

  const coerced = semver().coerce(version);

  if (coerced == null) {
    return false;
  }

  return semver().satisfies(coerced, range, loose);
}

function checkDependencies(dependencies, errors) {
  if (dependencies == null) {
    return;
  }

  const updaterVersion = dependencies["electron-updater"];
  const requiredElectronUpdaterVersion = "4.0.0";

  if (updaterVersion != null && !versionSatisfies(updaterVersion, `>=${requiredElectronUpdaterVersion}`)) {
    errors.push(`At least electron-updater ${requiredElectronUpdaterVersion} is recommended by current electron-builder version. Please set electron-updater version to "^${requiredElectronUpdaterVersion}"`);
  }

  const swVersion = dependencies["electron-builder-squirrel-windows"];

  if (swVersion != null && !versionSatisfies(swVersion, ">=20.32.0")) {
    errors.push(`At least electron-builder-squirrel-windows 20.32.0 is required by current electron-builder version. Please set electron-builder-squirrel-windows to "^20.32.0"`);
  }

  const deps = ["electron", "electron-prebuilt", "electron-rebuild"];

  if (process.env.ALLOW_ELECTRON_BUILDER_AS_PRODUCTION_DEPENDENCY !== "true") {
    deps.push("electron-builder");
  }

  for (const name of deps) {
    if (name in dependencies) {
      errors.push(`Package "${name}" is only allowed in "devDependencies". ` + `Please remove it from the "dependencies" section in your package.json.`);
    }
  }
} 
// __ts-babel@6.0.4
//# sourceMappingURL=packageMetadata.js.map