-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: installer #489
Merged
Merged
feat: installer #489
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
c689f8e
Add installer
syumai 38aea3d
Update README of deno_install
syumai 8f78c9d
replace wget with fetch
bartlomieju c865227
more prompts, handle situation without shebang, prompt on overwrite
bartlomieju b64f0a6
better prompt
bartlomieju a8d0f63
even better prompt
bartlomieju 8435486
lint & fmt
bartlomieju fd4d530
remove shebang parsing
bartlomieju 4ff7fec
add help prompt
bartlomieju ffd3af8
fix arg parsing
bartlomieju 0331109
add uninstall command
bartlomieju aabd191
don't show PATH prompt if dir in path
bartlomieju 86a6e4e
install local scripts
bartlomieju 86bd510
lint
bartlomieju 1dda37e
add simple test case
bartlomieju 0576c4c
lint
bartlomieju 75ab128
reset CI
bartlomieju cbd05ea
add env permission
bartlomieju 29d891e
add debug statement
bartlomieju 25b9ae5
remove debug statement
bartlomieju 84143ca
Add missing await
bartlomieju b7a703b
properly parse script flags
bartlomieju 77c37db
add more tests for installer
bartlomieju 81030d6
fix windows test
bartlomieju b77746c
update README
bartlomieju 6137f6e
explicitly require name for installed executable
bartlomieju 637a6f3
s/deno_install/deno_installer/
bartlomieju 09ac618
remove installer/deno_installer.ts
bartlomieju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# deno_installer | ||
|
||
Install remote or local script as executables. | ||
|
||
```` | ||
## Installation | ||
|
||
`installer` can be install using iteself: | ||
|
||
```sh | ||
deno -A https://deno.land/std/installer/mod.ts deno_installer https://deno.land/std/installer/mod.ts -A | ||
```` | ||
|
||
Installer uses `~/.deno/bin` to store installed scripts so make sure it's in `$PATH` | ||
|
||
``` | ||
echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc # change this to your shell | ||
``` | ||
|
||
## Usage | ||
|
||
Install script | ||
|
||
```sh | ||
$ deno_installer file_server https://deno.land/std/http/file_server.ts --allow-net --allow-read | ||
> Downloading: https://deno.land/std/http/file_server.ts | ||
> | ||
> ✅ Successfully installed file_server. | ||
|
||
# local script | ||
$ deno_installer file_server ./deno_std/http/file_server.ts --allow-net --allow-read | ||
> Looking for: /dev/deno_std/http/file_server.ts | ||
> | ||
> ✅ Successfully installed file_server. | ||
``` | ||
|
||
Use installed script: | ||
|
||
```sh | ||
$ file_server | ||
HTTP server listening on http://0.0.0.0:4500/ | ||
``` | ||
|
||
Update installed script | ||
|
||
```sh | ||
$ deno_installer file_server https://deno.land/std/http/file_server.ts --allow-net --allow-read | ||
> ⚠️ file_server is already installed, do you want to overwrite it? [yN] | ||
> y | ||
> | ||
> Downloading: https://deno.land/std/http/file_server.ts | ||
> | ||
> ✅ Successfully installed file_server. | ||
``` | ||
|
||
Show help | ||
|
||
```sh | ||
$ deno_installer --help | ||
> deno installer | ||
Install remote or local script as executables. | ||
|
||
USAGE: | ||
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...] | ||
|
||
ARGS: | ||
EXE_NAME Name for executable | ||
SCRIPT_URL Local or remote URL of script to install | ||
[FLAGS...] List of flags for script, both Deno permission and script specific flag can be used. | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
#!/usr/bin/env deno --allow-all | ||
|
||
const { | ||
args, | ||
env, | ||
readDirSync, | ||
mkdirSync, | ||
writeFile, | ||
exit, | ||
stdin, | ||
stat, | ||
readAll, | ||
run, | ||
remove | ||
} = Deno; | ||
import * as path from "../fs/path.ts"; | ||
|
||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder("utf-8"); | ||
|
||
enum Permission { | ||
Read, | ||
Write, | ||
Net, | ||
Env, | ||
Run, | ||
All | ||
} | ||
|
||
function getPermissionFromFlag(flag: string): Permission | undefined { | ||
switch (flag) { | ||
case "--allow-read": | ||
return Permission.Read; | ||
case "--allow-write": | ||
return Permission.Write; | ||
case "--allow-net": | ||
return Permission.Net; | ||
case "--allow-env": | ||
return Permission.Env; | ||
case "--allow-run": | ||
return Permission.Run; | ||
case "--allow-all": | ||
return Permission.All; | ||
case "-A": | ||
return Permission.All; | ||
} | ||
} | ||
|
||
function getFlagFromPermission(perm: Permission): string { | ||
switch (perm) { | ||
case Permission.Read: | ||
return "--allow-read"; | ||
case Permission.Write: | ||
return "--allow-write"; | ||
case Permission.Net: | ||
return "--allow-net"; | ||
case Permission.Env: | ||
return "--allow-env"; | ||
case Permission.Run: | ||
return "--allow-run"; | ||
case Permission.All: | ||
return "--allow-all"; | ||
} | ||
return ""; | ||
} | ||
|
||
async function readCharacter(): Promise<string> { | ||
const byteArray = new Uint8Array(1024); | ||
await stdin.read(byteArray); | ||
const line = decoder.decode(byteArray); | ||
return line[0]; | ||
} | ||
|
||
async function yesNoPrompt(message: string): Promise<boolean> { | ||
console.log(`${message} [yN]`); | ||
const input = await readCharacter(); | ||
console.log(); | ||
return input === "y" || input === "Y"; | ||
} | ||
|
||
function createDirIfNotExists(path: string): void { | ||
try { | ||
readDirSync(path); | ||
} catch (e) { | ||
mkdirSync(path, true); | ||
} | ||
} | ||
|
||
function checkIfExistsInPath(path: string): boolean { | ||
const { PATH } = env(); | ||
|
||
const paths = (PATH as string).split(":"); | ||
|
||
return paths.includes(path); | ||
} | ||
|
||
function getInstallerDir(): string { | ||
const { HOME } = env(); | ||
|
||
if (!HOME) { | ||
throw new Error("$HOME is not defined."); | ||
} | ||
|
||
return path.join(HOME, ".deno", "bin"); | ||
} | ||
|
||
// TODO: fetch doesn't handle redirects yet - once it does this function | ||
// can be removed | ||
async function fetchWithRedirects( | ||
url: string, | ||
redirectLimit: number = 10 | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): Promise<any> { | ||
// TODO: `Response` is not exposed in global so 'any' | ||
const response = await fetch(url); | ||
|
||
if (response.status === 301 || response.status === 302) { | ||
if (redirectLimit > 0) { | ||
const redirectUrl = response.headers.get("location")!; | ||
return await fetchWithRedirects(redirectUrl, redirectLimit - 1); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async function fetchModule(url: string): Promise<any> { | ||
const response = await fetchWithRedirects(url); | ||
|
||
if (response.status !== 200) { | ||
// TODO: show more debug information like status and maybe body | ||
throw new Error(`Failed to get remote script ${url}.`); | ||
} | ||
|
||
const body = await readAll(response.body); | ||
return decoder.decode(body); | ||
} | ||
|
||
function showHelp(): void { | ||
console.log(`deno installer | ||
Install remote or local script as executables. | ||
|
||
USAGE: | ||
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...] | ||
|
||
ARGS: | ||
EXE_NAME Name for executable | ||
SCRIPT_URL Local or remote URL of script to install | ||
[FLAGS...] List of flags for script, both Deno permission and script specific flag can be used. | ||
`); | ||
} | ||
|
||
export async function install( | ||
moduleName: string, | ||
moduleUrl: string, | ||
flags: string[] | ||
): Promise<void> { | ||
const installerDir = getInstallerDir(); | ||
createDirIfNotExists(installerDir); | ||
|
||
const FILE_PATH = path.join(installerDir, moduleName); | ||
|
||
let fileInfo; | ||
try { | ||
fileInfo = await stat(FILE_PATH); | ||
} catch (e) { | ||
// pass | ||
} | ||
|
||
if (fileInfo) { | ||
const msg = `⚠️ ${moduleName} is already installed, do you want to overwrite it?`; | ||
if (!(await yesNoPrompt(msg))) { | ||
return; | ||
} | ||
} | ||
|
||
// ensure script that is being installed exists | ||
if (moduleUrl.startsWith("http")) { | ||
// remote module | ||
console.log(`Downloading: ${moduleUrl}\n`); | ||
await fetchModule(moduleUrl); | ||
} else { | ||
// assume that it's local file | ||
moduleUrl = path.resolve(moduleUrl); | ||
console.log(`Looking for: ${moduleUrl}\n`); | ||
await stat(moduleUrl); | ||
} | ||
|
||
const grantedPermissions: Permission[] = []; | ||
const scriptArgs: string[] = []; | ||
|
||
for (const flag of flags) { | ||
const permission = getPermissionFromFlag(flag); | ||
if (permission === undefined) { | ||
scriptArgs.push(flag); | ||
} else { | ||
grantedPermissions.push(permission); | ||
} | ||
} | ||
|
||
const commands = [ | ||
"deno", | ||
...grantedPermissions.map(getFlagFromPermission), | ||
moduleUrl, | ||
...scriptArgs, | ||
"$@" | ||
]; | ||
|
||
// TODO: add windows Version | ||
const template = `#/bin/sh\n${commands.join(" ")}`; | ||
await writeFile(FILE_PATH, encoder.encode(template)); | ||
|
||
const makeExecutable = run({ args: ["chmod", "+x", FILE_PATH] }); | ||
const { code } = await makeExecutable.status(); | ||
makeExecutable.close(); | ||
|
||
if (code !== 0) { | ||
throw new Error("Failed to make file executable"); | ||
} | ||
|
||
console.log(`✅ Successfully installed ${moduleName}.`); | ||
// TODO: add Windows version | ||
if (!checkIfExistsInPath(installerDir)) { | ||
console.log("\nℹ️ Add ~/.deno/bin to PATH"); | ||
console.log( | ||
" echo 'export PATH=\"$HOME/.deno/bin:$PATH\"' >> ~/.bashrc # change this to your shell" | ||
); | ||
} | ||
} | ||
|
||
export async function uninstall(moduleName: string): Promise<void> { | ||
const installerDir = getInstallerDir(); | ||
const FILE_PATH = path.join(installerDir, moduleName); | ||
|
||
try { | ||
await stat(FILE_PATH); | ||
} catch (e) { | ||
if (e instanceof Deno.DenoError && e.kind === Deno.ErrorKind.NotFound) { | ||
throw new Error(`ℹ️ ${moduleName} not found`); | ||
} | ||
} | ||
|
||
await remove(FILE_PATH); | ||
console.log(`ℹ️ Uninstalled ${moduleName}`); | ||
} | ||
|
||
async function main(): Promise<void> { | ||
if (args.length < 3) { | ||
return showHelp(); | ||
} | ||
|
||
if (["-h", "--help"].includes(args[1])) { | ||
return showHelp(); | ||
} | ||
|
||
const moduleName = args[1]; | ||
const moduleUrl = args[2]; | ||
const flags = args.slice(3); | ||
try { | ||
await install(moduleName, moduleUrl, flags); | ||
} catch (e) { | ||
console.log(e); | ||
exit(1); | ||
} | ||
} | ||
|
||
if (import.meta.main) { | ||
main(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems when I run it with no arguments, there's no error message or help text:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yikes, can we just dropinstaller/deno_installer.ts
and leaveinstaller/mod.ts
?Removed
installer/deno_installer.ts
it's not needed anymore - previously it was discovering module name from path, but now it's explicitly passed as an arg.Please try
deno -A installer/mod.ts