UNPKG

791 BJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4var path = require('path');
5var exists = require('fs-exists-sync');
6
7/**
8 * Return true if a file exists
9 *
10 * ```js
11 * <%= exists("foo.js") %>
12 * ```
13 *
14 * @param {String} `filepath` Path of the file to check.
15 * @return {Boolean} True if the file exists
16 * @api public
17 */
18
19exports.exists = function(filepath) {
20 return filepath && exists(filepath);
21};
22
23/**
24 * Read a file from the file system and inject its content
25 *
26 * ```js
27 * <%= read("foo.js") %>
28 * ```
29 *
30 * @param {String} `filepath` Path of the file to read.
31 * @return {String} Contents of the given file.
32 * @api public
33 */
34
35exports.read = function read(filepath) {
36 filepath = path.resolve(filepath);
37 if (!exists(filepath)) return '';
38 return fs.readFileSync(filepath, 'utf8');
39};