@webiny/cli 0.0.0-mt-3 → 0.0.0-unstable.06b2ede40f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +1 -1
  2. package/bin.js +94 -27
  3. package/commands/about/getDatabaseSetup.js +45 -0
  4. package/commands/about/getNpmVersion.js +5 -0
  5. package/commands/about/getNpxVersion.js +5 -0
  6. package/commands/about/getPulumiVersions.js +43 -0
  7. package/commands/about/getYarnVersion.js +5 -0
  8. package/commands/about/index.js +97 -0
  9. package/commands/index.js +9 -1
  10. package/commands/run/index.js +12 -4
  11. package/commands/telemetry/index.js +9 -9
  12. package/commands/upgrade/index.js +6 -5
  13. package/commands/wcp/hooks.js +133 -0
  14. package/commands/wcp/index.js +8 -0
  15. package/commands/wcp/login.js +228 -0
  16. package/commands/wcp/logout.js +28 -0
  17. package/commands/wcp/project.js +203 -0
  18. package/commands/wcp/utils/getProjectEnvironment.js +120 -0
  19. package/commands/wcp/utils/getUser.js +100 -0
  20. package/commands/wcp/utils/getWcpOrgProjectId.js +9 -0
  21. package/commands/wcp/utils/getWcpPat.js +5 -0
  22. package/commands/wcp/utils/getWcpProjectId.js +3 -0
  23. package/commands/wcp/utils/index.js +19 -0
  24. package/commands/wcp/utils/setProjectId.js +44 -0
  25. package/commands/wcp/utils/setWcpPat.js +5 -0
  26. package/commands/wcp/utils/updateUserLastActiveOn.js +28 -0
  27. package/commands/wcp/whoami.js +43 -0
  28. package/context.js +3 -3
  29. package/files/README.md +1 -0
  30. package/files/duplicates.json +1 -0
  31. package/files/references.json +1 -0
  32. package/index.d.ts +5 -0
  33. package/index.js +5 -0
  34. package/package.json +22 -15
  35. package/regions.d.ts +6 -0
  36. package/regions.js +30 -0
  37. package/types.d.ts +120 -35
  38. package/utils/createProjectApplicationWorkspace.js +16 -0
  39. package/utils/ensureSameWebinyPackageVersions.js +99 -0
  40. package/utils/getProjectApplication.js +27 -7
  41. package/utils/index.d.ts +28 -0
  42. package/utils/index.js +13 -2
  43. package/utils/loadEnvVariables.js +63 -0
  44. package/utils/log.js +15 -17
  45. package/utils/sendEvent.js +2 -6
  46. package/utils/sleep.js +3 -0
  47. package/utils/sleepSync.js +8 -0
  48. package/utils/suppressPunycodeWarnings.js +7 -0
  49. package/CHANGELOG.md +0 -2896
  50. package/cli.js +0 -107
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import {CliContext} from "./types";
2
+
3
+ export * from "./regions"
4
+
5
+ export declare const cli: CliContext;
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ const context = require("./context");
2
+ const regions = require("./regions");
3
+
4
+ module.exports.cli = context;
5
+ module.exports.regions = regions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/cli",
3
- "version": "0.0.0-mt-3",
3
+ "version": "0.0.0-unstable.06b2ede40f",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "webiny": "./bin.js"
@@ -13,19 +13,28 @@
13
13
  "author": "Pavel Denisjuk <pavel@webiny.com>",
14
14
  "description": "A tool to bootstrap a Webiny project.",
