本文整理汇总了TypeScript中fs.watch函数的典型用法代码示例。如果您正苦于以下问题:TypeScript watch函数的具体用法?TypeScript watch怎么用?TypeScript watch使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了watch函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: addDirWatcher
function addDirWatcher(dirPath: Path): void {
if (dirWatchers.contains(dirPath)) {
const watcher = dirWatchers.get(dirPath);
watcher.referenceCount += 1;
return;
}
const watcher: DirectoryWatcher = _fs.watch(
dirPath,
{ persistent: true },
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath)
);
watcher.referenceCount = 1;
dirWatchers.set(dirPath, watcher);
return;
}
示例2: addDirWatcher
function addDirWatcher(dirPath: string): void {
if (hasProperty(dirWatchers, dirPath)) {
const watcher = dirWatchers[dirPath];
watcher.referenceCount += 1;
return;
}
const watcher: DirectoryWatcher = _fs.watch(
dirPath,
{ persistent: true },
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath)
);
watcher.referenceCount = 1;
dirWatchers[dirPath] = watcher;
return;
}
示例3: watchDebouncedByFilename
export function watchDebouncedByFilename(directory, eventHandler) {
log(`Setting up file watching for '${directory}'`);
let callback = eventHandler;
// Handle Node.js + FSEvents bug
if (process.platform === 'darwin') {
callback = function(event, filename) {
if (filename != null) {
arguments[1] = iconv.decode(iconv.encode(filename, 'iso-8859-1'), 'utf-8');
}
eventHandler.apply(null, arguments);
};
}
// Group events based on filename (2nd argument)
fs.watch(directory, { recursive: true }, debounce(callback, 50, 1));
}
示例4: watchFile
export function watchFile(filepath: string, onChange: () => void): Disposable {
let callback = debounce(onChange, 100)
try {
let watcher = fs.watch(filepath, {
persistent: true,
recursive: false,
encoding: 'utf8'
}, () => {
callback()
})
return Disposable.create(() => {
watcher.close()
})
} catch (e) {
return Disposable.create(() => {
// noop
})
}
}
示例5: function
executeTrigger: async function (trigger)
{
switch (trigger.name)
{
case 'watch':
var stat = await promisify(fs.stat)(trigger.params['path'] as string);
if (stat.isDirectory() || stat.isFile())
{
var id = uuid();
var watcher = fs.watch(trigger.params['path'] as string, function (event, fileName)
{
if (!trigger.params['event'] || trigger.params['event'] == event)
server.trigger({ id: id, data: { path: fileName, mtime: new Date().toJSON() } });
});
registeredTriggers[id] = watcher;
}
break;
}
return null;
}
示例6: constructor
constructor(isWatch: boolean) {
this._isWatch = isWatch;
this.stream = es.through();
this._inputFiles = monacodts.getIncludesInRecipe().map((moduleId) => {
if (/\.d\.ts$/.test(moduleId)) {
// This source file is already in .d.ts form
return path.join(REPO_SRC_FOLDER, moduleId);
} else {
return path.join(REPO_SRC_FOLDER, `${moduleId}.ts`);
}
});
// Install watchers
this._watchers = [];
if (this._isWatch) {
this._inputFiles.forEach((filePath) => {
const watcher = fs.watch(filePath);
watcher.addListener('change', () => {
this._inputFileChanged[filePath] = true;
// Avoid hitting empty files... :/
setTimeout(() => this.execute(), 10);
});
this._watchers.push(watcher);
});
const recipeWatcher = fs.watch(monacodts.RECIPE_PATH);
recipeWatcher.addListener('change', () => {
this._recipeFileChanged = true;
// Avoid hitting empty files... :/
setTimeout(() => this.execute(), 10);
});
this._watchers.push(recipeWatcher);
}
this._inputFileChanged = {};
this._inputFiles.forEach(file => this._inputFileChanged[file] = true);
this._recipeFileChanged = true;
this._dtsFilesContents = {};
this._dtsFilesContents2 = {};
}
示例7: transpile
function transpile(fileNames: string[], options: ts.CompilerOptions) {
console.time("transpile");
var files = fileNames.filter(f => !isDTS(f));
if (isIncremental) {
files = files.filter(hasChanged);
}
files.forEach(tsPath => {
var tsSource = fs.readFileSync(tsPath, { encoding: "utf8" });
var jsSource = ts.transpile(tsSource, options);
var jsPath = getJsPath(tsPath);
fs.writeFileSync(jsPath, jsSource, { flag: "w" }, function(err) { console.log(err); });
if (isIncremental) {
console.log(" - " + tsPath);
}
});
console.timeEnd("transpile");
if (isWatching) {
// NOTE: Perhaps on file change before incremental compilation we should read the tsconfig.json again and update only tsconfig.json files.
console.log("Watching for changes...");
fs.watch(".", { persistent: true, recursive: true, encoding: "utf8" }, (event, file) => {
try {
if (isTS(file) && !isDTS(file) && file.indexOf("platforms/android/") < 0 && file.indexOf("platforms/ios/") < 0) {
var tsPath = file;
var label = " - " + tsPath;
console.time(label);
var tsSource = fs.readFileSync(tsPath, { encoding: "utf8" });
var jsSource = ts.transpile(tsSource, options);
var jsPath = getJsPath(tsPath);
fs.writeFileSync(jsPath, jsSource, { flag: "w" }, function(err) { console.log(err); });
console.timeEnd(label);
}
} catch(e) {
// console.log(e);
}
});
}
}
示例8:
let server = net.createServer((socket) => {
console.log('Subscriber connected');
socket.write(JSON.stringify({
type: 'watching',
file: filename
}) + '\n');
//watcher setup
let watcher = fs.watch(filename, () => {
socket.write(JSON.stringify({
type: 'changed',
file: filename,
timestamp: Date.now()
}) + '\n');
});
//Cleanup
socket.on('close', () => {
console.log('Subscriber disconnected');
watcher.close();
});
});
示例9: setTimeout
fs.stat(crashFolder, (err, stats) => {
let crashObject: { [key: string]: string } = {};
if (err) {
// If the directory isn't there, we have a problem...
crashObject["fs.stat: err.code"] = err.code;
telemetry.logLanguageServerEvent("MacCrash", crashObject, null);
return;
}
// vscode.workspace.createFileSystemWatcher only works in workspace folders.
try {
fs.watch(crashFolder, (event, filename) => {
if (event !== "rename") {
return;
}
if (filename === prevCrashFile) {
return;
}
prevCrashFile = filename;
if (!filename.startsWith("Microsoft.VSCode.CPP.")) {
return;
}
// Wait 5 seconds to allow time for the crash log to finish being written.
setTimeout(() => {
fs.readFile(path.resolve(crashFolder, filename), 'utf8', (err, data) => {
if (err) {
// Try again?
fs.readFile(path.resolve(crashFolder, filename), 'utf8', handleCrashFileRead);
return;
}
handleCrashFileRead(err, data);
});
}, 5000);
});
} catch (e) {
// The file watcher limit is hit (may not be possible on Mac, but just in case).
}
});
示例10: Error
/// <reference path="../../typings/node/node.d.ts" />
"use strict";
import * as fs from 'fs';
import { spawn } from 'child_process';
const filename = process.argv[2];
if(!filename) {
throw Error('A file to watch must be specified');
}
fs.watch(filename, () => {
let ls = spawn('ls', ['-lh', filename]);
ls.stdout.pipe(process.stdout);
});
console.log(`Watching ${filename} for changes`);