2019-07-28 07:10:29 -04:00
|
|
|
|
#!/usr/bin/env -S deno --allow-all
|
2020-01-02 15:13:47 -05:00
|
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-06-20 10:52:18 -04:00
|
|
|
|
const { env, stdin, args, exit, writeFile, chmod, run } = Deno;
|
|
|
|
|
import { parse } from "../flags/mod.ts";
|
2019-06-18 12:25:53 -04:00
|
|
|
|
import { exists } from "../fs/exists.ts";
|
2019-06-20 10:52:18 -04:00
|
|
|
|
import { ensureDir } from "../fs/ensure_dir.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
|
import * as path from "../path/mod.ts";
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
2019-06-19 00:22:01 -04:00
|
|
|
|
// Regular expression to test disk driver letter. eg "C:\\User\username\path\to"
|
|
|
|
|
const driverLetterReg = /^[c-z]:/i;
|
2019-09-09 13:58:36 -04:00
|
|
|
|
const isWindows = Deno.build.os === "win";
|
2019-06-20 10:52:18 -04:00
|
|
|
|
|
|
|
|
|
function showHelp(): void {
|
|
|
|
|
console.log(`deno installer
|
|
|
|
|
Install remote or local script as executables.
|
|
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
|
deno -A https://deno.land/std/installer/mod.ts [OPTIONS] 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.
|
2019-07-28 07:10:29 -04:00
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
OPTIONS:
|
|
|
|
|
-d, --dir <PATH> Installation directory path (defaults to ~/.deno/bin)
|
|
|
|
|
`);
|
|
|
|
|
}
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
|
|
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 "";
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
function getInstallerDir(): string {
|
|
|
|
|
// In Windows's Powershell $HOME environmental variable maybe null
|
2019-12-08 08:59:27 -05:00
|
|
|
|
// if so use $USERPROFILE instead.
|
|
|
|
|
const { HOME, USERPROFILE } = env();
|
2019-06-20 10:52:18 -04:00
|
|
|
|
|
2019-12-08 08:59:27 -05:00
|
|
|
|
const HOME_PATH = HOME || USERPROFILE;
|
2019-06-20 10:52:18 -04:00
|
|
|
|
|
|
|
|
|
if (!HOME_PATH) {
|
|
|
|
|
throw new Error("$HOME is not defined.");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 08:59:27 -05:00
|
|
|
|
return path.resolve(HOME_PATH, ".deno", "bin");
|
2019-06-20 10:52:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 11:43:06 -04:00
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
function checkIfExistsInPath(filePath: string): boolean {
|
|
|
|
|
// In Windows's Powershell $PATH not exist, so use $Path instead.
|
|
|
|
|
// $HOMEDRIVE is only used on Windows.
|
|
|
|
|
const { PATH, Path, HOMEDRIVE } = env();
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-10-05 12:02:34 -04:00
|
|
|
|
const envPath = (PATH as string) || (Path as string) || "";
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
const paths = envPath.split(isWindows ? ";" : ":");
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
let fileAbsolutePath = filePath;
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
for (const p of paths) {
|
|
|
|
|
const pathInEnv = path.normalize(p);
|
2019-06-19 00:22:01 -04:00
|
|
|
|
// On Windows paths from env contain drive letter.
|
|
|
|
|
// (eg. C:\Users\username\.deno\bin)
|
|
|
|
|
// But in the path of Deno, there is no drive letter.
|
|
|
|
|
// (eg \Users\username\.deno\bin)
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (isWindows) {
|
|
|
|
|
if (driverLetterReg.test(pathInEnv)) {
|
|
|
|
|
fileAbsolutePath = HOMEDRIVE + "\\" + fileAbsolutePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pathInEnv === fileAbsolutePath) {
|
|
|
|
|
return true;
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
2019-06-18 12:25:53 -04:00
|
|
|
|
fileAbsolutePath = filePath;
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
return false;
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
export function isRemoteUrl(url: string): boolean {
|
|
|
|
|
return /^https?:\/\//.test(url);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
function validateModuleName(moduleName: string): boolean {
|
|
|
|
|
if (/^[a-z][\w-]*$/i.test(moduleName)) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("Invalid module name: " + moduleName);
|
|
|
|
|
}
|
2019-06-18 12:25:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function generateExecutable(
|
|
|
|
|
filePath: string,
|
|
|
|
|
commands: string[]
|
|
|
|
|
): Promise<void> {
|
2019-06-19 10:16:34 -04:00
|
|
|
|
commands = commands.map((v): string => JSON.stringify(v));
|
2019-06-19 00:22:01 -04:00
|
|
|
|
// On Windows if user is using Powershell .cmd extension is need to run the
|
|
|
|
|
// installed module.
|
2019-06-18 12:25:53 -04:00
|
|
|
|
// Generate batch script to satisfy that.
|
2019-06-19 00:22:01 -04:00
|
|
|
|
const templateHeader =
|
|
|
|
|
"This executable is generated by Deno. Please don't modify it unless you " +
|
|
|
|
|
"know what it means.";
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (isWindows) {
|
2019-06-19 00:22:01 -04:00
|
|
|
|
const template = `% ${templateHeader} %
|
2019-06-18 12:25:53 -04:00
|
|
|
|
@IF EXIST "%~dp0\deno.exe" (
|
|
|
|
|
"%~dp0\deno.exe" ${commands.slice(1).join(" ")} %*
|
|
|
|
|
) ELSE (
|
|
|
|
|
@SETLOCAL
|
|
|
|
|
@SET PATHEXT=%PATHEXT:;.TS;=;%
|
|
|
|
|
${commands.join(" ")} %*
|
|
|
|
|
)
|
|
|
|
|
`;
|
|
|
|
|
const cmdFile = filePath + ".cmd";
|
|
|
|
|
await writeFile(cmdFile, encoder.encode(template));
|
|
|
|
|
await chmod(cmdFile, 0o755);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// generate Shell script
|
2019-06-20 10:52:18 -04:00
|
|
|
|
const template = `#!/bin/sh
|
2019-06-19 00:22:01 -04:00
|
|
|
|
# ${templateHeader}
|
2019-06-18 12:25:53 -04:00
|
|
|
|
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
|
|
|
|
|
|
|
|
|
|
case \`uname\` in
|
|
|
|
|
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
if [ -x "$basedir/deno" ]; then
|
|
|
|
|
"$basedir/deno" ${commands.slice(1).join(" ")} "$@"
|
|
|
|
|
ret=$?
|
|
|
|
|
else
|
|
|
|
|
${commands.join(" ")} "$@"
|
|
|
|
|
ret=$?
|
|
|
|
|
fi
|
|
|
|
|
exit $ret
|
|
|
|
|
`;
|
|
|
|
|
await writeFile(filePath, encoder.encode(template));
|
|
|
|
|
await chmod(filePath, 0o755);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function install(
|
|
|
|
|
moduleName: string,
|
|
|
|
|
moduleUrl: string,
|
2019-06-20 10:52:18 -04:00
|
|
|
|
flags: string[],
|
|
|
|
|
installationDir?: string
|
2019-06-14 11:43:06 -04:00
|
|
|
|
): Promise<void> {
|
2019-06-20 10:52:18 -04:00
|
|
|
|
if (!installationDir) {
|
|
|
|
|
installationDir = getInstallerDir();
|
|
|
|
|
}
|
|
|
|
|
await ensureDir(installationDir);
|
|
|
|
|
|
|
|
|
|
// if install local module
|
|
|
|
|
if (!isRemoteUrl(moduleUrl)) {
|
|
|
|
|
moduleUrl = path.resolve(moduleUrl);
|
|
|
|
|
}
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
validateModuleName(moduleName);
|
|
|
|
|
const filePath = path.join(installationDir, moduleName);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (await exists(filePath)) {
|
2019-06-19 00:22:01 -04:00
|
|
|
|
const msg =
|
2019-06-20 10:52:18 -04:00
|
|
|
|
"⚠️ " +
|
|
|
|
|
moduleName +
|
|
|
|
|
" is already installed" +
|
|
|
|
|
", do you want to overwrite it?";
|
2019-06-14 11:43:06 -04:00
|
|
|
|
if (!(await yesNoPrompt(msg))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ensure script that is being installed exists
|
2019-06-18 12:25:53 -04:00
|
|
|
|
const ps = run({
|
2019-08-13 20:03:29 -04:00
|
|
|
|
args: [Deno.execPath(), "fetch", "--reload", moduleUrl],
|
2019-06-18 12:25:53 -04:00
|
|
|
|
stdout: "inherit",
|
|
|
|
|
stderr: "inherit"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { code } = await ps.status();
|
|
|
|
|
|
|
|
|
|
if (code !== 0) {
|
|
|
|
|
throw new Error("Failed to fetch module.");
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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",
|
2019-06-18 12:25:53 -04:00
|
|
|
|
"run",
|
2019-06-14 11:43:06 -04:00
|
|
|
|
...grantedPermissions.map(getFlagFromPermission),
|
|
|
|
|
moduleUrl,
|
2019-06-18 12:25:53 -04:00
|
|
|
|
...scriptArgs
|
2019-06-14 11:43:06 -04:00
|
|
|
|
];
|
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
await generateExecutable(filePath, commands);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-15 09:51:11 -04:00
|
|
|
|
console.log(`✅ Successfully installed ${moduleName}`);
|
|
|
|
|
console.log(filePath);
|
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
if (!checkIfExistsInPath(installationDir)) {
|
|
|
|
|
console.log(`\nℹ️ Add ${installationDir} to PATH`);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
console.log(
|
2019-06-20 10:52:18 -04:00
|
|
|
|
" echo 'export PATH=\"" +
|
|
|
|
|
installationDir +
|
|
|
|
|
":$PATH\"' >> ~/.bashrc # change" +
|
2019-06-19 00:22:01 -04:00
|
|
|
|
" this to your shell"
|
2019-06-14 11:43:06 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function main(): Promise<void> {
|
2020-01-09 13:37:01 -05:00
|
|
|
|
const parsedArgs = parse(args, { stopEarly: true });
|
2019-06-20 10:52:18 -04:00
|
|
|
|
|
|
|
|
|
if (parsedArgs.h || parsedArgs.help) {
|
2019-06-14 11:43:06 -04:00
|
|
|
|
return showHelp();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
if (parsedArgs._.length < 2) {
|
2019-06-14 11:43:06 -04:00
|
|
|
|
return showHelp();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 10:52:18 -04:00
|
|
|
|
const moduleName = parsedArgs._[0];
|
|
|
|
|
const moduleUrl = parsedArgs._[1];
|
|
|
|
|
const flags = parsedArgs._.slice(2);
|
|
|
|
|
const installationDir = parsedArgs.d || parsedArgs.dir;
|
|
|
|
|
|
2019-06-14 11:43:06 -04:00
|
|
|
|
try {
|
2019-06-20 10:52:18 -04:00
|
|
|
|
await install(moduleName, moduleUrl, flags, installationDir);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
|
main();
|
|
|
|
|
}
|