15
15
  "dependencies": {
16
- "@webiny/telemetry": "0.0.0-mt-3",
17
- "boolean": "3.1.4",
18
- "camelcase": "5.3.1",
16
+ "@webiny/system-requirements": "0.0.0-unstable.06b2ede40f",
17
+ "@webiny/telemetry": "0.0.0-unstable.06b2ede40f",
18
+ "@webiny/wcp": "0.0.0-unstable.06b2ede40f",
19
+ "boolean": "3.2.0",
20
+ "camelcase": "6.3.0",
19
21
  "chalk": "4.1.2",
20
- "dotenv": "8.2.0",
21
- "execa": "2.1.0",
22
- "fast-glob": "3.2.7",
22
+ "ci-info": "4.2.0",
23
+ "dotenv": "8.6.0",
24
+ "execa": "5.1.1",
25
+ "fast-glob": "3.2.12",
23
26
  "find-up": "5.0.0",
24
- "pirates": "4.0.1",
25
- "semver": "7.3.4",
26
- "typescript": "4.1.3",
27
+ "fs-extra": "11.2.0",
28
+ "graphql-request": "3.7.0",
29
+ "inquirer": "8.2.6",
30
+ "ncp": "2.0.0",
31
+ "open": "8.4.2",
32
+ "pirates": "4.0.6",
33
+ "semver": "7.6.3",
34
+ "ts-morph": "11.0.3",
35
+ "typescript": "5.3.3",
27
36
  "uniqid": "5.4.0",
28
- "yargs": "14.2.3"
37
+ "yargs": "17.7.2"
29
38
  },
30
39
  "license": "MIT",
31
40
  "publishConfig": {
@@ -43,9 +52,7 @@
43
52
  "@webiny/project-utils",
44
53
  "@webiny/api-file-manager",
45
54
  "@webiny/cli-plugin-deploy-pulumi",
46
- "@webiny/handler",
47
- "@webiny/handler-http",
48
- "@webiny/handler-args",
55
+ "@webiny/api",
49
56
  "@webiny/handler-client",
50
57
  "@webiny/api-elasticsearch",
51
58
  "@webiny/api-tenancy",
@@ -59,5 +66,5 @@
59
66
  ]
60
67
  }
61
68
  },
62
- "gitHead": "ebea815be2be99404591cba465cc1fe88355bd48"
69
+ "gitHead": "06b2ede40fc2212a70eeafd74afd50b56fb0ce82"
63
70
  }
package/regions.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export interface RegionValue {
2
+ name: string;
3
+ value: string;
4
+ }
5
+
6
+ export declare const regions: RegionValue[];
package/regions.js ADDED
@@ -0,0 +1,30 @@
1
+ const regions = [
2
+ { value: "us-east-1", name: "us-east-1 (US East, N. Virginia)" },
3
+ { value: "us-east-2", name: "us-east-2 (US East, Ohio)" },
4
+ { value: "us-west-1", name: "us-west-1 (US West, N. California)" },
5
+ { value: "us-west-2", name: "us-west-2 (US West, Oregon)" },
6
+ { value: "ca-central-1", name: "ca-central-1 (Canada, Central)" },
7
+ { value: "eu-central-1", name: "eu-central-1 (EU, Frankfurt)" },
8
+ { value: "eu-west-1", name: "eu-west-1 (EU, Ireland)" },
9
+ { value: "eu-west-2", name: "eu-west-2 (EU, London)" },
10
+ /**
11
+ * This was commented out because cognito was not available in this region in 2021.
12
+ * TODO - uncomment and test it out
13
+ */
14
+ /*{ value: "eu-south-1", name: "eu-south-1 (EU, Milan)" },*/
15
+ { value: "eu-west-3", name: "eu-west-3 (EU, Paris)" },
16
+ { value: "eu-north-1", name: "eu-north-1 (EU, Stockholm)" },
17
+ { value: "af-south-1", name: "af-south-1 (Africa, Cape Town)" },
18
+ { value: "ap-east-1", name: "ap-east-1 (Asia Pacific, Hong Kong)" },
19
+ { value: "ap-south-1", name: "ap-south-1 (Asia Pacific, Mumbai)" },
20
+ { value: "ap-northeast-2", name: "ap-northeast-2 (Asia Pacific, Seoul)" },
21
+ { value: "ap-southeast-1", name: "ap-southeast-1 (Asia Pacific, Singapore)" },
22
+ { value: "ap-southeast-2", name: "ap-southeast-2 (Asia Pacific, Sydney)" },
23
+ { value: "ap-northeast-1", name: "ap-northeast-1 (Asia Pacific, Tokyo)" },
24
+ // { value: "me-south-1", name: "me-south-1 (Middle East, Bahrain)" },
25
+ { value: "sa-east-1", name: "sa-east-1 (South America, São Paulo)" }
26
+ ];
27
+
28
+ module.exports = {
29
+ regions
30
+ };
package/types.d.ts CHANGED
@@ -1,13 +1,20 @@
1
+ import type yargs from "yargs";
2
+
1
3
  /**
2
4
  * Rename file to types.ts when switching the package to Typescript.
3
5
  */
