apollo-language-server
Version:
A language server for Apollo GraphQL projects
117 lines • 5.91 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfig = exports.keyEnvVar = exports.legacyKeyEnvVar = void 0;
const cosmiconfig_1 = require("cosmiconfig");
const cosmiconfig_typescript_loader_1 = __importDefault(require("@endemolshinegroup/cosmiconfig-typescript-loader"));
const path_1 = require("path");
const fs_1 = require("fs");
const lodash_merge_1 = __importDefault(require("lodash.merge"));
const config_1 = require("./config");
const utils_1 = require("./utils");
const vscode_uri_1 = __importDefault(require("vscode-uri"));
const utilities_1 = require("../utilities");
const MODULE_NAME = "apollo";
const defaultFileNames = [
"package.json",
`${MODULE_NAME}.config.js`,
`${MODULE_NAME}.config.ts`,
`${MODULE_NAME}.config.cjs`,
];
const envFileNames = [".env", ".env.local"];
const loaders = {
".cjs": cosmiconfig_1.defaultLoaders[".js"],
".js": cosmiconfig_1.defaultLoaders[".js"],
".json": cosmiconfig_1.defaultLoaders[".json"],
".ts": cosmiconfig_typescript_loader_1.default,
};
exports.legacyKeyEnvVar = "ENGINE_API_KEY";
exports.keyEnvVar = "APOLLO_KEY";
async function loadConfig({ configPath, configFileName, requireConfig = false, name, type, }) {
const explorer = (0, cosmiconfig_1.cosmiconfig)(MODULE_NAME, {
searchPlaces: configFileName ? [configFileName] : defaultFileNames,
loaders,
});
let loadedConfig;
try {
loadedConfig = (await explorer.search(configPath));
}
catch (error) {
return utilities_1.Debug.error(`A config file failed to load with options: ${JSON.stringify(arguments[0])}.
The error was: ${error}`);
}
if (configPath && !loadedConfig) {
return utilities_1.Debug.error(`A config file failed to load at '${configPath}'. This is likely because this file is empty or malformed. For more information, please refer to: https://go.apollo.dev/t/config`);
}
if (loadedConfig && loadedConfig.filepath.endsWith("package.json")) {
utilities_1.Debug.warning('The "apollo" package.json configuration key will no longer be supported in Apollo v3. Please use the apollo.config.js file for Apollo project configuration. For more information, see: https://go.apollo.dev/t/config');
}
if (requireConfig && !loadedConfig) {
return utilities_1.Debug.error(`No Apollo config found for project. For more information, please refer to: https://go.apollo.dev/t/config`);
}
let engineConfig = {}, apiKey, nameFromKey;
envFileNames.forEach((envFile) => {
const dotEnvPath = configPath
? (0, path_1.resolve)(configPath, envFile)
: (0, path_1.resolve)(process.cwd(), envFile);
if ((0, fs_1.existsSync)(dotEnvPath) && (0, fs_1.lstatSync)(dotEnvPath).isFile()) {
const env = require("dotenv").parse((0, fs_1.readFileSync)(dotEnvPath));
const legacyKey = env[exports.legacyKeyEnvVar];
const key = env[exports.keyEnvVar];
if (legacyKey && key) {
utilities_1.Debug.warning(`Both ${exports.legacyKeyEnvVar} and ${exports.keyEnvVar} were found. ${exports.keyEnvVar} will take precedence.`);
}
if (legacyKey) {
utilities_1.Debug.warning(`[Deprecation warning] Setting the key via ${exports.legacyKeyEnvVar} is deprecated and will not be supported in future versions. Please use ${exports.keyEnvVar} instead.`);
}
apiKey = key || legacyKey;
}
});
if (apiKey) {
engineConfig = { engine: { apiKey } };
nameFromKey = (0, utils_1.getServiceFromKey)(apiKey);
}
let projectType;
if (type)
projectType = type;
else if (loadedConfig && loadedConfig.config.client)
projectType = "client";
else if (loadedConfig && loadedConfig.config.service)
projectType = "service";
else
return utilities_1.Debug.error("Unable to resolve project type. Please add either a client or service config. For more information, please refer to https://go.apollo.dev/t/config");
let serviceName = name || nameFromKey;
if (projectType === "client" &&
loadedConfig &&
loadedConfig.config.client &&
typeof loadedConfig.config.client.service === "string") {
serviceName = loadedConfig.config.client.service;
}
if (!loadedConfig ||
serviceName ||
!(loadedConfig.config.client || loadedConfig.config.service)) {
loadedConfig = {
filepath: configPath || process.cwd(),
config: Object.assign(Object.assign({}, (loadedConfig && loadedConfig.config)), (projectType === "client"
? {
client: Object.assign(Object.assign(Object.assign({}, config_1.DefaultConfigBase), (loadedConfig && loadedConfig.config.client)), { service: serviceName }),
}
: {
service: Object.assign(Object.assign(Object.assign({}, config_1.DefaultConfigBase), (loadedConfig && loadedConfig.config.service)), { name: serviceName }),
})),
};
}
let { config, filepath } = loadedConfig;
if (config.client)
config = (0, lodash_merge_1.default)({ client: config_1.DefaultClientConfig }, config);
if (config.service)
config = (0, lodash_merge_1.default)({ service: config_1.DefaultServiceConfig }, config);
if (engineConfig)
config = (0, lodash_merge_1.default)(engineConfig, config);
config = (0, lodash_merge_1.default)({ engine: config_1.DefaultEngineConfig }, config);
return new config_1.ApolloConfig(config, vscode_uri_1.default.file((0, path_1.resolve)(filepath)));
}
exports.loadConfig = loadConfig;
//# sourceMappingURL=loadConfig.js.map
;