1 path

  1. exports.resolveInclude = function(name, filename, isDir) {
  2. var dirname = path.dirname;
  3. var extname = path.extname;
  4. var resolve = path.resolve;
  5. var includePath = resolve(isDir ? filename : dirname(filename), name);
  6. var ext = extname(name);
  7. if (!ext) {
  8. includePath += '.ejs';
  9. }
  10. return includePath;
  11. };

path.dirname : 返回绝对路劲

path.extname: 文件扩展名

path.resolve: 对一系列路劲求绝对路劲,从右向左

2 fs

  1. if (options.filename) {
  2. filePath = exports.resolveInclude(path, options.filename);
  3. if (fs.existsSync(filePath)) {
  4. includePath = filePath;
  5. }
  6. }

fs.existsSync : Returns true if the path exists, false otherwise.

  1. if (fs.existsSync('/etc/passwd')) {
  2. console.log('The file exists.');
  3. }