本文整理匯總了TypeScript中fs.watchFile函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript watchFile函數的具體用法?TypeScript watchFile怎麽用?TypeScript watchFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了watchFile函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: _reject
return this._makeChild((child, resolve, reject) => {
function _reject(message: string) {
const pidFile = 'testingbot-tunnel.pid';
if (existsSync(pidFile)) {
// Remove the pidfile to ensure the running tunnel app shuts
// down
unlinkSync(pidFile);
}
reject(message);
}
// Polling API is used because we are only watching for one file, so
// efficiency is not a big deal, and the `fs.watch` API has extra
// restrictions which are best avoided
watchFile(readyFile, { persistent: false, interval: 1007 }, function(
current,
previous
) {
if (Number(current.mtime) === Number(previous.mtime)) {
// readyFile hasn't been modified, so ignore the event
return;
}
unwatchFile(readyFile);
resolve();
});
let lastMessage: string;
this._handle = on(child.stderr, 'data', (data: string) => {
data = String(data);
data.split('\n').forEach(message => {
if (message.indexOf('INFO: ') === 0) {
message = message.slice('INFO: '.length);
// the tunnel produces a lot of repeating messages
// during setup when the status is pending; deduplicate
// them for sanity
if (
message !== lastMessage &&
message.indexOf('>> [') === -1 &&
message.indexOf('<< [') === -1
) {
this.emit({
type: 'status',
target: this,
status: message
});
lastMessage = message;
}
} else if (message.indexOf('SEVERE: ') === 0) {
_reject(message);
} else if (message.indexOf('An error ocurred:') === 0) {
_reject(message);
}
});
});
executor(child, resolve, reject);
}, readyFile);
示例2: constructor
constructor() {
marked.setOptions({
langPrefix: 'hljs ',
highlight: function (code: string, lang: string) {
return hljs.highlight(lang, code).value;
}
});
fs.watchFile(entriesFilename, () => this.loadBlogEntries());
this.loadBlogEntries();
}
示例3: writeFile
writeFile('testfile', 'test content', () => {
watchFile('testfile', {persistent: false, interval: 1000}, (curr, prev) => {
expect(curr.size).toBe(16);
expect(prev.size).toBe(12);
expect(zoneASpec.onScheduleTask).toHaveBeenCalled();
expect(Zone.current.name).toBe('A');
unwatchFile('testfile');
unlink('testfile', () => {
done();
});
});
writeFile('testfile', 'test new content', () => {});
});
示例4: close
watchFile: (fileName, callback) => {
// watchFile polls a file every 250ms, picking up file notifications.
_fs.watchFile(fileName, { persistent: true, interval: 250 }, fileChanged);
return {
close() { _fs.unwatchFile(fileName, fileChanged); }
};
function fileChanged(curr: any, prev: any) {
if (+curr.mtime <= +prev.mtime) {
return;
}
callback(fileName);
};
},
示例5: setTimeout
setTimeout(() => {
const selfSourceFiles = new Set<string>()
for (let filename of fs.readdirSync(__dirname)) {
selfSourceFiles.add(Path.join(__dirname, filename))
}
selfSourceFiles.add(sourceDir)
let isClosing = false
const onSourceChanged = (curr: fs.Stats, prev :fs.Stats) => {
// console.log(process.pid, 'server changed')
for (let filename of selfSourceFiles) {
fs.unwatchFile(filename)
}
for (let filename of sourceFiles) {
fs.unwatchFile(filename)
}
if (process.env.IS_SUBPROCESS) {
process.exit(0)
return
}
if (isClosing) {
return
}
isClosing = true
if (httpServer) {
// console.log(process.pid, 'closing server', httpServer.listening)
httpServer.close(() => {
setTimeout(restartAsSubproc, 100)
})
httpServer.unref()
} else {
setTimeout(restartAsSubproc, 100)
}
}
for (let filename of selfSourceFiles) {
fs.watchFile(filename, {interval:200}, onSourceChanged)
}
}, 500)
示例6: fileChanged
watchFile: (fileName, callback, pollingInterval) => {
if (useNonPollingWatchers) {
const watchedFile = watchedFileSet.addFile(fileName, callback);
return {
close: () => watchedFileSet.removeFile(watchedFile)
};
}
else {
_fs.watchFile(fileName, { persistent: true, interval: pollingInterval || 250 }, fileChanged);
return {
close: () => _fs.unwatchFile(fileName, fileChanged)
};
}
function fileChanged(curr: any, prev: any) {
if (+curr.mtime <= +prev.mtime) {
return;
}
callback(fileName);
}
},
示例7: watchFile
return this._makeChild((child, resolve, reject) => {
let stdout: string[] | null = [];
// Polling API is used because we are only watching for one file, so
// efficiency is not a big deal, and the `fs.watch` API has extra
// restrictions which are best avoided
watchFile(readyFile, { persistent: false, interval: 1007 }, function(
current,
previous
) {
if (Number(current.mtime) === Number(previous.mtime)) {
// readyFile hasn't been modified, so ignore the event
return;
}
unwatchFile(readyFile);
readHandle.destroy();
exitHandle.destroy();
stdout = null;
resolve();
});
// The cbt tunnel outputs its startup error messages on stdout.
// Capture any data on stdout and display it if the process exits
// early.
const readHandle = on(child.stdout, 'data', (data: any) => {
stdout!.push(String(data));
});
const exitHandle = on(child, 'exit', function() {
process.stderr.write(stdout!.join(''));
});
this._handle = createCompositeHandle(readHandle, exitHandle);
executor(child, resolve, reject);
}, readyFile);
示例8: onModify
/** Calls the callback if the file is modified. */
onModify(callback: () => void) {
fs.watchFile(this.path, (curr, prev) => {
if (curr.mtime > prev.mtime) return callback();
});
}
示例9: require
const fs = require('fs');
// read and watch config json.
export let config = readConfig();
fs.watchFile('./config.json', function () {
config = readConfig();
console.info('*** CONFIG CHANGED ***');
console.log('New Config: ', config);
});
function readConfig() {
let cfg = JSON.parse(fs.readFileSync('./config.json'));
// now override settings with secrets from environment variables
cfg.azure.storageConnectionString = process.env.HOOPY_CONNECTION_STRING;
cfg.pushbullet.apiKey = process.env.HOOPY_PUSHBULLET_KEY;
cfg.hoopHouseThresholds.email = process.env.HOOPY_HOOP_HOUSE_EMAIL;
cfg.ifttt.key = process.env.HOOPY_IFTTT_KEY;
let channelMap = cfg.thingSpeak.channelMap;
channelMap.find((e) => e.name === 'tom-office').key = process.env.HOOPY_THINGSPEAK_OFFICE_KEY;
channelMap.find((e) => e.name === 'hoopy-inside').key = process.env.HOOPY_THINGSPEAK_HOOPHOUSE_KEY;
return cfg;
}
示例10: executeFile
console.log('File changed => Kill process: ', currentChild.pid);
currentChild.send('exit');
//currentChild.kill();
//currentChild.emit('exit');
//currentChild.stderr.removeAllListeners();
//currentChild.stdout.removeAllListeners();
//currentChild.removeAllListeners();
process.kill(currentChild.pid);
currentChild = undefined;
}
}
function executeFile() {
console.log('Executing file: ', filename);
currentChild = child_process.fork(filename); //'node ' +
//receive message from child;
//currentChild.on('message', value => {
// console.log('result emitted: ', value);
//});
}
fs.watchFile(filename, (curr, prev) => {
killChild();
if (fs.existsSync(filename)) executeFile();
});
executeFile();
process.on('exit', () => {
killChild();
});