How to use safefs - 10 common examples

To help you get started, we’ve selected a few safefs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bevry / watchr / source / index.js View on Github external
tasks.addTask('check if the file still exists', complete => {
			// Log
			this.log(
				'debug',
				`watch evaluating on: ${this.path} [state: ${this.state}]`
			)

			// Check if this is still needed
			if (this.state !== 'active') {
				this.log('debug', `watch discarded on: ${this.path}`)
				tasks.clearRemaining()
				return complete()
			}

			// Check if the file still exists
			fsUtil.exists(this.path, exists => {
				// Apply local global property
				previousStat = this.stat

				// If the file still exists, then update the stat
				if (exists === false) {
					// Log
					this.log('debug', `watch emit delete: ${this.path}`)

					// Apply
					this.stat = null
					this.close('deleted')
					this.emit('change', 'delete', this.path, null, previousStat)

					// Clear the remaining tasks, as they are no longer needed
					tasks.clearRemaining()
					return complete()
github bevry / watchr / source / index.js View on Github external
if (this.state !== 'active') return this

		// Close
		this.log('debug', `close: ${this.path}`)

		// Close our children
		eachr(this.children, child => {
			child.close(reason)
		})

		// Close watch listener
		if (this.fswatcher != null) {
			this.fswatcher.close()
			this.fswatcher = null
		} else {
			fsUtil.unwatchFile(this.path)
		}

		// Updated state
		if (reason === 'deleted') {
			this.state = 'deleted'
		} else {
			this.state = 'closed'
		}

		// Emit our close event
		this.log('debug', `watch closed because ${reason} on ${this.path}`)
		this.emit('close', reason)

		// Chain
		return this
	}
github bevry / watchr / source / index.js View on Github external
// Set this sub group to execute in parallel
			this.setConfig({ concurrency: 0 })

			// So let's check if we are a directory
			if (currentStat.isDirectory() === false) {
				// If we are a file, lets simply emit the change event
				this.log('debug', `watch emit update: ${this.path}`)
				this.emit('change', 'update', this.path, currentStat, previousStat)
				return done()
			}

			// We are a direcotry
			// Chances are something actually happened to a child (rename or delete)
			// and if we are the same, then we should scan our children to look for renames and deletes
			fsUtil.readdir(this.path, (err, newFileRelativePaths) => {
				// Error?
				if (err) return done(err)

				// Log
				this.log('debug', `watch read dir: ${this.path}`, newFileRelativePaths)

				// Find deleted files
				eachr(this.children, (child, childFileRelativePath) => {
					// Skip if the file still exists
					if (newFileRelativePaths.indexOf(childFileRelativePath) !== -1) return

					// Fetch full path
					const childFileFullPath = pathUtil.join(
						this.path,
						childFileRelativePath
					)
github bevry-archive / feedr / source / index.js View on Github external
function readFile(path, readFileComplete) {
			// Log
			me.log(
				'debug',
				`Feedr === reading [${feed.url}] on [${path}], checking exists`
			)

			// Check the the file exists
			safefs.exists(path, function(exists) {
				// Check it exists
				if (!exists) {
					// Log
					me.log(
						'debug',
						`Feedr === reading [${feed.url}] on [${path}], it doesn't exist`
					)

					// Exit
					readFileComplete()
					return
				}

				// Log
				me.log(
					'debug',
github bevry-archive / feedr / source / index.js View on Github external
`Feedr === reading [${feed.url}] on [${path}], it doesn't exist`
					)

					// Exit
					readFileComplete()
					return
				}

				// Log
				me.log(
					'debug',
					`Feedr === reading [${feed.url}] on [${path}], it exists, now reading`
				)

				// It does exist, so let's continue to read the cached fie
				safefs.readFile(path, null, function(err, rawData) {
					// Check
					if (err) {
						// Log
						me.log(
							'debug',
							`Feedr === reading [${feed.url}] on [${path}], it exists, read failed`,
							err.stack
						)

						// Exit
						readFileComplete(err)
						return
					}

					// Log
					me.log(
github bevry / watchr / source / index.js View on Github external
watchMethod(method /* :MethodEnum */, next /* :ErrorCallback */) /* :void */ {
		if (method === 'watch') {
			// Check
			if (fsUtil.watch == null) {
				const err = new Error(
					'watch method is not supported on this environment, fs.watch does not exist'
				)
				next(err)
				return
			}

			// Watch
			try {
				this.fswatcher = fsUtil.watch(this.path, (...args) =>
					this.listener({ method, args })
				)
				// must pass the listener here instead of doing fswatcher.on('change', opts.listener)
				// as the latter is not supported on node 0.6 (only 0.8+)
			} catch (err) {
				next(err)
				return
			}

			// Success
			next()
			return
		} else if (method === 'watchFile') {
			// Check
			if (fsUtil.watchFile == null) {
				const err = new Error(
github bevry / watchr / source / index.js View on Github external
// Success
			next()
			return
		} else if (method === 'watchFile') {
			// Check
			if (fsUtil.watchFile == null) {
				const err = new Error(
					'watchFile method is not supported on this environment, fs.watchFile does not exist'
				)
				next(err)
				return
			}

			// Watch
			try {
				fsUtil.watchFile(
					this.path,
					{
						persistent: this.config.persistent,
						interval: this.config.interval
					},
					(...args) => this.listener({ method, args })
				)
			} catch (err) {
				next(err)
				return
			}

			// Success
			next()
			return
		} else {
github bevry / projectz / source / lib / projectz.js View on Github external
tasks.addTask(complete => {
			readdir(this.cwd, (err, files) => {
				if (err) return complete(err)
				files.forEach(file => {
					const filePath = join(this.cwd, file)

					packages.forEach(key => {
						const basename = file
							.toLowerCase()
							.split('.')
							.slice(0, -1)
							.join('.')
						if (basename === key) {
							const message = `Reading package file: ${filePath}`
							tasks.addTask(message, complete => {
								this.log('info', message)
								CSON.readFile(filePath, (err, data) => {
									if (err) return complete(err)
github bevry / projectz / source / lib / projectz.js View on Github external
tasks.addTask(message, complete => {
				this.log('info', message)
				const data = this.dataForReadmeFilesEnhanced[name]
				writeFile(filepath, data, complete)
			})
		})
github bevry-archive / feedr / source / index.js View on Github external
writeTasks.addTask('store the meta data in a cache somewhere', function(
				writeTaskComplete
			) {
				const writeData = JSON.stringify(
					{
						headers: response.headers,
						parse: feed.parse
					},
					null,
					'  '
				)
				safefs.writeFile(feed.metaPath, writeData, writeTaskComplete)
			})

safefs

Stop getting EMFILE errors! Open only as many files as the operating system supports.

Artistic-2.0
Latest version published 3 months ago

Package Health Score

75 / 100
Full package analysis