@cloudcommerce/cli 0.0.27 → 0.0.30

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.
@@ -1,5 +1,5 @@
1
- @cloudcommerce/cli:build: cache hit, replaying output a996dada591e884a
2
- @cloudcommerce/cli:build: 
3
- @cloudcommerce/cli:build: > @cloudcommerce/cli@0.0.26 build /home/leo/code/ecomplus/cloud-commerce/packages/cli
4
- @cloudcommerce/cli:build: > sh ../../scripts/build-lib.sh
5
- @cloudcommerce/cli:build: 
1
+ @cloudcommerce/cli:build: cache hit, replaying output 03ffef12ef5d212e
2
+ @cloudcommerce/cli:build: 
3
+ @cloudcommerce/cli:build: > @cloudcommerce/cli@0.0.29 build /home/leo/code/ecomplus/cloud-commerce/packages/cli
4
+ @cloudcommerce/cli:build: > sh ../../scripts/build-lib.sh
5
+ @cloudcommerce/cli:build: 
package/create-auth.js ADDED
@@ -0,0 +1,5 @@
1
+ import createAuth from './lib/create-auth.js';
2
+
3
+ export default createAuth;
4
+
5
+ export { createAuth };
@@ -0,0 +1,48 @@
1
+ import { fetch } from 'zx';
2
+ import api from '@cloudcommerce/api';
3
+
4
+ const defaultAgent = {
5
+ name: 'Cloud Commerce default agent',
6
+ email: 'cloudcommerce-noreply@e-com.plus',
7
+ };
8
+
9
+ export default async (storeId, accessToken) => {
10
+ const apiConfig = {
11
+ storeId,
12
+ accessToken,
13
+ fetch,
14
+ };
15
+ const { data } = await api.get('authentications', {
16
+ ...apiConfig,
17
+ params: {
18
+ email: defaultAgent.email,
19
+ limit: 1,
20
+ },
21
+ });
22
+ let authenticationId = data.result[0]?._id;
23
+ if (!authenticationId) {
24
+ const { data: { _id } } = await api.post('authentications', {
25
+ ...defaultAgent,
26
+ username: `cloudcomm${Date.now()}`,
27
+ permissions: {
28
+ applications: ['all'],
29
+ brands: ['GET'],
30
+ carts: ['all'],
31
+ categories: ['GET'],
32
+ collections: ['GET'],
33
+ customers: ['all'],
34
+ grids: ['GET'],
35
+ orders: ['GET', 'POST', 'PATCH'],
36
+ products: ['GET', 'PATCH'],
37
+ stores: ['GET'],
38
+ },
39
+ }, apiConfig);
40
+ authenticationId = _id;
41
+ }
42
+ const { data: apiKey } = await api.get(`authentications/${authenticationId}/api_key`, apiConfig);
43
+ return {
44
+ storeId,
45
+ authenticationId,
46
+ apiKey,
47
+ };
48
+ };
package/lib/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  import url from 'url';
2
2
  import path from 'path';
3
- import { $, argv, fs } from 'zx';
3
+ import {
4
+ $, argv, fs, echo, chalk,
5
+ } from 'zx';
6
+ import login from './login.js';
4
7
 
5
8
  const { FIREBASE_PROJECT_ID, GOOGLE_APPLICATION_CREDENTIALS } = process.env;
6
9
  const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
