0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/js/copy_file.ts

48 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-09-30 18:06:41 -04:00
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
2018-10-03 21:18:23 -04:00
import * as msg from "gen/msg_generated";
import * as flatbuffers from "./flatbuffers";
2018-09-30 18:06:41 -04:00
import * as dispatch from "./dispatch";
2018-10-14 16:29:50 -04:00
/** Copies the contents of a file to another by name synchronously.
2018-09-30 18:06:41 -04:00
* Creates a new file if target does not exists, and if target exists,
* overwrites original content of the target file.
2018-10-14 16:29:50 -04:00
*
2018-09-30 18:06:41 -04:00
* It would also copy the permission of the original file
* to the destination.
*
2018-10-14 16:29:50 -04:00
* import { copyFileSync } from "deno";
* copyFileSync("from.txt", "to.txt");
2018-09-30 18:06:41 -04:00
*/
export function copyFileSync(from: string, to: string): void {
dispatch.sendSync(...req(from, to));
}
2018-10-14 16:29:50 -04:00
/** Copies the contents of a file to another by name.
*
2018-09-30 18:06:41 -04:00
* Creates a new file if target does not exists, and if target exists,
* overwrites original content of the target file.
2018-10-14 16:29:50 -04:00
*
2018-09-30 18:06:41 -04:00
* It would also copy the permission of the original file
* to the destination.
*
2018-10-14 16:29:50 -04:00
* import { copyFile } from "deno";
* await copyFile("from.txt", "to.txt");
2018-09-30 18:06:41 -04:00
*/
export async function copyFile(from: string, to: string): Promise<void> {
await dispatch.sendAsync(...req(from, to));
}
function req(
from: string,
to: string
2018-10-03 21:18:23 -04:00
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = flatbuffers.createBuilder();
2018-09-30 18:06:41 -04:00
const from_ = builder.createString(from);
const to_ = builder.createString(to);
2018-10-03 21:18:23 -04:00
msg.CopyFile.startCopyFile(builder);
msg.CopyFile.addFrom(builder, from_);
msg.CopyFile.addTo(builder, to_);
const inner = msg.CopyFile.endCopyFile(builder);
return [builder, msg.Any.CopyFile, inner];
2018-09-30 18:06:41 -04:00
}