apify-cli 1.11.1-beta.6 → 1.11.1-beta.8
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,4 +1,4 @@
|
|
|
1
|
-
import e from"node:process";import t from"chalk";import n from"indent-string";import r from"widest-line";import i from"wrap-ansi";import{appendFileSync as a,chmodSync as s,closeSync as c,constants as l,copyFileSync as u,createWriteStream as f,existsSync as p,fstat as m,mkdirSync as h,openSync as g,readFileSync as _,readdirSync as v,realpathSync as y,renameSync as b,rmSync as x,statSync as S,unlinkSync as C,writeFileSync as w,writeSync as T}from"node:fs";import{ApifyApiError as E,ApifyClient as D,DownloadItemsFormat as O}from"apify-client";import k,{AxiosHeaders as A}from"axios";import{ACTOR_ENV_VARS as j,ACTOR_JOB_STATUSES as M,ACTOR_JOB_TERMINAL_STATUSES as ee,ACTOR_JOB_TYPES as N,ACTOR_NAME as P,ACTOR_SOURCE_TYPES as te,APIFY_ENV_VARS as F,KEY_VALUE_STORE_KEYS as I,LOCAL_ACTOR_ENV_VARS as ne,LOCAL_STORAGE_SUBDIRS as re,MAX_MULTIFILE_BYTES as ie,META_ORIGINS as L,SOURCE_FILE_FORMATS as ae}from"@apify/consts";import{createHmacSignature as oe,cryptoRandomObjectId as se}from"@apify/utilities";import{homedir as ce,platform as le}from"node:os";import ue,{basename as de,dirname as R,isAbsolute as fe,join as z,posix as pe,relative as B,resolve as V,sep as me}from"node:path";import he,{isCI as ge}from"ci-info";import{access as _e,appendFile as ve,mkdir as H,readFile as ye,readdir as be,rename as xe,rm as Se,stat as U,symlink as Ce,unlink as we,writeFile as W}from"node:fs/promises";import{setTimeout as Te}from"node:timers/promises";import{once as Ee}from"node:events";import{inspect as De,parseArgs as Oe,promisify as ke}from"node:util";import{Result as Ae,err as je,none as Me,ok as Ne,some as Pe}from"@sapphire/result";import{execa as Fe}from"execa";import Ie from"configparser";import Le from"@inquirer/input";import Re from"which";import{gt as ze,gte as Be,minVersion as Ve,satisfies as He}from"semver";import Ue from"string-width";import We from"strip-ansi";import{jaroWinkler as Ge}from"@skyra/jaro-winkler";import Ke from"js-levenshtein";import{execFileSync as qe,execSync as Je,spawn as Ye}from"node:child_process";import{DurationFormatter as Xe,Time as Ze,TimeTypes as Qe}from"@sapphire/duration";import{Timestamp as $e}from"@sapphire/timestamp";import et from"adm-zip";import tt from"ajv/dist/2019.js";import{ZipArchive as nt}from"archiver";import rt from"escape-string-regexp";import it from"ignore";import{getEncoding as at}from"istextorbinary";import ot,{Mime as st}from"mime";import ct from"mime/types/other.js";import lt from"mime/types/standard.js";import{glob as ut}from"tinyglobby";import{calculateRunDynamicMemory as dt}from"@apify/actor-memory-expression";import{Separator as ft,createPrompt as pt,isEnterKey as mt,makeTheme as ht,useKeypress as gt,usePrefix as _t,useState as vt}from"@inquirer/core";import{compile as yt}from"json-schema-to-typescript";import{validateInputSchema as bt,validateInputUsingValidator as xt}from"@apify/input_schema";import{getDatasetSchemaValidator as St,getKeyValueStoreSchemaValidator as Ct,getOutputSchemaValidator as wt}from"@apify/json_schemas";import{pipeline as Tt}from"node:stream/promises";import{MemoryStorage as Et}from"@crawlee/memory-storage";import Dt from"cli-table3";import{downloadTemplate as Ot}from"giget";import kt from"jju";import At from"open";import{fetchManifest as jt,manifestUrl as Mt,wrapperManifestUrl as Nt}from"@apify/actor-templates";import Pt from"@inquirer/checkbox";import Ft from"computer-name";import It from"@inquirer/password";import{createServer as Lt}from"node:http";import{createHash as Rt,randomBytes as zt}from"node:crypto";import Bt from"@inquirer/select";import Vt from"node:assert";import{ReadStream as Ht}from"node:tty";import Ut from"detect-indent";import{fileURLToPath as Wt}from"node:url";import Gt from"@root/walk";import Kt from"handlebars";var qt={node:`>=22`};const Jt=`storage`,Yt={$schema:`https://apify.com/schemas/v1/actor.ide.json`,actorSpecification:1,name:null,version:`0.0`,buildTag:`latest`,environmentVariables:{}},Xt=Date.UTC(2026,9,1),Zt=[`SIGINT`,`SIGTERM`,`SIGHUP`],GLOBAL_CONFIGS_FOLDER=()=>{let t=z(ce(),`.apify`);return e.env.__APIFY_INTERNAL_TEST_AUTH_PATH__?z(t,e.env.__APIFY_INTERNAL_TEST_AUTH_PATH__):t},AUTH_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`auth.json`),SECRETS_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`secrets.json`),STATE_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`state.json`),TELEMETRY_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`telemetry.json`),Qt=`apify.json`,G=`.actor`,K=z(G,`actor.json`),$t=qt.node,en=he.GITHUB_ACTIONS?`${e.env.GITHUB_SERVER_URL}/${e.env.GITHUB_REPOSITORY}/actions/runs/${e.env.GITHUB_RUN_ID}`:void 0,tn={"X-Apify-Request-Origin":he.isCI?L.CI:L.CLI,...en&&{"X-Apify-Github-Actions-Run-Url":en}},nn=`3.11.0`,updateLocalJson=async(e,t={},n=null)=>{let r=await ye(e,`utf-8`),i=JSON.parse(r),a;n?(a=i,a[n]={...i[n],...t}):a={...i,...t},await W(e,JSON.stringify(a,null,` `))},ensureFolderExistsSync=(e,t)=>{t||(t=e,e=`.`),t.split(me).reduce((e,t)=>(e=z(e,t),p(e)||h(e),e),e)},rimrafPromised=async e=>{let t=Array.isArray(e)?e:[e];await Promise.all(t.map(async e=>Se(e,{recursive:!0,force:!0,maxRetries:10,retryDelay:100})))},deleteFile=async e=>{(await U(e)).isFile()&&await we(e)};function ensureApifyDirectory(e){let t=R(e);h(t,{recursive:!0})}const sumFilesSizeInBytes=async(e,t)=>(await Promise.all(e.map(async e=>U(z(t,e))))).map(e=>e.size).reduce((e,t)=>e+t,0);function internalLog(e){e.stdoutOutput&&console.log(...e.stdoutOutput),e.stderrOutput&&console.error(...e.stderrOutput)}function simpleLog(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[e.message]})}function error(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.red(`Error:`),e.message]})}function warning(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.yellow.bold(`Warning:`),e.message]})}function success(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.green(`Success:`),e.message]})}function run(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.gray(`Run:`),e.message]})}function info(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.white(`Info:`),e.message]})}function cliDebugPrint(e,...n){process.env.APIFY_CLI_DEBUG&&console.error(t.gray(`[${e}]`),...n)}let rn;function parseAuthFile(){if(!p(AUTH_FILE_PATH()))return{};try{let e=JSON.parse(_(AUTH_FILE_PATH(),`utf-8`));return typeof e!=`object`||!e||Array.isArray(e)?null:e}catch{return null}}function readAuthFile(){return parseAuthFile()??{}}function writeAuthFile(e){atomicWriteJson(AUTH_FILE_PATH(),e)}function atomicWriteJson(e,t){ensureApifyDirectory(e);let n=`${e}.tmp-${se(8)}`;try{w(n,JSON.stringify(t,null,` `),{mode:384}),b(n,e)}catch(e){throw x(n,{force:!0}),e}}function v1Profile(e){return{...typeof e.username==`string`?{username:e.username}:{},name:null,...typeof e.organizationOwnerUserId==`string`?{organizationOwnerUserId:e.organizationOwnerUserId}:{},authMethod:`token`,expiresAt:null,hasRefreshToken:!1,loggedInAt:null}}function toV2(e){let t={version:2,profiles:{}};return typeof e.id==`string`&&(t.activeProfile=e.id,t.profiles[e.id]=v1Profile(e)),typeof e.token==`string`&&(t.token=e.token),typeof e.proxy?.password==`string`&&(t.proxy={password:e.proxy.password}),t}const an={1:toV2};async function migrateAuthFile(){return rn??=(async()=>{try{let e=parseAuthFile();if(!e||Object.keys(e).length===0)return;let t=typeof e.version==`number`?e.version:1;if(t>=2)return;let n=e;for(let e=t;e<2;e++){let t=an[e];if(!t)throw Error(`No migration step from auth file version ${e}.`);n=t(n)}writeAuthFile(n)}catch(e){cliDebugPrint(`auth-file`,`auth file migration failed`,e),warning({message:`Your stored login cannot be read until ${AUTH_FILE_PATH()} is updated to the current format, and the update failed. Make the directory it is in writable, then run the command again. Set APIFY_CLI_DEBUG=1 to see why.`})}})(),rn}function assertSupportedAuthFileVersion(){let{version:e}=readAuthFile();if(typeof e==`number`&&e>2)throw Error(`Your credentials in ${AUTH_FILE_PATH()} were written by a newer Apify CLI. It uses auth file version ${e} and this one reads 2. Upgrade the CLI, or run "apify logout" to discard them.`)}async function ensureAuthFileCurrent(){await migrateAuthFile(),assertSupportedAuthFileVersion()}function lookUpActiveProfile(){let e=readAuthFile();if(e.version!==2){let t=e;return typeof t.id==`string`?{profile:{id:t.id,...v1Profile(t)}}:{}}if(!e.activeProfile)return{};let t=e.profiles?.[e.activeProfile];return t?{profile:{id:e.activeProfile,...t}}:{missingProfile:e.activeProfile}}function profileLabel(e){return e.name??e.username??e.id}function listProfiles(){let e=readAuthFile();if(e.version!==2){let{profile:e}=lookUpActiveProfile();return e?[e]:[]}return Object.entries(e.profiles??{}).map(([e,t])=>({id:e,...t}))}function matchProfiles(e){let t=listProfiles(),n=t.find(({id:t})=>t===e);return n?[n]:t.filter(t=>profileLabel(t)===e)}function lookUpProfile(e){let t=listProfiles().find(({id:t})=>t===e);return t?{profile:t}:{missingProfile:e}}function setActiveProfile(e){let t=readAuthFile();t.profiles?.[e]&&(t.activeProfile=e,writeAuthFile(t))}function getActiveProfile(){return lookUpActiveProfile().profile}function getActiveProfileId(){let e=readAuthFile();if(e.version!==2){let t=e;return typeof t.id==`string`?t.id:void 0}return e.activeProfile}function readProfileSecret(e,t){let n=readAuthFile().profiles?.[e];if(n)switch(t){case`token`:return n.token;case`refresh-token`:return n.refreshToken;default:return n.proxy?.password}}function writeProfileSecret(e,t,n){updateProfile(e,e=>setProfileSecret(e,t,n))}function clearProfileFileSecrets(e){let t=readAuthFile().profiles?.[e];(t?.token!==void 0||t?.proxy!==void 0||t?.refreshToken!==void 0)&&updateProfile(e,e=>{delete e.token,delete e.proxy,delete e.refreshToken})}function setProfileSecret(e,t,n){t===`token`?e.token=n:t===`refresh-token`?e.refreshToken=n:e.proxy={...e.proxy,password:n}}function deleteProfileSecret(e,t){readProfileSecret(e,t)!==void 0&&updateProfile(e,e=>{t===`token`?delete e.token:t===`refresh-token`?delete e.refreshToken:delete e.proxy})}function updateProfile(e,t){let n=readAuthFile(),r=n.profiles?.[e];r&&(t(r),writeAuthFile(n))}function updateProfileAuth(e,t){updateProfile(e,e=>{e.authMethod=t.authMethod,e.expiresAt=t.expiresAt,e.hasRefreshToken=t.hasRefreshToken,t.oauth?e.oauth=t.oauth:delete e.oauth})}function upsertProfile(e,t){assertSupportedAuthFileVersion();let n=readAuthFile(),r=n.version===2?n:{version:2};r.profiles??={},delete r.token,delete r.proxy;let i=r.profiles[e];r.profiles[e]={...t,...i?.token?{token:i.token}:{},...i?.proxy?{proxy:i.proxy}:{},...i?.refreshToken?{refreshToken:i.refreshToken}:{}},r.activeProfile=e,writeAuthFile(r)}function removeProfile(e){let t=readAuthFile();if(t.version!==2)return discardAuthFile(),{};let n=e??t.activeProfile,r=n?t.profiles?.[n]:void 0,i=n&&r?{id:n,...r}:void 0;if(n&&t.profiles&&delete t.profiles[n],n!==t.activeProfile){writeAuthFile(t);let e=t.activeProfile?t.profiles?.[t.activeProfile]:void 0;return{removed:i,active:e&&{id:t.activeProfile,...e}}}delete t.activeProfile,delete t.token,delete t.proxy;let[a]=Object.entries(t.profiles??{}).sort(([,e],[,t])=>(t.loggedInAt??``).localeCompare(e.loggedInAt??``)).map(([e])=>e);return a?(t.activeProfile=a,writeAuthFile(t),{removed:i,active:{id:a,...t.profiles[a]}}):(discardAuthFile(),{removed:i})}function removeAllProfiles(){discardAuthFile()}function discardAuthFile(){x(AUTH_FILE_PATH(),{force:!0,maxRetries:10,retryDelay:100})}function detectInstallMethod(){if(process.env.APIFY_CLI_BUNDLE)return`bundle`;let e=process.argv[1];if(!e)return warning({message:`Failed to detect install method of CLI, assuming npm`}),`npm`;let t=y(e);return process.env.VOLTA_HOME&&t.includes(process.env.VOLTA_HOME)?`volta`:t.includes(`homebrew/Cellar`)||t.includes(`linuxbrew/Cellar`)?`homebrew`:process.env.PNPM_HOME&&t.includes(process.env.PNPM_HOME)?`pnpm`:process.env.BUN_INSTALL&&t.includes(process.env.BUN_INSTALL)?`bun`:`npm`}function getRuntimeInfo(){return process.versions.bun?{runtime:`bun`,version:process.versions.bun,nodeVersion:process.versions.node}:process.versions.deno?{runtime:`deno`,version:process.versions.deno,nodeVersion:process.versions.node}:{runtime:`node`,version:process.versions.node}}let on=null;function useCLIMetadata(){if(on)return on;let e=detectInstallMethod(),t=getRuntimeInfo();return on={version:`1.11.1-beta.6`,hash:`49258315a65efb1be163cda34c118214dacb31ce`,arch:process.env.APIFY_BUNDLE_ARCH??process.arch,platform:process.platform===`win32`?`windows`:process.platform,runtime:t,extraRuntimeData:t.nodeVersion?`(emulating node ${t.nodeVersion})`:``,installMethod:e,get fullVersionString(){return`apify-cli/${this.version} (${this.hash.slice(0,7)}) running on ${this.platform}-${this.arch} with ${this.runtime.runtime}-${t.version}${this.extraRuntimeData?` ${this.extraRuntimeData}`:``}, installed via ${this.installMethod}`},get isBeta(){return this.version.includes(`beta`)||this.version===`0.0.0`}},e===`bundle`&&(on.installPath=R(process.execPath)),on}const sn=`com.apify.cli`,cn=[`token`,`proxy-password`,`refresh-token`];function keyringKey(e,t){return{service:`${sn}.${t}`,account:e}}function legacyKeyringKey(e){return{service:sn,account:e}}let ln,un,dn,fn;async function loadKeyringModule(){return ln===void 0&&(ln=await importKeyringModule()),ln}async function importKeyringModule(){if(useCLIMetadata().installMethod===`bundle`)try{let e=await import(`__APIFY_KEYRING_NATIVE_SUBPACKAGE__`),t=e.Entry??e.default?.Entry;return t?{Entry:t}:null}catch(e){return cliDebugPrint(`credentials`,`failed to load bundled keyring`,e),null}try{return await import(`@napi-rs/keyring`)}catch(e){return cliDebugPrint(`credentials`,`failed to load @napi-rs/keyring`,e),null}}async function getBackend(){return un||(un=(async()=>e.env.APIFY_DISABLE_KEYRING===`1`?`file`:await loadKeyringModule()?`keyring`:`file`)(),un)}function stripProxyPassword(e){e.proxy&&(delete e.proxy.password,Object.keys(e.proxy).length===0&&delete e.proxy)}async function getKeyringEntry({service:e,account:t}){let n=await loadKeyringModule();return n?new n.Entry(e,t):null}async function readKeyring(e){try{let t=await getKeyringEntry(e);return t?t.getPassword()??void 0:void 0}catch(t){cliDebugPrint(`credentials`,`failed to read ${e.service}/${e.account} from keyring`,t);return}}async function writeKeyring(e,t){let n=await getKeyringEntry(e);if(!n)throw Error(`OS keyring is not available.`);n.setPassword(t)}async function deleteKeyring(e){try{let t=await getKeyringEntry(e);return t&&t.deletePassword(),null}catch(t){return cliDebugPrint(`credentials`,`failed to delete ${e.service}/${e.account} from keyring`,t),await readKeyring(e)===void 0?null:{key:e,error:t}}}async function backendFor(e){return readProfileSecret(e,`token`)===void 0?getBackend():`file`}async function getSecret(e,t){return await backendFor(e)===`keyring`?await readKeyring(keyringKey(e,t))??readProfileSecret(e,t):readProfileSecret(e,t)}async function setSecret(e,t,n,r={}){let i=await backendFor(e),a=t===`token`?await getBackend():i;if(!(r.skipIfUnchanged&&i===a&&await getSecret(e,t)===n)){if(a===`keyring`)try{await writeKeyring(keyringKey(e,t),n),t===`token`&&clearProfileFileSecrets(e);return}catch(n){cliDebugPrint(`credentials`,`keyring write failed; falling back to file`,n),t===`token`&&await moveKeyringSecretsToFile(e)}writeProfileSecret(e,t,n)}}async function moveKeyringSecretsToFile(e){for(let t of cn){if(t===`token`)continue;let n=await readKeyring(keyringKey(e,t));n!==void 0&&(writeProfileSecret(e,t,n),readProfileSecret(e,t)===n&&await deleteKeyring(keyringKey(e,t)))}}async function deleteSecret(e,t){let n=await backendFor(e)===`keyring`?await deleteKeyring(keyringKey(e,t)):null;return deleteProfileSecret(e,t),n}async function clearKeyringSecrets(e,{keepLegacy:t=!1}={}){let n=[];for(let r of cn)e&&n.push(await deleteKeyring(keyringKey(e,r))),t||n.push(await deleteKeyring(legacyKeyringKey(r)));return n.filter(e=>e!==null)}function describeLeftovers(e){return e.map(({key:e})=>`${e.service}/${e.account}`).join(`, `)}function leftoverReasons(e){let t=e.map(({error:e})=>e instanceof Error?e.message:String(e));return[...new Set(t)].join(` `)}async function ensureMigrated(){return dn||(dn=(async()=>{try{let e=readAuthFile();if(typeof e.version==`number`&&e.version>2||!e.token&&!e.proxy?.password||await getBackend()===`file`)return;try{e.token&&await writeKeyring(legacyKeyringKey(`token`),e.token),e.proxy?.password&&await writeKeyring(legacyKeyringKey(`proxy-password`),e.proxy.password)}catch(e){cliDebugPrint(`credentials`,`keyring write failed during migration; keeping secrets in the file`,e);return}delete e.token,stripProxyPassword(e),writeAuthFile(e)}catch(e){cliDebugPrint(`credentials`,`migration failed`,e)}})(),dn)}async function dropUnkeyedSecrets(e){for(let e of cn)await deleteKeyring(legacyKeyringKey(e));(e.token!==void 0||e.proxy!==void 0)&&(delete e.token,delete e.proxy,writeAuthFile(e))}async function keyKeyringSecrets(e){let t;for(let n of cn){let r=legacyKeyringKey(n),i=await readKeyring(r);if(i!==void 0){if(t??=await readKeyring(keyringKey(e,`token`))===void 0,!t||await getSecret(e,n)!==void 0)await deleteKeyring(r);else{if(await backendFor(e)===`keyring`){let t=keyringKey(e,n);try{await writeKeyring(t,i),await readKeyring(t)===i&&await deleteKeyring(r);continue}catch(e){cliDebugPrint(`credentials`,`keyring write failed while keying secrets by user`,e)}}writeProfileSecret(e,n,i),readProfileSecret(e,n)===i&&await deleteKeyring(r)}}}}function keyFileSecrets(e,t){let n=t.profiles?.[e];if(!n)return;let{token:r}=t,i=t.proxy?.password;(r!==void 0||i!==void 0)&&(r!==void 0&&(n.token=r),i!==void 0&&(n.proxy={password:i}),delete t.token,delete t.proxy,writeAuthFile(t))}async function ensureSecretsKeyed(){return fn??=(async()=>{try{let e=readAuthFile();if(e.version!==2)return;let t=e.activeProfile;if(!t){await dropUnkeyedSecrets(e);return}keyFileSecrets(t,e),await backendFor(t)===`keyring`&&await keyKeyringSecrets(t)}catch(e){cliDebugPrint(`credentials`,`keying secrets by user failed`,e)}})(),fn}async function ensureCredentialsCurrent(){await ensureMigrated(),await ensureAuthFileCurrent(),await ensureSecretsKeyed()}const pn=`full_api_access`,mn=3e5,hn=3e4;function readUrlEnv(t,n){let r=e.env[t];if(!r)return n;let i=r.replace(/\/+$/,``);if(!URL.canParse(i))throw Error(`Invalid ${t} environment variable: "${r}" is not a valid URL.`);return i}function getOAuthIssuerUrl(){return readUrlEnv(`APIFY_CLI_OAUTH_ISSUER_URL`,`https://console-backend.apify.com`)}function getOAuthClientId(){let t=e.env.APIFY_CLI_OAUTH_CLIENT_ID;if(!t)return`https://apify.com/.well-known/oauth-clients/apify-cli.json`;if(!URL.canParse(t))throw Error(`Invalid APIFY_CLI_OAUTH_CLIENT_ID environment variable: "${t}" is not a valid URL.`);return t}var OAuthError=class extends Error{code;status;description;name=`OAuthError`;constructor(e,t,n){super(n?`${e}: ${n}`:e),this.code=e,this.status=t,this.description=n}};async function postTokenRequest(e,t){let n=await fetch(e,{method:`POST`,headers:{"Content-Type":`application/x-www-form-urlencoded`,Accept:`application/json`,...tn},body:new URLSearchParams(t),signal:AbortSignal.timeout(hn)}),r=await n.text(),i={};try{i=r?JSON.parse(r):{}}catch{}if(n.ok&&typeof i.access_token==`string`)return{ok:!0,tokens:i};let a=typeof i.error==`string`?i.error:`http_${n.status}`,s=typeof i.error_description==`string`?i.error_description:void 0;return{ok:!1,error:new OAuthError(a,n.status,s)}}async function refreshAccessToken(e){let t=await postTokenRequest(e.tokenEndpoint,{grant_type:`refresh_token`,refresh_token:e.refreshToken,client_id:e.clientId});if(!t.ok)throw t.error;return t.tokens}var OAuthSessionError=class extends Error{name=`OAuthSessionError`},OAuthSessionExpiredError=class extends OAuthSessionError{name=`OAuthSessionExpiredError`;constructor(){super(`Your Apify login session has expired. Run "apify login" to sign in again.`)}};const gn=/* @__PURE__ */ new Map;function sessionOf(e){let t=gn.get(e);return t||(t={refreshAttempted:!1},gn.set(e,t)),t}function readMetadata(e){let{profile:t}=lookUpProfile(e);if(t?.authMethod!==`oauth2`||!t.oauth||!t.expiresAt)return;let n=Date.parse(t.expiresAt);if(!Number.isFinite(n))return;let r=t.oauth.refreshTokenExpiresAt?Date.parse(t.oauth.refreshTokenExpiresAt):void 0;return{...t.oauth,expiresAt:n,...Number.isFinite(r)?{refreshTokenExpiresAtMs:r}:{}}}async function getAccessToken(e,{minRemainingMs:t=6e4}={}){let n=readMetadata(e);if(!n)return getSecret(e,`token`);let r=sessionOf(e),i=Date.now();if(!needsRefresh(n,i,t)){if(r.cached&&r.cached.expiresAt===n.expiresAt)return r.cached.token;let t=await getSecret(e,`token`);return t&&(r.cached={token:t,expiresAt:n.expiresAt}),t}if(n.refreshTokenExpiresAtMs!==void 0&&n.refreshTokenExpiresAtMs<=i)throw new OAuthSessionExpiredError;if(r.refreshAttempted){if(r.refreshFailure)throw r.refreshFailure;return r.cached?.token??getSecret(e,`token`)}return r.inflightRefresh??=refreshSession(e,r,n,t).catch(e=>{throw r.refreshFailure=e,e}).finally(()=>{r.inflightRefresh=void 0}),r.inflightRefresh}async function saveOAuthSession(e,t,n){await persistTokens(e,t,{...n,refreshTokenExpiresAt:null})}async function clearOAuthSession(e){gn.delete(e);let t=await deleteSecret(e,`refresh-token`);t&&cliDebugPrint(`oauth`,`the keyring kept the refresh token`,t.error),lookUpProfile(e).profile?.authMethod===`oauth2`&&updateProfileAuth(e,{authMethod:`token`,expiresAt:null,hasRefreshToken:!1})}function needsRefresh(e,t,n){return e.expiresAt-t<n||e.refreshTokenExpiresAtMs!==void 0&&e.refreshTokenExpiresAtMs-t<18e5}async function refreshSession(e,t,n,r){let i=await acquireRefreshLock(e);try{let i=readMetadata(e)??n;if(!needsRefresh(i,Date.now(),r)){let n=await getSecret(e,`token`);return n&&(t.cached={token:n,expiresAt:i.expiresAt}),n}let a=await getSecret(e,`refresh-token`);if(!a)throw new OAuthSessionExpiredError;t.refreshAttempted=!0;let s;try{s=await refreshAccessToken({tokenEndpoint:i.tokenEndpoint,clientId:i.clientId,refreshToken:a})}catch(n){return handleRefreshFailure(e,t,n,i,a)}return await persistTokens(e,s,i),s.access_token}finally{i()}}async function handleRefreshFailure(e,t,n,r,i){cliDebugPrint(`oauth`,`refresh failed`,n);let a=n instanceof OAuthError&&(n.code===`invalid_grant`||n.code===`expired_token`);if(a){let n=await getSecret(e,`refresh-token`);if(n&&n!==i){let n=await getSecret(e,`token`),r=readMetadata(e);return n&&r&&(t.cached={token:n,expiresAt:r.expiresAt}),n}}if(Date.now()<r.expiresAt)return getSecret(e,`token`);throw a?(await clearOAuthSession(e),new OAuthSessionExpiredError):new OAuthSessionError(`Could not refresh your Apify session. Check your connection or run "apify login".`)}async function persistTokens(e,t,n){let r=Date.now(),i=r+(t.expires_in??3600)*1e3,a=t.refresh_token_expires_in===void 0?n.refreshTokenExpiresAt:new Date(r+t.refresh_token_expires_in*1e3).toISOString();t.refresh_token&&await setSecret(e,`refresh-token`,t.refresh_token,{skipIfUnchanged:!0}),await setSecret(e,`token`,t.access_token,{skipIfUnchanged:!0}),updateProfileAuth(e,{authMethod:`oauth2`,expiresAt:new Date(i).toISOString(),hasRefreshToken:!!t.refresh_token||await getSecret(e,`refresh-token`)!==void 0,oauth:{issuer:n.issuer,clientId:n.clientId,tokenEndpoint:n.tokenEndpoint,refreshTokenExpiresAt:a}}),sessionOf(e).cached={token:t.access_token,expiresAt:i}}async function acquireRefreshLock(t){let n=z(GLOBAL_CONFIGS_FOLDER(),`oauth-refresh-${t}.lock`),release=()=>{try{C(n)}catch{}},noop=()=>{};try{ensureApifyDirectory(AUTH_FILE_PATH())}catch{return noop}for(let t=0;t<50;t++){try{let t=g(n,`wx`);return T(t,`${e.pid}`),c(t),release}catch(e){if(e.code!==`EEXIST`)return noop}try{if(Date.now()-S(n).mtimeMs>3e4){C(n);continue}}catch{continue}await Te(100)}return cliDebugPrint(`oauth`,`refresh lock wait timed out; continuing without it`),noop}const _n=/* @__PURE__ */ new Set([`undefined`,`null`,`nil`,`none`,`nan`,`false`,`0`,`-`]);function readEnvToken(){let t=e.env[F.TOKEN]?.trim();return t?_n.has(t.toLowerCase())?{kind:`invalid`,raw:t}:{kind:`token`,token:t}:{kind:`unset`}}function invalidEnvTokenMessage(e){return`${F.TOKEN} is set to "${e}", which is not an API token. Unset ${F.TOKEN} and try again.`}const vn={env:`${F.TOKEN} environment variable`,stored:`apify login`,profile:`--profile flag`};let yn,bn;const xn=`No accounts are stored. Run "apify login" to add one.`;function envTokenOverridesProfileMessage(e){return`${F.TOKEN} is set, so commands ignore ${e}. Unset ${F.TOKEN} and try again.`}async function requireProfile(t){await ensureCredentialsCurrent();let n=matchProfiles(t);if(n.length===1)return n[0];if(e.exitCode=5,n.length>1)throw Error(`More than one stored account is called "${t}": ${n.map(({id:e})=>e).join(`, `)}. Use the user ID instead.`);let r=listProfiles().map(profileLabel);throw Error(r.length?`No stored account is called "${t}". Stored accounts: ${r.join(`, `)}.`:`No stored account is called "${t}". Run "apify login" to add one.`)}function getCurrentProfile(){return bn??getActiveProfile()}async function selectProfile(t){if(readEnvToken().kind!==`unset`)throw e.exitCode=5,Error(envTokenOverridesProfileMessage(`--profile`));bn=await requireProfile(t),yn=void 0}const resolveAuth=async()=>{yn??=(async()=>{await ensureMigrated();let t=readEnvToken();if(t.kind===`invalid`)throw e.exitCode=5,Error(invalidEnvTokenMessage(t.raw));if(t.kind===`token`)return p(AUTH_FILE_PATH())&&warning({message:`Using the API token from ${F.TOKEN}.`}),{token:t.token,source:`env`};if(await ensureCredentialsCurrent(),bn){let t=await getAccessToken(bn.id);if(!t)throw e.exitCode=1,Error(missingProfileTokenMessage(bn));return{token:t,source:`profile`,profile:profileRef(bn)}}let n=getActiveProfileId(),r=n?await getAccessToken(n):void 0;if(!r)return;let i=getActiveProfile();return{token:r,source:`stored`,...i?{profile:profileRef(i)}:{}}})();try{return await yn}catch(e){throw yn=void 0,e}};function profileRef(e){return{id:e.id,label:profileLabel(e)}}function missingProfileTokenMessage(e){return`No API token is stored for ${profileLabel(e)}. Run "apify login" to log in to ${profileLabel(e)} again.`}function describeAuthFailure(e,t){if(!e)return`You are not logged in with your Apify account. Call "apify login" to fix that.`;let n=t instanceof E?t.statusCode:void 0;if(t&&n!==401&&n!==403)return`Could not verify your API token. The Apify API request failed: ${t instanceof Error?t.message:String(t)}`;switch(e.source){case`env`:return`The API token in ${F.TOKEN} was rejected. Unset it to use your stored login instead.`;default:return e.profile?`The stored API token for ${e.profile.label} was rejected. Run "apify login" to log in to ${e.profile.label} again.`:`Your stored API token was rejected. Call "apify login" to log in again.`}}const getAnonymousApifyClientOptions=t=>({baseUrl:t||e.env.APIFY_CLIENT_BASE_URL,requestInterceptors:[e=>{e.headers??=new A;for(let[t,n]of Object.entries(tn))e.headers[t]=n;return e}]}),getApifyClientOptionsForToken=(e,t)=>({...getAnonymousApifyClientOptions(t),token:e});async function loginWithToken(e,t){let n=new D(getApifyClientOptionsForToken(e,t)),r;try{r=await n.user(`me`).get()}catch(e){return cliDebugPrint(`[loginWithToken] error getting user info`,{error:e,apiBaseUrl:t}),null}if(!r.id)throw Error(`The Apify API returned no user ID for this token, so the login cannot be stored.`);let i=r.proxy?.password;await ensureCredentialsCurrent();let{organizationOwnerUserId:a}=r;upsertProfile(r.id,{username:r.username,name:r.username||r.id,...a?{organizationOwnerUserId:a}:{},authMethod:`token`,expiresAt:null,hasRefreshToken:!1,loggedInAt:(/* @__PURE__ */ new Date()).toISOString()});let s=await clearKeyringSecrets();if(await clearOAuthSession(r.id),await setSecret(r.id,`token`,e,{skipIfUnchanged:!0}),i)await setSecret(r.id,`proxy-password`,i,{skipIfUnchanged:!0});else{let e=await deleteSecret(r.id,`proxy-password`);e&&s.push(e)}return s.length&&warning({message:`Secrets this login could not remove are still in the OS keyring at ${describeLeftovers(s)}; delete them with your OS keyring app.`}),{client:n,userInfo:r}}const Sn=ke(m);let Cn;async function useStdin(){if(Cn)return Cn;let t=e.stdin,n={isTTY:t.isTTY,hasData:!1,waitDelay:0,stream:t},r=await Sn(0).then(t=>{if(cliDebugPrint(`useStdin`,{stat:t,isRegularFile:t.isFile(),isDirectory:t.isDirectory(),isBlockDevice:t.isBlockDevice(),isCharDevice:t.isCharacterDevice(),isSymbolicLink:t.isSymbolicLink(),isFIFO:t.isFIFO(),isSocket:t.isSocket()}),e.platform===`win32`){if((t.mode&l.S_IFIFO)===l.S_IFIFO)return 100;if(t.isFile())return 50}return t.isFIFO()||t.isFile()||(t.isSocket()?50:!1)}).catch(()=>!1);return cliDebugPrint(`useStdin`,{hasData:n.hasData,waitDelay:n.waitDelay,isTTY:n.isTTY,pipedIn:r,readableEnded:t.readableEnded}),(!t.isTTY||r!==!1&&(t.isTTY!==void 0||!t.readableEnded))&&(n.hasData=!0),typeof r==`number`&&(n.waitDelay=r),Cn=n,n}const wn=5e3;let Tn,En,Dn=!1,On=!1;async function readStdin(e={}){return Tn??=_readStdin(e).then(e=>(En=e,Dn=!0,e)),Tn}function stdinWasCutShort(){return On}function describeStdinRead(){return Dn?En?`Yes`:`No`:`Not read`}async function _readStdin({implicit:e}){let{hasData:t,waitDelay:n,stream:r}=await useStdin();if(!t)return;let i=e?n||wn:n,a=[],s=new AbortController,c=null,armTimeout=e=>{e&&(c=setTimeout(()=>s.abort(),e).unref())},disarmTimeout=()=>{c&&=(clearTimeout(c),null)};armTimeout(i);let onData=t=>{a.push(t),disarmTimeout(),e&&armTimeout(1e3)};r.on(`data`,onData);try{await Ee(r,`end`,{signal:s.signal})}catch(t){if(t.name===`AbortError`){if(!e)return;On=a.length>0||i===wn}}finally{r.off(`data`,onData),r.pause()}disarmTimeout();let l=Buffer.concat(a);if(l.length)return l}const kn={output:e.stderr},An=`confirm`,jn=`no-${An}`;function stdinCheckWrapper(e,{errorMessageForStdin:t=`Please use the --${An}/--${jn} flags to confirm the action.`}={}){return async(n,...r)=>{let i=n;if(i.preAnswered!==void 0)return i.preAnswered;let{isTTY:a}=await useStdin();if(ge||!a){if(i.defaultWhenNonInteractive===void 0)throw Error(i.errorMessageForStdin??t);return i.defaultWhenNonInteractive}return await e(n,...r)}}const Mn=stdinCheckWrapper(async({message:e,validate:t,default:n})=>await Le({message:e,validate:t,default:n},kn),{errorMessageForStdin:`Please provide a valid input based on the command options.`});var Spider=class{name;class_name;start_urls;pathname;constructor(e){this.name=e.name,this.class_name=e.class_name,this.start_urls=e.start_urls,this.pathname=e.pathname}},SpiderFileAnalyzer=class{pathname;constructor(e){this.pathname=e}getSpiders(){let e=_(this.pathname,`utf8`),t=/class\s+(\w+)/g,n=[],r=t.exec(e);for(;r;)n.push(new Spider({class_name:r[1],pathname:this.pathname})),r=t.exec(e);return n}},ScrapyProjectAnalyzer=class{pathname;configuration=null;settings=null;constructor(e){this.pathname=e,this.settings=null,this.loadScrapyCfg()}static isApplicable(e){return p(z(e,`scrapy.cfg`))}async init(){await this.loadSettings()}loadScrapyCfg(){let e=new Ie,t=V(z(this.pathname,`scrapy.cfg`));if(!p(t))throw Error(`scrapy.cfg not found in "${t}".
|
|
1
|
+
import e from"node:process";import t from"chalk";import n from"indent-string";import r from"widest-line";import i from"wrap-ansi";import{appendFileSync as a,chmodSync as s,closeSync as c,constants as l,copyFileSync as u,createWriteStream as f,existsSync as p,fstat as m,mkdirSync as h,openSync as g,readFileSync as _,readdirSync as v,realpathSync as y,renameSync as b,rmSync as x,statSync as S,unlinkSync as C,writeFileSync as w,writeSync as T}from"node:fs";import{ApifyApiError as E,ApifyClient as D,DownloadItemsFormat as O}from"apify-client";import k,{AxiosHeaders as A}from"axios";import{ACTOR_ENV_VARS as j,ACTOR_JOB_STATUSES as M,ACTOR_JOB_TERMINAL_STATUSES as ee,ACTOR_JOB_TYPES as N,ACTOR_NAME as P,ACTOR_SOURCE_TYPES as te,APIFY_ENV_VARS as F,KEY_VALUE_STORE_KEYS as I,LOCAL_ACTOR_ENV_VARS as ne,LOCAL_STORAGE_SUBDIRS as re,MAX_MULTIFILE_BYTES as ie,META_ORIGINS as L,SOURCE_FILE_FORMATS as ae}from"@apify/consts";import{createHmacSignature as oe,cryptoRandomObjectId as se}from"@apify/utilities";import{homedir as ce,platform as le}from"node:os";import ue,{basename as de,dirname as R,isAbsolute as fe,join as z,posix as pe,relative as B,resolve as V,sep as me}from"node:path";import he,{isCI as ge}from"ci-info";import{access as _e,appendFile as ve,mkdir as H,readFile as ye,readdir as be,rename as xe,rm as Se,stat as U,symlink as Ce,unlink as we,writeFile as W}from"node:fs/promises";import{setTimeout as Te}from"node:timers/promises";import{once as Ee}from"node:events";import{inspect as De,parseArgs as Oe,promisify as ke}from"node:util";import{Result as Ae,err as je,none as Me,ok as Ne,some as Pe}from"@sapphire/result";import{execa as Fe}from"execa";import Ie from"configparser";import Le from"@inquirer/input";import Re from"which";import{gt as ze,gte as Be,minVersion as Ve,satisfies as He}from"semver";import Ue from"string-width";import We from"strip-ansi";import{jaroWinkler as Ge}from"@skyra/jaro-winkler";import Ke from"js-levenshtein";import{execFileSync as qe,execSync as Je,spawn as Ye}from"node:child_process";import{DurationFormatter as Xe,Time as Ze,TimeTypes as Qe}from"@sapphire/duration";import{Timestamp as $e}from"@sapphire/timestamp";import et from"adm-zip";import tt from"ajv/dist/2019.js";import{ZipArchive as nt}from"archiver";import rt from"escape-string-regexp";import it from"ignore";import{getEncoding as at}from"istextorbinary";import ot,{Mime as st}from"mime";import ct from"mime/types/other.js";import lt from"mime/types/standard.js";import{glob as ut}from"tinyglobby";import{calculateRunDynamicMemory as dt}from"@apify/actor-memory-expression";import{Separator as ft,createPrompt as pt,isEnterKey as mt,makeTheme as ht,useKeypress as gt,usePrefix as _t,useState as vt}from"@inquirer/core";import{compile as yt}from"json-schema-to-typescript";import{validateInputSchema as bt,validateInputUsingValidator as xt}from"@apify/input_schema";import{getDatasetSchemaValidator as St,getKeyValueStoreSchemaValidator as Ct,getOutputSchemaValidator as wt}from"@apify/json_schemas";import{pipeline as Tt}from"node:stream/promises";import{MemoryStorage as Et}from"@crawlee/memory-storage";import Dt from"cli-table3";import{downloadTemplate as Ot}from"giget";import kt from"jju";import At from"open";import{fetchManifest as jt,manifestUrl as Mt,wrapperManifestUrl as Nt}from"@apify/actor-templates";import Pt from"@inquirer/checkbox";import Ft from"computer-name";import It from"@inquirer/password";import{createServer as Lt}from"node:http";import{createHash as Rt,randomBytes as zt}from"node:crypto";import Bt from"@inquirer/select";import Vt from"node:assert";import{ReadStream as Ht}from"node:tty";import Ut from"detect-indent";import{fileURLToPath as Wt}from"node:url";import Gt from"@root/walk";import Kt from"handlebars";var qt={node:`>=22`};const Jt=`storage`,Yt={$schema:`https://apify.com/schemas/v1/actor.ide.json`,actorSpecification:1,name:null,version:`0.0`,buildTag:`latest`,environmentVariables:{}},Xt=Date.UTC(2026,9,1),Zt=[`SIGINT`,`SIGTERM`,`SIGHUP`],GLOBAL_CONFIGS_FOLDER=()=>{let t=z(ce(),`.apify`);return e.env.__APIFY_INTERNAL_TEST_AUTH_PATH__?z(t,e.env.__APIFY_INTERNAL_TEST_AUTH_PATH__):t},AUTH_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`auth.json`),SECRETS_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`secrets.json`),STATE_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`state.json`),TELEMETRY_FILE_PATH=()=>z(GLOBAL_CONFIGS_FOLDER(),`telemetry.json`),Qt=`apify.json`,G=`.actor`,K=z(G,`actor.json`),$t=qt.node,en=he.GITHUB_ACTIONS?`${e.env.GITHUB_SERVER_URL}/${e.env.GITHUB_REPOSITORY}/actions/runs/${e.env.GITHUB_RUN_ID}`:void 0,tn={"X-Apify-Request-Origin":he.isCI?L.CI:L.CLI,...en&&{"X-Apify-Github-Actions-Run-Url":en}},nn=`3.11.0`,updateLocalJson=async(e,t={},n=null)=>{let r=await ye(e,`utf-8`),i=JSON.parse(r),a;n?(a=i,a[n]={...i[n],...t}):a={...i,...t},await W(e,JSON.stringify(a,null,` `))},ensureFolderExistsSync=(e,t)=>{t||(t=e,e=`.`),t.split(me).reduce((e,t)=>(e=z(e,t),p(e)||h(e),e),e)},rimrafPromised=async e=>{let t=Array.isArray(e)?e:[e];await Promise.all(t.map(async e=>Se(e,{recursive:!0,force:!0,maxRetries:10,retryDelay:100})))},deleteFile=async e=>{(await U(e)).isFile()&&await we(e)};function ensureApifyDirectory(e){let t=R(e);h(t,{recursive:!0})}const sumFilesSizeInBytes=async(e,t)=>(await Promise.all(e.map(async e=>U(z(t,e))))).map(e=>e.size).reduce((e,t)=>e+t,0);function internalLog(e){e.stdoutOutput&&console.log(...e.stdoutOutput),e.stderrOutput&&console.error(...e.stderrOutput)}function simpleLog(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[e.message]})}function error(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.red(`Error:`),e.message]})}function warning(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.yellow.bold(`Warning:`),e.message]})}function success(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.green(`Success:`),e.message]})}function run(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.gray(`Run:`),e.message]})}function info(e){internalLog({[e.stdout?`stdoutOutput`:`stderrOutput`]:[t.white(`Info:`),e.message]})}function cliDebugPrint(e,...n){process.env.APIFY_CLI_DEBUG&&console.error(t.gray(`[${e}]`),...n)}let rn;function parseAuthFile(){if(!p(AUTH_FILE_PATH()))return{};try{let e=JSON.parse(_(AUTH_FILE_PATH(),`utf-8`));return typeof e!=`object`||!e||Array.isArray(e)?null:e}catch{return null}}function readAuthFile(){return parseAuthFile()??{}}function writeAuthFile(e){atomicWriteJson(AUTH_FILE_PATH(),e)}function atomicWriteJson(e,t){ensureApifyDirectory(e);let n=`${e}.tmp-${se(8)}`;try{w(n,JSON.stringify(t,null,` `),{mode:384}),b(n,e)}catch(e){throw x(n,{force:!0}),e}}function v1Profile(e){return{...typeof e.username==`string`?{username:e.username}:{},name:null,...typeof e.organizationOwnerUserId==`string`?{organizationOwnerUserId:e.organizationOwnerUserId}:{},authMethod:`token`,expiresAt:null,hasRefreshToken:!1,loggedInAt:null}}function toV2(e){let t={version:2,profiles:{}};return typeof e.id==`string`&&(t.activeProfile=e.id,t.profiles[e.id]=v1Profile(e)),typeof e.token==`string`&&(t.token=e.token),typeof e.proxy?.password==`string`&&(t.proxy={password:e.proxy.password}),t}const an={1:toV2};async function migrateAuthFile(){return rn??=(async()=>{try{let e=parseAuthFile();if(!e||Object.keys(e).length===0)return;let t=typeof e.version==`number`?e.version:1;if(t>=2)return;let n=e;for(let e=t;e<2;e++){let t=an[e];if(!t)throw Error(`No migration step from auth file version ${e}.`);n=t(n)}writeAuthFile(n)}catch(e){cliDebugPrint(`auth-file`,`auth file migration failed`,e),warning({message:`Your stored login cannot be read until ${AUTH_FILE_PATH()} is updated to the current format, and the update failed. Make the directory it is in writable, then run the command again. Set APIFY_CLI_DEBUG=1 to see why.`})}})(),rn}function assertSupportedAuthFileVersion(){let{version:e}=readAuthFile();if(typeof e==`number`&&e>2)throw Error(`Your credentials in ${AUTH_FILE_PATH()} were written by a newer Apify CLI. It uses auth file version ${e} and this one reads 2. Upgrade the CLI, or run "apify logout" to discard them.`)}async function ensureAuthFileCurrent(){await migrateAuthFile(),assertSupportedAuthFileVersion()}function lookUpActiveProfile(){let e=readAuthFile();if(e.version!==2){let t=e;return typeof t.id==`string`?{profile:{id:t.id,...v1Profile(t)}}:{}}if(!e.activeProfile)return{};let t=e.profiles?.[e.activeProfile];return t?{profile:{id:e.activeProfile,...t}}:{missingProfile:e.activeProfile}}function profileLabel(e){return e.name??e.username??e.id}function listProfiles(){let e=readAuthFile();if(e.version!==2){let{profile:e}=lookUpActiveProfile();return e?[e]:[]}return Object.entries(e.profiles??{}).map(([e,t])=>({id:e,...t}))}function matchProfiles(e){let t=listProfiles(),n=t.find(({id:t})=>t===e);return n?[n]:t.filter(t=>profileLabel(t)===e)}function lookUpProfile(e){let t=listProfiles().find(({id:t})=>t===e);return t?{profile:t}:{missingProfile:e}}function setActiveProfile(e){let t=readAuthFile();t.profiles?.[e]&&(t.activeProfile=e,writeAuthFile(t))}function getActiveProfile(){return lookUpActiveProfile().profile}function getActiveProfileId(){let e=readAuthFile();if(e.version!==2){let t=e;return typeof t.id==`string`?t.id:void 0}return e.activeProfile}function readProfileSecret(e,t){let n=readAuthFile().profiles?.[e];if(n)switch(t){case`token`:return n.token;case`refresh-token`:return n.refreshToken;default:return n.proxy?.password}}function writeProfileSecret(e,t,n){updateProfile(e,e=>setProfileSecret(e,t,n))}function clearProfileFileSecrets(e){let t=readAuthFile().profiles?.[e];(t?.token!==void 0||t?.proxy!==void 0||t?.refreshToken!==void 0)&&updateProfile(e,e=>{delete e.token,delete e.proxy,delete e.refreshToken})}function setProfileSecret(e,t,n){t===`token`?e.token=n:t===`refresh-token`?e.refreshToken=n:e.proxy={...e.proxy,password:n}}function deleteProfileSecret(e,t){readProfileSecret(e,t)!==void 0&&updateProfile(e,e=>{t===`token`?delete e.token:t===`refresh-token`?delete e.refreshToken:delete e.proxy})}function updateProfile(e,t){let n=readAuthFile(),r=n.profiles?.[e];r&&(t(r),writeAuthFile(n))}function updateProfileAuth(e,t){updateProfile(e,e=>{e.authMethod=t.authMethod,e.expiresAt=t.expiresAt,e.hasRefreshToken=t.hasRefreshToken,t.oauth?e.oauth=t.oauth:delete e.oauth})}function upsertProfile(e,t){assertSupportedAuthFileVersion();let n=readAuthFile(),r=n.version===2?n:{version:2};r.profiles??={},delete r.token,delete r.proxy;let i=r.profiles[e];r.profiles[e]={...t,...i?.token?{token:i.token}:{},...i?.proxy?{proxy:i.proxy}:{},...i?.refreshToken?{refreshToken:i.refreshToken}:{}},r.activeProfile=e,writeAuthFile(r)}function removeProfile(e){let t=readAuthFile();if(t.version!==2)return discardAuthFile(),{};let n=e??t.activeProfile,r=n?t.profiles?.[n]:void 0,i=n&&r?{id:n,...r}:void 0;if(n&&t.profiles&&delete t.profiles[n],n!==t.activeProfile){writeAuthFile(t);let e=t.activeProfile?t.profiles?.[t.activeProfile]:void 0;return{removed:i,active:e&&{id:t.activeProfile,...e}}}delete t.activeProfile,delete t.token,delete t.proxy;let[a]=Object.entries(t.profiles??{}).sort(([,e],[,t])=>(t.loggedInAt??``).localeCompare(e.loggedInAt??``)).map(([e])=>e);return a?(t.activeProfile=a,writeAuthFile(t),{removed:i,active:{id:a,...t.profiles[a]}}):(discardAuthFile(),{removed:i})}function removeAllProfiles(){discardAuthFile()}function discardAuthFile(){x(AUTH_FILE_PATH(),{force:!0,maxRetries:10,retryDelay:100})}function detectInstallMethod(){if(process.env.APIFY_CLI_BUNDLE)return`bundle`;let e=process.argv[1];if(!e)return warning({message:`Failed to detect install method of CLI, assuming npm`}),`npm`;let t=y(e);return process.env.VOLTA_HOME&&t.includes(process.env.VOLTA_HOME)?`volta`:t.includes(`homebrew/Cellar`)||t.includes(`linuxbrew/Cellar`)?`homebrew`:process.env.PNPM_HOME&&t.includes(process.env.PNPM_HOME)?`pnpm`:process.env.BUN_INSTALL&&t.includes(process.env.BUN_INSTALL)?`bun`:`npm`}function getRuntimeInfo(){return process.versions.bun?{runtime:`bun`,version:process.versions.bun,nodeVersion:process.versions.node}:process.versions.deno?{runtime:`deno`,version:process.versions.deno,nodeVersion:process.versions.node}:{runtime:`node`,version:process.versions.node}}let on=null;function useCLIMetadata(){if(on)return on;let e=detectInstallMethod(),t=getRuntimeInfo();return on={version:`1.11.1-beta.8`,hash:`1668bd534c86b1be06c8491d356c2428bc244aa2`,arch:process.env.APIFY_BUNDLE_ARCH??process.arch,platform:process.platform===`win32`?`windows`:process.platform,runtime:t,extraRuntimeData:t.nodeVersion?`(emulating node ${t.nodeVersion})`:``,installMethod:e,get fullVersionString(){return`apify-cli/${this.version} (${this.hash.slice(0,7)}) running on ${this.platform}-${this.arch} with ${this.runtime.runtime}-${t.version}${this.extraRuntimeData?` ${this.extraRuntimeData}`:``}, installed via ${this.installMethod}`},get isBeta(){return this.version.includes(`beta`)||this.version===`0.0.0`}},e===`bundle`&&(on.installPath=R(process.execPath)),on}const sn=`com.apify.cli`,cn=[`token`,`proxy-password`,`refresh-token`];function keyringKey(e,t){return{service:`${sn}.${t}`,account:e}}function legacyKeyringKey(e){return{service:sn,account:e}}let ln,un,dn,fn;async function loadKeyringModule(){return ln===void 0&&(ln=await importKeyringModule()),ln}async function importKeyringModule(){if(useCLIMetadata().installMethod===`bundle`)try{let e=await import(`__APIFY_KEYRING_NATIVE_SUBPACKAGE__`),t=e.Entry??e.default?.Entry;return t?{Entry:t}:null}catch(e){return cliDebugPrint(`credentials`,`failed to load bundled keyring`,e),null}try{return await import(`@napi-rs/keyring`)}catch(e){return cliDebugPrint(`credentials`,`failed to load @napi-rs/keyring`,e),null}}async function getBackend(){return un||(un=(async()=>e.env.APIFY_DISABLE_KEYRING===`1`?`file`:await loadKeyringModule()?`keyring`:`file`)(),un)}function stripProxyPassword(e){e.proxy&&(delete e.proxy.password,Object.keys(e.proxy).length===0&&delete e.proxy)}async function getKeyringEntry({service:e,account:t}){let n=await loadKeyringModule();return n?new n.Entry(e,t):null}async function readKeyring(e){try{let t=await getKeyringEntry(e);return t?t.getPassword()??void 0:void 0}catch(t){cliDebugPrint(`credentials`,`failed to read ${e.service}/${e.account} from keyring`,t);return}}async function writeKeyring(e,t){let n=await getKeyringEntry(e);if(!n)throw Error(`OS keyring is not available.`);n.setPassword(t)}async function deleteKeyring(e){try{let t=await getKeyringEntry(e);return t&&t.deletePassword(),null}catch(t){return cliDebugPrint(`credentials`,`failed to delete ${e.service}/${e.account} from keyring`,t),await readKeyring(e)===void 0?null:{key:e,error:t}}}async function backendFor(e){return readProfileSecret(e,`token`)===void 0?getBackend():`file`}async function getSecret(e,t){return await backendFor(e)===`keyring`?await readKeyring(keyringKey(e,t))??readProfileSecret(e,t):readProfileSecret(e,t)}async function setSecret(e,t,n,r={}){let i=await backendFor(e),a=t===`token`?await getBackend():i;if(!(r.skipIfUnchanged&&i===a&&await getSecret(e,t)===n)){if(a===`keyring`)try{await writeKeyring(keyringKey(e,t),n),t===`token`&&clearProfileFileSecrets(e);return}catch(n){cliDebugPrint(`credentials`,`keyring write failed; falling back to file`,n),t===`token`&&await moveKeyringSecretsToFile(e)}writeProfileSecret(e,t,n)}}async function moveKeyringSecretsToFile(e){for(let t of cn){if(t===`token`)continue;let n=await readKeyring(keyringKey(e,t));n!==void 0&&(writeProfileSecret(e,t,n),readProfileSecret(e,t)===n&&await deleteKeyring(keyringKey(e,t)))}}async function deleteSecret(e,t){let n=await backendFor(e)===`keyring`?await deleteKeyring(keyringKey(e,t)):null;return deleteProfileSecret(e,t),n}async function clearKeyringSecrets(e,{keepLegacy:t=!1}={}){let n=[];for(let r of cn)e&&n.push(await deleteKeyring(keyringKey(e,r))),t||n.push(await deleteKeyring(legacyKeyringKey(r)));return n.filter(e=>e!==null)}function describeLeftovers(e){return e.map(({key:e})=>`${e.service}/${e.account}`).join(`, `)}function leftoverReasons(e){let t=e.map(({error:e})=>e instanceof Error?e.message:String(e));return[...new Set(t)].join(` `)}async function ensureMigrated(){return dn||(dn=(async()=>{try{let e=readAuthFile();if(typeof e.version==`number`&&e.version>2||!e.token&&!e.proxy?.password||await getBackend()===`file`)return;try{e.token&&await writeKeyring(legacyKeyringKey(`token`),e.token),e.proxy?.password&&await writeKeyring(legacyKeyringKey(`proxy-password`),e.proxy.password)}catch(e){cliDebugPrint(`credentials`,`keyring write failed during migration; keeping secrets in the file`,e);return}delete e.token,stripProxyPassword(e),writeAuthFile(e)}catch(e){cliDebugPrint(`credentials`,`migration failed`,e)}})(),dn)}async function dropUnkeyedSecrets(e){for(let e of cn)await deleteKeyring(legacyKeyringKey(e));(e.token!==void 0||e.proxy!==void 0)&&(delete e.token,delete e.proxy,writeAuthFile(e))}async function keyKeyringSecrets(e){let t;for(let n of cn){let r=legacyKeyringKey(n),i=await readKeyring(r);if(i!==void 0){if(t??=await readKeyring(keyringKey(e,`token`))===void 0,!t||await getSecret(e,n)!==void 0)await deleteKeyring(r);else{if(await backendFor(e)===`keyring`){let t=keyringKey(e,n);try{await writeKeyring(t,i),await readKeyring(t)===i&&await deleteKeyring(r);continue}catch(e){cliDebugPrint(`credentials`,`keyring write failed while keying secrets by user`,e)}}writeProfileSecret(e,n,i),readProfileSecret(e,n)===i&&await deleteKeyring(r)}}}}function keyFileSecrets(e,t){let n=t.profiles?.[e];if(!n)return;let{token:r}=t,i=t.proxy?.password;(r!==void 0||i!==void 0)&&(r!==void 0&&(n.token=r),i!==void 0&&(n.proxy={password:i}),delete t.token,delete t.proxy,writeAuthFile(t))}async function ensureSecretsKeyed(){return fn??=(async()=>{try{let e=readAuthFile();if(e.version!==2)return;let t=e.activeProfile;if(!t){await dropUnkeyedSecrets(e);return}keyFileSecrets(t,e),await backendFor(t)===`keyring`&&await keyKeyringSecrets(t)}catch(e){cliDebugPrint(`credentials`,`keying secrets by user failed`,e)}})(),fn}async function ensureCredentialsCurrent(){await ensureMigrated(),await ensureAuthFileCurrent(),await ensureSecretsKeyed()}const pn=`full_api_access`,mn=3e5,hn=3e4;function readUrlEnv(t,n){let r=e.env[t];if(!r)return n;let i=r.replace(/\/+$/,``);if(!URL.canParse(i))throw Error(`Invalid ${t} environment variable: "${r}" is not a valid URL.`);return i}function getOAuthIssuerUrl(){return readUrlEnv(`APIFY_CLI_OAUTH_ISSUER_URL`,`https://console-backend.apify.com`)}function getOAuthClientId(){let t=e.env.APIFY_CLI_OAUTH_CLIENT_ID;if(!t)return`https://apify.com/.well-known/oauth-clients/apify-cli.json`;if(!URL.canParse(t))throw Error(`Invalid APIFY_CLI_OAUTH_CLIENT_ID environment variable: "${t}" is not a valid URL.`);return t}var OAuthError=class extends Error{code;status;description;name=`OAuthError`;constructor(e,t,n){super(n?`${e}: ${n}`:e),this.code=e,this.status=t,this.description=n}};async function postTokenRequest(e,t){let n=await fetch(e,{method:`POST`,headers:{"Content-Type":`application/x-www-form-urlencoded`,Accept:`application/json`,...tn},body:new URLSearchParams(t),signal:AbortSignal.timeout(hn)}),r=await n.text(),i={};try{i=r?JSON.parse(r):{}}catch{}if(n.ok&&typeof i.access_token==`string`)return{ok:!0,tokens:i};let a=typeof i.error==`string`?i.error:`http_${n.status}`,s=typeof i.error_description==`string`?i.error_description:void 0;return{ok:!1,error:new OAuthError(a,n.status,s)}}async function refreshAccessToken(e){let t=await postTokenRequest(e.tokenEndpoint,{grant_type:`refresh_token`,refresh_token:e.refreshToken,client_id:e.clientId});if(!t.ok)throw t.error;return t.tokens}var OAuthSessionError=class extends Error{name=`OAuthSessionError`},OAuthSessionExpiredError=class extends OAuthSessionError{name=`OAuthSessionExpiredError`;constructor(){super(`Your Apify login session has expired. Run "apify login" to sign in again.`)}};const gn=/* @__PURE__ */ new Map;function sessionOf(e){let t=gn.get(e);return t||(t={refreshAttempted:!1},gn.set(e,t)),t}function readMetadata(e){let{profile:t}=lookUpProfile(e);if(t?.authMethod!==`oauth2`||!t.oauth||!t.expiresAt)return;let n=Date.parse(t.expiresAt);if(!Number.isFinite(n))return;let r=t.oauth.refreshTokenExpiresAt?Date.parse(t.oauth.refreshTokenExpiresAt):void 0;return{...t.oauth,expiresAt:n,...Number.isFinite(r)?{refreshTokenExpiresAtMs:r}:{}}}async function getAccessToken(e,{minRemainingMs:t=6e4}={}){let n=readMetadata(e);if(!n)return getSecret(e,`token`);let r=sessionOf(e),i=Date.now();if(!needsRefresh(n,i,t)){if(r.cached&&r.cached.expiresAt===n.expiresAt)return r.cached.token;let t=await getSecret(e,`token`);return t&&(r.cached={token:t,expiresAt:n.expiresAt}),t}if(n.refreshTokenExpiresAtMs!==void 0&&n.refreshTokenExpiresAtMs<=i)throw new OAuthSessionExpiredError;if(r.refreshAttempted){if(r.refreshFailure)throw r.refreshFailure;return r.cached?.token??getSecret(e,`token`)}return r.inflightRefresh??=refreshSession(e,r,n,t).catch(e=>{throw r.refreshFailure=e,e}).finally(()=>{r.inflightRefresh=void 0}),r.inflightRefresh}async function saveOAuthSession(e,t,n){await persistTokens(e,t,{...n,refreshTokenExpiresAt:null})}async function clearOAuthSession(e){gn.delete(e);let t=await deleteSecret(e,`refresh-token`);t&&cliDebugPrint(`oauth`,`the keyring kept the refresh token`,t.error),lookUpProfile(e).profile?.authMethod===`oauth2`&&updateProfileAuth(e,{authMethod:`token`,expiresAt:null,hasRefreshToken:!1})}function needsRefresh(e,t,n){return e.expiresAt-t<n||e.refreshTokenExpiresAtMs!==void 0&&e.refreshTokenExpiresAtMs-t<18e5}async function refreshSession(e,t,n,r){let i=await acquireRefreshLock(e);try{let i=readMetadata(e)??n;if(!needsRefresh(i,Date.now(),r)){let n=await getSecret(e,`token`);return n&&(t.cached={token:n,expiresAt:i.expiresAt}),n}let a=await getSecret(e,`refresh-token`);if(!a)throw new OAuthSessionExpiredError;t.refreshAttempted=!0;let s;try{s=await refreshAccessToken({tokenEndpoint:i.tokenEndpoint,clientId:i.clientId,refreshToken:a})}catch(n){return handleRefreshFailure(e,t,n,i,a)}return await persistTokens(e,s,i),s.access_token}finally{i()}}async function handleRefreshFailure(e,t,n,r,i){cliDebugPrint(`oauth`,`refresh failed`,n);let a=n instanceof OAuthError&&(n.code===`invalid_grant`||n.code===`expired_token`);if(a){let n=await getSecret(e,`refresh-token`);if(n&&n!==i){let n=await getSecret(e,`token`),r=readMetadata(e);return n&&r&&(t.cached={token:n,expiresAt:r.expiresAt}),n}}if(Date.now()<r.expiresAt)return getSecret(e,`token`);throw a?(await clearOAuthSession(e),new OAuthSessionExpiredError):new OAuthSessionError(`Could not refresh your Apify session. Check your connection or run "apify login".`)}async function persistTokens(e,t,n){let r=Date.now(),i=r+(t.expires_in??3600)*1e3,a=t.refresh_token_expires_in===void 0?n.refreshTokenExpiresAt:new Date(r+t.refresh_token_expires_in*1e3).toISOString();t.refresh_token&&await setSecret(e,`refresh-token`,t.refresh_token,{skipIfUnchanged:!0}),await setSecret(e,`token`,t.access_token,{skipIfUnchanged:!0}),updateProfileAuth(e,{authMethod:`oauth2`,expiresAt:new Date(i).toISOString(),hasRefreshToken:!!t.refresh_token||await getSecret(e,`refresh-token`)!==void 0,oauth:{issuer:n.issuer,clientId:n.clientId,tokenEndpoint:n.tokenEndpoint,refreshTokenExpiresAt:a}}),sessionOf(e).cached={token:t.access_token,expiresAt:i}}async function acquireRefreshLock(t){let n=z(GLOBAL_CONFIGS_FOLDER(),`oauth-refresh-${t}.lock`),release=()=>{try{C(n)}catch{}},noop=()=>{};try{ensureApifyDirectory(AUTH_FILE_PATH())}catch{return noop}for(let t=0;t<50;t++){try{let t=g(n,`wx`);return T(t,`${e.pid}`),c(t),release}catch(e){if(e.code!==`EEXIST`)return noop}try{if(Date.now()-S(n).mtimeMs>3e4){C(n);continue}}catch{continue}await Te(100)}return cliDebugPrint(`oauth`,`refresh lock wait timed out; continuing without it`),noop}const _n=/* @__PURE__ */ new Set([`undefined`,`null`,`nil`,`none`,`nan`,`false`,`0`,`-`]);function readEnvToken(){let t=e.env[F.TOKEN]?.trim();return t?_n.has(t.toLowerCase())?{kind:`invalid`,raw:t}:{kind:`token`,token:t}:{kind:`unset`}}function invalidEnvTokenMessage(e){return`${F.TOKEN} is set to "${e}", which is not an API token. Unset ${F.TOKEN} and try again.`}const vn={env:`${F.TOKEN} environment variable`,stored:`apify login`,profile:`--profile flag`};let yn,bn;const xn=`No accounts are stored. Run "apify login" to add one.`;function envTokenOverridesProfileMessage(e){return`${F.TOKEN} is set, so commands ignore ${e}. Unset ${F.TOKEN} and try again.`}async function requireProfile(t){await ensureCredentialsCurrent();let n=matchProfiles(t);if(n.length===1)return n[0];if(e.exitCode=5,n.length>1)throw Error(`More than one stored account is called "${t}": ${n.map(({id:e})=>e).join(`, `)}. Use the user ID instead.`);let r=listProfiles().map(profileLabel);throw Error(r.length?`No stored account is called "${t}". Stored accounts: ${r.join(`, `)}.`:`No stored account is called "${t}". Run "apify login" to add one.`)}function getCurrentProfile(){return bn??getActiveProfile()}async function selectProfile(t){if(readEnvToken().kind!==`unset`)throw e.exitCode=5,Error(envTokenOverridesProfileMessage(`--profile`));bn=await requireProfile(t),yn=void 0}const resolveAuth=async()=>{yn??=(async()=>{await ensureMigrated();let t=readEnvToken();if(t.kind===`invalid`)throw e.exitCode=5,Error(invalidEnvTokenMessage(t.raw));if(t.kind===`token`)return p(AUTH_FILE_PATH())&&warning({message:`Using the API token from ${F.TOKEN}.`}),{token:t.token,source:`env`};if(await ensureCredentialsCurrent(),bn){let t=await getAccessToken(bn.id);if(!t)throw e.exitCode=1,Error(missingProfileTokenMessage(bn));return{token:t,source:`profile`,profile:profileRef(bn)}}let n=getActiveProfileId(),r=n?await getAccessToken(n):void 0;if(!r)return;let i=getActiveProfile();return{token:r,source:`stored`,...i?{profile:profileRef(i)}:{}}})();try{return await yn}catch(e){throw yn=void 0,e}};function profileRef(e){return{id:e.id,label:profileLabel(e)}}function missingProfileTokenMessage(e){return`No API token is stored for ${profileLabel(e)}. Run "apify login" to log in to ${profileLabel(e)} again.`}function describeAuthFailure(e,t){if(!e)return`You are not logged in with your Apify account. Call "apify login" to fix that.`;let n=t instanceof E?t.statusCode:void 0;if(t&&n!==401&&n!==403)return`Could not verify your API token. The Apify API request failed: ${t instanceof Error?t.message:String(t)}`;switch(e.source){case`env`:return`The API token in ${F.TOKEN} was rejected. Unset it to use your stored login instead.`;default:return e.profile?`The stored API token for ${e.profile.label} was rejected. Run "apify login" to log in to ${e.profile.label} again.`:`Your stored API token was rejected. Call "apify login" to log in again.`}}const getAnonymousApifyClientOptions=t=>({baseUrl:t||e.env.APIFY_CLIENT_BASE_URL,requestInterceptors:[e=>{e.headers??=new A;for(let[t,n]of Object.entries(tn))e.headers[t]=n;return e}]}),getApifyClientOptionsForToken=(e,t)=>({...getAnonymousApifyClientOptions(t),token:e});async function loginWithToken(e,t){let n=new D(getApifyClientOptionsForToken(e,t)),r;try{r=await n.user(`me`).get()}catch(e){return cliDebugPrint(`[loginWithToken] error getting user info`,{error:e,apiBaseUrl:t}),null}if(!r.id)throw Error(`The Apify API returned no user ID for this token, so the login cannot be stored.`);let i=r.proxy?.password;await ensureCredentialsCurrent();let{organizationOwnerUserId:a}=r;upsertProfile(r.id,{username:r.username,name:r.username||r.id,...a?{organizationOwnerUserId:a}:{},authMethod:`token`,expiresAt:null,hasRefreshToken:!1,loggedInAt:(/* @__PURE__ */ new Date()).toISOString()});let s=await clearKeyringSecrets();if(await clearOAuthSession(r.id),await setSecret(r.id,`token`,e,{skipIfUnchanged:!0}),i)await setSecret(r.id,`proxy-password`,i,{skipIfUnchanged:!0});else{let e=await deleteSecret(r.id,`proxy-password`);e&&s.push(e)}return s.length&&warning({message:`Secrets this login could not remove are still in the OS keyring at ${describeLeftovers(s)}; delete them with your OS keyring app.`}),{client:n,userInfo:r}}const Sn=ke(m);let Cn;async function useStdin(){if(Cn)return Cn;let t=e.stdin,n={isTTY:t.isTTY,hasData:!1,waitDelay:0,stream:t},r=await Sn(0).then(t=>{if(cliDebugPrint(`useStdin`,{stat:t,isRegularFile:t.isFile(),isDirectory:t.isDirectory(),isBlockDevice:t.isBlockDevice(),isCharDevice:t.isCharacterDevice(),isSymbolicLink:t.isSymbolicLink(),isFIFO:t.isFIFO(),isSocket:t.isSocket()}),e.platform===`win32`){if((t.mode&l.S_IFIFO)===l.S_IFIFO)return 100;if(t.isFile())return 50}return t.isFIFO()||t.isFile()||(t.isSocket()?50:!1)}).catch(()=>!1);return cliDebugPrint(`useStdin`,{hasData:n.hasData,waitDelay:n.waitDelay,isTTY:n.isTTY,pipedIn:r,readableEnded:t.readableEnded}),(!t.isTTY||r!==!1&&(t.isTTY!==void 0||!t.readableEnded))&&(n.hasData=!0),typeof r==`number`&&(n.waitDelay=r),Cn=n,n}const wn=5e3;let Tn,En,Dn=!1,On=!1;async function readStdin(e={}){return Tn??=_readStdin(e).then(e=>(En=e,Dn=!0,e)),Tn}function stdinWasCutShort(){return On}function describeStdinRead(){return Dn?En?`Yes`:`No`:`Not read`}async function _readStdin({implicit:e}){let{hasData:t,waitDelay:n,stream:r}=await useStdin();if(!t)return;let i=e?n||wn:n,a=[],s=new AbortController,c=null,armTimeout=e=>{e&&(c=setTimeout(()=>s.abort(),e).unref())},disarmTimeout=()=>{c&&=(clearTimeout(c),null)};armTimeout(i);let onData=t=>{a.push(t),disarmTimeout(),e&&armTimeout(1e3)};r.on(`data`,onData);try{await Ee(r,`end`,{signal:s.signal})}catch(t){if(t.name===`AbortError`){if(!e)return;On=a.length>0||i===wn}}finally{r.off(`data`,onData),r.pause()}disarmTimeout();let l=Buffer.concat(a);if(l.length)return l}const kn={output:e.stderr},An=`confirm`,jn=`no-${An}`;function stdinCheckWrapper(e,{errorMessageForStdin:t=`Please use the --${An}/--${jn} flags to confirm the action.`}={}){return async(n,...r)=>{let i=n;if(i.preAnswered!==void 0)return i.preAnswered;let{isTTY:a}=await useStdin();if(ge||!a){if(i.defaultWhenNonInteractive===void 0)throw Error(i.errorMessageForStdin??t);return i.defaultWhenNonInteractive}return await e(n,...r)}}const Mn=stdinCheckWrapper(async({message:e,validate:t,default:n})=>await Le({message:e,validate:t,default:n},kn),{errorMessageForStdin:`Please provide a valid input based on the command options.`});var Spider=class{name;class_name;start_urls;pathname;constructor(e){this.name=e.name,this.class_name=e.class_name,this.start_urls=e.start_urls,this.pathname=e.pathname}},SpiderFileAnalyzer=class{pathname;constructor(e){this.pathname=e}getSpiders(){let e=_(this.pathname,`utf8`),t=/class\s+(\w+)/g,n=[],r=t.exec(e);for(;r;)n.push(new Spider({class_name:r[1],pathname:this.pathname})),r=t.exec(e);return n}},ScrapyProjectAnalyzer=class{pathname;configuration=null;settings=null;constructor(e){this.pathname=e,this.settings=null,this.loadScrapyCfg()}static isApplicable(e){return p(z(e,`scrapy.cfg`))}async init(){await this.loadSettings()}loadScrapyCfg(){let e=new Ie,t=V(z(this.pathname,`scrapy.cfg`));if(!p(t))throw Error(`scrapy.cfg not found in "${t}".
|
|
2
2
|
Are you sure there is a Scrapy project there?`);e.read(t),this.configuration=e}async loadSettings(){let e=this.configuration.get(`settings`,`default`).split(`.`)[0],t=await Mn({message:`Enter the Scrapy BOT_NAME (see settings.py):`,default:e}),n=await Mn({message:`What folder are the Scrapy spider modules stored in? (see SPIDER_MODULES in settings.py):`,default:`${e}.spiders`});this.settings={BOT_NAME:t,SPIDER_MODULES:typeof n==`string`?[n]:n}}getName(){return this.settings?.BOT_NAME}getAvailableSpiders(){let e=this.settings?.SPIDER_MODULES;if(!e)throw Error(`SPIDER_MODULES path not found in settings.`);let t=[];for(let n of e){let e=z(this.pathname,n.replaceAll(`.`,`/`)),r=v(e,{withFileTypes:!0});for(let n of r)n.isFile()&&n.name.endsWith(`.py`)&&n.name!==`__init__.py`&&t.push(...new SpiderFileAnalyzer(z(e,n.name)).getSpiders())}return t}};const Nn=/* @__PURE__ */ new Map,Pn={node:[`--version`],deno:[`eval`,`"console.log(process.versions.node)"`],bun:[`--eval`,`"console.log(process.versions.node)"`]};async function getRuntimeVersion(t,n){try{let r=await Fe(t,n,{shell:!0,windowsHide:!0,verbose:e.env.APIFY_CLI_DEBUG?`full`:void 0});return r.stdout?r.stdout.trim().replace(/^v/,``):null}catch{return null}}async function getNpmVersion(t){let n=await Fe(t,[`--version`],{shell:!0,windowsHide:!0,verbose:e.env.APIFY_CLI_DEBUG?`full`:void 0});return n.stdout?n.stdout.trim().replace(/^v/,``):null}async function useJavaScriptRuntime(t=e.cwd()){let n=Nn.get(t);if(n)return cliDebugPrint(`useJavaScriptRuntime`,{cacheHit:!0,cwd:t,runtime:n.unwrapOr(null)}),n;for(let[e,n]of Object.entries(Pn))try{let r=normalizeExecutablePath(await Re(e)),i=await getRuntimeVersion(r,n);if(i){let n={executablePath:r,version:i};if(e===`node`){let e=normalizeExecutablePath(await Re(`npm`).catch(()=>null));e&&(n.pmPath=e,n.pmVersion=await getNpmVersion(e),n.pmName=`npm`)}else n.runtimeShorthand=e,n.pmPath=r,n.pmVersion=i,n.pmName=e;return Nn.set(t,Pe(n)),cliDebugPrint(`useJavaScriptRuntime`,{cacheHit:!1,cwd:t,runtime:Nn.get(t)?.unwrap()}),Pe(n)}}catch{}return Nn.set(t,Me),cliDebugPrint(`useJavaScriptRuntime`,{cacheHit:!1,cwd:t,runtime:null}),Me}const Fn=/* @__PURE__ */ new Map;async function getPythonVersion(t){try{let n=await Fe(t,[`-c`,`"import platform; print(platform.python_version())"`],{shell:!0,windowsHide:!0,verbose:e.env.APIFY_CLI_DEBUG?`full`:void 0});return n.stdout?n.stdout.trim():null}catch{return null}}async function usePythonRuntime({cwd:t=e.cwd(),force:n=!1}={}){let r=Fn.get(t);if(r&&!n)return cliDebugPrint(`usePythonRuntime`,{cacheHit:!0,cwd:t,runtime:r.unwrapOr(null)}),r;let i=le()===`win32`,a=i?[`Scripts`,`python.exe`]:[`bin`,`python3`],s;s=e.env.VIRTUAL_ENV?z(e.env.VIRTUAL_ENV,...a):z(t,`.venv`,...a),s=normalizeExecutablePath(s);try{let e=await getPythonVersion(s);if(e)return Fn.set(t,Pe({executablePath:s,version:e})),cliDebugPrint(`usePythonRuntime`,{cacheHit:!1,cwd:t,runtime:Fn.get(t)?.unwrap()}),Fn.get(t)}catch{}let c=[`python3`,`python`,...i?[`python3.exe`,`python.exe`]:[]];for(let e of c)try{let n=normalizeExecutablePath(await Re(e)),r=await getPythonVersion(n);if(r)return Fn.set(t,Pe({executablePath:n,version:r})),cliDebugPrint(`usePythonRuntime`,{cacheHit:!1,cwd:t,runtime:Fn.get(t)?.unwrap()}),Fn.get(t)}catch{}return Fn.set(t,Me),cliDebugPrint(`usePythonRuntime`,{cacheHit:!1,cwd:t,runtime:null}),Me}const In=/* @__PURE__ */ new Map;async function useCwdProject({cwd:t=e.cwd()}={}){let n=In.get(t);if(n)return cliDebugPrint(`useCwdProject`,{cacheHit:!0,project:n}),Ne(n);let r={type:3},check=async()=>{if(await checkScrapyProject(t)){r.type=2;let e=await usePythonRuntime({cwd:t});r.runtime=e.unwrapOr(void 0),await fileExists(z(t,`uv.lock`))&&(r.packageManager=`uv`);let n=new ScrapyProjectAnalyzer(t);if(n.loadScrapyCfg(),n.configuration.hasKey(`apify`,`mainpy_location`))r.entrypoint={path:n.configuration.get(`apify`,`mainpy_location`)};else try{let e=await checkPythonProject(t);e&&(r.entrypoint={path:e})}catch{}return}let e=await checkNodeProject(t);if(!e){let e=null;try{e=await checkPythonProject(t)}catch(e){return je({message:e instanceof Error?e.message:String(e)})}if(e){r.type=1;let n=await usePythonRuntime({cwd:t});r.entrypoint={path:e},r.runtime=n.unwrapOr(void 0),await fileExists(z(t,`uv.lock`))&&(r.packageManager=`uv`);let i=z(t,e.replace(/\./g,`/`));await fileExists(z(i,`__main__.py`))||(r.warnings=[`The detected Python package "${e}" is missing __main__.py. Running with "python -m" will fail without it.`]);return}}if(e){r.type=0;let t=await useJavaScriptRuntime();r.runtime=t.unwrapOr(void 0),e.type===`file`?r.entrypoint={path:e.path}:e.type===`script`&&(r.entrypoint={script:e.script})}else return Ne(r)},i=await check();return i?.isErr()?(cliDebugPrint(`useCwdProject`,{cacheHit:!1,error:i}),i):(cliDebugPrint(`useCwdProject`,{cacheHit:!1,project:r}),In.set(t,r),Ne(r))}async function checkNodeProject(e){let t=z(e,`package.json`);try{let n=await ye(t,`utf-8`),r=JSON.parse(n);if(r.scripts?.start)return{type:`script`,script:`start`};if(r.main)try{return await _e(V(e,r.main)),{path:V(e,r.main),type:`file`}}catch{}return{type:`unknown-entrypoint`}}catch{}let n=[z(e,`index.js`),z(e,`index.mjs`),z(e,`index.cjs`),z(e,`main.js`),z(e,`main.mjs`),z(e,`main.cjs`),z(e,`src`,`index.js`),z(e,`src`,`index.mjs`),z(e,`src`,`index.cjs`),z(e,`src`,`main.js`),z(e,`src`,`main.mjs`),z(e,`src`,`main.cjs`),z(e,`dist`,`index.js`),z(e,`dist`,`index.mjs`),z(e,`dist`,`index.cjs`),z(e,`dist`,`main.js`),z(e,`dist`,`main.mjs`),z(e,`dist`,`main.cjs`)];for(let e of n)try{return await _e(e),{path:e,type:`file`}}catch{}return null}async function fileExists(e){try{return await _e(e),!0}catch{return!1}}async function dirExists(e){try{return(await U(e)).isDirectory()}catch{return!1}}function isValidPythonIdentifier(e){return/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(e)}async function hasPythonFilesInDirOrSubdirs(e){try{let t=await be(e,{withFileTypes:!0});if(t.some(e=>e.isFile()&&e.name.endsWith(`.py`)))return!0;for(let n of t){if(!n.isDirectory()||n.name.startsWith(`.`)||n.name.startsWith(`_`))continue;let t=z(e,n.name);try{let e=await be(t,{withFileTypes:!0});if(e.some(e=>e.isFile()&&e.name.endsWith(`.py`)))return!0;if(n.name===`src`){for(let n of e)if(n.isDirectory()&&!(n.name.startsWith(`.`)||n.name.startsWith(`_`)))try{if((await be(z(t,n.name),{withFileTypes:!0})).some(e=>e.isFile()&&e.name.endsWith(`.py`)))return!0}catch{}}}catch{}}return!1}catch{return!1}}async function dirHasPyFiles(e){try{return(await be(e,{withFileTypes:!0})).some(e=>e.isFile()&&e.name.endsWith(`.py`)&&e.name!==`__init__.py`)}catch{return!1}}async function findNearMissPackagesInDir(e){try{let t=await be(e,{withFileTypes:!0}),n=[];for(let r of t){if(!r.isDirectory()||r.name.startsWith(`.`)||r.name.startsWith(`_`))continue;let t=isValidPythonIdentifier(r.name),i=await fileExists(z(e,r.name,`__init__.py`)),a=i||await dirHasPyFiles(z(e,r.name));t&&i||a&&n.push({name:r.name,needsRename:!t,needsInit:!i})}return n}catch{return[]}}async function findPackagesInDir(e){try{let t=await be(e,{withFileTypes:!0}),n=[];for(let r of t){if(!r.isDirectory())continue;let{name:t}=r;t.startsWith(`.`)||t.startsWith(`_`)||!isValidPythonIdentifier(t)||await fileExists(z(e,t,`__init__.py`))&&n.push({name:t,path:z(e,t)})}return n}catch{return[]}}async function discoverPythonPackages(e){let t=[],n=await findPackagesInDir(e);t.push(...n.map(e=>e.name));let r=z(e,`src`),i=await fileExists(z(r,`__init__.py`));if(await dirExists(r)&&!i){let e=await findPackagesInDir(r);t.push(...e.map(e=>`src.${e.name}`))}return t}async function checkPythonProject(e){let t=await discoverPythonPackages(e);if(t.length===1)return t[0];if(t.length>1){let e=t.map(e=>` - ${e}`).join(`
|
|
3
3
|
`);throw Error(`Multiple Python packages found:\n${e}\n\nApify CLI cannot determine which package to run.
|
|
4
4
|
Please specify the package using the --entrypoint flag, e.g.:
|
|
@@ -47,8 +47,7 @@ Common package structures:
|
|
|
47
47
|
`)}`):n.push(`To solve this, provide the flag like this: --${e.name}=<value>`)):e.expectsValue?n.push(`expects a value`):n.push(`does not take an argument`),n.map(e=>t.gray(e)).join(` `)}static into(e,t){if(e instanceof CommandError)return e;if(e instanceof Error&&`code`in e){let n=e;switch(n.code){case`ERR_PARSE_ARGS_INVALID_OPTION_VALUE`:return new CommandError({code:0,message:n.message,command:t});case`ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL`:return new CommandError({code:1,message:n.message,command:t});case`ERR_PARSE_ARGS_UNKNOWN_OPTION`:return new CommandError({code:2,message:n.message,command:t});default:return new CommandError({code:9,message:`Unknown error: ${e instanceof Error?e.message:String(e)}`,command:t})}}return new CommandError({code:9,message:`Unknown error: ${e instanceof Error?e.message:String(e)}`,command:t})}};function migrateStateV0ToV1$1(e){if(e.version&&e.version>=1)return!1;let t=e;if(t.latestNpmVersionCheckedAt){let n=new Date(t.latestNpmVersionCheckedAt).getTime(),r=t.latestNpmVersion;cliDebugPrint(`LocalStateV0ToV1`,`Migrating state from v0 to v1`,{oldState:e,newState:{versionCheck:{lastChecked:n,lastVersion:r}}}),updateLocalState({version:1},e=>{e.versionCheck={lastChecked:n,lastVersion:r}})}return!0}const Jn={version:1};function useLocalState(){let e=STATE_FILE_PATH();if(!p(e))return Jn;let t=JSON.parse(_(e,`utf-8`));return migrateStateV0ToV1$1(t)?useLocalState():t}function updateLocalState(e,t){let n={...e};t(n);let r=STATE_FILE_PATH(),i=R(r);h(i,{recursive:!0}),w(STATE_FILE_PATH(),JSON.stringify(n,null,` `))}const Yn=useCLIMetadata(),Xn=`Apify CLI/${Yn.version} (https://github.com/apify/apify-cli)`,Zn=[`https://1.1.1.1`,`https://8.8.8.8`];async function isOnline(e=500){let t=new AbortController,n=setTimeout(()=>{t.abort()},e),r=await Promise.any(Zn.map(async e=>fetch(e,{signal:t.signal,headers:{"User-Agent":Xn},keepalive:!1}))).catch(()=>null);return clearTimeout(n),r?r.ok?(cliDebugPrint(`isOnline`,{state:`online`,site:r.url}),!0):(cliDebugPrint(`isOnline`,{state:`offline`}),!1):(cliDebugPrint(`isOnline`,{state:`timeout`}),!1)}async function getLatestVersion(e){let t=await fetch(`https://api.github.com/repos/apify/apify-cli/releases/latest`,{headers:{"User-Agent":Xn}});if(!t.ok)return cliDebugPrint(`useCLIVersionCheck`,`Failed to fetch latest version`,{statusCode:t.status,body:await t.text()}),warning({message:`Failed to fetch latest version of Apify CLI, using the cached version instead.`}),null;let n=(await t.json()).tag_name.replace(/^v/,``);return cliDebugPrint(`useCLIVersionCheck`,`Fetched latest version`,{version:n}),updateLocalState(e,e=>{e.versionCheck={lastChecked:Date.now(),lastVersion:n}}),n}function shouldSkipVersionCheck(){return!(!process.env.APIFY_CLI_SKIP_UPDATE_CHECK||[`0`,`false`].includes(process.env.APIFY_CLI_SKIP_UPDATE_CHECK))}async function useCLIVersionCheck(e=!1){let t=useLocalState(),n=!t.versionCheck||Date.now()-t.versionCheck.lastChecked>864e5,r=e||n&&await isOnline(),i=r?await getLatestVersion(t):t.versionCheck?.lastVersion;if(!i)return{currentVersion:Yn.version,latestVersion:`unknown`,shouldUpdate:!1,cacheHit:!1};let a=ze(i,Yn.version);return{currentVersion:Yn.version,latestVersion:i,shouldUpdate:a,cacheHit:!r}}function useCommandSuggestions(e){let t=[...J.entries()].sort(([e],[t])=>e.localeCompare(t)),n=e.toLowerCase();return t.map(([e,t])=>{let r=e.toLowerCase(),i=e.split(` `),a=i[i.length-1].toLowerCase(),s=t.aliases?.includes(a)||t.hiddenAliases?.includes(a)||!1,c=Ke(n,r),l=Ge(n,r),u=c<=2||l>=.975;return u?(cliDebugPrint(`useCommandSuggestions`,{inputString:n,lowercased:r,matches:u,levenshtein:c,jaroWinkler:l}),s?{string:`${r} (alias for ${t.name})`,distance:l}:{string:`${r}`,distance:l}):null}).filter(e=>e!==null).sort((e,t)=>t.distance-e.distance).map(e=>e.string)}const Qn=useCLIMetadata(),$n=`Apify CLI/${Qn.version} (https://github.com/apify/apify-cli)`;function processVersionCheck(t){Qn.installMethod!==`bundle`&&(He(e.version,$t)||(error({message:`${t} CLI requires Node.js version ${$t}. Your current version is ${e.version}.`}),e.exit(1)))}function printCLIVersionAndExitIfFlagUsed(t){t.values.version===!0&&t.positionals.length===0&&(console.log(Qn.fullVersionString),e.exit(0))}function printHelpAndExitIfFlagUsedOrNoCommandPassed(t,n){(t.values.help===!0&&t.positionals.length===0||t.positionals.length===0)&&(console.log(renderMainHelpMenu(n)),e.exit(0))}function handleCommandNotFound(n){let r=useCommandSuggestions(String(n)),i=t.gray(`Command ${t.whiteBright(n)} not found`);r.length&&(i+=`
|
|
48
48
|
`,i+=t.gray(`Did you mean: ${r.map(e=>t.whiteBright(e)).join(`, `)}?`)),error({message:i}),e.exit(1)}async function runVersionCheck(e,t){let n=J.get(`upgrade`);[n.name,...n.aliases??[]].some(e=>t===e)?cliDebugPrint(`[VersionCheckMiddleware]`,`upgrade command detected, skipping version check`):shouldSkipVersionCheck()?cliDebugPrint(`[VersionCheckMiddleware]`,`skipping version check because APIFY_CLI_SKIP_UPDATE_CHECK is set`):await internalRunCommand(e,n,{flags_internalAutomaticCall:!0})}async function runCLI(t){migrateLegacyBundleInstallIfNeeded(),cliDebugPrint(`CLIMetadata`,{...Qn,fullVersionString:Qn.fullVersionString,argv:e.argv,cwd:e.cwd(),execPath:e.execPath});let n=e.argv.slice(2);cliDebugPrint(`ProcessArgv`,n);let r=Oe({allowPositionals:!0,strict:!1,options:{help:{type:`boolean`,short:`h`},version:{type:`boolean`,short:`v`}},args:n});printCLIVersionAndExitIfFlagUsed(r),printHelpAndExitIfFlagUsedOrNoCommandPassed(r,t),await runVersionCheck(t,r.positionals[0]),cliDebugPrint(`TopLevelOptions`,r);let i=r.positionals[0],a=-1;if(J.get(i)?.subcommands?.length){if(r.positionals[1]?.toLowerCase()===`help`)a=1;else if(r.positionals[2]?.toLowerCase()===`help`){let e=J.get(`${i} ${r.positionals[1]}`);e&&!e.args&&(a=2)}}if(a!==-1){let e=r.positionals[a],t=n.indexOf(e);t!==-1&&n.splice(t,1),!n.includes(`--help`)&&!n.includes(`-h`)&&n.push(`--help`),r.positionals.splice(a,1),r.values.help=!0}let s=r.positionals[1],c=!1,l=J.get(i);if(!l)return handleCommandNotFound(i);let u=l;if(l.subcommands?.length){if(!s)return l.printHelp();c=!0,u=J.get(`${i} ${s}`)}if(!u)return handleCommandNotFound(`${i} ${s}`);let f=[...n],p=f.indexOf(i);if(cliDebugPrint(`CommandNameIndex`,p),f.splice(p,1),c){let e=f.indexOf(s);cliDebugPrint(`SubcommandNameIndex`,e),f.splice(e,1)}cliDebugPrint(`RebuiltArgs`,f),cliDebugPrint(`CommandToRun`,u);let m=new u(t,c?`${l.name} ${s}`:l.name,i,c?s:void 0),h=m._buildParseArgsOption();cliDebugPrint(`ParserOptionsForCommand`,h);try{let e=Oe({...h,args:f});await m._run(e),cliDebugPrint(`CommandArgsResult`,e)}catch(t){error({message:qn.into(t,u).getPrettyMessage()}),e.exit(1)}}const er=`__CLI_`;function resolveInputKey(){return process.env[j.INPUT_KEY]||process.env[F.INPUT_KEY]||process.env.CRAWLEE_INPUT_KEY||`INPUT`}function inputFileRegExp(e){return RegExp(`(^${rt(e)}(?:\\.[^.]+)?$)`)}const tr=`__metadata__.json`,nr=`.${tr}`;function encodeRecordKey(e){return encodeURIComponent(e).replaceAll(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`)}const recordMetadataFileName=e=>`${encodeRecordKey(e)}${nr}`;async function writeKvsRecord({storePath:e,key:t,fileName:n,contentType:r,body:i}){let a={key:t,contentType:r};n!==encodeRecordKey(t)&&(a.filename=n),await Promise.all([W(z(e,n),i),W(z(e,recordMetadataFileName(t)),JSON.stringify(a,null,2))])}function readKvsRecordMetadata(e,t){let n;try{n=JSON.parse(_(z(e,recordMetadataFileName(t)),`utf8`))}catch{return}if(typeof n?.contentType!=`string`)return;let{filename:r}=n;return r===void 0||typeof r==`string`&&![``,`.`,`..`].includes(r)&&de(r)===r&&r!==tr&&!r.endsWith(nr)?n:void 0}async function deleteKvsRecord(e,t){await Promise.all([Se(e,{force:!0}),Se(z(R(e),recordMetadataFileName(t)),{force:!0})])}const rr=it,ir=tt,getLocalStorageDir=()=>{let t=F.LOCAL_STORAGE_DIR;return e.env[t]||e.env.CRAWLEE_STORAGE_DIR||`storage`},getLocalKeyValueStorePath=t=>{let n=j.DEFAULT_KEY_VALUE_STORE_ID,r=t||e.env[n]||ne[n];return z(getLocalStorageDir(),re.keyValueStores,r)},getLocalDatasetPath=t=>{let n=j.DEFAULT_DATASET_ID,r=t||e.env[n]||ne[n];return z(getLocalStorageDir(),re.datasets,r)},getLocalRequestQueuePath=t=>{let n=j.DEFAULT_REQUEST_QUEUE_ID,r=t||e.env[n]||ne[n];return z(getLocalStorageDir(),re.requestQueues,r)},getLocalUserInfo=async e=>{await ensureCredentialsCurrent();let{profile:t,missingProfile:n}=e?lookUpProfile(e):lookUpActiveProfile();if(n)throw Error(`Your ${e?``:`active `}profile "${n}" is missing from ${AUTH_FILE_PATH()}. Run "apify login" to log in again.`);if(!t)return{};let r={id:t.id};t.username&&(r.username=t.username),t.organizationOwnerUserId&&(r.organizationOwnerUserId=t.organizationOwnerUserId);let i=await getAccessToken(t.id);i&&(r.token=i);let a=await getSecret(t.id,`proxy-password`);return a&&(r.proxy={password:a}),r};async function getLoggedClientOrThrow(){let{client:t,auth:n,error:r}=await getLoggedClient();if(!t)throw e.exitCode=1,Error(describeAuthFailure(n,r));return t}let ar;async function getLoggedClient(){let e=await resolveAuth();if(!e)return{client:null};let t=new D(getApifyClientOptionsForToken(e.token));try{let n=await t.user(`me`).get();ar={token:e.token,userInfo:n}}catch(t){return cliDebugPrint(`[getLoggedClient] error getting user info`,{error:t}),{client:null,auth:e,error:t}}return{client:t,auth:e}}async function getCurrentUserInfo(){let e=await resolveAuth();if(!e)return getLocalUserInfo();if(ar?.token===e.token)return ar.userInfo;if(e.source!==`env`)return getLocalUserInfo(e.profile?.id);let t=await new D({...getApifyClientOptionsForToken(e.token),maxRetries:1,timeoutSecs:10}).user(`me`).get();return ar={token:e.token,userInfo:t},t}const getLocalConfigPath=e=>z(e,K),getJsonFileContent=e=>{if(p(e))return JSON.parse(_(e,{encoding:`utf-8`}))},getLocalConfig=e=>getJsonFileContent(getLocalConfigPath(e)),setLocalConfig=async(t,n)=>{let r=z(n||e.cwd(),K);await H(R(r),{recursive:!0}),w(r,JSON.stringify(t,null,` `))},or=[getLocalStorageDir(),`node_modules`,`.venv`],setLocalEnv=async e=>{let t=getLocalKeyValueStorePath();ensureFolderExistsSync(e,getLocalDatasetPath()),ensureFolderExistsSync(e,getLocalRequestQueuePath()),ensureFolderExistsSync(e,t);let n=z(e,`.gitignore`),r=``;p(n)&&(r=_(n,{encoding:`utf-8`}));let i=[];for(let e of or)RegExp(`^${rt(e)}$`,`mg`).test(r)||i.push(e);i.length>0&&(r.length>0?(i.unshift(`# Added by Apify CLI`),w(n,`\n${i.join(`
|
|
49
49
|
`)}\n`,{flag:`a`})):w(n,`${i.join(`
|
|
50
|
-
`)}\n`,{flag:`w`}))},sr=new st(lt,ct).define({"application/gzip":[`tgz`],"text/typescript":[`ts`,`tsx`,`mts`]},!0),getSourceFileFormat=(e,t)=>{let n=sr.getType(e);return n?n.startsWith(`text/`)||n.includes(`javascript`)||n.includes(`json`)||n.includes(`xml`)||n.includes(`application/node`)||n.includes(`application/toml`)||n.includes(`application/x-sh`)||n.includes(`application/x-httpd-php`)?ae.TEXT:ae.BASE64:at(t)===`binary`?ae.BASE64:ae.TEXT},createSourceFiles=async(e,t)=>e.map(e=>{let n=_(z(t,e)),r=getSourceFileFormat(e,n);return{name:e,format:r,content:r===ae.TEXT?n.toString(`utf8`):n.toString(`base64`)}}),
|
|
51
|
-
`).filter(Boolean);if(t.length===0)return()=>!1;let n=rr().add(t);return e=>n.ignores(e)}catch{return getGitignoreFallbackMatcher(e)}},parseActorIgnore=async e=>{let t=z(e,`.actorignore`);if(!p(t))return{excludeFilter:null,forceIncludePatterns:[]};let n=(await ye(t,`utf-8`)).split(`
|
|
50
|
+
`)}\n`,{flag:`w`}))},sr=new st(lt,ct).define({"application/gzip":[`tgz`],"text/typescript":[`ts`,`tsx`,`mts`]},!0),getSourceFileFormat=(e,t)=>{let n=sr.getType(e);return n?n.startsWith(`text/`)||n.includes(`javascript`)||n.includes(`json`)||n.includes(`xml`)||n.includes(`application/node`)||n.includes(`application/toml`)||n.includes(`application/x-sh`)||n.includes(`application/x-httpd-php`)?ae.TEXT:ae.BASE64:at(t)===`binary`?ae.BASE64:ae.TEXT},createSourceFiles=async(e,t)=>e.map(e=>{let n=_(z(t,e)),r=getSourceFileFormat(e,n);return{name:e,format:r,content:r===ae.TEXT?n.toString(`utf8`):n.toString(`base64`)}}),gitOutput=(e,t,n)=>{try{return Je(`git ${e}`,{cwd:t,input:n,encoding:`utf-8`,stdio:[n===void 0?`ignore`:`pipe`,`pipe`,`ignore`]})}catch{return null}},readFileOrEmpty=async e=>ye(e,`utf-8`).catch(()=>``),posixRelative=(e,t)=>B(e,t).split(me).join(`/`),keepLinesForCwd=(e,t)=>{let n=e.normalize(`NFC`).split(/\r?\n/),r=`${t}/`.normalize(`NFC`);return!t||!rr().add(n).ignores(r)?n:n.filter(e=>!rr().add(e).ignores(r))},scopeLines=(e,t)=>{if(!t)return e;let n=t.replace(/[\\*?[\]]/g,`\\$&`).replace(/^[!#]/,`\\$&`);return e.flatMap(e=>{let t=e.replace(/(?<!\\) +$/,``),r=t.startsWith(`!`)?`!`:``,i=t.slice(r.length);if(!i||t.startsWith(`#`))return[];let a=i.slice(0,-1).includes(`/`);return[`${r}${n}/${a?i.replace(/^\//,``):`**/${i}`}`]})},getGitExcludeFilePaths=(t,n)=>{let r=gitOutput(`rev-parse --git-path info/exclude`,t)?.trim();if(!r)return[];let i=gitOutput(`config --path --get core.excludesFile`,t)?.trim()??z(e.env.XDG_CONFIG_HOME||z(ce(),`.config`),`git`,`ignore`);return[...i?[V(n,i)]:[],V(t,r)]},getGitignoreRulesMatcher=async e=>{let t=p(e)?y(e):e,n=[],r=t;for(;!p(z(r,`.git`))&&r!==R(r);)r=R(r),n.unshift(r);let i=await ut(`**/.gitignore`,{dot:!0,cwd:t,ignore:[`.git/**`],expandDirectories:!1});i.sort((e,t)=>e.split(`/`).length-t.split(`/`).length);let a=posixRelative(r,t),s=[];for(let e of getGitExcludeFilePaths(t,r))s.push(...keepLinesForCwd(await readFileOrEmpty(e),a));for(let e of n){let n=await readFileOrEmpty(z(e,`.gitignore`));s.push(...scopeLines(keepLinesForCwd(n,posixRelative(e,t)),posixRelative(r,e)))}for(let e of i){let n=await readFileOrEmpty(z(t,e));s.push(...scopeLines(n.split(/\r?\n/),posixRelative(r,z(t,R(e)))))}let c=rr().add(s.map(e=>e.normalize(`NFC`)));return e=>c.ignores((a?`${a}/${e}`:e).normalize(`NFC`))},getGitignoreMatcher=async(e,t=[])=>{let n=gitOutput(`check-ignore -q --no-index ./`,e)===null?gitOutput(`ls-files -z --others --ignored --exclude-standard --directory`,e)?.split(`\0`).filter(Boolean):null;if(!n||n.includes(`./`))return getGitignoreRulesMatcher(e);let r=t.length?gitOutput(`check-ignore --no-index -z --stdin`,e,t.join(`\0`))?.split(`\0`)??[]:[],i=new Set([...n,...r].map(e=>e.normalize(`NFC`))),a=n.map(e=>(e.endsWith(`/`)?e:`${e}/`).normalize(`NFC`));return e=>{let t=e.normalize(`NFC`);return i.has(t)||a.some(e=>t.startsWith(e))}},parseActorIgnore=async e=>{let t=z(e,`.actorignore`);if(!p(t))return{excludeFilter:null,forceIncludePatterns:[]};let n=(await ye(t,`utf-8`)).split(`
|
|
52
51
|
`),r=[],i=[];for(let e of n){let t=e.trim();t&&!t.startsWith(`#`)&&(t.startsWith(`!`)?i.push(t.slice(1)):r.push(t))}return{excludeFilter:r.length>0?e=>{let t=rr().add(r);return e.filter(e=>!t.ignores(e))}:null,forceIncludePatterns:i}},cr=[`.git/**`,`apify_storage`,`node_modules`,`storage`,`crawlee_storage`],getActorLocalFilePaths=async t=>{let n=t??e.cwd(),r=cr,{excludeFilter:i,forceIncludePatterns:a}=await parseActorIgnore(n),s=await getGitignoreMatcher(n),c=await ut([`*`,`**/**`],{ignore:r,dot:!0,expandDirectories:!1,cwd:n}),l=c.filter(e=>!s(e));if(i&&(l=i(l)),a.length>0){let e=rr().add(a),t=c.filter(t=>e.ignores(t)),n=new Set(l);for(let e of t)n.add(e);l=[...n]}let u=c.filter(e=>e===`.actor`||e.startsWith(`.actor/`));if(u.length>0){let e=new Set(l);for(let t of u)e.add(t);l=[...e]}return l},createActZip=async(e,t,n)=>{p(e)&&await deleteFile(e);let r=f(e),i=new nt({zlib:{level:6}});i.pipe(r),t.forEach(e=>i.file(z(n,e),{name:e})),await i.finalize()},getLocalInput=(e,t)=>{let n=t??I.INPUT,r=V(e,getLocalKeyValueStorePath());if(!p(r))return;let i=readKvsRecordMetadata(r,n);if(i){let e=i.filename??encodeRecordKey(n);if(p(z(r,e)))return{body:_(z(r,e)),contentType:i.contentType,fileName:e}}let a=v(r).find(e=>!!e.match(inputFileRegExp(n)));return a?{body:_(z(r,a)),contentType:sr.getType(a),fileName:a}:void 0},purgeDefaultQueue=async()=>{await rimrafPromised(V(e.cwd(),getLocalRequestQueuePath()))},purgeDefaultDataset=async()=>{await rimrafPromised(V(e.cwd(),getLocalDatasetPath()))},purgeDefaultKeyValueStore=async(...t)=>{let n=V(e.cwd(),getLocalKeyValueStorePath());if(!p(n))return;let r=v(n),i=t.length>0?t:[I.INPUT],a=i.map(inputFileRegExp),s=new Set(i.flatMap(e=>[recordMetadataFileName(e),readKvsRecordMetadata(n,e)?.filename])),c=[];r.forEach(e=>{!s.has(e)&&!a.some(t=>t.test(e))&&c.push(deleteFile(z(n,e)))}),await Promise.all(c)},outputJobLog=async({job:t,timeoutMillis:n,apifyClient:r})=>{let{id:i,status:a}=t,s=r||new D({baseUrl:e.env.APIFY_CLIENT_BASE_URL});if(ee.includes(a)){if(e.env.APIFY_NO_LOGS_IN_TESTS)return;let t=await s.log(i).get();e.stderr.write(t)}else return new Promise(async t=>{let r=await s.log(i).stream();if(!r){t(`no-logs`);return}let a=null;r.on(`data`,t=>{e.env.APIFY_NO_LOGS_IN_TESTS||e.stderr.write(t.toString())}),r.once(`end`,()=>{t(`finished`),a&&clearTimeout(a)}),n!==void 0&&(a=setTimeout(()=>{r.destroy(),t(`timeouts`)},n))})},checkIfStorageIsEmpty=async t=>{let n=t||I.INPUT,r=getLocalStorageDir().replaceAll(`\\`,`/`),i=getLocalKeyValueStorePath().replaceAll(`\\`,`/`);return(await ut([`${r}/**`,...[n,`${er}${n}`].flatMap(e=>[`!${i}/${e}`,`!${i}/${e}.*`])],{cwd:e.cwd()})).length===0},validateActorName=e=>{if(!P.REGEX.test(e))throw Error(`The Actor name must be a DNS hostname-friendly string (e.g. my-newest-actor).`);if(e.length<P.MIN_LENGTH)throw Error(`The Actor name must be at least ${P.MIN_LENGTH} characters long.`);if(e.length>P.MAX_LENGTH)throw Error(`The Actor name must be at most ${P.MAX_LENGTH} characters long.`)},validateActorNameForPrompt=e=>{try{validateActorName(e)}catch(e){return e.message}return!0},sanitizeActorName=e=>{let t=e.replaceAll(/[^a-zA-Z0-9-]/g,`-`);return t.length<P.MIN_LENGTH&&(t=`${t}-apify-actor`),t=t.replaceAll(/^-+/g,``).replaceAll(/-+$/g,``),t.slice(0,P.MAX_LENGTH)},isPythonVersionSupported=e=>He(e,`^${nn}`),isNodeVersionSupported=e=>{let t=Ve($t);return Be(e,t)},downloadZip=async e=>{let t=await k.get(e,{responseType:`arraybuffer`,validateStatus:()=>!0,headers:{"User-Agent":`Apify CLI/${useCLIMetadata().version} (https://github.com/apify/apify-cli)`}});if(t.status<200||t.status>=300){let n=Buffer.from(t.data).toString(`utf8`).trim().slice(0,500);throw Error(`Failed to download the archive from ${e} (HTTP ${t.status}).${n?`\nServer response: ${n}`:``}`)}let n=Buffer.from(t.data);if(n.subarray(0,2).toString()!==`PK`)throw Error(`The file downloaded from ${e} is not a valid zip archive - a proxy or firewall likely intercepted the download. Check your network or try a different one.`);return new et(n)},downloadAndUnzip=async({url:e,pathTo:t})=>{(await downloadZip(e)).extractAllTo(t,!0)},q=new $e(`YYYY-MM-DD [at] HH:mm:ss`),lr=new $e(`YYYY-MM-DD[
|
|
53
52
|
]HH:mm:ss`),ur=new $e(`YYYY-MM-DD`),dr=new Xe,fr=new Xe({[Qe.Day]:{DEFAULT:`d`},[Qe.Hour]:{DEFAULT:`h`},[Qe.Minute]:{DEFAULT:`m`},[Qe.Month]:{DEFAULT:`M`},[Qe.Second]:{DEFAULT:`s`},[Qe.Week]:{DEFAULT:`w`},[Qe.Year]:{DEFAULT:`y`}});function objectGroupBy(e,t){if(`groupBy`in Object)return Object.groupBy(e,t);let n={},r=0;for(let i of e){let e=t(i,r++);n[e]||(n[e]=[]),n[e].push(i)}return n}function printJsonToStdout(e){console.log(JSON.stringify(e,null,2))}const userHomeDir=()=>e.env.HOME??ce(),tildify=e=>{let t=userHomeDir();return t&&e.startsWith(t)?e.replace(t,`~`):e};function detectShell(){let t=e.env.SHELL??``;return t.includes(`zsh`)?`zsh`:t.includes(`bash`)?`bash`:t.includes(`fish`)?`fish`:`unknown`}function shellConfigFile(t,n){switch(n){case`bash`:{let n=[z(t,`.bashrc`),z(t,`.bash_profile`)];e.env.XDG_CONFIG_HOME&&n.push(z(e.env.XDG_CONFIG_HOME,`.bashrc`),z(e.env.XDG_CONFIG_HOME,`.bash_profile`),z(e.env.XDG_CONFIG_HOME,`bashrc`),z(e.env.XDG_CONFIG_HOME,`bash_profile`));for(let e of n)if(p(e))return e;return null}case`zsh`:{let n=e.env.ZDOTDIR||t||userHomeDir();return z(n,`.zshrc`)}case`fish`:return z(t,`.config`,`fish`,`config.fish`);case`unknown`:return null}}function parseWaitForFinishMillis(e){if(e===void 0)return;let t=Number.parseInt(e,10);if(!(!Number.isFinite(t)||t<0))return t*1e3}const pr=[`Apify collects telemetry data about general usage of Apify CLI to help us improve the product.`,`This feature is enabled by default, and you can disable it by setting the "APIFY_CLI_DISABLE_TELEMETRY" environment variable to "1", or by running "apify telemetry disable".`,`You can find more information about our telemetry in https://docs.apify.com/cli/docs/telemetry.`].join(`
|
|
54
53
|
`);function createAnonymousId(){return`CLI:${se()}`}async function migrateStateV0ToV1(e){if(e.version&&e.version>=1)return!1;let t=e,n=await getLocalUserInfo().catch(()=>({}));return updateTelemetryState({version:1,enabled:!0},e=>{n.id&&t.distinctId===n.id?(e.anonymousId=createAnonymousId(),e.userId=n.id):e.anonymousId=t.distinctId}),!0}async function useTelemetryState(){let e=TELEMETRY_FILE_PATH();if(!p(e)){let e=await getLocalUserInfo().catch(()=>({}));return updateTelemetryState({version:1,enabled:!0,anonymousId:createAnonymousId(),userId:e.id}),(!process.env.APIFY_CLI_DISABLE_TELEMETRY||[`false`,`0`].includes(process.env.APIFY_CLI_DISABLE_TELEMETRY))&&info({message:pr}),useTelemetryState()}let t=JSON.parse(_(e,`utf-8`));return await migrateStateV0ToV1(t)?useTelemetryState():t}function updateTelemetryState(e,t){let n={...e};t?.(n);let r=TELEMETRY_FILE_PATH(),i=R(r);h(i,{recursive:!0}),w(TELEMETRY_FILE_PATH(),JSON.stringify(n,null,` `))}async function updateUserId(e){updateTelemetryState(await useTelemetryState(),t=>{t.userId=e})}async function checkAndUpdateLastCommand(e){try{let t=await useTelemetryState(),n=Date.now(),r=t.lastCommand===e&&n-(t.lastCommandTimestamp??0)<1e4;return updateTelemetryState(t,t=>{t.lastCommand=e,t.lastCommandTimestamp=n}),r}catch{return!1}}async function updateTelemetryEnabled(e){updateTelemetryState(await useTelemetryState(),t=>{t.enabled=e})}async function useTelemetryEnabled(){if(process.env.APIFY_CLI_DISABLE_TELEMETRY&&![`false`,`0`].includes(process.env.APIFY_CLI_DISABLE_TELEMETRY))return cliDebugPrint(`telemetry`,`disabled by env variable`),!1;let e=await useTelemetryState();return cliDebugPrint(`telemetry state`,{telemetryState:e}),e.enabled}async function useTelemetryIdentifiers(){let e=await useTelemetryState();return{anonymousId:e.anonymousId,userId:e.userId}}const mr=useCLIMetadata(),hr=mr.isBeta?`rT67mFpIQD5qS9bJBoIYSFbZucrt2DZC`:`2uPK6yhPqjC0eNUFhaY78S26cRKyaa6t`;async function trackEvent(e,t){let n=await useTelemetryIdentifiers(),r={anonymousId:n.anonymousId,context:{app:{name:`apify-cli`,version:mr.version,build:mr.hash},library:{name:`apify-cli`,version:mr.version},os:{name:mr.platform},userAgent:$n,channel:`server`},event:e,properties:{...t,app:`cli`},timestamp:(/* @__PURE__ */ new Date()).toISOString(),userId:n.userId,writeKey:hr};if(cliDebugPrint(`trackEvent`,r),!await useTelemetryEnabled())cliDebugPrint(`trackEvent`,`telemetry disabled`);else try{let e=await fetch(`https://api.segment.io/v1/track`,{method:`POST`,body:JSON.stringify(r)});if(!e.ok){cliDebugPrint(`trackEvent`,`failed to send event`,await e.text());return}cliDebugPrint(`trackEvent`,`event sent`)}catch(e){cliDebugPrint(`trackEvent`,`failed to send event`,e)}}function shouldSkipRentalSunsetNotice({now:e,isCi:t,skipEnvValue:n}){return!!(n&&![`0`,`false`].includes(n)||t||e>=Xt)}function wasNotifiedRecently(e,t,n){return!e?.lastNotifiedAt||e.username!==t?!1:n-e.lastNotifiedAt<864e5}function renderRentalSunsetNotice(e){let n=e===1?`rental Actor`:`rental Actors`;return[t.bold(`Rental model sunset`),``,`You have ${e} ${n} published in Apify Store. Apify is sunsetting the rental pricing model.`,``,` ${t.bold(`April 1, 2026`)} Publishing new rental Actors and pricing changes on existing ones were disabled.`,` ${t.bold(`October 1, 2026`)} Rental Actors are fully retired. All remaining Actors move to pay-per-usage pricing.`,``,`Switch your ${n} to pay-per-event before October 1 to keep control over what you charge.`,`This guide walks through picking events, setting prices, and shipping the change:`,` ${t.cyan(`https://blog.apify.com/migrating-to-pay-per-event-pricing/`)}`,``,`Questions? Ask in the ${t.cyan(`#project-rentals`)} channel on Apify Discord:`,` ${t.cyan(`https://apify.com/discord`)}`,``,t.dim(`To silence this notice, set APIFY_CLI_SKIP_RENTAL_SUNSET_NOTICE=1.`),``].join(`
|
|
@@ -100,7 +99,7 @@ To inspect the input schema before creating a JSON input, use "apify actors info
|
|
|
100
99
|
`,t.gray(`${fr.format(r)} ago`)):n.push(`
|
|
101
100
|
`,t.gray(`On`,ur.display(e.lastRun.finishedAt)))}return n.join(` `)}return``})();u.pushRow({Name:`${e.title}\n${t.gray(`${e.username}/${e.name}`)}`,Runs:Q.isSmallTerminal()&&f>=56?Fr:t.cyan(`${e.stats?.totalRuns??0}`),"Last run started at":n,"Last run":n,"Last run status":e.lastRun?prettyPrintStatus(e.lastRun.status):``,"Modified at":lr.display(e.modifiedAt),Builds:e.actor?.stats?t.cyan(e.actor.stats.totalBuilds):t.gray(`Unknown`),"Last run duration":Q.isSmallTerminal()?Fr:t.cyan(r),"Default build":i,_Small_LastRunText:a})}simpleLog({message:u.render(1),stdout:!0})}sortByModifiedAt(e){return e.sort((e,t)=>{let n=new Date(e.modifiedAt);return new Date(t.modifiedAt).getTime()-n.getTime()})}sortByLastRun(e){return e.sort((e,t)=>{let n=new Date(e.stats?.lastRunStartedAt??`1970-01-01T00:00Z`);return new Date(t.stats?.lastRunStartedAt??`1970-01-01T00:00Z`).getTime()-n.getTime()})}};const extractGitHubZip=async(e,t)=>{let n=await downloadZip(e);n.extractEntryTo(n.getEntries()[0].entryName,t,!1)};var ActorsPullCommand=class extends ApifyCommand{static name=`pull`;static enableProfileFlag=!0;static description=`Download Actor code to current directory. Clones Git repositories or fetches Actor files based on the source type.`;static group=`Local Actor Development`;static examples=[{description:`Pull the Actor linked to the current directory from the Apify platform.`,command:`apify pull`},{description:`Pull a specific Actor by its full name into a target directory.`,command:`apify pull apify/hello-world --dir ./hello-world`},{description:`Pull a specific version of an Actor.`,command:`apify pull apify/hello-world --version 1.2`}];static docsUrl=`https://docs.apify.com/cli/docs/reference#apify-pull`;static flags={version:Y.string({char:`v`,description:`Actor version number which will be pulled, e.g. 1.2. Default: the highest version.`,required:!1}),dir:Y.string({description:`Directory where the Actor should be pulled to.`,required:!1})};static args={actorId:Z.string({required:!1,description:`Name or ID of the Actor to run (e.g. "apify/hello-world" or "E2jjCZBezvAZnX8Rb"). If not provided, the command will update the Actor in the current directory based on its name in ".actor/actor.json" file.`})};async run(){let t=e.cwd(),n=await useActorConfig({cwd:t});if(n.isErr()){error({message:n.unwrapErr().message}),e.exitCode=5;return}let{config:r}=n.unwrap(),i=await getLoggedClientOrThrow(),a=await getCurrentUserInfo(),s=!this.args.actorId,c=a.username||a.id,l=this.args?.actorId||r?.id||(r?.name?`${c}/${r.name}`:void 0);if(!l)throw Error(`Cannot find Actor in this directory.`);let u;try{u=await i.actor(l).get()}catch{throw Error(`Cannot find Actor with ID/name '${l}' in your account.`)}if(!u)throw Error(`Cannot find Actor with ID/name '${l}' in your account.`);let{name:f,versions:p}=u,throwMissingSourceCodeAccessError=()=>{throw Error(`You cannot pull source code of this Actor because you do not have permission to do so.`)};if(!u.versions.length)throw Error(`Actor ${l} has no versions.`);let m=null;if(this.flags.version&&(m=p.find(e=>e.versionNumber===this.flags.version),!m))throw Error(`Cannot find version ${this.flags.version} of Actor ${l}.`);m||=p.reduce((e,t)=>ze(`${t.versionNumber}.0`,`${e.versionNumber}.0`)?t:e);let g=s?t:z(t,this.flags.dir??f);if(h(g,{recursive:!0}),!s&&v(g).length!==0){error({message:`Directory ${g} is not empty. Please empty it or choose another directory.`});return}let{sourceType:_}=m;switch(_){case`TARBALL`:m.tarballUrl||throwMissingSourceCodeAccessError(),await extractGitHubZip(m.tarballUrl,g);break;case`SOURCE_FILES`:{m.sourceFiles||throwMissingSourceCodeAccessError();let{sourceFiles:e}=m;for(let t of e){let e=R(t.name);if(h(`${g}/${e}`,{recursive:!0}),!t.folder){let e=t.format===`BASE64`?Buffer.from(t.content,`base64`).toString():t.content;if(t.name===K){let n=kt.parse(e);n.name=u.name,w(`${g}/${t.name}`,kt.update(e,n))}else w(`${g}/${t.name}`,e)}}break}case`GIT_REPO`:{m.gitRepoUrl||throwMissingSourceCodeAccessError();let{gitRepoUrl:e}=m;try{await Ot(`git:${e}`,{dir:g})}catch(t){throw Error(`Failed to pull Actor from ${e}. ${t.message}`)}break}case`GITHUB_GIST`:m.gitHubGistUrl||throwMissingSourceCodeAccessError(),await extractGitHubZip(`${m.gitHubGistUrl}/archive/master.zip`,g);break;default:throw Error(`Unknown source type: ${_}`)}success({message:s?`Actor ${f} updated at ${g}/`:`Pulled to ${g}/`})}};function resolveDockerContext(e,t){if(t==null||t===``)return{kind:`none`};if(typeof t!=`string`)return{kind:`invalid`,message:`"dockerContextDir" in .actor/actor.json must be a string.`};let n;try{n=y(e)}catch{return{kind:`invalid`,message:`The Actor folder ${e} does not exist.`}}let r=V(n,G,t),i=S(r,{throwIfNoEntry:!1});if(!i)return{kind:`invalid`,message:`"dockerContextDir" in .actor/actor.json points to ${r}, which does not exist.`};if(!i.isDirectory())return{kind:`invalid`,message:`"dockerContextDir" in .actor/actor.json points to ${r}, which is not a directory.`};let a=y(r),s=B(n,r);if(s===``||!isOutside(s))return{kind:`within`,contextRoot:r};let c=git(n,[`rev-parse`,`--show-toplevel`]);if(c){let e=y(c);return isOutside(B(e,a))?{kind:`invalid`,message:`"dockerContextDir" in .actor/actor.json points to ${a}, outside the Git repository ${e}. The Apify platform builds from a clone of the repository, so it could not build this Actor.`}:{kind:`outside`,contextRoot:r,sourceRoot:e,actorPath:toPosix(B(e,n))}}let l=B(a,n);return isOutside(l)?{kind:`invalid`,message:`"dockerContextDir" in .actor/actor.json points to ${a}, which does not contain the Actor's folder.`}:{kind:`outside`,contextRoot:r,sourceRoot:a,actorPath:toPosix(l)}}function isOutside(e){return fe(e)||e===`..`||e.startsWith(`..${me}`)}function toPosix(e){return e.split(me).join(`/`)}function git(e,t){try{return qe(`git`,t,{cwd:e,encoding:`utf-8`,stdio:[`ignore`,`pipe`,`ignore`]}).trim()}catch{return}}function monorepoUnsupportedMessage(e){return[`Actor ${e} sets "dockerContextDir" in .actor/actor.json, so it builds from a Docker context outside its own folder (a monorepo Actor).`,`apify push does not support such Actors on the Apify platform yet. Follow the progress at:`,` https://github.com/apify/apify-core/issues/28685`,`Until then, set the Actor's source in Apify Console to the Git repository with the folder (<repository URL>#<branch>:<folder>).`].join(`
|
|
102
101
|
`)}const Vr=[`dockerfile`,`readme`,`changelog`],Hr=[{field:`input`,alias:`inputSchema`},{field:`output`,alias:`outputSchema`},{field:`webServerSchema`}],Ur=[`keyValueStore`,`dataset`],Wr=/* @__PURE__ */ new Set([`readme`,`changelog`]),Gr=[`./Dockerfile`,`../Dockerfile`];function isRecord(e){return typeof e==`object`&&!!e}function collectPathFields(e){let t=[],add=(e,n,r=!1)=>{typeof n==`string`&&n!==``&&t.push({field:e,value:n,caseInsensitive:r})};for(let t of Vr)add(t,e[t],!0);for(let{field:t,alias:n}of Hr)n&&e[n]?add(n,e[n]):add(t,e[t]);if(isRecord(e.storages)){for(let t of Ur)add(`storages.${t}`,e.storages[t]);if(isRecord(e.storages.datasets))for(let[t,n]of Object.entries(e.storages.datasets))add(`storages.datasets.${t}`,n)}return t}function listDirectory(e){try{return v(e)}catch{return[]}}function findEntry(e){let t=de(e),n=listDirectory(R(e));return{sameCase:n.find(e=>e===t),anyCase:n.find(e=>e.toLowerCase()===t.toLowerCase())}}function checkPathField({field:e,value:t,caseInsensitive:n},{actorDir:r,uploadRoot:i,uploadedFiles:a,dockerContextRoot:s}){let problem=(n,r,i=!1)=>({field:e,value:t,resolvedPath:n,reason:r,severity:i&&Wr.has(e)?`warning`:`error`}),absolute=()=>problem(t,`is absolute; the path is relative to ${G}/`),c=V(r,G,t),outside=()=>problem(c,`lies outside the uploaded folder ${i}`);if(t.startsWith(`/`))return absolute();let l=pe.join(toPosix(B(i,r)),G,t);if(l===`..`||l.startsWith(`../`))return outside();if(t.includes(`\\`))return problem(t,`uses a backslash; the platform resolves paths with forward slashes`,!0);if(fe(t))return absolute();if(isOutside(B(i,c)))return outside();let{sameCase:u,anyCase:f}=findEntry(c),p=u??(n?f:void 0);if(!p)return problem(c,f?`does not exist (the name must match ${f} exactly)`:`does not exist`,!0);let m=z(R(c),p),h;try{h=S(m)}catch{return problem(c,`does not exist`,!0)}if(h.isDirectory())return problem(c,`is a directory, not a file`);if(s&&e===`dockerfile`&&isOutside(B(s,m)))return problem(c,`lies outside the Docker context ${s}`);let g=toPosix(B(i,m));if(!a.has(g)){let e=[...a].find(e=>e.toLowerCase()===g.toLowerCase());return problem(c,e?`does not match the case of the uploaded file ${e}`:`is not among the files apify push uploads (excluded by .gitignore or .actorignore?)`,!0)}}function isUploaded(e,{uploadRoot:t,uploadedFiles:n}){let r=toPosix(B(t,e));return n.has(r)||[...n].some(e=>e.startsWith(`${r}/`))}function checkDefaultDockerfile(e){let{actorDir:t,dockerContextRoot:n}=e,r=Gr.find(n=>{let r=V(t,G,n),{sameCase:i,anyCase:a}=findEntry(r),s=i??a;return s!==void 0&&isUploaded(z(R(r),s),e)});if(r){let t=checkPathField({field:`dockerfile`,value:r,caseInsensitive:!0},e);return t&&{...t,value:``,reason:`is not set, so the platform would use the default ${t.resolvedPath}, which ${t.reason}`}}if(B(t,n)!==``)return{field:`dockerfile`,value:``,resolvedPath:z(n,`Dockerfile`),reason:`is not set and push uploads no ${G}/Dockerfile or Dockerfile in the Actor folder, so the platform would build the Docker context ${n} with its built-in Node.js Dockerfile; add a Dockerfile, or set "dockerfile" to one inside the Docker context`,severity:`error`}}function checkActorPaths(e){let t={actorDir:y(e.actorDir),uploadRoot:y(e.uploadRoot),uploadedFiles:new Set(e.uploadedFiles.map(toPosix)),dockerContextRoot:e.dockerContextRoot},n=collectPathFields(e.config),r=n.flatMap(e=>checkPathField(e,t)??[]),{dockerContextRoot:i}=t;if(i&&!n.some(({field:e})=>e===`dockerfile`)){let e=checkDefaultDockerfile({...t,dockerContextRoot:i});e&&r.push(e)}return r}const Kr={error:`.actor/actor.json refers to files the Apify platform could not use:`,warning:`.actor/actor.json refers to files the Apify platform will not find. The push continues, and the platform falls back to its default README or CHANGELOG:`};function formatActorPathProblems(e,t){let n=e.filter(e=>e.severity===t);return n.length===0?``:[Kr[t],...n.map(({field:e,value:t,resolvedPath:n,reason:r})=>t?` "${e}": "${t}" (${n}) ${r}.`:` "${e}" ${r}.`),`Paths are relative to ${G}/ and must point to files inside the uploaded folder.`].join(`
|
|
103
|
-
`)}const qr=new Set(cr.map(e=>e.replace(/\/\*\*$/,``)));function toPosixRelative(e,t){return B(e,t).split(me).join(`/`)}function readActorName(e){try{let t=getJsonFileContent(z(e,K))?.name;return typeof t==`string`?t:void 0}catch{return}}async function findActorDirs(e){let t=[],walk=async n=>{let r;try{r=await be(n,{withFileTypes:!0})}catch(e){cliDebugPrint(`findActorDirs`,{event:`unreadable-directory`,dir:n,err:e});return}for(let i of r){if(!i.isDirectory()||qr.has(i.name))continue;let r=z(n,i.name);p(z(r,K))?t.push({dir:toPosixRelative(e,r),name:readActorName(r)}):await walk(r)}};if(await walk(e),!t.length)return[];let n=await getGitignoreMatcher(e);return t.map(e=>({...e,ignored:n(`${e.dir}/`)})).sort((e,t)=>e.dir<t.dir?-1:+(e.dir>t.dir))}const Jr=stdinCheckWrapper(async({message:e,choices:t,pageSize:n,loop:r})=>await Pt({message:e,choices:t,pageSize:n,loop:r},kn),{errorMessageForStdin:`Please provide the selection using the command options.`}),Yr=5e4,getSecretsFile=()=>{try{return JSON.parse(_(SECRETS_FILE_PATH(),`utf-8`))||{}}catch{return{}}},writeSecretsFile=e=>(ensureApifyDirectory(SECRETS_FILE_PATH()),w(SECRETS_FILE_PATH(),JSON.stringify(e,null,` `)),e),addSecret=(e,t)=>{let n=getSecretsFile();if(n[e])throw Error(`Secret with name ${e} already exists. Call "apify secrets rm ${e}" to remove it.`);if(typeof e!=`string`||e.length>100)throw Error(`Secret name has to be string with maximum length 100.`);if(typeof t!=`string`||t.length>Yr)throw Error(`Secret value has to be string with maximum length ${Yr}.`);return n[e]=t,writeSecretsFile(n)},removeSecret=e=>{let t=getSecretsFile();if(!t[e])throw Error(`Secret with name ${e} doesn't exist.`);delete t[e],writeSecretsFile(t)},isSecretKey=e=>(/* @__PURE__ */ RegExp(`^@.{1}`)).test(e),replaceSecretsValue=(e,t,{allowMissing:n=!1}={})=>{t||=getSecretsFile();let r={},i=[];if(Object.keys(e).forEach(n=>{if(isSecretKey(e[n])){let a=e[n].replace(/* @__PURE__ */ RegExp(`^@`),``);t[a]?r[n]=t[a]:i.push(a)}else r[n]=e[n]}),i.length>0){let e=i.map(e=>` - ${e}`).join(`
|
|
102
|
+
`)}const qr=new Set(cr.map(e=>e.replace(/\/\*\*$/,``)));function toPosixRelative(e,t){return B(e,t).split(me).join(`/`)}function readActorName(e){try{let t=getJsonFileContent(z(e,K))?.name;return typeof t==`string`?t:void 0}catch{return}}async function findActorDirs(e){let t=[],walk=async n=>{let r;try{r=await be(n,{withFileTypes:!0})}catch(e){cliDebugPrint(`findActorDirs`,{event:`unreadable-directory`,dir:n,err:e});return}for(let i of r){if(!i.isDirectory()||qr.has(i.name))continue;let r=z(n,i.name);p(z(r,K))?t.push({dir:toPosixRelative(e,r),name:readActorName(r)}):await walk(r)}};if(await walk(e),!t.length)return[];let n=await getGitignoreMatcher(e,t.map(e=>`${e.dir}/`));return t.map(e=>({...e,ignored:n(`${e.dir}/`)})).sort((e,t)=>e.dir<t.dir?-1:+(e.dir>t.dir))}const Jr=stdinCheckWrapper(async({message:e,choices:t,pageSize:n,loop:r})=>await Pt({message:e,choices:t,pageSize:n,loop:r},kn),{errorMessageForStdin:`Please provide the selection using the command options.`}),Yr=5e4,getSecretsFile=()=>{try{return JSON.parse(_(SECRETS_FILE_PATH(),`utf-8`))||{}}catch{return{}}},writeSecretsFile=e=>(ensureApifyDirectory(SECRETS_FILE_PATH()),w(SECRETS_FILE_PATH(),JSON.stringify(e,null,` `)),e),addSecret=(e,t)=>{let n=getSecretsFile();if(n[e])throw Error(`Secret with name ${e} already exists. Call "apify secrets rm ${e}" to remove it.`);if(typeof e!=`string`||e.length>100)throw Error(`Secret name has to be string with maximum length 100.`);if(typeof t!=`string`||t.length>Yr)throw Error(`Secret value has to be string with maximum length ${Yr}.`);return n[e]=t,writeSecretsFile(n)},removeSecret=e=>{let t=getSecretsFile();if(!t[e])throw Error(`Secret with name ${e} doesn't exist.`);delete t[e],writeSecretsFile(t)},isSecretKey=e=>(/* @__PURE__ */ RegExp(`^@.{1}`)).test(e),replaceSecretsValue=(e,t,{allowMissing:n=!1}={})=>{t||=getSecretsFile();let r={},i=[];if(Object.keys(e).forEach(n=>{if(isSecretKey(e[n])){let a=e[n].replace(/* @__PURE__ */ RegExp(`^@`),``);t[a]?r[n]=t[a]:i.push(a)}else r[n]=e[n]}),i.length>0){let e=i.map(e=>` - ${e}`).join(`
|
|
104
103
|
`);if(n)for(let e of i)warning({message:`Value for ${e} not found in local secrets. Set it by calling "apify secrets add ${e} [SECRET_VALUE]"`});else throw Error(`The following secrets are missing:\n${e}\n\nSet them by calling "apify secrets add <SECRET_NAME> <SECRET_VALUE>" for each missing secret.
|
|
105
104
|
If you want to skip missing secrets, run the command with the --allow-missing-secrets flag.`)}return r},transformEnvToEnvVars=(e,t,{allowMissing:n=!1}={})=>{t||=getSecretsFile();let r=[],i=[];if(Object.keys(e).forEach(n=>{if(isSecretKey(e[n])){let a=e[n].replace(/* @__PURE__ */ RegExp(`^@`),``);t[a]?r.push({name:n,value:t[a],isSecret:!0}):i.push(a)}else r.push({name:n,value:e[n]})}),i.length>0){let e=i.map(e=>` - ${e}`).join(`
|
|
106
105
|
`);if(n)for(let e of i)warning({message:`Value for ${e} not found in local secrets. Set it by calling "apify secrets add ${e} [SECRET_VALUE]"`});else throw Error(`The following secrets are missing:\n${e}\n\nSet them by calling "apify secrets add <SECRET_NAME> <SECRET_VALUE>" for each missing secret.
|
|
@@ -250,4 +249,4 @@ When no path is provided, validates all schemas found in '${K}':
|
|
|
250
249
|
- Dataset schema (from "storages.dataset")
|
|
251
250
|
- Output schema (from "output")
|
|
252
251
|
- Key-Value Store schema (from "storages.keyValueStore")`;static group=`Local Actor Development`;static examples=[{description:`Validate the input schema discovered from the default locations.`,command:`apify validate-schema`},{description:`Validate a specific INPUT_SCHEMA.json file.`,command:`apify validate-schema ./my-schema.json`}];static docsUrl=`https://docs.apify.com/cli/docs/reference#apify-validate-schema`;static args={path:Z.string({required:!1,description:`Optional path to your INPUT_SCHEMA.json file. If not provided, validates all schemas in '${K}'.`})};static hiddenAliases=[`vis`];async run(){this.args.path?await this.validateInputSchemaAtPath(this.args.path):await this.validateAllSchemas()}async validateInputSchemaAtPath(t){await readAndValidateInputSchema({forcePath:t,cwd:e.cwd(),getMessage:e=>`Validating input schema at ${e??t}`}),success({message:`Input schema is valid.`})}async validateAllSchemas(){let t=e.cwd(),n=!1,r=!1;try{let{inputSchema:e,inputSchemaPath:r}=await readInputSchema({cwd:t,throwOnMissing:!0});if(e){n=!0,info({message:`Validating input schema ${r?`at ${r}`:`embedded in '${K}'`}`});let t=new ir({strict:!1});bt(t,e),success({message:`Input schema is valid.`})}}catch(e){n=!0,r=!0,error({message:e.message})}let i=[{label:`Dataset`,read:()=>readStorageSchema({cwd:t,key:`dataset`,label:`Dataset`,throwOnMissing:!0}),validate:validateDatasetSchema},{label:`Output`,read:()=>readStorageSchema({cwd:t,key:`output`,label:`Output`,getRef:e=>e?.output,throwOnMissing:!0}),validate:validateOutputSchema},{label:`Key-Value Store`,read:()=>readStorageSchema({cwd:t,key:`keyValueStore`,label:`Key-Value Store`,throwOnMissing:!0}),validate:validateKvsSchema}];for(let{label:e,read:t,validate:a}of i)try{let r=t();r&&(n=!0,info({message:`Validating ${e} schema ${r.schemaPath?`at ${r.schemaPath}`:`embedded in '${K}'`}`}),a(r.schema),success({message:`${e} schema is valid.`}))}catch(e){n=!0,r=!0,error({message:e.message})}if(!n)throw Error(`No schemas found. Make sure '${K}' exists and defines at least one schema.`);r&&(e.exitCode=5)}};const aa=[ActorIndexCommand,ActorsIndexCommand,AuthIndexCommand,BuildsIndexCommand,DatasetsIndexCommand,KeyValueStoresIndexCommand,MCPIndexCommand,RequestQueuesIndexCommand,RunsIndexCommand,SecretsIndexCommand,TasksIndexCommand,TelemetryIndexCommand,TemplatesIndexCommand,ApiCommand,TopLevelCallCommand,UpgradeCommand,InstallCommand,CreateCommand,EditInputSchemaCommand,InfoCommand,WrapScrapyCommand,InitCommand,LoginCommand,LogoutCommand,TopLevelPullCommand,ToplevelPushCommand,RunCommand,ValidateSchemaCommand,HelpCommand],oa=[ActorSetValueCommand,ActorPushDataCommand,ActorGetValueCommand,ActorGetPublicUrlCommand,ActorGetInputCommand,ActorChargeCommand,ActorCalculateMemoryCommand,ActorGenerateSchemaTypesCommand,HelpCommand,UpgradeCommand,InstallCommand,TelemetryIndexCommand];export{runCLI as i,aa as n,processVersionCheck as r,oa as t};
|
|
253
|
-
//# sourceMappingURL=_register-
|
|
252
|
+
//# sourceMappingURL=_register-CD7C2fNK.js.map
|