summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2018-09-19 23:13:59 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-20 02:13:59 -0400
commitfab4bdf40d1fe1a53d00dbc0bc902c71d4446403 (patch)
tree843aa0d5cf867fdcefeee991655332da372d9b47
parent017ef096dfb0592d828804175e582895a3f39954 (diff)
Add deno.arch/deno.platform (#773)
-rw-r--r--BUILD.gn1
-rw-r--r--js/deno.ts1
-rw-r--r--js/platform.ts5
-rw-r--r--js/platform_test.ts9
-rw-r--r--js/types.ts26
-rw-r--r--js/unit_tests.ts1
-rw-r--r--package.json1
-rw-r--r--rollup.config.js38
8 files changed, 82 insertions, 0 deletions
diff --git a/BUILD.gn b/BUILD.gn
index 9ea56582a..a1ffc750f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -71,6 +71,7 @@ ts_sources = [
"js/make_temp_dir.ts",
"js/mock_builtin.js",
"js/os.ts",
+ "js/platform.ts",
"js/plugins.d.ts",
"js/read_file.ts",
"js/remove.ts",
diff --git a/js/deno.ts b/js/deno.ts
index 7d52441ec..9be05e916 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -12,4 +12,5 @@ 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 const argv: string[] = [];
diff --git a/js/platform.ts b/js/platform.ts
new file mode 100644
index 000000000..26fc317a1
--- /dev/null
+++ b/js/platform.ts
@@ -0,0 +1,5 @@
+// Dummy. Injected in rollup.config.js
+import { DenoArch, DenoPlatform } from "./types";
+
+export const arch: DenoArch = "unknown";
+export const platform: DenoPlatform = "unknown";
diff --git a/js/platform_test.ts b/js/platform_test.ts
new file mode 100644
index 000000000..80a1d9325
--- /dev/null
+++ b/js/platform_test.ts
@@ -0,0 +1,9 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+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");
+});
diff --git a/js/types.ts b/js/types.ts
index 7af0a5201..7a7bc44d9 100644
--- a/js/types.ts
+++ b/js/types.ts
@@ -151,3 +151,29 @@ declare global {
stackTraceLimit: number;
}
}
+
+// 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";
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index f1eed319e..a535aea55 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -14,3 +14,4 @@ import "./rename_test.ts";
import "./blob_test.ts";
import "./timers_test.ts";
import "./symlink_test.ts";
+import "./platform_test.ts";
diff --git a/package.json b/package.json
index fe00a2916..31f176aa6 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,7 @@
"@types/text-encoding": "0.0.33",
"base64-js": "^1.3.0",
"flatbuffers": "^1.9.0",
+ "magic-string": "^0.22.5",
"prettier": "^1.14.0",
"rollup": "^0.63.2",
"rollup-plugin-alias": "^1.4.0",
diff --git a/rollup.config.js b/rollup.config.js
index d5574a7d5..2fdaee08c 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -7,8 +7,10 @@ import nodeResolve from "rollup-plugin-node-resolve";
import typescriptPlugin from "rollup-plugin-typescript2";
import { createFilter } from "rollup-pluginutils";
import typescript from "typescript";
+import MagicString from "magic-string";
const mockPath = path.join(__dirname, "js", "mock_builtin.js");
+const platformPath = path.join(__dirname, "js", "platform.ts");
const tsconfig = path.join(__dirname, "tsconfig.json");
const typescriptPath = `${
process.env.BASEPATH
@@ -78,6 +80,37 @@ function strings({ include, exclude } = {}) {
};
}
+// Inject deno.arch/deno.platform from Node's process.arch/process.platform
+function platform({ include, exclude } = {}) {
+ if (!include) {
+ throw new Error("include option must be passed");
+ }
+
+ const filter = createFilter(include, exclude);
+
+ return {
+ name: "platform",
+ /**
+ * @param {any} _code
+ * @param {string} id
+ */
+ transform(_code, id) {
+ if (filter(id)) {
+ // Adapted from https://github.com/rollup/rollup-plugin-inject/blob/master/src/index.js
+ 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
+ return {
+ code: magicString.toString(),
+ map: magicString.generateMap()
+ };
+ }
+ }
+ };
+}
+
// This plugin resolves at bundle time any generated resources that are
// in the build path under `gen` and specified with a MID starting with `gen/`.
// The plugin assumes that the MID needs to have the `.ts` extension appended.
@@ -104,6 +137,11 @@ export default function makeConfig(commandOptions) {
},
plugins: [
+ // inject platform and arch from Node
+ platform({
+ include: [platformPath]
+ }),
+
// would prefer to use `rollup-plugin-virtual` to inject the empty module, but there
// is an issue with `rollup-plugin-commonjs` which causes errors when using the
// virtual plugin (see: rollup/rollup-plugin-commonjs#315), this means we have to use