2019-06-14 11:43:06 -04:00
|
|
|
|
#!/usr/bin/env deno --allow-all
|
2019-06-18 12:25:53 -04:00
|
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-06-14 11:43:06 -04:00
|
|
|
|
const {
|
|
|
|
|
args,
|
|
|
|
|
env,
|
|
|
|
|
readDirSync,
|
|
|
|
|
mkdirSync,
|
|
|
|
|
writeFile,
|
|
|
|
|
exit,
|
|
|
|
|
stdin,
|
2019-06-18 12:25:53 -04:00
|
|
|
|
chmod,
|
|
|
|
|
remove,
|
|
|
|
|
run
|
2019-06-14 11:43:06 -04:00
|
|
|
|
} = Deno;
|
|
|
|
|
import * as path from "../fs/path.ts";
|
2019-06-18 12:25:53 -04:00
|
|
|
|
import { exists } from "../fs/exists.ts";
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
2019-06-18 12:25:53 -04:00
|
|
|
|
const isWindows = Deno.platform.os === "win";
|
|
|
|
|
const driverLetterReg = /^[c-z]:/i; // Regular expression to test disk driver letter. eg "C:\\User\username\path\to"
|
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 "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-06-18 12:25:53 -04:00
|
|
|
|
let 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);
|
|
|
|
|
// 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)
|
|
|
|
|
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-18 12:25:53 -04:00
|
|
|
|
function getInstallerDir(): string {
|
|
|
|
|
// In Windows's Powershell $HOME environmental variable maybe null, if so use $HOMEPATH instead.
|
|
|
|
|
let { HOME, HOMEPATH } = env();
|
|
|
|
|
|
|
|
|
|
const HOME_PATH = HOME || HOMEPATH;
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (!HOME_PATH) {
|
|
|
|
|
throw new Error("$HOME is not defined.");
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
return path.join(HOME_PATH, ".deno", "bin");
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showHelp(): void {
|
|
|
|
|
console.log(`deno installer
|
|
|
|
|
Install remote or local script as executables.
|
2019-06-18 12:25:53 -04:00
|
|
|
|
|
2019-06-14 11:43:06 -04:00
|
|
|
|
USAGE:
|
2019-06-18 12:25:53 -04:00
|
|
|
|
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...]
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
|
|
ARGS:
|
2019-06-18 12:25:53 -04:00
|
|
|
|
EXE_NAME Name for executable
|
2019-06-14 11:43:06 -04:00
|
|
|
|
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-06-18 12:25:53 -04:00
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function generateExecutable(
|
|
|
|
|
filePath: string,
|
|
|
|
|
commands: string[]
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
// On Windows if user is using Powershell .cmd extension is need to run the installed module.
|
|
|
|
|
// Generate batch script to satisfy that.
|
|
|
|
|
if (isWindows) {
|
|
|
|
|
const template = `% This executable is generated by Deno. Please don't modify it unless you know what it means. %
|
|
|
|
|
@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
|
|
|
|
|
const template = `#/bin/sh
|
|
|
|
|
# This executable is generated by Deno. Please don't modify it unless you know what it means.
|
|
|
|
|
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,
|
|
|
|
|
flags: string[]
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const installerDir = getInstallerDir();
|
|
|
|
|
createDirIfNotExists(installerDir);
|
|
|
|
|
|
2019-06-15 09:51:11 -04:00
|
|
|
|
const filePath = path.join(installerDir, moduleName);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (await exists(filePath)) {
|
2019-06-14 11:43:06 -04:00
|
|
|
|
const msg = `⚠️ ${moduleName} is already installed, do you want to overwrite it?`;
|
|
|
|
|
if (!(await yesNoPrompt(msg))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ensure script that is being installed exists
|
2019-06-18 12:25:53 -04:00
|
|
|
|
const ps = run({
|
|
|
|
|
args: ["deno", "fetch", moduleUrl],
|
|
|
|
|
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-14 11:43:06 -04:00
|
|
|
|
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();
|
2019-06-15 09:51:11 -04:00
|
|
|
|
const filePath = path.join(installerDir, moduleName);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (!(await exists(filePath))) {
|
|
|
|
|
throw new Error(`ℹ️ ${moduleName} not found`);
|
2019-06-14 11:43:06 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 09:51:11 -04:00
|
|
|
|
await remove(filePath);
|
2019-06-18 12:25:53 -04:00
|
|
|
|
if (isWindows) {
|
|
|
|
|
await remove(filePath + ".cmd");
|
|
|
|
|
}
|
2019-06-14 11:43:06 -04:00
|
|
|
|
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();
|
|
|
|
|
}
|