本文整理汇总了TypeScript中fs.openSync函数的典型用法代码示例。如果您正苦于以下问题:TypeScript openSync函数的具体用法?TypeScript openSync怎么用?TypeScript openSync使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了openSync函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: copyFile
function copyFile(fromPath: string, toPath: string) {
console.log("Copying from '" + fromPath + "' to '" + toPath);
makePathTo(toPath);
const fileSize = (fs.statSync(fromPath)).size,
bufferSize = Math.min(0x10000, fileSize),
buffer = new Buffer(bufferSize),
progress = makeProgress(fileSize),
handleFrom = fs.openSync(fromPath, "r"),
handleTo = fs.openSync(toPath, "w");
try {
for (;;) {
const got = fs.readSync(handleFrom, buffer, 0, bufferSize, null);
if (got <= 0) break;
fs.writeSync(handleTo, buffer, 0, got);
progress(got);
}
progress(null);
} finally {
fs.closeSync(handleFrom);
fs.closeSync(handleTo);
}
}
示例2: copyFile
function copyFile(sourceFile, destPath): void {
checkFileCopy(sourceFile, destPath);
var filename = path.basename(sourceFile);
if (fs.existsSync(destPath)) {
if (fs.lstatSync(destPath).isDirectory()) {
destPath += "/" + filename;
}
} else {
if (destPath[destPath.length - 1] === "/" || destPath[destPath.length - 1] === "\\") {
mkdirParentsSync(destPath);
destPath += filename;
} else {
mkdirParentsSync(path.dirname(destPath));
}
}
var bufLength = 64 * 1024;
var buff = new Buffer(bufLength);
var fdr = fs.openSync(sourceFile, "r");
var fdw = fs.openSync(destPath, "w");
var bytesRead = 1;
var pos = 0;
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, buff, 0, bufLength, pos);
fs.writeSync(fdw, buff, 0, bytesRead);
pos += bytesRead;
}
fs.closeSync(fdr);
fs.closeSync(fdw);
}
示例3: it
it('should not remove files if nothing is matched', () => {
fs.closeSync(fs.openSync(path.resolve(tmpDir, 'bar-123'), 'w'));
fs.closeSync(fs.openSync(path.resolve(tmpDir, 'bar-456'), 'w'));
fs.closeSync(fs.openSync(path.resolve(tmpDir, 'bar-789'), 'w'));
expect(removeFiles(tmpDir, [/zebra-.*/g])).toBe('');
expect(fs.readdirSync(tmpDir).length).toBe(3);
});
示例4: beforeAll
beforeAll(() => {
// create the directory
try {
fs.mkdirSync(tmpDir);
} catch (err) {
}
// create files that should be deleted.
fs.closeSync(fs.openSync(path.resolve(tmpDir, 'chromedriver_2.41'), 'w'));
fs.closeSync(
fs.openSync(path.resolve(tmpDir, 'chromedriver_foo.zip'), 'w'));
fs.closeSync(
fs.openSync(path.resolve(tmpDir, 'chromedriver.config.json'), 'w'));
fs.closeSync(fs.openSync(path.resolve(tmpDir, 'chromedriver.xml'), 'w'));
});
示例5: convert
function convert(inFilename, outFilename) {
let inFile = fs.openSync(inFilename, 'r');
let outFile = fs.openSync(outFilename, 'w');
return new Promise((resolve, reject) => {
runCmd('sqlite3', [cluesDb], {
stdio: [inFile, outFile, 'inherit']
}, (err) => {
fs.closeSync(inFile);
fs.closeSync(outFile);
if (err) { return reject(err); }
resolve();
});
});
};
示例6: latestVersion
latestVersion("mirakurun").then(latest => {
if (!req.query.force && current === latest) {
api.responseError(res, 409, "Update Nothing");
return;
}
let command;
const args = [
"install",
"mirakurun@latest",
"-g",
"--production"
];
if (process.platform === "win32") {
command = "npm.cmd";
} else {
command = "npm";
args.push("--unsafe");
}
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.status(202);
res.write("Updating...\n");
const path = join(tmpdir(), "Mirakurun_Updating.log");
if (existsSync(path) === true) {
unlinkSync(path);
}
const out = openSync(path, "a");
const err = openSync(path, "a");
res.write(`> ${command} ${args.join(" ")}\n\n`);
const npm = spawn(command, args, {
detached: true,
stdio: ["ignore", out, err]
});
npm.unref();
const tail = new Tail(path);
tail.on("line", data => res.write(data + "\n"));
req.once("close", () => {
tail.removeAllListener("line");
tail.unwatch();
});
});
示例7: touch
export function touch(filename: string): any {
try {
return closeSync(openSync(filename, 'w'));
} catch (err) {
console.log('file already exists');
}
}
示例8: spawnNpmInstall
function spawnNpmInstall(version: string): ChildProcess {
let command = "npm";
const args = [
"install",
`${pkg.name}@${version}`,
"-g",
"--production"
];
if (process.platform === "win32") {
command = "npm.cmd";
} else {
args.push("--unsafe-perm");
}
console.log(">", command, ...args);
let out: number;
if (process.env.UPDATER_LOG_PATH) {
out = openSync(process.env.UPDATER_LOG_PATH, "a");
}
const npm = spawn(command, args, {
detached: true,
stdio: [
"ignore",
out || process.stdout,
out || process.stderr
]
});
npm.unref();
return npm;
}
示例9: it
it('fs.write should work with util.promisify', (done: DoneFn) => {
const promisifyWrite = util.promisify(write);
const dest = __filename + 'write';
const fd = openSync(dest, 'a');
const stats = fstatSync(fd);
const chunkSize = 512;
const buffer = new Buffer(chunkSize);
for (let i = 0; i < chunkSize; i++) {
buffer[i] = 0;
}
// fd, buffer, offset, length, position, callback
promisifyWrite(fd, buffer, 0, chunkSize, 0)
.then(
(value) => {
expect(value.bytesWritten).toBe(chunkSize);
closeSync(fd);
unlinkSync(dest);
done();
},
err => {
closeSync(fd);
unlinkSync(dest);
fail(`should not be here with error: ${error}.`);
});
});
示例10: writeFileSync
export function writeFileSync(path: string, data: string | Buffer, options?: IWriteFileOptions): void {
const ensuredOptions = ensureWriteOptions(options);
if (ensuredOptions.encoding) {
data = encode(data, ensuredOptions.encoding.charset, { addBOM: ensuredOptions.encoding.addBOM });
}
if (!canFlush) {
return fs.writeFileSync(path, data, { mode: ensuredOptions.mode, flag: ensuredOptions.flag });
}
// Open the file with same flags and mode as fs.writeFile()
const fd = fs.openSync(path, ensuredOptions.flag, ensuredOptions.mode);
try {
// It is valid to pass a fd handle to fs.writeFile() and this will keep the handle open!
fs.writeFileSync(fd, data);
// Flush contents (not metadata) of the file to disk
try {
fs.fdatasyncSync(fd);
} catch (syncError) {
console.warn('[node.js fs] fdatasyncSync is now disabled for this session because it failed: ', syncError);
canFlush = false;
}
} finally {
fs.closeSync(fd);
}
}