@open-wa/wa-automate 4.44.3 → 4.44.4

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.
@@ -105,8 +105,8 @@ exports.optionList = [{
105
105
  name: 'config',
106
106
  alias: 'c',
107
107
  type: String,
108
- typeLabel: '{yellowBright {underline ./config.json}}',
109
- description: "The relative json file that contains the config. By default the system will look for config.json which will override any config variables set. Default: './config.json'."
108
+ typeLabel: '{yellowBright {underline ./cli.config.json}}',
109
+ description: "The relative json file that contains the config. By default the system will look for cli.config.json which will override any config variables set. Default: './cli.config.json'."
110
110
  },
111
111
  {
112
112
  name: 'session',
@@ -1 +1 @@
1
- export declare const tryOpenFileAsObject: (fileLocation: string, needArray?: boolean) => any;
1
+ export declare const tryOpenFileAsObject: (fileLocation: string, needArray?: boolean) => Promise<any>;
@@ -18,6 +18,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
21
30
  var __importDefault = (this && this.__importDefault) || function (mod) {
22
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
32
  };
@@ -27,7 +36,7 @@ const path = __importStar(require("path"));
27
36
  const fs = __importStar(require("fs"));
28
37
  const json5_1 = __importDefault(require("json5"));
29
38
  const logging_1 = require("../logging/logging");
30
- const tryOpenFileAsObject = (fileLocation, needArray = false) => {
39
+ const tryOpenFileAsObject = (fileLocation, needArray = false) => __awaiter(void 0, void 0, void 0, function* () {
31
40
  let res = undefined;
32
41
  let fp = undefined;
33
42
  const relativePath = path.join(path.resolve(process.cwd(), fileLocation || ''));
@@ -40,6 +49,10 @@ const tryOpenFileAsObject = (fileLocation, needArray = false) => {
40
49
  const data = isJs ? (require(fp) || {}).default : json5_1.default.parse(fs.readFileSync(fp, 'utf8'));
41
50
  if (data && (Array.isArray(data) == needArray))
42
51
  res = data;
52
+ if (data && typeof data === "function") {
53
+ logging_1.log.info("Found config as function, executing.");
54
+ res = yield data(process.env.CURRENT_SESSION_ID || "session");
55
+ }
43
56
  }
44
57
  catch (error) {
45
58
  throw `Unable to parse config file as JSON. Please make sure ${fp} is a valid JSON config file`;
@@ -49,5 +62,5 @@ const tryOpenFileAsObject = (fileLocation, needArray = false) => {
49
62
  return;
50
63
  logging_1.log.info(`${fp} is ${res ? 'valid' : 'invalid'}`);
51
64
  return res && Object.assign(Object.assign({}, (res || {})), { confPath: fp });
52
- };
65
+ });
53
66
  exports.tryOpenFileAsObject = tryOpenFileAsObject;
package/dist/cli/index.js CHANGED
@@ -38,7 +38,7 @@ const ready = (config) => __awaiter(void 0, void 0, void 0, function* () {
38
38
  });
39
39
  function start() {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
- const { cliConfig, createConfig, PORT, spinner } = (0, setup_1.cli)();
41
+ const { cliConfig, createConfig, PORT, spinner } = yield (0, setup_1.cli)();
42
42
  process.env.OWA_CLI = "true";
43
43
  spinner.start("Launching EASY API");
44
44
  (0, server_1.setUpExpressApp)();
@@ -19,12 +19,12 @@ export declare const cliOptionNames: import("type-fest").Simplify<import("type-f
19
19
  export declare const meowFlags: () => AnyFlags;
20
20
  export declare const helptext: string;
21
21
  export declare const envArgs: () => JsonObject;
22
- export declare const configFile: (config?: string) => JsonObject;
23
- export declare const cli: () => {
22
+ export declare const configFile: (config?: string) => Promise<JsonObject>;
23
+ export declare const cli: () => Promise<{
24
24
  createConfig: ConfigObject;
25
25
  cliConfig: Merge<ConfigObject, {
26
26
  [k: string]: any;
27
27
  }>;
28
28
  PORT: number;
29
29
  spinner: Spin;
30
- };
30
+ }>;
package/dist/cli/setup.js CHANGED
@@ -43,6 +43,7 @@ const uuid_apikey_1 = __importDefault(require("uuid-apikey"));
43
43
  const events_1 = require("../controllers/events");
44
44
  const is_url_superb_1 = __importDefault(require("is-url-superb"));
45
45
  const path = __importStar(require("path"));
46
+ const fs = __importStar(require("fs"));
46
47
  const logging_1 = require("../logging/logging");
47
48
  const cli_options_1 = require("./cli-options");
48
49
  let checkUrl = url => typeof url === 'string' ? (0, is_url_superb_1.default)(url) : false;
@@ -123,44 +124,56 @@ const envArgs = () => {
123
124
  return env;
124
125
  };
125
126
  exports.envArgs = envArgs;
126
- const configFile = (config) => {
127
- let confFile = {};
127
+ const configFile = (config) => __awaiter(void 0, void 0, void 0, function* () {
128
+ let confFile = null;
128
129
  const conf = config || process.env.WA_CLI_CONFIG;
129
- const backup = () => {
130
+ //check if it is a directory:
131
+ const isDir = fs.existsSync(conf) && fs.lstatSync(conf).isDirectory();
132
+ logging_1.log.info(`Config ${config} is directory: ${isDir}`);
133
+ const backup = () => __awaiter(void 0, void 0, void 0, function* () {
130
134
  if (!confFile)
131
- confFile = (0, file_utils_1.tryOpenFileAsObject)(`cli.config.json`);
135
+ confFile = yield (0, file_utils_1.tryOpenFileAsObject)(`cli.config.json`);
132
136
  if (!confFile)
133
- confFile = (0, file_utils_1.tryOpenFileAsObject)(`cli.config.js`);
134
- };
135
- const attempt = (firstAttempt) => {
137
+ confFile = yield (0, file_utils_1.tryOpenFileAsObject)(`cli.config.js`);
138
+ });
139
+ const attempt = (firstAttempt, skipBackup) => __awaiter(void 0, void 0, void 0, function* () {
136
140
  try {
137
- confFile = (0, file_utils_1.tryOpenFileAsObject)(firstAttempt || `cli.config.json`);
138
- backup();
141
+ if (!confFile)
142
+ confFile = yield (0, file_utils_1.tryOpenFileAsObject)(firstAttempt || `cli.config.json`);
143
+ if (!skipBackup)
144
+ yield backup();
139
145
  }
140
146
  catch (error) {
141
147
  logging_1.log.error(error);
142
148
  logging_1.log.error("Trying cli.config.js");
143
- backup();
149
+ yield backup();
144
150
  }
145
- };
151
+ });
146
152
  if (conf) {
147
153
  if ((0, tools_1.isBase64)(conf)) {
148
154
  confFile = JSON.parse(Buffer.from(conf, 'base64').toString('ascii'));
149
155
  }
150
156
  else {
151
- attempt(conf);
157
+ if (isDir) {
158
+ yield attempt(`${isDir && conf}/cli.config.json`, true);
159
+ yield attempt(`${isDir && conf}/cli.config.js`, true);
160
+ yield backup();
161
+ }
162
+ else
163
+ attempt(conf);
152
164
  if (!confFile)
153
165
  console.error(`Unable to read config file json: ${conf}`);
154
166
  }
155
167
  }
156
168
  else {
157
- attempt();
169
+ yield attempt();
158
170
  }
159
171
  logging_1.log.info(`Using config file: ${(confFile || {}).confPath || "???"}`);
160
- return confFile;
161
- };
172
+ return confFile || {};
173
+ });
162
174
  exports.configFile = configFile;
163
- const cli = () => {
175
+ const cli = () => __awaiter(void 0, void 0, void 0, function* () {
176
+ var _a;
164
177
  let loggingSetup = false;
165
178
  const _cli = (0, meow_1.default)(exports.helptext, {
166
179
  flags: Object.assign(Object.assign({}, (0, exports.meowFlags)()), { popup: {
@@ -169,6 +182,7 @@ const cli = () => {
169
182
  } }),
170
183
  booleanDefault: undefined
171
184
  });
185
+ process.env.CURRENT_SESSION_ID = ((_a = _cli.flags) === null || _a === void 0 ? void 0 : _a.sessionId) || process.env.WA_SESSION_ID || 'session';
172
186
  const _setupLogging = (_config) => {
173
187
  if (loggingSetup)
174
188
  return;
@@ -191,11 +205,13 @@ const cli = () => {
191
205
  * 2. Config file
192
206
  * 3. CLI flags
193
207
  */
194
- const nonCliConfigs = Object.assign(Object.assign({}, (0, exports.envArgs)()), ((0, exports.configFile)(_cli.flags.config) || {}));
208
+ const resolvedConfigFromFile = ((yield (0, exports.configFile)(_cli.flags.config)) || {});
209
+ const nonCliConfigs = Object.assign(Object.assign({}, (0, exports.envArgs)()), resolvedConfigFromFile);
195
210
  cli_options_1.optionList.filter(option => option.default);
196
211
  const cliConfig = Object.assign(Object.assign(Object.assign({ sessionId: "session" }, nonCliConfigs), _cli.flags), exports.optionKeysWithDefalts.reduce((p, c) => nonCliConfigs.hasOwnProperty(c) ? Object.assign(Object.assign({}, p), { [c]: nonCliConfigs[c] }) : p, {}));
197
212
  //firstly set up logger
198
213
  _setupLogging(cliConfig);
214
+ process.env.CURRENT_SESSION_ID = cliConfig.sessionId;
199
215
  const PORT = Number((typeof cliConfig.forcePort === "boolean" && cliConfig.forcePort ? process.env.PORT : cliConfig.forcePort) || cliConfig.port || process.env.PORT || 8080);
200
216
  const spinner = new events_1.Spin(cliConfig.sessionId, 'STARTUP', cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.disableSpins);
201
217
  const createConfig = Object.assign({}, cliConfig);
@@ -282,5 +298,5 @@ const cli = () => {
282
298
  return {
283
299
  createConfig, cliConfig, PORT, spinner
284
300
  };
285
- };
301
+ });
286
302
  exports.cli = cli;
@@ -191,7 +191,7 @@ function initPage(sessionId, config, qrManager, customUserAgent, spinner, _page,
191
191
  spinner === null || spinner === void 0 ? void 0 : spinner.fail(`${error instanceof pico_s3_1.FileNotFoundError ? 'The session data file was not found in the cloud storage bucket' : 'Something went wrong while fetching session data from cloud storage bucket'}. Continuing...`);
192
192
  }
193
193
  }
194
- if (sessionjson) {
194
+ if (sessionjson && Object.keys(sessionjson).length) {
195
195
  spinner === null || spinner === void 0 ? void 0 : spinner.info(config.multiDevice ? "multi-device enabled. Session data skipped..." : 'Existing session data detected. Injecting...');
196
196
  if (!(config === null || config === void 0 ? void 0 : config.multiDevice))
197
197
  yield waPage.evaluateOnNewDocument(session => {
@@ -204,14 +204,15 @@ function initPage(sessionId, config, qrManager, customUserAgent, spinner, _page,
204
204
  if (config === null || config === void 0 ? void 0 : config.multiDevice) {
205
205
  spinner === null || spinner === void 0 ? void 0 : spinner.info("No session data detected. Opting in for MD.");
206
206
  spinner === null || spinner === void 0 ? void 0 : spinner.info("Make sure to keep the session alive for at least 5 minutes after scanning the QR code before trying to restart a session!!");
207
- yield waPage.evaluateOnNewDocument(session => {
208
- localStorage.clear();
209
- Object.keys(session).forEach(key => localStorage.setItem(key, session[key]));
210
- }, {
211
- "md-opted-in": "true",
212
- "MdUpgradeWamFlag": "true",
213
- "remember-me": "true"
214
- });
207
+ if (config === null || config === void 0 ? void 0 : config.legacy)
208
+ yield waPage.evaluateOnNewDocument(session => {
209
+ localStorage.clear();
210
+ Object.keys(session).forEach(key => localStorage.setItem(key, session[key]));
211
+ }, {
212
+ "md-opted-in": "true",
213
+ "MdUpgradeWamFlag": "true",
214
+ "remember-me": "true"
215
+ });
215
216
  }
216
217
  }
217
218
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.44.3",
3
+ "version": "4.44.4",
4
4
  "licenseCheckUrl": "https://funcs.openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://funcs.openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",