1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 17:19:06 -05:00

Add deno.arch/deno.platform (#773)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-09-19 23:13:59 -07:00 committed by Ryan Dahl
parent 017ef096df
commit fab4bdf40d
8 changed files with 82 additions and 0 deletions

View file

@ -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",

View file

@ -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[] = [];

5
js/platform.ts Normal file
View file

@ -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";

9
js/platform_test.ts Normal file
View file

@ -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");
});

View file

@ -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";

View file

@ -14,3 +14,4 @@ import "./rename_test.ts";
import "./blob_test.ts";
import "./timers_test.ts";
import "./symlink_test.ts";
import "./platform_test.ts";

View file

@ -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",

View file

@ -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