@webiny/cli 6.0.0-beta.0 → 6.0.0-rc.1

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 (48) hide show
  1. package/README.md +10 -6
  2. package/bin.js +10 -33
  3. package/files/duplicates.json +1 -0
  4. package/files/references.json +1 -0
  5. package/index.js +2 -2
  6. package/package.json +16 -55
  7. package/utils/ensureSameWebinyPackageVersions.js +99 -0
  8. package/utils/suppressPunycodeWarnings.js +7 -0
  9. package/cli.js +0 -93
  10. package/commands/about/getDatabaseSetup.js +0 -45
  11. package/commands/about/getNpmVersion.js +0 -10
  12. package/commands/about/getNpxVersion.js +0 -10
  13. package/commands/about/getPulumiVersions.js +0 -43
  14. package/commands/about/getYarnVersion.js +0 -10
  15. package/commands/about/index.js +0 -97
  16. package/commands/index.js +0 -21
  17. package/commands/run/index.js +0 -38
  18. package/commands/telemetry/index.js +0 -31
  19. package/commands/upgrade/index.js +0 -108
  20. package/commands/wcp/hooks.js +0 -133
  21. package/commands/wcp/index.js +0 -8
  22. package/commands/wcp/login.js +0 -227
  23. package/commands/wcp/logout.js +0 -28
  24. package/commands/wcp/project.js +0 -202
  25. package/commands/wcp/utils/getProjectEnvironment.js +0 -120
  26. package/commands/wcp/utils/getUser.js +0 -100
  27. package/commands/wcp/utils/getWcpOrgProjectId.js +0 -9
  28. package/commands/wcp/utils/getWcpPat.js +0 -5
  29. package/commands/wcp/utils/getWcpProjectId.js +0 -3
  30. package/commands/wcp/utils/index.js +0 -21
  31. package/commands/wcp/utils/setProjectId.js +0 -44
  32. package/commands/wcp/utils/setWcpPat.js +0 -5
  33. package/commands/wcp/utils/sleep.js +0 -1
  34. package/commands/wcp/utils/updateUserLastActiveOn.js +0 -28
  35. package/commands/wcp/whoami.js +0 -43
  36. package/context.js +0 -137
  37. package/index.d.ts +0 -3
  38. package/types.d.ts +0 -172
  39. package/utils/PluginsContainer.js +0 -49
  40. package/utils/createProjectApplicationWorkspace.js +0 -16
  41. package/utils/getProject.js +0 -48
  42. package/utils/getProjectApplication.js +0 -83
  43. package/utils/importModule.js +0 -43
  44. package/utils/index.js +0 -24
  45. package/utils/loadEnvVariables.js +0 -63
  46. package/utils/localStorage.js +0 -44
  47. package/utils/log.js +0 -67
  48. package/utils/sendEvent.js +0 -11
