Skip to content

Commit 4252e6c

Browse files
vladfranguB4nan
andauthored
feat: upgrade command upgrading CLI + install command (#856)
Co-authored-by: Martin Adámek <[email protected]>
1 parent bbd0200 commit 4252e6c

39 files changed

+1025
-394
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,4 @@ afterAll(async () => {
134134

135135
### Running commands
136136

137-
Running commands in tests can be done by calling `runCommand` (imported from the command framework)
137+
Running commands in tests can be done by calling `testRunCommand` (imported from the command framework)

scripts/build-cli-bundles.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ for (const entryPoint of entryPoints) {
9393

9494
if (systemType.toLowerCase().includes('arm')) {
9595
arch = 'arm64';
96+
97+
// On arm, process.arch will still return x64, which will break the upgrade command.
98+
// So we override the arch to arm64
99+
100+
const newNewContent = newContent.replace('process.env.APIFY_BUNDLE_ARCH', '"arm64"');
101+
102+
await writeFile(metadataFile, newNewContent);
96103
}
97104
}
98105

@@ -103,6 +110,9 @@ for (const entryPoint of entryPoints) {
103110
console.log(`Building ${cliName} for ${target} (result: ${fileName})...`);
104111
// TODO: --sourcemap crashes for w/e reason and --bytecode doesn't support ESM (TLA to be exact)
105112
await $`bun build --compile --minify --target=${target} --outfile=${outFile} ${entryPoint}`;
113+
114+
// Remove the arch override
115+
await writeFile(metadataFile, newContent);
106116
}
107117
}
108118

scripts/install/install.ps1

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ if ($WinVer.Major -lt 10 -or ($WinVer.Major -eq 10 -and $WinVer.Build -lt $MinBu
3535

3636
$ErrorActionPreference = "Stop"
3737

38+
$UpgradeScriptURL = "https://raw.githubusercontent.com/apify/apify-cli/main/scripts/install/upgrade.ps1"
39+
3840
# These three environment functions are roughly copied from https://github.com/prefix-dev/pixi/pull/692
3941
# They are used instead of `SetEnvironmentVariable` because of unwanted variable expansions.
4042
function Publish-Env {
@@ -179,7 +181,7 @@ function Install-Apify {
179181

180182
try {
181183
# Use Invoke-RestMethod instead of Invoke-WebRequest because Invoke-WebRequest breaks on
182-
# some machines, see
184+
# some machines
183185
Invoke-RestMethod -Uri $DownloadURL -OutFile $DownloadPath
184186
}
185187
catch {
@@ -216,11 +218,30 @@ function Install-Apify {
216218
}
217219
}
218220

221+
$UpgradeScriptPath = "${ApifyBin}\upgrade.ps1"
222+
curl.exe "-#SfLo" "$UpgradeScriptPath" "$UpgradeScriptURL"
223+
224+
if ($LASTEXITCODE -ne 0) {
225+
Write-Warning "The command 'curl.exe $UpgradeScriptURL -o $UpgradeScriptPath' exited with code ${LASTEXITCODE}`nTrying an alternative download method..."
226+
227+
try {
228+
# Use Invoke-RestMethod instead of Invoke-WebRequest because Invoke-WebRequest breaks on
229+
# some machines
230+
Invoke-RestMethod -Uri $UpgradeScriptURL -OutFile $UpgradeScriptPath
231+
}
232+
catch {
233+
Write-Output "Install Failed - could not download $UpgradeScriptURL"
234+
Write-Output "The command 'Invoke-RestMethod $UpgradeScriptURL -OutFile $UpgradeScriptPath' exited with code ${LASTEXITCODE}`n"
235+
return 1
236+
}
237+
}
238+
219239
$C_RESET = [char]27 + "[0m"
220240
$C_GREEN = [char]27 + "[1;32m"
241+
$C_DIM = [char]27 + "[0;2m"
221242

222-
Write-Output "${C_GREEN}Apify CLI ${ApifyVersion} was installed successfully!${C_RESET}"
223-
Write-Output "The binary is located at ${ApifyBin}\apify.exe`n"
243+
Write-Output "${C_GREEN}Apify and Actor CLI ${ApifyVersion} were installed successfully!${C_RESET}"
244+
Write-Output "${C_DIM}The binaries are located at ${ApifyBin}\apify.exe and ${ApifyBin}\actor.exe${C_RESET}`n"
224245

225246
$hasExistingOther = $false;
226247
try {
@@ -241,6 +262,8 @@ function Install-Apify {
241262
$env:PATH = $Path;
242263
}
243264

265+
# Unlike on Unix systems where we just have to symlink a file to make it just work without a reload,
266+
# on Windows you _need_ to restart the running process for it to get the new path variable.
244267
Write-Output "To get started, restart your terminal/editor, then type `"apify`"`n"
245268
}
246269

scripts/install/install.sh

Lines changed: 7 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ construct_download_url() {
164164
}
165165

166166
install_env=APIFY_CLI_INSTALL
167-
bin_env=\$$install_env/bin
168-
169167
install_dir=${!install_env:-$HOME/.apify}
170168
bin_dir=$install_dir/bin
171169

@@ -178,7 +176,9 @@ for executable_name in "${executable_names[@]}"; do
178176
download_url=$(construct_download_url "$executable_name" "$target")
179177
output_filename="${executable_name}"
180178

181-
curl --fail --location --progress-bar --output "$bin_dir/$output_filename" "$download_url" 2>/dev/null ||
179+
info "Downloading $executable_name bundle for version $version and target $target"
180+
181+
curl --fail --location --progress-bar --output "$bin_dir/$output_filename" "$download_url" ||
182182
error "Failed to download $executable_name bundle for version $version and target $target (might not exist for this platform/arch combination)"
183183

184184
chmod +x "$bin_dir/$output_filename" ||
@@ -200,161 +200,8 @@ tildify() {
200200
fi
201201
}
202202

203-
success "Apify CLI was installed successfully to $Bold_Green$(tildify "$bin_dir/apify")"
204-
success "Actor CLI was installed successfully to $Bold_Green$(tildify "$bin_dir/actor")"
205-
206-
if command -v apify >/dev/null; then
207-
success "Run 'apify --help' to get started"
208-
exit
209-
fi
210-
211-
refresh_command=''
212-
213-
tilde_bin_dir=$(tildify "$bin_dir")
214-
quoted_install_dir=\"${install_dir//\"/\\\"}\"
215-
216-
if [[ $quoted_install_dir = \"$HOME/* ]]; then
217-
quoted_install_dir=${quoted_install_dir/$HOME\//\$HOME/}
218-
fi
219-
220-
if [[ -d $HOME/.local/bin ]]; then
221-
# First, remove the symlinks if they exist
222-
rm -f $HOME/.local/bin/apify
223-
rm -f $HOME/.local/bin/actor
224-
rm -f $HOME/.local/bin/apify-cli
225-
226-
# Symlink the three executables to /usr/local/bin
227-
ln -s "$bin_dir/apify" $HOME/.local/bin/apify
228-
ln -s "$bin_dir/actor" $HOME/.local/bin/actor
229-
ln -s "$bin_dir/apify-cli" $HOME/.local/bin/apify-cli
230-
231-
info "Symlinked apify, actor, and apify-cli to $HOME/.local/bin"
232-
fi
233-
234-
echo
235-
236-
case $(basename "$SHELL") in
237-
fish)
238-
commands=(
239-
"set --export $install_env $quoted_install_dir"
240-
"set --export PATH $bin_env \$PATH"
241-
)
242-
243-
fish_config=$HOME/.config/fish/config.fish
244-
tilde_fish_config=$(tildify "$fish_config")
245-
246-
if [[ -w $fish_config ]]; then
247-
{
248-
echo -e '\n# apify cli'
249-
250-
for command in "${commands[@]}"; do
251-
echo "$command"
252-
done
253-
} >>"$fish_config"
254-
255-
info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_fish_config\""
256-
257-
refresh_command="source $tilde_fish_config"
258-
else
259-
echo "Manually add the directory to $tilde_fish_config (or similar):"
260-
261-
for command in "${commands[@]}"; do
262-
info_bold " $command"
263-
done
264-
fi
265-
;;
266-
zsh)
267-
commands=(
268-
"export $install_env=$quoted_install_dir"
269-
"export PATH=\"$bin_env:\$PATH\""
270-
)
271-
272-
zsh_config=$HOME/.zshrc
273-
tilde_zsh_config=$(tildify "$zsh_config")
274-
275-
if [[ -w $zsh_config ]]; then
276-
{
277-
echo -e '\n# apify cli'
278-
279-
for command in "${commands[@]}"; do
280-
echo "$command"
281-
done
282-
} >>"$zsh_config"
283-
284-
info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_zsh_config\""
285-
286-
refresh_command="exec $SHELL"
287-
else
288-
echo "Manually add the directory to $tilde_zsh_config (or similar):"
289-
290-
for command in "${commands[@]}"; do
291-
info_bold " $command"
292-
done
293-
fi
294-
;;
295-
bash)
296-
commands=(
297-
"export $install_env=$quoted_install_dir"
298-
"export PATH=\"$bin_env:\$PATH\""
299-
)
300-
301-
bash_configs=(
302-
"$HOME/.bashrc"
303-
"$HOME/.bash_profile"
304-
)
305-
306-
if [[ ${XDG_CONFIG_HOME:-} ]]; then
307-
bash_configs+=(
308-
"$XDG_CONFIG_HOME/.bash_profile"
309-
"$XDG_CONFIG_HOME/.bashrc"
310-
"$XDG_CONFIG_HOME/bash_profile"
311-
"$XDG_CONFIG_HOME/bashrc"
312-
)
313-
fi
314-
315-
set_manually=true
316-
for bash_config in "${bash_configs[@]}"; do
317-
tilde_bash_config=$(tildify "$bash_config")
318-
319-
if [[ -w $bash_config ]]; then
320-
{
321-
echo -e '\n# apify cli'
322-
323-
for command in "${commands[@]}"; do
324-
echo "$command"
325-
done
326-
} >>"$bash_config"
327-
328-
info "Added \"$tilde_bin_dir\" to \$PATH in \"$tilde_bash_config\""
329-
330-
refresh_command="source $bash_config"
331-
set_manually=false
332-
break
333-
fi
334-
done
335-
336-
if [[ $set_manually = true ]]; then
337-
echo "Manually add the directory to $tilde_bash_config (or similar):"
338-
339-
for command in "${commands[@]}"; do
340-
info_bold " $command"
341-
done
342-
fi
343-
;;
344-
*)
345-
echo 'Manually add the directory to ~/.bashrc (or similar):'
346-
info_bold " export $install_env=$quoted_install_dir"
347-
info_bold " export PATH=\"$bin_env:\$PATH\""
348-
;;
349-
esac
350-
351-
echo
352-
info "To get started, run:"
353-
echo
354-
355-
if [[ $refresh_command ]]; then
356-
info_bold " $refresh_command $(info "(if the shell is not automatically refreshed)")"
357-
fi
203+
success "Apify and Actor CLI $version were installed successfully!"
204+
info "The binaries are located at $Bold_Green$(tildify "$bin_dir/apify") ${Dim}and $Bold_Green$(tildify "$bin_dir/actor")"
358205

359-
info_bold " apify --help"
360-
echo
206+
# Invoke the CLI to handle shell integrations nicely
207+
PROVIDED_INSTALL_DIR="$install_dir" FINAL_BIN_DIR="$bin_dir" "$bin_dir/apify" install

0 commit comments

Comments
 (0)