Skip to content
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 28 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c689f8e
Add installer
syumai Jun 9, 2019
38aea3d
Update README of deno_install
syumai Jun 9, 2019
8f78c9d
replace wget with fetch
bartlomieju Jun 13, 2019
c865227
more prompts, handle situation without shebang, prompt on overwrite
bartlomieju Jun 13, 2019
b64f0a6
better prompt
bartlomieju Jun 13, 2019
a8d0f63
even better prompt
bartlomieju Jun 13, 2019
8435486
lint & fmt
bartlomieju Jun 13, 2019
fd4d530
remove shebang parsing
bartlomieju Jun 13, 2019
4ff7fec
add help prompt
bartlomieju Jun 13, 2019
ffd3af8
fix arg parsing
bartlomieju Jun 13, 2019
0331109
add uninstall command
bartlomieju Jun 13, 2019
aabd191
don't show PATH prompt if dir in path
bartlomieju Jun 13, 2019
86a6e4e
install local scripts
bartlomieju Jun 14, 2019
86bd510
lint
bartlomieju Jun 14, 2019
1dda37e
add simple test case
bartlomieju Jun 14, 2019
0576c4c
lint
bartlomieju Jun 14, 2019
75ab128
reset CI
bartlomieju Jun 14, 2019
cbd05ea
add env permission
bartlomieju Jun 14, 2019
29d891e
add debug statement
bartlomieju Jun 14, 2019
25b9ae5
remove debug statement
bartlomieju Jun 14, 2019
84143ca
Add missing await
bartlomieju Jun 14, 2019
b7a703b
properly parse script flags
bartlomieju Jun 14, 2019
77c37db
add more tests for installer
bartlomieju Jun 14, 2019
81030d6
fix windows test
bartlomieju Jun 14, 2019
b77746c
update README
bartlomieju Jun 14, 2019
6137f6e
explicitly require name for installed executable
bartlomieju Jun 14, 2019
637a6f3
s/deno_install/deno_installer/
bartlomieju Jun 14, 2019
09ac618
remove installer/deno_installer.ts
bartlomieju Jun 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/template.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ parameters:

steps:
- bash: deno${{ parameters.exe_suffix }} run --allow-run --allow-write --allow-read format.ts --check
- bash: deno${{ parameters.exe_suffix }} run --allow-run --allow-net --allow-write --allow-read --config=tsconfig.test.json test.ts
- bash: deno${{ parameters.exe_suffix }} run --allow-run --allow-net --allow-write --allow-read --allow-env --config=tsconfig.test.json test.ts
70 changes: 70 additions & 0 deletions installer/README.md
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:https://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.
```
Copy link
Member

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:

~/src/deno_std> deno -A installer/deno_installer.ts
[1/1] Compiling file:https:///Users/rld/src/deno_std/installer/deno_installer.ts
~/src/deno_std>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, can we just drop installer/deno_installer.ts and leave installer/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

270 changes: 270 additions & 0 deletions installer/mod.ts
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();
}