@@ -1,108 +0,0 @@
1
- module.exports = [
2
- {
3
- type: "cli-command",
4
- name: "cli-command-upgrade",
5
- create({ yargs, context }) {
6
- yargs.example("$0 upgrade");
7
- yargs.command(
8
- "upgrade",
9
- `Run an upgrade script for currently installed version of Webiny`,
10
- yargs => {
11
- yargs.option("skip-checks", {
12
- describe: "Do not perform CLI version and Git tree checks.",
13
- type: "boolean",
14
- default: false
15
- });
16
- yargs.option("debug", {
17
- default: false,
18
- describe: `Turn on debug logs`,
19
- type: "boolean"
20
- });
21
- yargs.option("use-version", {
22
- describe:
23
- "Use upgrade script for a specific version. Should only be used for development/testing purposes.",
24
- type: "string"
25
- });
26
- },
27
- async argv => {
28
- const { red } = require("chalk");
29
- const execa = require("execa");
30
- const semver = require("semver");
31
-
32
- if (!argv.skipChecks) {
33
- // Before doing any upgrading, there must not be any active changes in the current branch.
34
- let gitStatus = "";
35
- try {
36
- let { stdout } = execa.sync("git", ["status", "--porcelain"]);
37
- gitStatus = stdout.trim();
38
- } catch {}
39
-
40
- if (gitStatus) {
41
- console.error(
42
- red(
43
- "This git repository has untracked files or uncommitted changes:"
44
- ) +
45
- "\n\n" +
46
- gitStatus
47
- .split("\n")
48
- .map(line => line.match(/ .*/g)[0].trim())
49
- .join("\n") +
50
- "\n\n" +
51
- red(
52
- "Remove untracked files, stash or commit any changes, and try again."
53
- )
54
- );
55
- process.exit(1);
56
- }
57
- }
58
-
59
- const defaultUpgradeTargetVersion = semver.coerce(context.version).version;
60
-
61
- const command = [
62
- "https://github.com/webiny/webiny-upgrades",
63
- argv.useVersion || defaultUpgradeTargetVersion
64
- ];
65
-
66
- if (yargs.argv.debug) {
67
- context.debug("npx", ...command);
68
- }
69
-
70
- const npx = execa("npx", command, {
71
- env: {
72
- FORCE_COLOR: true
73
- },
74
- stdin: process.stdin
75
- });
76
-
77
- npx.stdout.on("data", data => {
78
- const lines = data.toString().replace(/\n$/, "").split("\n");
79
- for (let i = 0; i < lines.length; i++) {
80
- const line = lines[i];
81
- try {
82
- const json = JSON.parse(line);
83
- if (json.type === "error") {
84
- context.error(
85
- "An error occurred while performing the upgrade."
86
- );
87
- console.log(json.message);
88
- if (yargs.argv.debug) {
89
- context.debug(json.data.stack);
90
- }
91
- }
92
- } catch {
93
- // Not JSON, let's just print the line then.
94
- console.log(line);
95
- }
96
- }
97
- });
98
-
99
- npx.stderr.on("data", data => {
100
- console.log(data.toString());
101
- });
102
-
103
- await npx;
104
- }
105
- );
106
- }
107
- }
108
- ];
@@ -1,133 +0,0 @@
1
- const { encrypt, decrypt } = require("@webiny/wcp");
2
- const { getUser, getProjectEnvironment, updateUserLastActiveOn } = require("./utils");
3
-
4
- /**
5
- * The two environment variables we set via these hooks are the following:
6
- * - WCP_PROJECT_ENVIRONMENT - contains encrypted data about the deployed project environment
7
- * - WCP_PROJECT_ENVIRONMENT_API_KEY - for easier access, we also set the API key
8
- */
9
-
10
- /**
11
- * There are multiple ways the hooks below prepare the WCP-enabled project for deployment.
12
- * 1. If `WCP_PROJECT_ENVIRONMENT` metadata env var is defined, we decrypt it, retrieve the
13
- * API key from it, and assign it as the `WCP_PROJECT_ENVIRONMENT_API_KEY` env var.
14
- * 2. If `WCP_PROJECT_ENVIRONMENT_API_KEY` env var is defined, then we use that as the
15
- * project environment API key. We use that to load the project environment data
16
- * and to also assign the `WCP_PROJECT_ENVIRONMENT` metadata env var.
17
- * 3. If none of the above is defined, we retrieve (or create) the project environment,
18
- * retrieve its API key and again assign it as `WCP_PROJECT_ENVIRONMENT_API_KEY` env var.
19
- * As in 2), we also assign the `WCP_PROJECT_ENVIRONMENT` metadata env var.
20
- */
21
-
22
- let projectEnvironment;
23
-
24
- const getEnvironmentHookHandler = async (args, context) => {
25
- // If the project isn't linked with WCP, do nothing.
26
- const wcpProjectId = context.project.config.id || process.env.WCP_PROJECT_ID;
27
- if (!wcpProjectId) {
28
- return;
29
- }
30
-
31
- // For development purposes, we allow setting the WCP_PROJECT_ENVIRONMENT env var directly.
32
- if (process.env.WCP_PROJECT_ENVIRONMENT) {
33
- // If we have WCP_PROJECT_ENVIRONMENT env var, we set the WCP_PROJECT_ENVIRONMENT_API_KEY too.
34
- const decryptedProjectEnvironment = decrypt(process.env.WCP_PROJECT_ENVIRONMENT);
35
- process.env.WCP_PROJECT_ENVIRONMENT_API_KEY = decryptedProjectEnvironment.apiKey;
36
- return;
37
- }
38
-
39
- // The `id` has the orgId/projectId structure, for example `my-org-x/my-project-y`.
40
- const [orgId, projectId] = wcpProjectId.split("/");
41
-
42
- const apiKey = process.env.WCP_PROJECT_ENVIRONMENT_API_KEY;
43
-
44
- let projectEnvironment;
45
- if (apiKey) {
46
- projectEnvironment = await getProjectEnvironment({ apiKey });
47
- } else {
48
- const isValidId = orgId && projectId;
49
- if (!isValidId) {
50
- throw new Error(
51
- `It seems the project ID, specified in "webiny.project.ts" file, is invalid.`
52
- );
53
- }
54
-
55
- // If there is no API key, that means we need to retrieve the currently logged-in user.
56
- const user = await getUser();
57
- const project = user.projects.find(item => item.id === projectId);
58
- if (!project) {
59
- throw new Error(
60
- `It seems you don't belong to the current project or the current project has been deleted.`
61
- );
62
- }
63
-
64
- projectEnvironment = await getProjectEnvironment({
65
- orgId,
66
- projectId,
67
- userId: user.id,
68
- environmentId: args.env
69
- });
70
- }
71
-
72
- if (projectEnvironment.org.id !== orgId) {
73
- throw new Error(
74
- `Cannot proceed with the deployment because the "${projectEnvironment.name}" project environment doesn't belong to the "${orgId}" organization. Please check your WCP project ID (currently set to "${wcpProjectId}").`
75
- );
76
- }
77
-
78
- if (projectEnvironment.project.id !== projectId) {
79
- throw new Error(
80
- `Cannot proceed with the deployment because the "${projectEnvironment.name}" project environment doesn't belong to the "${wcpProjectId}" project. Please check your WCP project ID (currently set to "${wcpProjectId}").`
81
- );
82
- }
83
-
84
- if (projectEnvironment && projectEnvironment.status !== "enabled") {
85
- throw new Error(
86
- `Cannot proceed with the deployment because the "${projectEnvironment.name}" project environment has been disabled.`
87
- );
88
- }
89
-
90
- // Assign `WCP_PROJECT_ENVIRONMENT` and `WCP_PROJECT_ENVIRONMENT_API_KEY`
91
- const wcpProjectEnvironment = {
92
- id: projectEnvironment.id,
93
- apiKey: projectEnvironment.apiKey,
94
- org: { id: projectEnvironment.org.id },
95
- project: { id: projectEnvironment.project.id }
96
- };
97
-
98
- process.env.WCP_PROJECT_ENVIRONMENT = encrypt(wcpProjectEnvironment);
99
- process.env.WCP_PROJECT_ENVIRONMENT_API_KEY = projectEnvironment.apiKey;
100
- };
101
-
102
- const updateLastActiveOnHookHandler = async () => {
103
- if (!projectEnvironment) {
104
- return;
105
- }
106
-
107
- // Is this a user environment? If so, let's update his "last active" field.
108
- if (projectEnvironment.user) {
109
- await updateUserLastActiveOn();
110
- }
111
- };
112
-
113
- // Export hooks plugins for deploy and watch commands.
114
- module.exports = () => [
115
- // Deploy hook handlers.
116
- {
117
- type: "hook-before-deploy",
118
- name: "hook-before-deploy-environment-get-environment",
119
- hook: getEnvironmentHookHandler
120
- },
121
- {
122
- type: "hook-before-deploy",
123
- name: "hook-before-deploy-update-last-active-on",
124
- hook: updateLastActiveOnHookHandler
125
- },
126
-
127
- // Watch hook handlers.
128
- {
129
- type: "hook-before-watch",
130
- name: "hook-before-watch-environment-get-environment",
131
- hook: getEnvironmentHookHandler
132
- }
133
- ];
@@ -1,8 +0,0 @@
1
- const { command: login } = require("./login");
2
- const { command: logout } = require("./logout");
3
- const { command: whoami } = require("./whoami");
4
- const { command: project } = require("./project");
5
-
6
- const hooks = require("./hooks");
7
-
8
- module.exports = [login(), logout(), whoami(), project(), hooks()];
@@ -1,227 +0,0 @@
1
- const open = require("open");
2
- const { GraphQLClient } = require("graphql-request");
3
- const { setProjectId, setWcpPat, sleep } = require("./utils");
4
- const chalk = require("chalk");
5
- const { getWcpGqlApiUrl, getWcpAppUrl } = require("@webiny/wcp");
6
-
7
- // 120 retries * 2000ms interval = 4 minutes until the command returns an error.
8
- const LOGIN_RETRIES_COUNT = 30;
9
- const LOGIN_RETRIES_INTERVAL = 2000;
10
-
11
- const USER_PAT_FIELDS = /* GraphQL */ `
12
- fragment UserPatFields on UserPat {
13
- name
14
- meta
15
- token
16
- expiresOn
17
- user {
18
- email
19
- }
20
- }
21
- `;
22
-
23
- const GENERATE_USER_PAT = /* GraphQL */ `
24
- mutation GenerateUserPat {
25
- users {
26
- generateUserPat
27
- }
28
- }
29
- `;
30
-
31
- const GET_USER_PAT = /* GraphQL */ `
32
- ${USER_PAT_FIELDS}
33
- query GetUserPat($token: ID!) {
34
- users {
35
- getUserPat(token: $token) {
36
- ...UserPatFields
37
- }
38
- }
39
- }
40
- `;
41
-
42
- const CREATE_USER_PAT = /* GraphQL */ `
43
- ${USER_PAT_FIELDS}
44
- mutation CreateUserPat($expiresIn: Int, $token: ID, $data: CreateUserPatDataInput) {
45
- users {
46
- createUserPat(expiresIn: $expiresIn, token: $token, data: $data) {
47
- ...UserPatFields
48
- }
49
- }
50
- }
51
- `;
52
-
53
- module.exports.command = () => ({
54
- type: "cli-command",
55
- name: "cli-command-wcp-login",
56
- create({ yargs, context }) {
57
- yargs.command(
58
- "login [pat]",
59
- `Log in to the Webiny Control Panel`,
60
- yargs => {
61
- yargs.example("$0 login");
62
- yargs.positional("pat", {
63
- describe: `Personal access token (PAT)`,
64
- type: "string"
65
- });
66
- yargs.option("debug", {
67
- describe: `Turn on debug logs`,
68
- type: "boolean"
69
- });
70
- yargs.option("debug-level", {
71
- default: 1,
72
- describe: `Set the debug logs verbosity level`,
73
- type: "number"
74
- });
75
- },
76
- async ({ debug, debugLevel, pat: patFromParams }) => {
77
- const graphQLClient = new GraphQLClient(getWcpGqlApiUrl());
78
-
79
- let pat;
80
-
81
- if (patFromParams) {
82
- try {
83
- graphQLClient.setHeaders({ authorization: patFromParams });
84
- pat = await graphQLClient
85
- .request(GET_USER_PAT, { token: patFromParams })
86
- .then(({ users }) => users.getUserPat);
87
-
88
- // If we've received a PAT that has expiration, let's create a long-lived PAT.
89
- // We don't want to have our users interrupted because of an expired PAT.
90
- if (pat.expiresOn) {
91
- pat = await graphQLClient
92
- .request(CREATE_USER_PAT, { data: { meta: pat.meta } })
93
- .then(({ users }) => users.createUserPat);
94
- }
95
- } catch (e) {
96
- if (debug) {
97
- context.debug(
98
- `Could not use the provided %s PAT because of the following error:`,
99
- patFromParams
100
- );
101
- console.debug(e);
102
- }
103
-
104
- throw new Error(
105
- `Invalid PAT received. Please try again or login manually via the ${context.error.hl(
106
- "yarn webiny login"
107
- )} command.`
108
- );
109
- }
110
- } else {
111
- const generatedPat = await graphQLClient
112
- .request(GENERATE_USER_PAT)
113
- .then(({ users }) => users.generateUserPat);
114
-
115
- const queryParams = `pat=${generatedPat}&pat_name=${encodeURIComponent(
116
- "Webiny CLI"
117
- )}&ref=cli`;
118
- const openUrl = `${getWcpAppUrl()}/login/cli?${queryParams}`;
119
-
120
- debug && context.debug(`Opening %s...`, openUrl);
121
- await open(openUrl);
122
-
123
- const graphql = {
124
- variables: { token: generatedPat },
125
- headers: {
126
- Authorization: generatedPat
127
- }
128
- };
129
-
130
- graphQLClient.setHeaders(graphql.headers);
131
-
132
- let retries = 0;
133
- const result = await new Promise(resolve => {
134
- const interval = setInterval(async () => {
135
- retries++;
136
- if (retries > LOGIN_RETRIES_COUNT) {
137
- clearInterval(interval);
138
- resolve(null);
139
- }
140
-
141
- try {
142
- const pat = await graphQLClient
143
- .request(GET_USER_PAT, graphql.variables)
144
- .then(({ users }) => users.getUserPat);
145
-
146
- clearInterval(interval);
147
- resolve(pat);
148
- } catch (e) {
149
- // Do nothing.
150
- if (debug) {
151
- context.debug(
152
- `Could not login. Will try again in ${LOGIN_RETRIES_INTERVAL}ms.`
153
- );
154
- if (debugLevel > 1) {
155
- context.debug("GraphQL Request: ");
156
- console.log(JSON.stringify(graphql, null, 2));
157
- }
158
- if (debugLevel > 2) {
159
- context.debug(e.message);
160
- }
161
- }
162
- }
163
- }, LOGIN_RETRIES_INTERVAL);
164
- });
165
-
166
- if (!result) {
167
- throw new Error(
168
- `Could not login. Did you complete the sign in / sign up process at ${getWcpAppUrl()}?`
169
- );
170
- }
171
-
172
- pat = result;
173
- }
174
-
175
- setWcpPat(pat.token);
176
-
177
- console.log(
178
- `${chalk.green("✔")} You've successfully logged in to Webiny Control Panel.`
179
- );
180
-
181
- let projectInitialized = Boolean(
182
- context.project.config.id || process.env.WCP_PROJECT_ID
183
- );
184
-
185
- // If we have `orgId` and `projectId` in PAT's metadata, let's immediately link the project.
186
- if (pat.meta && pat.meta.orgId && pat.meta.projectId) {
187
- await sleep();
188
-
189
- console.log();
190
-
191
- const { orgId, projectId } = pat.meta;
192
-
193
- const id = `${orgId}/${projectId}`;
194
- console.log(`Project ${chalk.green(id)} detected. Linking...`);
195
-
196
- await sleep();
197
-
198
- await setProjectId({
199
- project: context.project,
200
- orgId,
201
- projectId
202
- });
203
-
204
- console.log(`Project ${context.success.hl(id)} linked successfully.`);
205
- projectInitialized = true;
206
- }
207
-
208
- await sleep();
209
-
210
- console.log();
211
- console.log(chalk.bold("Next Steps"));
212
-
213
- if (!projectInitialized) {
214
- console.log(
215
- `‣ link your project via the ${chalk.green(
216
- "yarn webiny project link"
217
- )} command`
218
- );
219
- }
220
-
221
- console.log(
222
- `‣ deploy your project via the ${chalk.green("yarn webiny deploy")} command`
223
- );
224
- }
225
- );
226
- }
227
- });
@@ -1,28 +0,0 @@
1
- const { setWcpPat } = require("./utils");
2
-
3
- module.exports.command = () => ({
4
- type: "cli-command",
5
- name: "cli-command-wcp-logout",
6
- create({ yargs }) {
7
- yargs.command(
8
- "logout",
9
- `Log out from the Webiny Control Panel`,
10
- yargs => {
11
- yargs.example("$0 logout");
12
- yargs.option("debug", {
13
- describe: `Turn on debug logs`,
14
- type: "boolean"
15
- });
16
- yargs.option("debug-level", {
17
- default: 1,
18
- describe: `Set the debug logs verbosity level`,
19
- type: "number"
20
- });
21
- },
22
- async () => {
23
- setWcpPat(null);
24
- console.log(`You've successfully logged out from Webiny Control Panel.`);
25
- }
26
- );
27
- }
28
- });