@@ -32,7 +35,7 @@ if (projectId) {
32
35
  export default async () => {
33
36
  fs.copySync(path.join(__dirname, '..', 'config'), pwd);
34
37
  const options = Object.keys(argv).reduce((opts, key) => {
35
- if (key !== '_') {
38
+ if (key !== '_' && key !== 'deploy' && key !== 'commit') {
36
39
  // eslint-disable-next-line no-param-reassign
37
40
  opts += ` --${key} ${argv[key]}`;
38
41
  }
@@ -53,5 +56,42 @@ export default async () => {
53
56
  if (argv._.includes('deploy')) {
54
57
  return $firebase('deploy');
55
58
  }
59
+ if (argv._.includes('login')) {
60
+ await $firebase('login');
61
+ return login();
62
+ }
63
+ if (argv._.includes('setup')) {
64
+ const { storeId, authenticationId, apiKey } = await login();
65
+ fs.writeFileSync(path.join(pwd, 'functions', '.env'), `ECOM_AUTHENTICATION_ID=${authenticationId}
66
+ ECOM_API_KEY=${apiKey}
67
+ ECOM_STORE_ID=${storeId}
68
+ `);
69
+ if (argv.deploy !== false) {
70
+ await $firebase('deploy');
71
+ }
72
+ if (argv.commit !== false) {
73
+ fs.writeFileSync(path.join(pwd, 'functions', 'config.json'), JSON.stringify({ storeId }, null, 2));
74
+ try {
75
+ await $`git add .firebaserc functions/config.json`;
76
+ await $`git commit -m "Setup store [skip ci]"`;
77
+ await $`git push`;
78
+ } catch (e) {
79
+ //
80
+ }
81
+ }
82
+ return echo`
83
+ ****
84
+
85
+ Finish by saving the following secrets to your GitHub repository:
86
+
87
+ ${chalk.bold('ECOM_AUTHENTICATION_ID')} = ${chalk.bgMagenta(authenticationId)}
88
+
89
+ ${chalk.bold('ECOM_API_KEY')} = ${chalk.bgMagenta(apiKey)}
90
+
91
+ ${chalk.bold('FIREBASE_SERVICE_ACCOUNT')} = {YOUR_SERVICE_ACCOUNT_JSON}
92
+
93
+ -- More info at https://github.com/ecomplus/store#getting-started
94
+ `;
95
+ }
56
96
  return $`echo 'Hello from @cloudcommerce/cli'`;
57
97
  };
package/lib/login.js ADDED
@@ -0,0 +1,41 @@
1
+ import * as readline from 'node:readline';
2
+ import { question, echo, fetch } from 'zx';
3
+ import md5 from 'md5';
4
+ import api from '@cloudcommerce/api';
5
+ import createAuth from './create-auth.js';
6
+
7
+ export default async () => {
8
+ await echo`-- Login with your E-Com Plus store admin account.
9
+ (i) same credentials used to enter the dashboard (https://ecomplus.app/)
10
+ `;
11
+ const username = await question('E-Com Plus username: ');
12
+ const passMd5 = await new Promise((resolve) => {
13
+ const rl = readline.createInterface({
14
+ input: process.stdin,
15
+ output: process.stdout,
16
+ });
17
+ rl.stdoutMuted = true;
18
+ rl.question('Password: ', (password) => {
19
+ rl.close();
20
+ rl.history = rl.history.slice(1);
21
+ resolve(md5(password));
22
+ });
23
+ rl._writeToOutput = function _writeToOutput(stringToWrite) {
24
+ if (rl.stdoutMuted) {
25
+ rl.output.write('*');
26
+ } else {
27
+ rl.output.write(stringToWrite);
28
+ }
29
+ };
30
+ });
31
+ const apiConfig = {
32
+ fetch,
33
+ };
34
+ const { data: login } = await api.post('login', {
35
+ username,
36
+ pass_md5_hash: passMd5,
37
+ }, apiConfig);
38
+ const storeId = login.store_ids[0];
39
+ const { data: { access_token: accessToken } } = await api.post('authenticate', login, apiConfig);
40
+ return createAuth(storeId, accessToken);
41
+ };
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@cloudcommerce/cli",
3
3
  "type": "module",
4
- "version": "0.0.27",
4
+ "version": "0.0.30",
5
5
  "description": "E-Com Plus Cloud Commerce CLI tools",
6
6
  "bin": {
7
7
  "cloudcommerce": "./bin/run.mjs"
8
8
  },
9
9
  "main": "lib/index.js",
10
+ "exports": {
11
+ ".": "./lib/index.js",
12
+ "./create-auth": "./create-auth.js"
13
+ },
10
14
  "repository": {
11
15
  "type": "git",
12
16
  "url": "git+https://github.com/ecomplus/cloud-commerce.git",
@@ -19,6 +23,8 @@
19
23
  },
20
24
  "homepage": "https://github.com/ecomplus/cloud-commerce/tree/main/packages/cli#readme",
21
25
  "dependencies": {
26
+ "@cloudcommerce/api": "0.0.30",
27
+ "md5": "^2.3.0",
22
28
  "zx": "^7.0.7"
23
29
  },
24
30
  "scripts": {
@@ -0,0 +1,53 @@
1
+ import { fetch } from 'zx';
2
+ import api from '@cloudcommerce/api';
3
+
4
+ const defaultAgent = {
5
+ name: 'Cloud Commerce default agent',
6
+ email: 'cloudcommerce-noreply@e-com.plus',
7
+ };
8
+
9
+ export default async (storeId: number, accessToken: string) => {
10
+ const apiConfig = {
11
+ storeId,
12
+ accessToken,
13
+ fetch: fetch as Window['fetch'],
14
+ };
15
+ const { data } = await api.get('authentications', {
16
+ ...apiConfig,
17
+ params: {
18
+ email: defaultAgent.email,
19
+ limit: 1,
20
+ },
21
+ });
22
+
23
+ let authenticationId = data.result[0]?._id;
24
+ if (!authenticationId) {
25
+ const { data: { _id } } = await api.post('authentications', {
26
+ ...defaultAgent,
27
+ username: `cloudcomm${Date.now()}`,
28
+ permissions: {
29
+ applications: ['all'],
30
+ brands: ['GET'],
31
+ carts: ['all'],
32
+ categories: ['GET'],
33
+ collections: ['GET'],
34
+ customers: ['all'],
35
+ grids: ['GET'],
36
+ orders: ['GET', 'POST', 'PATCH'],
37
+ products: ['GET', 'PATCH'],
38
+ stores: ['GET'],
39
+ },
40
+ }, apiConfig);
41
+ authenticationId = _id;
42
+ }
43
+
44
+ const { data: apiKey } = await api.get(
45
+ `authentications/${authenticationId}/api_key`,
46
+ apiConfig,
47
+ ) as { data: string };
48
+ return {
49
+ storeId,
50
+ authenticationId,
51
+ apiKey,
52
+ };
53
+ };
package/src/index.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import url from 'url';
2
2
  import path from 'path';
3
- import { $, argv, fs } from 'zx';
3
+ import {
4
+ $,
5
+ argv,
6
+ fs,
7
+ echo,
8
+ chalk,
9
+ } from 'zx';
10
+ import login from './login';
4
11
 
5
12
  const {
6
13
  FIREBASE_PROJECT_ID,
@@ -41,7 +48,7 @@ export default async () => {
41
48
  fs.copySync(path.join(__dirname, '..', 'config'), pwd);
42
49
 
43
50
  const options = Object.keys(argv).reduce((opts, key) => {
44
- if (key !== '_') {
51
+ if (key !== '_' && key !== 'deploy' && key !== 'commit') {
45
52
  // eslint-disable-next-line no-param-reassign
46
53
  opts += ` --${key} ${argv[key]}`;
47
54
  }
@@ -63,5 +70,51 @@ export default async () => {
63
70
  if (argv._.includes('deploy')) {
64
71
  return $firebase('deploy');
65
72
  }
73
+
74
+ if (argv._.includes('login')) {
75
+ await $firebase('login');
76
+ return login();
77
+ }
78
+
79
+ if (argv._.includes('setup')) {
80
+ const { storeId, authenticationId, apiKey } = await login();
81
+ fs.writeFileSync(
82
+ path.join(pwd, 'functions', '.env'),
83
+ `ECOM_AUTHENTICATION_ID=${authenticationId}
84
+ ECOM_API_KEY=${apiKey}
85
+ ECOM_STORE_ID=${storeId}
86
+ `,
87
+ );
88
+ if (argv.deploy !== false) {
89
+ await $firebase('deploy');
90
+ }
91
+ if (argv.commit !== false) {
92
+ fs.writeFileSync(
93
+ path.join(pwd, 'functions', 'config.json'),
94
+ JSON.stringify({ storeId }, null, 2),
95
+ );
96
+ try {
97
+ await $`git add .firebaserc functions/config.json`;
98
+ await $`git commit -m "Setup store [skip ci]"`;
99
+ await $`git push`;
100
+ } catch (e) {
101
+ //
102
+ }
103
+ }
104
+ return echo`
105
+ ****
106
+
107
+ Finish by saving the following secrets to your GitHub repository:
108
+
109
+ ${chalk.bold('ECOM_AUTHENTICATION_ID')} = ${chalk.bgMagenta(authenticationId)}
110
+
111
+ ${chalk.bold('ECOM_API_KEY')} = ${chalk.bgMagenta(apiKey)}
112
+
113
+ ${chalk.bold('FIREBASE_SERVICE_ACCOUNT')} = {YOUR_SERVICE_ACCOUNT_JSON}
114
+
115
+ -- More info at https://github.com/ecomplus/store#getting-started
116
+ `;
117
+ }
118
+
66
119
  return $`echo 'Hello from @cloudcommerce/cli'`;
67
120
  };
package/src/login.ts ADDED
@@ -0,0 +1,48 @@
1
+ import * as readline from 'node:readline';
2
+ import { question, echo, fetch } from 'zx';
3
+ import md5 from 'md5';
4
+ import api from '@cloudcommerce/api';
5
+ import createAuth from './create-auth';
6
+
7
+ export default async () => {
8
+ await echo`-- Login with your E-Com Plus store admin account.
9
+ (i) same credentials used to enter the dashboard (https://ecomplus.app/)
10
+ `;
11
+ const username = await question('E-Com Plus username: ');
12
+ const passMd5 = await new Promise((resolve) => {
13
+ const rl = readline.createInterface({
14
+ input: process.stdin,
15
+ output: process.stdout,
16
+ }) as any;
17
+ rl.stdoutMuted = true;
18
+ rl.question('Password: ', (password) => {
19
+ rl.close();
20
+ rl.history = rl.history.slice(1);
21
+ resolve(md5(password));
22
+ });
23
+ rl._writeToOutput = function _writeToOutput(stringToWrite) {
24
+ if (rl.stdoutMuted) {
25
+ rl.output.write('*');
26
+ } else {
27
+ rl.output.write(stringToWrite);
28
+ }
29
+ };
30
+ });
31
+
32
+ const apiConfig = {
33
+ fetch: fetch as Window['fetch'],
34
+ };
35
+ const { data: login } = await api.post('login', {
36
+ username,
37
+ pass_md5_hash: passMd5,
38
+ }, apiConfig);
39
+ const storeId = login.store_ids[0];
40
+
41
+ const {
42
+ data: {
43
+ access_token: accessToken,
44
+ },
45
+ } = await api.post('authenticate', login, apiConfig);
46
+
47
+ return createAuth(storeId, accessToken);
48
+ };