@commercelayer/cli-core 4.11.4 → 4.12.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.
- package/lib/cjs/api.js +107 -1
- package/lib/cjs/application.js +36 -1
- package/lib/cjs/color.js +116 -1
- package/lib/cjs/command.js +95 -1
- package/lib/cjs/config.js +182 -1
- package/lib/cjs/filter.js +81 -1
- package/lib/cjs/help.js +103 -5
- package/lib/cjs/index.js +46 -1
- package/lib/cjs/inflector.js +255 -1
- package/lib/cjs/jsonapi.js +38 -1
- package/lib/cjs/output.js +83 -3
- package/lib/cjs/raw.js +49 -1
- package/lib/cjs/schema.js +20 -1
- package/lib/cjs/style.js +39 -1
- package/lib/cjs/symbol.js +28 -1
- package/lib/cjs/text.js +23 -1
- package/lib/cjs/token.js +177 -1
- package/lib/cjs/update.js +22 -4
- package/lib/cjs/util.js +59 -2
- package/lib/esm/api.js +93 -1
- package/lib/esm/application.js +26 -1
- package/lib/esm/color.js +110 -1
- package/lib/esm/command.js +90 -1
- package/lib/esm/config.js +180 -1
- package/lib/esm/filter.js +71 -1
- package/lib/esm/help.js +100 -5
- package/lib/esm/index.js +26 -1
- package/lib/esm/inflector.js +253 -1
- package/lib/esm/jsonapi.js +42 -1
- package/lib/esm/output.js +72 -3
- package/lib/esm/raw.js +42 -1
- package/lib/esm/schema.js +14 -1
- package/lib/esm/style.js +19 -1
- package/lib/esm/symbol.js +25 -1
- package/lib/esm/text.js +13 -1
- package/lib/esm/token.js +166 -1
- package/lib/esm/update.js +16 -4
- package/lib/esm/util.js +51 -2
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -17
package/lib/cjs/raw.js
CHANGED
|
@@ -1 +1,49 @@
|
|
|
1
|
-
"use strict";
|
|
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.Operation = exports.rawRequest = exports.readDataFile = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const _1 = require(".");
|
|
10
|
+
const rawRequest = async (config, data, id) => {
|
|
11
|
+
return await axios_1.default.request({
|
|
12
|
+
method: config.operation,
|
|
13
|
+
baseURL: config.baseUrl,
|
|
14
|
+
url: `/api/${config.resource}` + (id ? `/${id}` : ''),
|
|
15
|
+
headers: {
|
|
16
|
+
'Content-Type': 'application/vnd.api+json',
|
|
17
|
+
Accept: 'application/vnd.api+json',
|
|
18
|
+
Authorization: `Bearer ${config.accessToken}`,
|
|
19
|
+
},
|
|
20
|
+
data,
|
|
21
|
+
}).then(res => {
|
|
22
|
+
return res.data;
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
exports.rawRequest = rawRequest;
|
|
26
|
+
const readDataFile = (file) => {
|
|
27
|
+
let dataFile;
|
|
28
|
+
let dataJson;
|
|
29
|
+
try {
|
|
30
|
+
dataFile = (0, fs_1.readFileSync)(file, 'utf-8');
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
throw new Error(`Unable to find or open the data file ${_1.clColor.msg.error(file)}`);
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
dataJson = JSON.parse(dataFile);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new Error(`Unable to parse the data file ${_1.clColor.msg.error(file)}: invalid JSON format`);
|
|
40
|
+
}
|
|
41
|
+
const data = dataJson.data ? dataJson : { data: dataJson };
|
|
42
|
+
return data;
|
|
43
|
+
};
|
|
44
|
+
exports.readDataFile = readDataFile;
|
|
45
|
+
var Operation;
|
|
46
|
+
(function (Operation) {
|
|
47
|
+
Operation["Create"] = "POST";
|
|
48
|
+
Operation["Update"] = "PATCH";
|
|
49
|
+
})(Operation || (exports.Operation = Operation = {}));
|
package/lib/cjs/schema.js
CHANGED
|
@@ -1 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
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.download = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const config_1 = __importDefault(require("./config"));
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
const downloadSchema = async (version) => {
|
|
11
|
+
const domain = config_1.default.api.default_app_domain;
|
|
12
|
+
const baseUrl = `https://data.${domain}/schemas/`;
|
|
13
|
+
const schemaVersion = version ? ('_' + version.replace(/\./g, '-')) : '';
|
|
14
|
+
const fileName = `openapi${schemaVersion}.json`;
|
|
15
|
+
const schemaUrl = baseUrl + fileName;
|
|
16
|
+
const schemaFile = await axios_1.default.get(schemaUrl);
|
|
17
|
+
const schemaData = schemaFile.data;
|
|
18
|
+
return schemaData;
|
|
19
|
+
};
|
|
20
|
+
exports.download = downloadSchema;
|
package/lib/cjs/style.js
CHANGED
|
@@ -1 +1,39 @@
|
|
|
1
|
-
"use strict";
|
|
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.highlight = exports.warning = exports.success = exports.error = exports.flag = exports.command = exports.id = exports.slug = exports.magenta = exports.cyan = exports.blue = exports.yellow = exports.green = exports.red = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
// Colors
|
|
9
|
+
const red = (txt) => { return chalk_1.default.red(txt); };
|
|
10
|
+
exports.red = red;
|
|
11
|
+
const green = (txt) => { return chalk_1.default.green(txt); };
|
|
12
|
+
exports.green = green;
|
|
13
|
+
const yellow = (txt) => { return chalk_1.default.yellow(txt); };
|
|
14
|
+
exports.yellow = yellow;
|
|
15
|
+
const blue = (txt) => { return chalk_1.default.blue(txt); };
|
|
16
|
+
exports.blue = blue;
|
|
17
|
+
const cyan = (txt) => { return chalk_1.default.cyan(txt); };
|
|
18
|
+
exports.cyan = cyan;
|
|
19
|
+
const magenta = (txt) => { return chalk_1.default.magenta(txt); };
|
|
20
|
+
exports.magenta = magenta;
|
|
21
|
+
// API
|
|
22
|
+
const slug = (txt) => { return chalk_1.default.yellowBright(txt); };
|
|
23
|
+
exports.slug = slug;
|
|
24
|
+
const id = (txt) => { return chalk_1.default.magenta.bold(txt); };
|
|
25
|
+
exports.id = id;
|
|
26
|
+
// CLI
|
|
27
|
+
const command = (txt) => { return chalk_1.default.cyan.italic(txt); };
|
|
28
|
+
exports.command = command;
|
|
29
|
+
const flag = (txt) => { return chalk_1.default.magenta.italic(txt); };
|
|
30
|
+
exports.flag = flag;
|
|
31
|
+
// Messages
|
|
32
|
+
const error = (txt) => { return chalk_1.default.red(txt); };
|
|
33
|
+
exports.error = error;
|
|
34
|
+
const success = (txt) => { return chalk_1.default.green(txt); };
|
|
35
|
+
exports.success = success;
|
|
36
|
+
const warning = (txt) => { return chalk_1.default.yellow(txt); };
|
|
37
|
+
exports.warning = warning;
|
|
38
|
+
const highlight = (txt) => { return chalk_1.default.yellowBright(txt); };
|
|
39
|
+
exports.highlight = highlight;
|
package/lib/cjs/symbol.js
CHANGED
|
@@ -1 +1,28 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.symbols = void 0;
|
|
4
|
+
const symbols = {
|
|
5
|
+
check: {
|
|
6
|
+
small: '\u2714', // ✔ (heavy)
|
|
7
|
+
bkgGreen: '\u2705', // ✅ (whiteHeavy)
|
|
8
|
+
squareRoot: '\u221A' // √
|
|
9
|
+
},
|
|
10
|
+
cross: {
|
|
11
|
+
small: '\u2718', // ✘ (heavyBallot)
|
|
12
|
+
red: '\u274C' // ❌ (crossMark)
|
|
13
|
+
},
|
|
14
|
+
clock: {
|
|
15
|
+
stopwatch: '\u23F1' // ⏱
|
|
16
|
+
},
|
|
17
|
+
arrow: {
|
|
18
|
+
down: '\u2193' // ↓ (downArrow)
|
|
19
|
+
},
|
|
20
|
+
selection: {
|
|
21
|
+
fisheye: '\u25C9' // ◉ (fishEye)
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
exports.symbols = symbols;
|
|
25
|
+
// ALIASES
|
|
26
|
+
symbols.check.heavy = symbols.check.small;
|
|
27
|
+
symbols.check.whiteHeavy = symbols.check.bkgGreen;
|
|
28
|
+
symbols.cross.heavyBallot = symbols.cross.small;
|
package/lib/cjs/text.js
CHANGED
|
@@ -1 +1,23 @@
|
|
|
1
|
-
"use strict";
|
|
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.symbols = exports.camelize = exports.singularize = exports.pluralize = exports.capitalize = void 0;
|
|
7
|
+
const inflector_1 = __importDefault(require("./inflector"));
|
|
8
|
+
const capitalize = (str) => {
|
|
9
|
+
if (!str)
|
|
10
|
+
return str;
|
|
11
|
+
let s = str.toLowerCase();
|
|
12
|
+
s = s.substring(0, 1).toUpperCase() + s.substring(1);
|
|
13
|
+
return s;
|
|
14
|
+
};
|
|
15
|
+
exports.capitalize = capitalize;
|
|
16
|
+
const pluralize = inflector_1.default.pluralize;
|
|
17
|
+
exports.pluralize = pluralize;
|
|
18
|
+
const singularize = inflector_1.default.singularize;
|
|
19
|
+
exports.singularize = singularize;
|
|
20
|
+
const camelize = inflector_1.default.camelize;
|
|
21
|
+
exports.camelize = camelize;
|
|
22
|
+
var symbol_1 = require("./symbol");
|
|
23
|
+
Object.defineProperty(exports, "symbols", { enumerable: true, get: function () { return symbol_1.symbols; } });
|
package/lib/cjs/token.js
CHANGED
|
@@ -1 +1,177 @@
|
|
|
1
|
-
"use strict";
|
|
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.getTokenEnvironment = exports.isAccessTokenExpiring = exports.revokeAccessToken = exports.getAccessToken = exports.generateAccessToken = exports.decodeAccessToken = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const config_1 = __importDefault(require("./config"));
|
|
9
|
+
const https_1 = __importDefault(require("https"));
|
|
10
|
+
const application_1 = require("./application");
|
|
11
|
+
const util_1 = require("./util");
|
|
12
|
+
const api_1 = require("./api");
|
|
13
|
+
const errors_1 = require("@oclif/core/lib/errors");
|
|
14
|
+
const js_auth_1 = require("@commercelayer/js-auth");
|
|
15
|
+
/** Decode a Commerce Layer access token */
|
|
16
|
+
const decodeAccessToken = (accessToken) => {
|
|
17
|
+
const info = jsonwebtoken_1.default.decode(accessToken);
|
|
18
|
+
if (info === null)
|
|
19
|
+
throw new Error('Error decoding access token');
|
|
20
|
+
return info;
|
|
21
|
+
};
|
|
22
|
+
exports.decodeAccessToken = decodeAccessToken;
|
|
23
|
+
/** generate a custom access token */
|
|
24
|
+
const generateAccessToken = (token, sharedSecret, minutes) => {
|
|
25
|
+
const tokenData = token;
|
|
26
|
+
const payload = Object.assign(Object.assign({}, tokenData), { exp: Math.floor(Date.now() / 1000) + (minutes * 60), rand: Math.random() });
|
|
27
|
+
const algo = config_1.default.api.token_encoding_algorithm;
|
|
28
|
+
const accessToken = jsonwebtoken_1.default.sign(payload, sharedSecret, { algorithm: algo, noTimestamp: true });
|
|
29
|
+
const info = jsonwebtoken_1.default.verify(accessToken, sharedSecret, { algorithms: [algo] });
|
|
30
|
+
return {
|
|
31
|
+
accessToken,
|
|
32
|
+
info: info,
|
|
33
|
+
expMinutes: minutes,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
exports.generateAccessToken = generateAccessToken;
|
|
37
|
+
const getAccessToken = async (auth) => {
|
|
38
|
+
let accessToken;
|
|
39
|
+
if ((0, application_1.isProvisioningApp)(auth))
|
|
40
|
+
accessToken = await getAccessTokenProvisioning(auth);
|
|
41
|
+
else {
|
|
42
|
+
const scope = auth.scope ? (Array.isArray(auth.scope) ? auth.scope.map(s => s.trim()).join(',') : auth.scope) : '';
|
|
43
|
+
const credentials = {
|
|
44
|
+
clientId: auth.clientId,
|
|
45
|
+
clientSecret: auth.clientSecret,
|
|
46
|
+
slug: auth.slug,
|
|
47
|
+
domain: auth.domain,
|
|
48
|
+
scope
|
|
49
|
+
};
|
|
50
|
+
if (auth.email && auth.password) {
|
|
51
|
+
credentials.username = auth.email;
|
|
52
|
+
credentials.password = auth.password;
|
|
53
|
+
accessToken = await js_auth_1.core.authentication('password', credentials);
|
|
54
|
+
}
|
|
55
|
+
else
|
|
56
|
+
accessToken = await js_auth_1.core.authentication('client_credentials', credentials);
|
|
57
|
+
}
|
|
58
|
+
if (!accessToken)
|
|
59
|
+
throw new Error('Unable to get access token');
|
|
60
|
+
else if (accessToken.error)
|
|
61
|
+
throw new Error(`Unable to get access token: ${accessToken.error}`);
|
|
62
|
+
return accessToken;
|
|
63
|
+
};
|
|
64
|
+
exports.getAccessToken = getAccessToken;
|
|
65
|
+
const getAccessTokenProvisioning = async (auth) => {
|
|
66
|
+
const credentials = {
|
|
67
|
+
clientId: auth.clientId,
|
|
68
|
+
clientSecret: auth.clientSecret,
|
|
69
|
+
domain: auth.domain
|
|
70
|
+
};
|
|
71
|
+
return js_auth_1.provisioning.authentication(credentials);
|
|
72
|
+
};
|
|
73
|
+
const revokeAccessToken = async (app, token, logger) => {
|
|
74
|
+
/*
|
|
75
|
+
return axios
|
|
76
|
+
.post(`${app.baseUrl}/oauth/revoke`, {
|
|
77
|
+
grant_type: 'client_credentials',
|
|
78
|
+
client_id: app.clientId,
|
|
79
|
+
client_secret: app.clientSecret,
|
|
80
|
+
token,
|
|
81
|
+
})
|
|
82
|
+
*/
|
|
83
|
+
const scope = Array.isArray(app.scope) ? app.scope.join(';') : app.scope;
|
|
84
|
+
const data = JSON.stringify({
|
|
85
|
+
grant_type: 'client_credentials',
|
|
86
|
+
client_id: app.clientId,
|
|
87
|
+
client_secret: app.clientSecret,
|
|
88
|
+
scope,
|
|
89
|
+
token,
|
|
90
|
+
});
|
|
91
|
+
const provisioning = (0, application_1.isProvisioningApp)(app);
|
|
92
|
+
const contentType = provisioning ? 'application/vnd.api+json' : 'application/json';
|
|
93
|
+
const slug = provisioning ? 'auth' : app.slug || '';
|
|
94
|
+
const options = {
|
|
95
|
+
hostname: (0, api_1.baseURL)(slug, app.domain).replace('https://', '').replace('http://', ''),
|
|
96
|
+
port: 443,
|
|
97
|
+
path: '/oauth/revoke',
|
|
98
|
+
method: 'POST',
|
|
99
|
+
headers: {
|
|
100
|
+
'Content-Type': contentType,
|
|
101
|
+
'Content-Length': data.length,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
if (logger)
|
|
105
|
+
logger.log(options);
|
|
106
|
+
if (logger)
|
|
107
|
+
logger.log(data);
|
|
108
|
+
let err = false;
|
|
109
|
+
try {
|
|
110
|
+
const req = https_1.default.request(options /* , res => {
|
|
111
|
+
console.log(`statusCode: ${res.statusCode}`)
|
|
112
|
+
|
|
113
|
+
res.on('data', d => {
|
|
114
|
+
process.stdout.write(d)
|
|
115
|
+
})
|
|
116
|
+
} */);
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
118
|
+
req.on('error', error => {
|
|
119
|
+
err = true;
|
|
120
|
+
throw new errors_1.CLIError(error.message || 'Error revoking access token');
|
|
121
|
+
});
|
|
122
|
+
req.write(data);
|
|
123
|
+
req.end();
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
err = true;
|
|
127
|
+
if (logger)
|
|
128
|
+
logger.log(error.message);
|
|
129
|
+
if (error instanceof errors_1.CLIError)
|
|
130
|
+
throw error;
|
|
131
|
+
else
|
|
132
|
+
throw new errors_1.CLIError(error.message || 'Error revoking access token');
|
|
133
|
+
}
|
|
134
|
+
await (0, util_1.sleep)(300);
|
|
135
|
+
if (!err && logger)
|
|
136
|
+
logger.log('Access token revoked');
|
|
137
|
+
};
|
|
138
|
+
exports.revokeAccessToken = revokeAccessToken;
|
|
139
|
+
/*
|
|
140
|
+
const revokeAccessTokenAxios = async (app: AppAuth, token: string, logger?: { log: (msg: any) => void }): Promise<void> => {
|
|
141
|
+
|
|
142
|
+
const scope = Array.isArray(app.scope) ? app.scope.join(';') : app.scope
|
|
143
|
+
|
|
144
|
+
const data = {
|
|
145
|
+
grant_type: 'client_credentials',
|
|
146
|
+
client_id: app.clientId,
|
|
147
|
+
client_secret: app.clientSecret,
|
|
148
|
+
scope,
|
|
149
|
+
token,
|
|
150
|
+
}
|
|
151
|
+
if (logger) logger.log(data)
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
await axios.post(`${baseURL(app.slug, app.domain)}/oauth/revoke`, data, { headers: { 'Content-Type': 'application/json' }})
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (logger) logger.log((error as Error).message)
|
|
157
|
+
if (error instanceof CLIError) throw error
|
|
158
|
+
else throw new CLIError((error as Error).message || 'Error revoking access token')
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
await sleep(300)
|
|
162
|
+
if (logger) logger.log('Access token revoked')
|
|
163
|
+
|
|
164
|
+
}
|
|
165
|
+
*/
|
|
166
|
+
const isAccessTokenExpiring = (tokenData) => {
|
|
167
|
+
const safetyInterval = 30; // secs
|
|
168
|
+
const now = Math.floor(Date.now() / 1000); // secs
|
|
169
|
+
const expiration = Math.floor(new Date(tokenData.expires).getTime() / 1000); // secs
|
|
170
|
+
return (now >= (expiration - safetyInterval));
|
|
171
|
+
};
|
|
172
|
+
exports.isAccessTokenExpiring = isAccessTokenExpiring;
|
|
173
|
+
const getTokenEnvironment = (token) => {
|
|
174
|
+
const decodedToken = ((typeof token === 'string') ? decodeAccessToken(token) : token);
|
|
175
|
+
return decodedToken.test ? 'test' : 'live';
|
|
176
|
+
};
|
|
177
|
+
exports.getTokenEnvironment = getTokenEnvironment;
|
package/lib/cjs/update.js
CHANGED
|
@@ -1,4 +1,22 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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.checkUpdate = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
// import { resolve, join } from 'path'
|
|
9
|
+
const update_notifier_cjs_1 = __importDefault(require("update-notifier-cjs"));
|
|
10
|
+
const UPDATE_CHECK_INTERVAL_HOURS = 1;
|
|
11
|
+
const checkUpdate = (pkg) => {
|
|
12
|
+
const notifier = (0, update_notifier_cjs_1.default)({ pkg, updateCheckInterval: 1000 * 60 * 60 * UPDATE_CHECK_INTERVAL_HOURS });
|
|
13
|
+
if (notifier.update) {
|
|
14
|
+
const pluginMode = true; // resolve(__dirname).includes(join('@commercelayer', 'cli', 'node_modules', pkg.name))
|
|
15
|
+
const updateCommand = pluginMode ? 'commercelayer plugins:update' : '{updateCommand}';
|
|
16
|
+
notifier.notify({
|
|
17
|
+
isGlobal: !pluginMode,
|
|
18
|
+
message: `-= ${chalk_1.default.bgWhite.black.bold(` ${pkg.description} `)} =-\n\nNew version available: ${chalk_1.default.dim('{currentVersion}')} -> ${chalk_1.default.green('{latestVersion}')}\nRun ${chalk_1.default.cyanBright(updateCommand)} to update`,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.checkUpdate = checkUpdate;
|
package/lib/cjs/util.js
CHANGED
|
@@ -1,2 +1,59 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.userAgent = exports.generateGroupUID = exports.specialFolder = exports.log = exports.resetConsole = exports.sleep = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const os_1 = require("os");
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
/** Await ms milliseconds */
|
|
9
|
+
const sleep = async (ms) => {
|
|
10
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
11
|
+
};
|
|
12
|
+
exports.sleep = sleep;
|
|
13
|
+
// Reset terminal style after use of colors and text styles
|
|
14
|
+
const resetConsole = () => {
|
|
15
|
+
// Cursor
|
|
16
|
+
// const showCursor = '\u001B[?25l' // \x1B[?25l
|
|
17
|
+
const showCursor = '\u001B[?25h'; // \x1B[?25h
|
|
18
|
+
// Line wrap
|
|
19
|
+
// const lineWrap = '\u001B[?7l' // \x1B[?7l
|
|
20
|
+
const lineWrap = '\u001B[?7h'; // \x1B[?7h
|
|
21
|
+
// eslint-disable-next-line no-console
|
|
22
|
+
// console.log(`${showCursor}${lineWrap}`)
|
|
23
|
+
process.stdout.write(`${showCursor}${lineWrap}`);
|
|
24
|
+
};
|
|
25
|
+
exports.resetConsole = resetConsole;
|
|
26
|
+
const log = (message = '', ...args) => {
|
|
27
|
+
message = (typeof message === 'string') ? message : (0, util_1.inspect)(message);
|
|
28
|
+
process.stdout.write((0, util_1.format)(message, ...args) + '\n');
|
|
29
|
+
};
|
|
30
|
+
exports.log = log;
|
|
31
|
+
const specialFolder = (filePath, createIfNotExists = false) => {
|
|
32
|
+
const specialFolders = ['desktop', 'home'];
|
|
33
|
+
// Special directory (home / desktop)
|
|
34
|
+
// eslint-disable-next-line no-useless-escape
|
|
35
|
+
const root = filePath.toLowerCase().split(/[\\\/]/g)[0];
|
|
36
|
+
if (specialFolders.includes(root)) {
|
|
37
|
+
let filePrefix = (0, os_1.homedir)();
|
|
38
|
+
if (root === 'desktop')
|
|
39
|
+
filePrefix += `${path_1.sep}Desktop`;
|
|
40
|
+
filePath = filePath.replace(root, filePrefix);
|
|
41
|
+
}
|
|
42
|
+
const fileDir = (0, path_1.dirname)(filePath);
|
|
43
|
+
if (createIfNotExists && !(0, fs_1.existsSync)(fileDir))
|
|
44
|
+
(0, fs_1.mkdirSync)(fileDir, { recursive: true });
|
|
45
|
+
return filePath;
|
|
46
|
+
};
|
|
47
|
+
exports.specialFolder = specialFolder;
|
|
48
|
+
const generateGroupUID = () => {
|
|
49
|
+
const firstPart = Math.trunc(Math.random() * 46656);
|
|
50
|
+
const secondPart = Math.trunc(Math.random() * 46656);
|
|
51
|
+
const firstPartStr = ('000' + firstPart.toString(36)).slice(-3);
|
|
52
|
+
const secondPartStr = ('000' + secondPart.toString(36)).slice(-3);
|
|
53
|
+
return firstPartStr + secondPartStr;
|
|
54
|
+
};
|
|
55
|
+
exports.generateGroupUID = generateGroupUID;
|
|
56
|
+
const userAgent = (config) => {
|
|
57
|
+
return `${config.name.replace(/@commercelayer\/cli-plugin/, 'CLI')}/${config.version}`;
|
|
58
|
+
};
|
|
59
|
+
exports.userAgent = userAgent;
|
package/lib/esm/api.js
CHANGED
|
@@ -1 +1,93 @@
|
|
|
1
|
-
import
|
|
1
|
+
import config from './config';
|
|
2
|
+
import { rawRequest, readDataFile, Operation } from './raw';
|
|
3
|
+
import { denormalize } from './jsonapi';
|
|
4
|
+
/** Build base URL */
|
|
5
|
+
const baseURL = (slug, domain, provisioning) => {
|
|
6
|
+
const subdomain = provisioning ? 'provisioning' : (slug || '');
|
|
7
|
+
return `https://${subdomain.toLowerCase()}.${domain || config.api.default_domain}`;
|
|
8
|
+
};
|
|
9
|
+
/** Extract domain name from URL */
|
|
10
|
+
const extractDomain = (baseUrl) => {
|
|
11
|
+
if (!baseUrl)
|
|
12
|
+
return undefined;
|
|
13
|
+
return baseUrl.substring(baseUrl.indexOf('.') + 1);
|
|
14
|
+
};
|
|
15
|
+
/** Decode API execution mode */
|
|
16
|
+
const execMode = (liveFlag) => {
|
|
17
|
+
return ((liveFlag === true) || (liveFlag === 'live')) ? 'live' : 'test';
|
|
18
|
+
};
|
|
19
|
+
const humanizeResource = (type) => {
|
|
20
|
+
return type.replace(/_/g, ' ');
|
|
21
|
+
};
|
|
22
|
+
export { baseURL, extractDomain, execMode, humanizeResource };
|
|
23
|
+
const CACHEABLE_RESOURCES = [
|
|
24
|
+
'bundles',
|
|
25
|
+
'imports',
|
|
26
|
+
'markets',
|
|
27
|
+
'prices',
|
|
28
|
+
'price_lists',
|
|
29
|
+
'promotions',
|
|
30
|
+
'external_promotions',
|
|
31
|
+
'fixed_amount_promotions',
|
|
32
|
+
'fixed_price_promotions',
|
|
33
|
+
'free_gift_promotions',
|
|
34
|
+
'free_shipping_promotions',
|
|
35
|
+
'percentage_discount_promotions',
|
|
36
|
+
'skus',
|
|
37
|
+
'sku_options',
|
|
38
|
+
'stock_items',
|
|
39
|
+
'stock_locations'
|
|
40
|
+
];
|
|
41
|
+
export const isResourceCacheable = (resource, method) => {
|
|
42
|
+
return CACHEABLE_RESOURCES.includes(resource || '') && ((method || 'get').toLowerCase() === 'get');
|
|
43
|
+
};
|
|
44
|
+
export const liveEnvironment = (env) => {
|
|
45
|
+
return (env === 'live');
|
|
46
|
+
};
|
|
47
|
+
export const requestRateLimitDelay = (options) => {
|
|
48
|
+
const env = (options === null || options === void 0 ? void 0 : options.environment) || 'test';
|
|
49
|
+
const parallelRequests = (options === null || options === void 0 ? void 0 : options.parallelRequests) || 1;
|
|
50
|
+
const resourceCacheable = isResourceCacheable(options === null || options === void 0 ? void 0 : options.resourceType, options === null || options === void 0 ? void 0 : options.method);
|
|
51
|
+
let requestsMaxNumBurst = resourceCacheable ? config.api.requests_max_num_burst_cacheable : config.api.requests_max_num_burst;
|
|
52
|
+
let requestsMaxNumAvg = resourceCacheable ? config.api.requests_max_num_avg_cacheable : config.api.requests_max_num_avg;
|
|
53
|
+
// Test env allows half number of requests than live
|
|
54
|
+
if (env !== 'live') {
|
|
55
|
+
requestsMaxNumBurst = resourceCacheable ? config.api.requests_max_num_burst_test_cacheable : config.api.requests_max_num_burst_test;
|
|
56
|
+
requestsMaxNumAvg = resourceCacheable ? config.api.requests_max_num_avg_test_cacheable : config.api.requests_max_num_avg_test;
|
|
57
|
+
}
|
|
58
|
+
const unitDelayBurst = config.api.requests_max_secs_burst / requestsMaxNumBurst;
|
|
59
|
+
const unitDelayAvg = config.api.requests_max_secs_avg / requestsMaxNumAvg;
|
|
60
|
+
const delayBurst = parallelRequests * unitDelayBurst;
|
|
61
|
+
const delayAvg = parallelRequests * unitDelayAvg;
|
|
62
|
+
// If the total number of requests is known the delay can be optimized
|
|
63
|
+
const totalRequests = options === null || options === void 0 ? void 0 : options.totalRequests;
|
|
64
|
+
let delay = 0;
|
|
65
|
+
if (totalRequests) {
|
|
66
|
+
if (totalRequests > requestsMaxNumBurst) {
|
|
67
|
+
if (totalRequests > requestsMaxNumAvg)
|
|
68
|
+
delay = delayAvg;
|
|
69
|
+
else
|
|
70
|
+
delay = delayBurst;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
delay = Math.max(delayBurst, delayAvg);
|
|
75
|
+
// Msec delay
|
|
76
|
+
delay = delay * 1000;
|
|
77
|
+
if (options === null || options === void 0 ? void 0 : options.minimumDelay)
|
|
78
|
+
delay = Math.max(options.minimumDelay, delay);
|
|
79
|
+
if (options === null || options === void 0 ? void 0 : options.securityDelay)
|
|
80
|
+
delay += options.securityDelay;
|
|
81
|
+
delay = Math.ceil(delay);
|
|
82
|
+
return delay;
|
|
83
|
+
};
|
|
84
|
+
export { rawRequest as requestRaw, readDataFile as requestDataFile, Operation };
|
|
85
|
+
export { denormalize as responseDenormalize };
|
|
86
|
+
export const request = {
|
|
87
|
+
raw: rawRequest,
|
|
88
|
+
readDataFile,
|
|
89
|
+
rateLimitDelay: requestRateLimitDelay
|
|
90
|
+
};
|
|
91
|
+
export const response = {
|
|
92
|
+
denormalize
|
|
93
|
+
};
|
package/lib/esm/application.js
CHANGED
|
@@ -1 +1,26 @@
|
|
|
1
|
-
import
|
|
1
|
+
import config from "./config";
|
|
2
|
+
/** Build application key */
|
|
3
|
+
const appKey = () => {
|
|
4
|
+
return Date.now().toString(36);
|
|
5
|
+
};
|
|
6
|
+
/** Check application key */
|
|
7
|
+
const appKeyValid = (appKey) => {
|
|
8
|
+
return (appKey.key !== undefined) && (appKey.key !== '');
|
|
9
|
+
};
|
|
10
|
+
/** Check if two application keys are equal */
|
|
11
|
+
const appKeyMatch = (app1, app2) => {
|
|
12
|
+
const a1 = app1 !== undefined;
|
|
13
|
+
const a2 = app2 !== undefined;
|
|
14
|
+
return (!a1 && !a2) || (a1 && a2 && (app1.key === app2.key));
|
|
15
|
+
};
|
|
16
|
+
const arrayScope = (scope) => {
|
|
17
|
+
if (!scope)
|
|
18
|
+
return [];
|
|
19
|
+
else
|
|
20
|
+
return Array.isArray(scope) ? scope : [scope];
|
|
21
|
+
};
|
|
22
|
+
const isProvisioningApp = (app) => {
|
|
23
|
+
const scope = arrayScope(app.scope);
|
|
24
|
+
return scope.includes(config.provisioning.scope) || (app.api === 'provisioning');
|
|
25
|
+
};
|
|
26
|
+
export { appKey, appKeyValid, appKeyMatch, arrayScope, isProvisioningApp };
|