6
+ export type GenericRecord<K extends PropertyKey = PropertyKey, V = any> = Record<K, V>;
7
+
8
+ export type NonEmptyArray<T> = [T, ...T[]];
4
9
 
5
10
  /**
6
11
  * A simplified plugins container interface, used specifically within the Webiny CLI.
7
12
  * Not in relation with "@webiny/plugins" package.
8
13
  */
9
14
  export interface PluginsContainer {
15
+ register(...args: any[]): void;
10
16
  byType<T extends Plugin>(type: T["type"]): T[];
17
+
11
18
  byName<T extends Plugin>(name: T["name"]): T;
12
19
  }
13
20
 
@@ -15,9 +22,10 @@ export interface PluginsContainer {
15
22
  * A simplified plugin interface, used specifically within the Webiny CLI.
16
23
  * Not in relation with "@webiny/plugins" package.
17
24
  */
18
- export interface Plugin<T = Record<string, any>> {
25
+ export interface Plugin {
19
26
  type: string;
20
27
  name?: string;
28
+
21
29
  [key: string]: any;
22
30
  }
23
31
 
@@ -29,20 +37,93 @@ interface Project {
29
37
  /**
30
38
  * Configurations.
31
39
  */
32
- config: Record<string, any>;
40
+ config: {
41
+ appAliases: {
42
+ [key: string]: string;
43
+ }
44
+ [key: string]: any;
45
+ };
33
46
  /**
34
47
  * Root path of the project.
35
48
  */
36
49
  root: string;
37
50
  }
38
51
 
52
+
53
+ export interface IProjectApplicationPackage {
54
+ name: string;
55
+ paths: {
56
+ root: string;
57
+ relative: string;
58
+ packageJson: string;
59
+ config: string;
60
+ };
61
+ packageJson: Record<string, any>;
62
+ get config(): any;
63
+ }
64
+
65
+ export interface IProjectApplicationConfigCli {
66
+ watch?: boolean;
67
+ }
68
+
69
+ export interface IProjectApplicationConfig {
70
+ appAliases?: Record<string, string>;
71
+ cli?: IProjectApplicationConfigCli;
72
+ [key: string]: unknown
73
+ }
74
+
75
+ export interface ProjectApplication {
76
+ /**
77
+ * Unique ID of the project application.
78
+ */
79
+ id: string;
80
+ /**
81
+ * Name of the project application.
82
+ */
83
+ name: string;
84
+ /**
85
+ * Description of the project application.
86
+ */
87
+ description: string;
88
+ /**
89
+ * Type of the project application.
90
+ */
91
+ type: string;
92
+ /**
93
+ * Root path of the project application.
94
+ */
95
+ root: string;
96
+ /**
97
+ * Commonly used paths.
98
+ */
99
+ paths: {
100
+ relative: string;
101
+ absolute: string;
102
+ workspace: string;
103
+ };
104
+ /**
105
+ * Project application config (exported via `webiny.application.ts` file).
106
+ */
107
+ config: IProjectApplicationConfig;
108
+ /**
109
+ * Project application package.json.
110
+ */
111
+ project: Project;
112
+
113
+ /**
114
+ * A list of all the packages in the project application.
115
+ */
116
+ get packages(): IProjectApplicationPackage[];
117
+ }
118
+
39
119
  /**
40
120
  * A type that represents the logging method.
41
121
  */
42
122
  interface Log {
43
- (...args): string;
44
- hl: (...args) => string;
45
- highlight: (...args) => string;
123
+ (...args: any): string;
124
+
125
+ hl: (...args: any) => string;
126
+ highlight: (...args: any) => string;
46
127
  }
47
128
 
48
129
  /**
@@ -53,6 +134,10 @@ export interface CliContext {
53
134
  * All registered plugins.
54
135
  */
55
136
  plugins: PluginsContainer;
137
+ /**
138
+ * Load environment variables from a given file.
139
+ */
140
+ loadEnv(filePath: string, options?: {debug?: boolean}): Promise<void>;
56
141
  /**
57
142
  * All the environment variables.
58
143
  */
@@ -100,50 +185,50 @@ export interface CliContext {
100
185
  /**
101
186
  * Resolve given dir or dirs against project root path.
102
187
  */
103
- resolve: (dir) => string;
188
+ resolve: (dir: string) => string;
104
189
 
105
190
  /**
106
- * Provides a way to store some meta data in the project's local ".webiny/cli.json" file.
191
+ * Provides a way to store some metadata in the project's local ".webiny/cli.json" file.
107
192
  * Only trivial data should be passed here, specific to the current project.
108
193
  */
109
194
  localStorage: {
110
- set: (key: string, value: string) => Record<string, any>;
195
+ set: (key: string, value: any) => Record<string, any>;
111
196
  get: (key: string) => any;
112
197
  };
113
198
  }
114
199
 
115
200
  /**
116
- * Args received from the CLI.
201
+ * Arguments for CliPlugin.create
202
+ *
203
+ * @category Cli
117
204
  */
118
- interface CliUpgradePluginOptions {
119
- /**
120
- * Targeted version of the upgrade.
121
- */
122
- targetVersion: string;
205
+ export interface CliCommandPluginArgs {
206
+ yargs: typeof yargs;
207
+ context: CliContext;
123
208
  }
209
+
124
210
  /**
211
+ * A plugin defining cli-command type.
125
212
  *
213
+ * @category Plugin
214
+ * @category Cli
126
215
  */
127
- export interface CliUpgradePlugin extends Plugin {
128
- /**
129
- * Name of the plugin to differentiate from others.
130
- * Something like: cli-upgrade-5.0.0
131
- */
216
+ export interface CliCommandPlugin extends Plugin {
217
+ type: "cli-command";
132
218
  name: string;
133
- /**
134
- * Type of the plugin.
135
- */
136
- type: "cli-upgrade";
137
- /**
138
- * Version the plugin is for.
139
- */
140
- version: string;
141
- /**
142
- * Is this plugin usable for the upgrade?
143
- */
144
- canUpgrade?: (options: CliUpgradePluginOptions, context: CliContext) => Promise<boolean>;
145
- /**
146
- * Apply the upgrade.
147
- */
148
- upgrade: (options: CliUpgradePluginOptions, context: CliContext) => Promise<void>;
219
+ create: (args: CliCommandPluginArgs) => void;
220
+ }
221
+
222
+ export interface CliCommandErrorPluginHandleParams {
223
+ context: CliContext;
224
+ error: Error;
225
+ }
226
+
227
+ export interface CliCommandErrorPluginHandle {
228
+ (params: CliCommandErrorPluginHandleParams):void;
229
+ }
230
+
231
+ export interface CliCommandErrorPlugin extends Plugin {
232
+ type: "cli-command-error";
233
+ handle: CliCommandErrorPluginHandle;
149
234
  }
@@ -0,0 +1,16 @@
1
+ const util = require("util");
2
+ const fs = require("fs-extra");
3
+ const ncpBase = require("ncp");
4
+ const ncp = util.promisify(ncpBase.ncp);
5
+
6
+ module.exports = async projectApplication => {
7
+ if (await fs.pathExists(projectApplication.paths.workspace)) {
8
+ await fs.remove(projectApplication.paths.workspace);
9
+ }
10
+
11
+ await fs.ensureDir(projectApplication.paths.workspace);
12
+ await ncp(projectApplication.paths.absolute, projectApplication.paths.workspace);
13
+
14
+ // Wait a bit and make sure the files are ready to have its content replaced.
15
+ return new Promise(resolve => setTimeout(resolve, 10));
16
+ };
@@ -0,0 +1,99 @@
1
+ const execa = require("execa");
2
+ const logger = require("./log");
3
+
4
+ const DEBUG_FLAG = "--debug";
5
+ const usingDebugFlag = process.argv.includes(DEBUG_FLAG);
6
+
7
+ const SKIP_WEBINY_VERSIONS_CHECK_FLAG = "--no-package-versions-check";
8
+ const skippingWebinyVersionsCheck = process.argv.includes(SKIP_WEBINY_VERSIONS_CHECK_FLAG);
9
+
10
+ function listWebinyPackageVersions() {
11
+ const { stdout } = execa.sync("yarn", ["info", "@webiny/*", "--name-only", "--all", "--json"], {
12
+ encoding: "utf-8"
13
+ });
14
+
15
+ // Each line is a JSON string, so parse them individually
16
+ const lines = stdout
17
+ .trim()
18
+ .split("\n")
19
+ .map(line => JSON.parse(line));
20
+
21
+ const versionMap = new Map();
22
+
23
+ for (const entry of lines) {
24
+ // An example entry: "@webiny/cli@npm:5.42.3"
25
+ const match = entry.match(/^(@webiny\/[^@]+)@npm:(.+)$/);
26
+ if (!match) {
27
+ continue;
28
+ }
29
+
30
+ const [, pkg, version] = match;
31
+ if (!versionMap.has(pkg)) {
32
+ versionMap.set(pkg, new Set());
33
+ }
34
+
35
+ versionMap.get(pkg).add(version);
36
+ }
37
+
38
+ return versionMap;
39
+ }
40
+
41
+ function ensureSameWebinyPackageVersions() {
42
+ // Just in case, we want to allow users to skip the check.
43
+ if (skippingWebinyVersionsCheck) {
44
+ return;
45
+ }
46
+
47
+ let webinyVersions;
48
+ try {
49
+ webinyVersions = listWebinyPackageVersions();
50
+ } catch (e) {
51
+ const message = ["Failed to inspect Webiny package versions."];
52
+
53
+ if (!usingDebugFlag) {
54
+ message.push(
55
+ `For more information, try running with ${logger.warning.hl(DEBUG_FLAG)}.`
56
+ );
57
+ }
58
+
59
+ message.push("Learn more: https://webiny.link/webiny-package-versions-check");
60
+
61
+ logger.warning(message.join(" "));
62
+ if (usingDebugFlag) {
63
+ logger.debug(e);
64
+ }
65
+
66
+ console.log();
67
+ return;
68
+ }
69
+
70
+ let hasMismatch = false;
71
+ const mismatchedPackages = [];
72
+ for (const [pkg, versions] of webinyVersions.entries()) {
73
+ if (versions.size > 1) {
74
+ hasMismatch = true;
75
+ mismatchedPackages.push([pkg, versions]);
76
+ }
77
+ }
78
+
79
+ if (hasMismatch) {
80
+ const message = [
81
+ "The following Webiny packages have mismatched versions:",
82
+ "",
83
+ ...mismatchedPackages.map(([pkg, versions]) => {
84
+ return `‣ ${pkg}: ${Array.from(versions).join(", ")}`;
85
+ }),
86
+ "",
87
+ `Please ensure all Webiny packages are using the same version. If you think this is a mistake, you can also skip this check by appending the ${logger.error.hl(
88
+ SKIP_WEBINY_VERSIONS_CHECK_FLAG
89
+ )} flag. Learn more: https://webiny.link/webiny-package-versions-check`
90
+ ];
91
+
92
+ logger.error(message.join("\n"));
93
+ process.exit(1);
94
+ }
95
+ }
96
+
97
+ module.exports = {
98
+ ensureSameWebinyPackageVersions
99
+ };
@@ -1,4 +1,4 @@
1
- const { dirname, basename, join } = require("path");
1
+ const { dirname, basename, join, relative } = require("path");
2
2
  const findUp = require("find-up");
3
3
  const getProject = require("./getProject");
4
4
  const { importModule } = require("./importModule");
@@ -15,31 +15,49 @@ module.exports = args => {
15
15
  }
16
16
 
17
17
  const rootFile = applicationRootFile.replace(/\\/g, "/");
18
- const applicationRoot = dirname(rootFile);
18
+ const projectAppRootPath = dirname(rootFile);
19
19
 
20
20
  let applicationConfig;
21
21
  if (appConfigs.includes(basename(rootFile))) {
22
22
  applicationConfig = importModule(rootFile);
23
23
  }
24
24
 
25
- let name, id;
25
+ let id, name, description;
26
26
  if (applicationConfig) {
27
27
  id = applicationConfig.id;
28
28
  name = applicationConfig.name;
29
+ description = applicationConfig.description;
29
30
  } else {
30
- name = basename(applicationRoot);
31
+ name = basename(projectAppRootPath);
32
+ description = name;
31
33
  id = name;
32
34
  }
33
35
 
36
+ const project = getProject(args);
37
+
38
+ const projectAppRelativePath = relative(project.root, projectAppRootPath);
39
+ const projectAppWorkspacePath = join(
40
+ project.root,
41
+ ".webiny",
42
+ "workspaces",
43
+ projectAppRelativePath
44
+ );
45
+
34
46
  return {
35
47
  id,
36
48
  name,
37
- root: applicationRoot,
49
+ description,
50
+ root: projectAppRootPath,
51
+ paths: {
52
+ relative: projectAppRelativePath,
53
+ absolute: projectAppRootPath,
54
+ workspace: projectAppWorkspacePath
55
+ },
38
56
  config: applicationConfig,
39
- project: getProject(args),
57
+ project,
40
58
  get packages() {
41
59
  const webinyConfigs = glob.sync(
42
- join(applicationRoot, "**/webiny.config*.{ts,js}").replace(/\\/g, "/")
60
+ join(projectAppRootPath, "**/webiny.config*.{ts,js}").replace(/\\/g, "/")
43
61
  );
44
62
 
45
63
  return webinyConfigs.map(config => {
@@ -48,6 +66,8 @@ module.exports = args => {
48
66
  return {
49
67
  name: packageJson.name,
50
68
  paths: {
69
+ absolute: dirname(config),
70
+ relative: relative(project.root, dirPath),
51
71
  root: dirname(config),
52
72
  packageJson: join(dirPath, "package.json"),
53
73
  config
@@ -0,0 +1,28 @@
1
+ import { Project, ProjectApplication } from "../types";
2
+
3
+ export interface IGetProjectParams {
4
+ cwd: string;
5
+ }
6
+
7
+ export declare function getProject(params?: IGetProjectParams): Project;
8
+
9
+ export interface IGetProjectApplicationParams {
10
+ cwd: string;
11
+ }
12
+
13
+ export declare function getProjectApplication(
14
+ params: IGetProjectApplicationParams
15
+ ): ProjectApplication;
16
+
17
+ export declare function sendEvent(event: string, properties?: Record<string, any>): Promise<void>;
18
+
19
+ export declare function sleepSync(ms?: number): void;
20
+
21
+ export declare const log: {
22
+ log: ((...args: any[]) => void) & { hl: (message: string) => string };
23
+ info: ((...args: any[]) => void) & { hl: (message: string) => string };
24
+ success: ((...args: any[]) => void) & { hl: (message: string) => string };
25
+ debug: ((...args: any[]) => void) & { hl: (message: string) => string };
26
+ warning: ((...args: any[]) => void) & { hl: (message: string) => string };
27
+ error: ((...args: any[]) => void) & { hl: (message: string) => string };
28
+ };
package/utils/index.js CHANGED
@@ -1,17 +1,28 @@
1
1
  const { importModule } = require("./importModule");
2
+ const createProjectApplicationWorkspace = require("./createProjectApplicationWorkspace");
2
3
  const getProject = require("./getProject");
3
4
  const getProjectApplication = require("./getProjectApplication");
4
5
  const localStorage = require("./localStorage");
5
6
  const log = require("./log");
6
7
  const sendEvent = require("./sendEvent");
7
8
  const PluginsContainer = require("./PluginsContainer");
9
+ const sleep = require("./sleep");
10
+ const sleepSync = require("./sleepSync");
11
+
12
+ const noop = () => {
13
+ // Do nothing.
14
+ };
8
15
 
9
16
  module.exports = {
10
- importModule,
17
+ createProjectApplicationWorkspace,
11
18
  getProject,
12
19
  getProjectApplication,
20
+ importModule,
13
21
  localStorage,
14
22
  log,
23
+ noop,
15
24
  sendEvent,
16
- PluginsContainer
25
+ PluginsContainer,
26
+ sleep,
27
+ sleepSync
17
28
  };
@@ -0,0 +1,63 @@
1
+ const path = require("path");
2
+ const yargs = require("yargs");
3
+ const log = require("./log");
4
+ const getProject = require("./getProject");
5
+ const { boolean } = require("boolean");
6
+
7
+ // Load environment variables from following sources:
8
+ // - `webiny.project.ts` file
9
+ // - `.env` file
10
+ // - `.env.{PASSED_ENVIRONMENT}` file
11
+
12
+ const project = getProject();
13
+
14
+ // `webiny.project.ts` file.
15
+ // Environment variables defined via the `env` property.
16
+ if (project.config.env) {
17
+ Object.assign(process.env, project.config.env);
18
+ }
19
+
20
+ // `.env.{PASSED_ENVIRONMENT}` and `.env` files.
21
+ let paths = [path.join(project.root, ".env")];
22
+
23
+ if (yargs.argv.env) {
24
+ paths.push(path.join(project.root, `.env.${yargs.argv.env}`));
25
+ }
26
+
27
+ // If the `WCP_PROJECT_ID` environment variable is not set already, we check if there's
28
+ // a WCP project ID set via the `webiny.project.ts` config file. If so, we assign it
29
+ // to the `WCP_PROJECT_ID` environment variable.
30
+ if (!process.env.WCP_PROJECT_ID) {
31
+ if (project.config.id) {
32
+ process.env.WCP_PROJECT_ID = project.config.id;
33
+ }
34
+ }
35
+
36
+ // Let's load environment variables
37
+ for (let i = 0; i < paths.length; i++) {
38
+ const path = paths[i];
39
+ const { error } = require("dotenv").config({ path });
40
+ if (boolean(yargs.argv.debug)) {
41
+ if (error) {
42
+ log.debug(`No environment file found on ${log.debug.hl(path)}.`);
43
+ } else {
44
+ log.success(`Successfully loaded environment variables from ${log.success.hl(path)}.`);
45
+ }
46
+ }
47
+ }
48
+
49
+ // Feature flags defined via the `featureFlags` property.
50
+ // We set twice, to be available for both backend and frontend application code.
51
+ // TODO: one day we might want to sync this up a bit.
52
+ if (project.config.featureFlags) {
53
+ process.env.WEBINY_FEATURE_FLAGS = JSON.stringify(project.config.featureFlags);
54
+ process.env.REACT_APP_WEBINY_FEATURE_FLAGS = JSON.stringify(project.config.featureFlags);
55
+ }
56
+
57
+ // With 5.38.0, we are hiding the `WEBINY_ELASTICSEARCH_INDEX_LOCALE` env variable and always setting it to `true`.
58
+ // This is because this variable is not something users should be concerned with, nor should they be able to change it.
59
+ // In order to ensure backwards compatibility, we first check if the variable is set, and if it is, we don't override it.
60
+ const esIndexLocaleEnvVarExists = "WEBINY_ELASTICSEARCH_INDEX_LOCALE" in process.env;
61
+ if (!esIndexLocaleEnvVarExists) {
62
+ process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true";
63
+ }