mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
feat: add Deno.osName() (#5714)
This commit is contained in:
parent
818a801092
commit
6ccf9037a6
8 changed files with 49 additions and 12 deletions
|
@ -529,7 +529,7 @@ impl Into<TsTypeDef> for &TsType {
|
|||
TsUnionOrIntersectionType(union_or_inter) => union_or_inter.into(),
|
||||
TsArrayType(array_type) => array_type.into(),
|
||||
TsTupleType(tuple_type) => tuple_type.into(),
|
||||
TsTypeOperator(type_op_type) => type_op_type.into(),
|
||||
TsTypeOperator(type_op_name) => type_op_name.into(),
|
||||
TsParenthesizedType(paren_type) => paren_type.into(),
|
||||
TsRestType(rest_type) => rest_type.into(),
|
||||
TsOptionalType(optional_type) => optional_type.into(),
|
||||
|
@ -737,7 +737,7 @@ pub fn ts_type_ann_to_def(type_ann: &TsTypeAnn) -> TsTypeDef {
|
|||
TsUnionOrIntersectionType(union_or_inter) => union_or_inter.into(),
|
||||
TsArrayType(array_type) => array_type.into(),
|
||||
TsTupleType(tuple_type) => tuple_type.into(),
|
||||
TsTypeOperator(type_op_type) => type_op_type.into(),
|
||||
TsTypeOperator(type_op_name) => type_op_name.into(),
|
||||
TsParenthesizedType(paren_type) => paren_type.into(),
|
||||
TsRestType(rest_type) => rest_type.into(),
|
||||
TsOptionalType(optional_type) => optional_type.into(),
|
||||
|
|
|
@ -53,7 +53,7 @@ export {
|
|||
export { metrics, Metrics } from "./ops/runtime.ts";
|
||||
export { mkdirSync, mkdir, MkdirOptions } from "./ops/fs/mkdir.ts";
|
||||
export { connect, listen, Listener, Conn } from "./net.ts";
|
||||
export { env, exit, execPath } from "./ops/os.ts";
|
||||
export { dir, env, exit, execPath, osName } from "./ops/os.ts";
|
||||
export { run, RunOptions, Process, ProcessStatus } from "./process.ts";
|
||||
export { DirEntry, readDirSync, readDir } from "./ops/fs/read_dir.ts";
|
||||
export { readFileSync, readFile } from "./read_file.ts";
|
||||
|
|
12
cli/js/lib.deno.unstable.d.ts
vendored
12
cli/js/lib.deno.unstable.d.ts
vendored
|
@ -32,6 +32,18 @@ declare namespace Deno {
|
|||
* Requires `allow-read` and `allow-write` permissions. */
|
||||
export function linkSync(oldpath: string, newpath: string): void;
|
||||
|
||||
/**
|
||||
* Returns the os name.
|
||||
*
|
||||
* ```ts
|
||||
* console.log(Deno.osName()); // e.g. "Linux"
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
|
||||
export function osName(): string;
|
||||
|
||||
/** Creates `newpath` as a hard link to `oldpath`.
|
||||
/** **UNSTABLE**: This API needs a security review.
|
||||
*
|
||||
* Creates `newpath` as a hard link to `oldpath`.
|
||||
|
|
|
@ -72,3 +72,7 @@ export function dir(kind: DirKind): string | null {
|
|||
export function execPath(): string {
|
||||
return sendSync("op_exec_path");
|
||||
}
|
||||
|
||||
export function osName(): string {
|
||||
return sendSync("op_name");
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ pub fn init(i: &mut CoreIsolate, s: &State) {
|
|||
i.register_op("op_hostname", s.stateful_json_op(op_hostname));
|
||||
i.register_op("op_loadavg", s.stateful_json_op(op_loadavg));
|
||||
i.register_op("op_os_release", s.stateful_json_op(op_os_release));
|
||||
i.register_op("op_name", s.stateful_json_op(op_name));
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -206,3 +207,14 @@ fn op_os_release(
|
|||
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
|
||||
Ok(JsonOp::Sync(json!(release)))
|
||||
}
|
||||
|
||||
fn op_name(
|
||||
state: &State,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<JsonOp, OpError> {
|
||||
state.check_unstable("Deno.osName");
|
||||
state.check_env()?;
|
||||
let os_name = sys_info::os_type().unwrap_or_else(|_| "".to_string());
|
||||
Ok(JsonOp::Sync(json!(os_name)))
|
||||
}
|
||||
|
|
|
@ -326,6 +326,22 @@ unitTest({ perms: { env: false } }, function hostnamePerm(): void {
|
|||
assert(caughtError);
|
||||
});
|
||||
|
||||
unitTest({ perms: { env: true } }, function osName(): void {
|
||||
assertNotEquals(Deno.osName(), "");
|
||||
});
|
||||
|
||||
unitTest({ perms: { env: false } }, function osNamePerm(): void {
|
||||
let caughtError = false;
|
||||
try {
|
||||
Deno.osName();
|
||||
} catch (err) {
|
||||
caughtError = true;
|
||||
assert(err instanceof Deno.errors.PermissionDenied);
|
||||
assertEquals(err.name, "PermissionDenied");
|
||||
}
|
||||
assert(caughtError);
|
||||
});
|
||||
|
||||
unitTest({ perms: { env: true } }, function releaseDir(): void {
|
||||
assertNotEquals(Deno.osRelease(), "");
|
||||
});
|
||||
|
|
|
@ -190,9 +190,9 @@ export function totalmem(): number {
|
|||
notImplemented(SEE_GITHUB_ISSUE);
|
||||
}
|
||||
|
||||
/** Not yet implemented */
|
||||
/** Returns the operating system name as returned by uname(3) */
|
||||
export function type(): string {
|
||||
notImplemented(SEE_GITHUB_ISSUE);
|
||||
return Deno.osName();
|
||||
}
|
||||
|
||||
/** Not yet implemented */
|
||||
|
|
|
@ -244,13 +244,6 @@ test({
|
|||
Error,
|
||||
"Not implemented"
|
||||
);
|
||||
assertThrows(
|
||||
() => {
|
||||
os.type();
|
||||
},
|
||||
Error,
|
||||
"Not implemented"
|
||||
);
|
||||
assertThrows(
|
||||
() => {
|
||||
os.uptime();
|
||||
|
|
Loading…
Reference in a new issue