提交 d39055d7 编写于 作者: R Ryan Dahl

Clean up deno.platform

Renames:
deno.platform -> deno.platform.os
deno.arch -> deno.platform.arch

Removes unsupported operating systems and CPU architectures from the
types. Uses the string "win" instead of "win32".
上级 0cdf1f45
......@@ -16,7 +16,7 @@ export { symlinkSync, symlink } from "./symlink";
export { writeFileSync, writeFile } from "./write_file";
export { ErrorKind, DenoError } from "./errors";
export { libdeno } from "./libdeno";
export { arch, platform } from "./platform";
export { platform } from "./platform";
export { trace } from "./trace";
export { truncateSync, truncate } from "./truncate";
export const args: string[] = [];
// Dummy. Injected in rollup.config.js
import { DenoArch, DenoPlatform } from "./types";
export const arch: DenoArch = "unknown";
export const platform: DenoPlatform = "unknown";
import { Platform } from "./types";
// 'platform' is injected by rollup.config.js at compile time.
export const platform: Platform = {};
......@@ -2,8 +2,10 @@
import { test, assert } from "./test_util.ts";
import * as deno from "deno";
test(function transformPlatformSuccess() {
// Make sure they are transformed
assert(deno.arch !== "unknown");
assert(deno.platform !== "unknown");
test(function platformTransform() {
// deno.platform is injected by rollup at compile time. Here
// we check it has been properly transformed.
const { arch, os } = deno.platform;
assert(arch === "x64");
assert(os === "mac" || os === "win" || os === "linux");
});
......@@ -152,28 +152,8 @@ declare global {
}
}
// Based on Node's arch
export type DenoArch =
| "arm"
| "arm64"
| "ia32"
| "mips"
| "mipsel"
| "ppc"
| "ppc64"
| "s390"
| "s390x"
| "x32"
| "x64"
| "unknown";
export type DenoPlatform =
| "aix"
| "darwin"
| "freebsd"
| "linux"
| "openbsd"
| "sunos"
| "win32"
| "android"
| "unknown";
// Do not add unsupported platforms.
export interface Platform {
arch?: "x64";
os?: "mac" | "win" | "linux";
}
......@@ -80,7 +80,16 @@ function strings({ include, exclude } = {}) {
};
}
// Inject deno.arch/deno.platform from Node's process.arch/process.platform
const archNodeToDeno = {
x64: "x64"
};
const osNodeToDeno = {
win32: "win",
darwin: "mac",
linux: "linux"
};
// Inject deno.platform.arch and deno.platform.os
function platform({ include, exclude } = {}) {
if (!include) {
throw new Error("include option must be passed");
......@@ -97,11 +106,11 @@ function platform({ include, exclude } = {}) {
transform(_code, id) {
if (filter(id)) {
// Adapted from https://github.com/rollup/rollup-plugin-inject/blob/master/src/index.js
const arch = archNodeToDeno[process.arch];
const os = osNodeToDeno[process.platform];
const magicString = new MagicString(`
import { DenoArch, DenoPlatform } from "./types";
export const arch: DenoArch = "${process.arch}";
export const platform: DenoPlatform = "${process.platform}";`);
// arch and platform comes from Node
import { Platform } from "./types";
export const platform: Platform = { arch: "${arch}", os:"${os}" };`);
return {
code: magicString.toString(),
map: magicString.generateMap()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册