mirror of
https://github.com/denoland/deno.git
synced 2025-02-02 04:38:21 -05:00
Installer: support windows (denoland/deno_std#499)
Original: a68527f3fe
This commit is contained in:
parent
f430df5619
commit
d6e92582cc
2 changed files with 175 additions and 105 deletions
190
installer/mod.ts
190
installer/mod.ts
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/env deno --allow-all
|
#!/usr/bin/env deno --allow-all
|
||||||
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
const {
|
const {
|
||||||
args,
|
args,
|
||||||
env,
|
env,
|
||||||
|
@ -8,15 +8,17 @@ const {
|
||||||
writeFile,
|
writeFile,
|
||||||
exit,
|
exit,
|
||||||
stdin,
|
stdin,
|
||||||
stat,
|
chmod,
|
||||||
readAll,
|
remove,
|
||||||
run,
|
run
|
||||||
remove
|
|
||||||
} = Deno;
|
} = Deno;
|
||||||
import * as path from "../fs/path.ts";
|
import * as path from "../fs/path.ts";
|
||||||
|
import { exists } from "../fs/exists.ts";
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const decoder = new TextDecoder("utf-8");
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
const isWindows = Deno.platform.os === "win";
|
||||||
|
const driverLetterReg = /^[c-z]:/i; // Regular expression to test disk driver letter. eg "C:\\User\username\path\to"
|
||||||
|
|
||||||
enum Permission {
|
enum Permission {
|
||||||
Read,
|
Read,
|
||||||
|
@ -86,69 +88,103 @@ function createDirIfNotExists(path: string): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkIfExistsInPath(path: string): boolean {
|
function checkIfExistsInPath(filePath: string): boolean {
|
||||||
const { PATH } = env();
|
// In Windows's Powershell $PATH not exist, so use $Path instead.
|
||||||
|
// $HOMEDRIVE is only used on Windows.
|
||||||
|
const { PATH, Path, HOMEDRIVE } = env();
|
||||||
|
|
||||||
const paths = (PATH as string).split(":");
|
let envPath = (PATH as string) || (Path as string) || "";
|
||||||
|
|
||||||
return paths.includes(path);
|
const paths = envPath.split(isWindows ? ";" : ":");
|
||||||
|
|
||||||
|
let fileAbsolutePath = filePath;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
fileAbsolutePath = filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInstallerDir(): string {
|
function getInstallerDir(): string {
|
||||||
const { HOME } = env();
|
// In Windows's Powershell $HOME environmental variable maybe null, if so use $HOMEPATH instead.
|
||||||
|
let { HOME, HOMEPATH } = env();
|
||||||
|
|
||||||
if (!HOME) {
|
const HOME_PATH = HOME || HOMEPATH;
|
||||||
|
|
||||||
|
if (!HOME_PATH) {
|
||||||
throw new Error("$HOME is not defined.");
|
throw new Error("$HOME is not defined.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.join(HOME, ".deno", "bin");
|
return path.join(HOME_PATH, ".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 {
|
function showHelp(): void {
|
||||||
console.log(`deno installer
|
console.log(`deno installer
|
||||||
Install remote or local script as executables.
|
Install remote or local script as executables.
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...]
|
deno https://deno.land/std/installer/mod.ts EXE_NAME SCRIPT_URL [FLAGS...]
|
||||||
|
|
||||||
ARGS:
|
ARGS:
|
||||||
EXE_NAME Name for executable
|
EXE_NAME Name for executable
|
||||||
SCRIPT_URL Local or remote URL of script to install
|
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.
|
[FLAGS...] List of flags for script, both Deno permission and script specific flag can be used.
|
||||||
`);
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function install(
|
export async function install(
|
||||||
|
@ -161,14 +197,7 @@ export async function install(
|
||||||
|
|
||||||
const filePath = path.join(installerDir, moduleName);
|
const filePath = path.join(installerDir, moduleName);
|
||||||
|
|
||||||
let fileInfo;
|
if (await exists(filePath)) {
|
||||||
try {
|
|
||||||
fileInfo = await stat(filePath);
|
|
||||||
} catch (e) {
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fileInfo) {
|
|
||||||
const msg = `⚠️ ${moduleName} is already installed, do you want to overwrite it?`;
|
const msg = `⚠️ ${moduleName} is already installed, do you want to overwrite it?`;
|
||||||
if (!(await yesNoPrompt(msg))) {
|
if (!(await yesNoPrompt(msg))) {
|
||||||
return;
|
return;
|
||||||
|
@ -176,15 +205,16 @@ export async function install(
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure script that is being installed exists
|
// ensure script that is being installed exists
|
||||||
if (moduleUrl.startsWith("http")) {
|
const ps = run({
|
||||||
// remote module
|
args: ["deno", "fetch", moduleUrl],
|
||||||
console.log(`Downloading: ${moduleUrl}\n`);
|
stdout: "inherit",
|
||||||
await fetchModule(moduleUrl);
|
stderr: "inherit"
|
||||||
} else {
|
});
|
||||||
// assume that it's local file
|
|
||||||
moduleUrl = path.resolve(moduleUrl);
|
const { code } = await ps.status();
|
||||||
console.log(`Looking for: ${moduleUrl}\n`);
|
|
||||||
await stat(moduleUrl);
|
if (code !== 0) {
|
||||||
|
throw new Error("Failed to fetch module.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const grantedPermissions: Permission[] = [];
|
const grantedPermissions: Permission[] = [];
|
||||||
|
@ -201,28 +231,17 @@ export async function install(
|
||||||
|
|
||||||
const commands = [
|
const commands = [
|
||||||
"deno",
|
"deno",
|
||||||
|
"run",
|
||||||
...grantedPermissions.map(getFlagFromPermission),
|
...grantedPermissions.map(getFlagFromPermission),
|
||||||
moduleUrl,
|
moduleUrl,
|
||||||
...scriptArgs,
|
...scriptArgs
|
||||||
"$@"
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// TODO: add windows Version
|
await generateExecutable(filePath, commands);
|
||||||
const template = `#/bin/sh\n${commands.join(" ")}`;
|
|
||||||
await writeFile(filePath, encoder.encode(template));
|
|
||||||
|
|
||||||
const makeExecutable = run({ args: ["chmod", "+x", filePath] });
|
|
||||||
const { code } = await makeExecutable.status();
|
|
||||||
makeExecutable.close();
|
|
||||||
|
|
||||||
if (code !== 0) {
|
|
||||||
throw new Error("Failed to make file executable");
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`✅ Successfully installed ${moduleName}`);
|
console.log(`✅ Successfully installed ${moduleName}`);
|
||||||
console.log(filePath);
|
console.log(filePath);
|
||||||
|
|
||||||
// TODO: add Windows version
|
|
||||||
if (!checkIfExistsInPath(installerDir)) {
|
if (!checkIfExistsInPath(installerDir)) {
|
||||||
console.log("\nℹ️ Add ~/.deno/bin to PATH");
|
console.log("\nℹ️ Add ~/.deno/bin to PATH");
|
||||||
console.log(
|
console.log(
|
||||||
|
@ -235,15 +254,14 @@ export async function uninstall(moduleName: string): Promise<void> {
|
||||||
const installerDir = getInstallerDir();
|
const installerDir = getInstallerDir();
|
||||||
const filePath = path.join(installerDir, moduleName);
|
const filePath = path.join(installerDir, moduleName);
|
||||||
|
|
||||||
try {
|
if (!(await exists(filePath))) {
|
||||||
await stat(filePath);
|
throw new Error(`ℹ️ ${moduleName} not found`);
|
||||||
} catch (e) {
|
|
||||||
if (e instanceof Deno.DenoError && e.kind === Deno.ErrorKind.NotFound) {
|
|
||||||
throw new Error(`ℹ️ ${moduleName} not found`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await remove(filePath);
|
await remove(filePath);
|
||||||
|
if (isWindows) {
|
||||||
|
await remove(filePath + ".cmd");
|
||||||
|
}
|
||||||
console.log(`ℹ️ Uninstalled ${moduleName}`);
|
console.log(`ℹ️ Uninstalled ${moduleName}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
const { readFile, run, stat, makeTempDir, remove, env } = Deno;
|
const { run, stat, makeTempDir, remove, env } = Deno;
|
||||||
|
|
||||||
import { test, runIfMain, TestFunction } from "../testing/mod.ts";
|
import { test, runIfMain, TestFunction } from "../testing/mod.ts";
|
||||||
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
||||||
|
@ -7,8 +7,10 @@ import { BufReader, EOF } from "../io/bufio.ts";
|
||||||
import { TextProtoReader } from "../textproto/mod.ts";
|
import { TextProtoReader } from "../textproto/mod.ts";
|
||||||
import { install, uninstall } from "./mod.ts";
|
import { install, uninstall } from "./mod.ts";
|
||||||
import * as path from "../fs/path.ts";
|
import * as path from "../fs/path.ts";
|
||||||
|
import * as fs from "../fs/mod.ts";
|
||||||
|
|
||||||
let fileServer: Deno.Process;
|
let fileServer: Deno.Process;
|
||||||
|
const isWindows = Deno.platform.os === "win";
|
||||||
|
|
||||||
// copied from `http/file_server_test.ts`
|
// copied from `http/file_server_test.ts`
|
||||||
async function startFileServer(): Promise<void> {
|
async function startFileServer(): Promise<void> {
|
||||||
|
@ -63,11 +65,40 @@ installerTest(async function installBasic(): Promise<void> {
|
||||||
const fileInfo = await stat(filePath);
|
const fileInfo = await stat(filePath);
|
||||||
assert(fileInfo.isFile());
|
assert(fileInfo.isFile());
|
||||||
|
|
||||||
const fileBytes = await readFile(filePath);
|
if (isWindows) {
|
||||||
const fileContents = new TextDecoder().decode(fileBytes);
|
assertEquals(
|
||||||
|
await fs.readFileStr(filePath + ".cmd"),
|
||||||
|
`% 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" run http://localhost:4500/http/file_server.ts %*
|
||||||
|
) ELSE (
|
||||||
|
@SETLOCAL
|
||||||
|
@SET PATHEXT=%PATHEXT:;.TS;=;%
|
||||||
|
deno run http://localhost:4500/http/file_server.ts %*
|
||||||
|
)
|
||||||
|
`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
fileContents,
|
await fs.readFileStr(filePath),
|
||||||
"#/bin/sh\ndeno http://localhost:4500/http/file_server.ts $@"
|
`#/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" run http://localhost:4500/http/file_server.ts "$@"
|
||||||
|
ret=$?
|
||||||
|
else
|
||||||
|
deno run http://localhost:4500/http/file_server.ts "$@"
|
||||||
|
ret=$?
|
||||||
|
fi
|
||||||
|
exit $ret
|
||||||
|
`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -81,11 +112,40 @@ installerTest(async function installWithFlags(): Promise<void> {
|
||||||
const { HOME } = env();
|
const { HOME } = env();
|
||||||
const filePath = path.resolve(HOME, ".deno/bin/file_server");
|
const filePath = path.resolve(HOME, ".deno/bin/file_server");
|
||||||
|
|
||||||
const fileBytes = await readFile(filePath);
|
if (isWindows) {
|
||||||
const fileContents = new TextDecoder().decode(fileBytes);
|
assertEquals(
|
||||||
|
await fs.readFileStr(filePath + ".cmd"),
|
||||||
|
`% 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" run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar %*
|
||||||
|
) ELSE (
|
||||||
|
@SETLOCAL
|
||||||
|
@SET PATHEXT=%PATHEXT:;.TS;=;%
|
||||||
|
deno run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar %*
|
||||||
|
)
|
||||||
|
`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
fileContents,
|
await fs.readFileStr(filePath),
|
||||||
"#/bin/sh\ndeno --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar $@"
|
`#/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" run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar "$@"
|
||||||
|
ret=$?
|
||||||
|
else
|
||||||
|
deno run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar "$@"
|
||||||
|
ret=$?
|
||||||
|
fi
|
||||||
|
exit $ret
|
||||||
|
`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -97,16 +157,8 @@ installerTest(async function uninstallBasic(): Promise<void> {
|
||||||
|
|
||||||
await uninstall("file_server");
|
await uninstall("file_server");
|
||||||
|
|
||||||
let thrown = false;
|
assert(!(await fs.exists(filePath)));
|
||||||
try {
|
assert(!(await fs.exists(filePath + ".cmd")));
|
||||||
await stat(filePath);
|
|
||||||
} catch (e) {
|
|
||||||
thrown = true;
|
|
||||||
assert(e instanceof Deno.DenoError);
|
|
||||||
assertEquals(e.kind, Deno.ErrorKind.NotFound);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(thrown);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
installerTest(async function uninstallNonExistentModule(): Promise<void> {
|
installerTest(async function uninstallNonExistentModule(): Promise<void> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue