apify-cli 0.10.0-beta.2 → 0.11.0-beta.0

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/src/lib/utils.js CHANGED
@@ -14,10 +14,21 @@ const https = require('https');
14
14
  const { ApifyClient } = require('apify-client');
15
15
  const { execSync, spawnSync } = require('child_process');
16
16
  const semver = require('semver');
17
- const { GLOBAL_CONFIGS_FOLDER, AUTH_FILE_PATH, LOCAL_CONFIG_NAME, INPUT_FILE_REG_EXP, DEFAULT_LOCAL_STORAGE_DIR } = require('./consts');
17
+ const {
18
+ GLOBAL_CONFIGS_FOLDER,
19
+ AUTH_FILE_PATH,
20
+ INPUT_FILE_REG_EXP,
21
+ DEFAULT_LOCAL_STORAGE_DIR,
22
+ LOCAL_CONFIG_PATH,
23
+ DEPRECATED_LOCAL_CONFIG_NAME,
24
+ ACTOR_SPECIFICATION_VERSION,
25
+ } = require('./consts');
18
26
  const { ensureFolderExistsSync, rimrafPromised, deleteFile } = require('./files');
19
27
  const { warning, info } = require('./outputs');
20
28
 
29
+ // Properties from apify.json file that will me migrated to actor specs in .actor/actor.json
30
+ const MIGRATED_APIFY_JSON_PROPERTIES = ['name', 'version', 'buildTag'];
31
+
21
32
  const getLocalStorageDir = () => {
22
33
  const envVar = ENV_VARS.LOCAL_STORAGE_DIR;
23
34
 
@@ -77,7 +88,7 @@ const getLoggedClient = async (token) => {
77
88
  ({ token } = loadJson.sync(AUTH_FILE_PATH));
78
89
  }
79
90
 
80
- const apifyClient = new ApifyClient({ token });
91
+ const apifyClient = new ApifyClient({ token, baseUrl: process.env.APIFY_CLIENT_BASE_URL });
81
92
  let userInfo;
82
93
  try {
83
94
  userInfo = await apifyClient.user('me').get();
@@ -91,41 +102,84 @@ const getLoggedClient = async (token) => {
91
102
  return apifyClient;
92
103
  };
93
104
 
94
- const getLocalConfigPath = () => path.join(process.cwd(), LOCAL_CONFIG_NAME);
105
+ const getLocalConfigPath = () => path.join(process.cwd(), LOCAL_CONFIG_PATH);
106
+
107
+ /**
108
+ * @deprecated Use getLocalConfigPath
109
+ * @returns {string}
110
+ */
111
+ const getDeprecatedLocalConfigPath = () => path.join(process.cwd(), DEPRECATED_LOCAL_CONFIG_NAME);
95
112
 
96
- const getLocalConfig = () => {
97
- const localConfigPath = getLocalConfigPath();
98
- if (!fs.existsSync(localConfigPath)) {
113
+ const getJsonFileContent = (filePath) => {
114
+ if (!fs.existsSync(filePath)) {
99
115
  return;
100
116
  }
101
- return loadJson.sync(localConfigPath);
117
+ return loadJson.sync(filePath);
102
118
  };
103
119
 
120
+ const getLocalConfig = () => getJsonFileContent(getLocalConfigPath());
121
+
122
+ /**
123
+ * @deprecated Use getLocalConfig
124
+ * @returns {string}
125
+ */
126
+ const getDeprecatedLocalConfig = () => getJsonFileContent(getDeprecatedLocalConfigPath());
127
+
104
128
  const getLocalConfigOrThrow = async () => {
105
129
  let localConfig = getLocalConfig();
106
- if (!localConfig) {
130
+ let deprecatedLocalConfig = getDeprecatedLocalConfig();
131
+
132
+ if (localConfig && deprecatedLocalConfig) {
133
+ const answer = await inquirer.prompt([{
134
+ name: 'isConfirm',
135
+ type: 'confirm',
136
+ // eslint-disable-next-line max-len
137
+ message: `The new version of Apify CLI uses the "${LOCAL_CONFIG_PATH}" instead of the "apify.json" file. Since we have found both files in your actor directory, "apify.json" will be renamed to "apify.json.deprecated". Going forward, all commands will use "${LOCAL_CONFIG_PATH}". You can read about the differences between the old and the new config at https://github.com/apify/apify-cli/blob/master/MIGRATIONS.md. Do you want to continue?`,
138
+ }]);
139
+ if (!answer.isConfirm) {
140
+ throw new Error('Command can not run with old "apify.json" file present in your actor directory., Please, either rename or remove it.');
141
+ }
142
+ try {
143
+ fs.renameSync(getDeprecatedLocalConfigPath(), `${getDeprecatedLocalConfigPath()}.deprecated`);
144
+ // eslint-disable-next-line max-len
145
+ info(`The "apify.json" file has been renamed to "apify.json.deprecated". The deprecated file is no longer used by the CLI or Apify Console. If you do not need it for some specific purpose, it can be safely deleted.`);
146
+ } catch (e) {
147
+ throw new Error('Failed to rename deprecated "apify.json".');
148
+ }
149
+ }
150
+
151
+ if (!localConfig && !deprecatedLocalConfig) {
107
152
  return {};
108
153
  }
109
- // 27-11-2018: Check if apify.json contains old deprecated structure. If so, updates it.
110
- if (localConfig.version && _.isObject(localConfig.version)) {
154
+
155
+ // If apify.json exists migrate it to .actor/actor.json
156
+ if (!localConfig && deprecatedLocalConfig) {
111
157
  const answer = await inquirer.prompt([{
112
158
  name: 'isConfirm',
113
159
  type: 'confirm',
114
160
  // eslint-disable-next-line max-len
115
- message: 'The new version of Apify CLI uses a new format of the "apify.json" file. Your file will be automatically updated to the new format.',
161
+ message: `The new version of Apify CLI uses the "${LOCAL_CONFIG_PATH}" instead of the "apify.json" file. Your "apify.json" file will be automatically updated to the new format under "${LOCAL_CONFIG_PATH}". The original file will be renamed by adding the ".deprecated" suffix. Do you want to continue?`,
116
162
  }]);
117
- if (answer.isConfirm) {
118
- try {
119
- localConfig = updateLocalConfigStructure(localConfig);
120
- writeJson.sync(getLocalConfigPath(), localConfig);
121
- info('apify.json was updated, do not forget to commit the new version of apify.json to Git repository.');
122
- } catch (e) {
123
- throw new Error('Can not update apify.json structure. '
124
- + 'Follow guide on https://github.com/apify/apify-cli/blob/master/MIGRATIONS.md and update it manually.');
163
+ if (!answer.isConfirm) {
164
+ throw new Error('Command can not run with old apify.json structure. Either let the CLI auto-update it or follow the guide on https://github.com/apify/apify-cli/blob/master/MIGRATIONS.md and update it manually.');
165
+ }
166
+ try {
167
+ // Check if apify.json contains old deprecated structure. If so, updates it.
168
+ if (_.isObject(deprecatedLocalConfig.version)) {
169
+ deprecatedLocalConfig = updateLocalConfigStructure(deprecatedLocalConfig);
125
170
  }
126
- } else {
127
- throw new Error('Command can not run with old apify.json structure. '
128
- + 'Follow guide on https://github.com/apify/apify-cli/blob/master/MIGRATIONS.md and update it manually.');
171
+ localConfig = {
172
+ actorSpecification: ACTOR_SPECIFICATION_VERSION,
173
+ environmentVariables: deprecatedLocalConfig.env || undefined,
174
+ ..._.pick(deprecatedLocalConfig, MIGRATED_APIFY_JSON_PROPERTIES),
175
+ };
176
+
177
+ writeJson.sync(getLocalConfigPath(), localConfig);
178
+ fs.renameSync(getDeprecatedLocalConfigPath(), `${getDeprecatedLocalConfigPath()}.deprecated`);
179
+ // eslint-disable-next-line max-len
180
+ info(`The "apify.json" file has been migrated to "${LOCAL_CONFIG_PATH}" and the original file renamed to "apify.json.deprecated". The deprecated file is no longer used by the CLI or Apify Console. If you do not need it for some specific purpose, it can be safely deleted. Do not forget to commit the new file to your Git repository.`);
181
+ } catch (e) {
182
+ throw new Error(`Can not update "${LOCAL_CONFIG_PATH}" structure. Follow guide on https://github.com/apify/apify-cli/blob/master/MIGRATIONS.md and update it manually.`);
129
183
  }
130
184
  }
131
185
 
@@ -134,7 +188,7 @@ const getLocalConfigOrThrow = async () => {
134
188
 
135
189
  const setLocalConfig = async (localConfig, actDir) => {
136
190
  actDir = actDir || process.cwd();
137
- writeJson.sync(path.join(actDir, LOCAL_CONFIG_NAME), localConfig);
191
+ writeJson.sync(path.join(actDir, LOCAL_CONFIG_PATH), localConfig);
138
192
  };
139
193
 
140
194
  const setLocalEnv = async (actDir) => {
@@ -322,7 +376,7 @@ const outputJobLog = async (job, jobStatus, timeout) => {
322
376
  const { id: logId, status } = job;
323
377
  // In case job was already done just output log
324
378
  if (ACT_JOB_TERMINAL_STATUSES.includes(status)) {
325
- const apifyClient = new ApifyClient();
379
+ const apifyClient = new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
326
380
  const log = await apifyClient.log(logId).get();
327
381
  process.stdout.write(log);
328
382
  }
@@ -455,4 +509,5 @@ module.exports = {
455
509
  getActorLocalFilePaths,
456
510
  createSourceFiles,
457
511
  validateActorName,
512
+ getJsonFileContent,
458
513
  };