1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.loadConfig = exports.keyEnvVar = exports.legacyKeyEnvVar = void 0;
|
7 | const cosmiconfig_1 = __importDefault(require("cosmiconfig"));
|
8 | const cosmiconfig_typescript_loader_1 = __importDefault(require("@endemolshinegroup/cosmiconfig-typescript-loader"));
|
9 | const path_1 = require("path");
|
10 | const fs_1 = require("fs");
|
11 | const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
12 | const config_1 = require("./config");
|
13 | const utils_1 = require("./utils");
|
14 | const vscode_uri_1 = __importDefault(require("vscode-uri"));
|
15 | const utilities_1 = require("../utilities");
|
16 | const MODULE_NAME = "apollo";
|
17 | const defaultFileNames = [
|
18 | "package.json",
|
19 | `${MODULE_NAME}.config.js`,
|
20 | `${MODULE_NAME}.config.ts`,
|
21 | `${MODULE_NAME}.config.cjs`
|
22 | ];
|
23 | const envFileNames = [".env", ".env.local"];
|
24 | const loaders = {
|
25 | ".json": cosmiconfig_1.default.loadJson,
|
26 | ".js": cosmiconfig_1.default.loadJs,
|
27 | ".cjs": cosmiconfig_1.default.loadJs,
|
28 | ".ts": {
|
29 | async: cosmiconfig_typescript_loader_1.default
|
30 | }
|
31 | };
|
32 | exports.legacyKeyEnvVar = "ENGINE_API_KEY";
|
33 | exports.keyEnvVar = "APOLLO_KEY";
|
34 | async function loadConfig({ configPath, configFileName, requireConfig = false, name, type }) {
|
35 | const explorer = cosmiconfig_1.default(MODULE_NAME, {
|
36 | searchPlaces: configFileName ? [configFileName] : defaultFileNames,
|
37 | loaders
|
38 | });
|
39 | let loadedConfig;
|
40 | try {
|
41 | loadedConfig = (await explorer.search(configPath));
|
42 | }
|
43 | catch (error) {
|
44 | return utilities_1.Debug.error(`A config file failed to load with options: ${JSON.stringify(arguments[0])}.
|
45 | The error was: ${error}`);
|
46 | }
|
47 | if (configPath && !loadedConfig) {
|
48 | 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`);
|
49 | }
|
50 | if (loadedConfig && loadedConfig.filepath.endsWith("package.json")) {
|
51 | 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');
|
52 | }
|
53 | if (requireConfig && !loadedConfig) {
|
54 | return utilities_1.Debug.error(`No Apollo config found for project. For more information, please refer to: https://go.apollo.dev/t/config`);
|
55 | }
|
56 | let engineConfig = {}, apiKey, nameFromKey;
|
57 | envFileNames.forEach(envFile => {
|
58 | const dotEnvPath = configPath
|
59 | ? path_1.resolve(configPath, envFile)
|
60 | : path_1.resolve(process.cwd(), envFile);
|
61 | if (fs_1.existsSync(dotEnvPath) && fs_1.lstatSync(dotEnvPath).isFile()) {
|
62 | const env = require("dotenv").parse(fs_1.readFileSync(dotEnvPath));
|
63 | const legacyKey = env[exports.legacyKeyEnvVar];
|
64 | const key = env[exports.keyEnvVar];
|
65 | if (legacyKey && key) {
|
66 | utilities_1.Debug.warning(`Both ${exports.legacyKeyEnvVar} and ${exports.keyEnvVar} were found. ${exports.keyEnvVar} will take precedence.`);
|
67 | }
|
68 | if (legacyKey) {
|
69 | 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.`);
|
70 | }
|
71 | apiKey = key || legacyKey;
|
72 | }
|
73 | });
|
74 | if (apiKey) {
|
75 | engineConfig = { engine: { apiKey } };
|
76 | nameFromKey = utils_1.getServiceFromKey(apiKey);
|
77 | }
|
78 | let projectType;
|
79 | if (type)
|
80 | projectType = type;
|
81 | else if (loadedConfig && loadedConfig.config.client)
|
82 | projectType = "client";
|
83 | else if (loadedConfig && loadedConfig.config.service)
|
84 | projectType = "service";
|
85 | else
|
86 | 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");
|
87 | let serviceName = name || nameFromKey;
|
88 | if (projectType === "client" &&
|
89 | loadedConfig &&
|
90 | loadedConfig.config.client &&
|
91 | typeof loadedConfig.config.client.service === "string") {
|
92 | serviceName = loadedConfig.config.client.service;
|
93 | }
|
94 | if (!loadedConfig ||
|
95 | serviceName ||
|
96 | !(loadedConfig.config.client || loadedConfig.config.service)) {
|
97 | loadedConfig = {
|
98 | filepath: configPath || process.cwd(),
|
99 | config: Object.assign(Object.assign({}, (loadedConfig && loadedConfig.config)), (projectType === "client"
|
100 | ? {
|
101 | client: Object.assign(Object.assign(Object.assign({}, config_1.DefaultConfigBase), (loadedConfig && loadedConfig.config.client)), { service: serviceName })
|
102 | }
|
103 | : {
|
104 | service: Object.assign(Object.assign(Object.assign({}, config_1.DefaultConfigBase), (loadedConfig && loadedConfig.config.service)), { name: serviceName })
|
105 | }))
|
106 | };
|
107 | }
|
108 | let { config, filepath } = loadedConfig;
|
109 | if (config.client)
|
110 | config = lodash_merge_1.default({ client: config_1.DefaultClientConfig }, config);
|
111 | if (config.service)
|
112 | config = lodash_merge_1.default({ service: config_1.DefaultServiceConfig }, config);
|
113 | if (engineConfig)
|
114 | config = lodash_merge_1.default(engineConfig, config);
|
115 | config = lodash_merge_1.default({ engine: config_1.DefaultEngineConfig }, config);
|
116 | return new config_1.ApolloConfig(config, vscode_uri_1.default.file(path_1.resolve(filepath)));
|
117 | }
|
118 | exports.loadConfig = loadConfig;
|
119 |
|
\ | No newline at end of file |