Skip to content

Commit 66524b4

Browse files
committed
Merge pull request #22 from firebase/tml-exist2
Stop using deprecated fs.existsSync method
2 parents ba6c95d + 939d31c commit 66524b4

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

bin/firebase

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var errorOut = require('../lib/errorOut');
1010
var winston = require('winston');
1111
var logger = require('../lib/logger');
1212
var fs = require('fs');
13+
var fsutils = require('../lib/fsutils');
1314
var path = require('path');
1415
var chalk = require('chalk');
1516
var configstore = require('../lib/configstore');
@@ -53,7 +54,7 @@ require('../lib/fetchMOTD')();
5354

5455
process.on('exit', function(code) {
5556
code = process.exitCode || code;
56-
if (!debugging && code < 2 && fs.existsSync(logFilename)) {
57+
if (!debugging && code < 2 && fsutils.fileExistsSync(logFilename)) {
5758
fs.unlinkSync(logFilename);
5859
}
5960

commands/init.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var Command = require('../lib/command');
33
var Config = require('../lib/config');
44
var prompt = require('../lib/prompt');
55
var fs = require('fs-extra');
6+
var fsutils = require('../lib/fsutils');
67
var path = require('path');
78
var defaultConfig = require('../templates/firebase.json');
89
var _ = require('lodash');
@@ -90,7 +91,7 @@ module.exports = new Command('init')
9091
return RSVP.resolve();
9192
}
9293
var absPath = path.resolve(cwd, options.public || '.');
93-
if (!fs.existsSync(absPath)) {
94+
if (!fsutils.dirExistsSync(absPath)) {
9495
fs.mkdirsSync(absPath);
9596
logger.info(chalk.green('✔ '), 'Public directory', chalk.bold(options.public), 'has been created');
9697
}

lib/config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var FirebaseError = require('./error');
55
var _ = require('lodash');
66
var resolveProjectPath = require('./resolveProjectPath');
77
var path = require('path');
8-
var fs = require('fs');
8+
var fsutils = require('./fsutils');
99
var loadCJSON = require('./loadCJSON');
1010
var parseBoltRules = require('./parseBoltRules');
1111
var triggerRulesGenerator = require('./triggerRulesGenerator');
@@ -86,7 +86,7 @@ Config.prototype._materialize = function(target) {
8686
Config.prototype._parseFile = function(target, filePath) {
8787
var fullPath = resolveProjectPath(this.options.cwd, filePath);
8888
var ext = path.extname(filePath);
89-
if (!fs.existsSync(fullPath)) {
89+
if (!fsutils.fileExistsSync(fullPath)) {
9090
throw new FirebaseError('Parse Error: Imported file ' + filePath + ' does not exist', {exit: 1});
9191
}
9292

lib/deploy/functions/prepare.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var _ = require('lodash');
44
var cjson = require('cjson');
55
var fs = require('fs');
6+
var fsutils = require('../../fsutils');
67
var path = require('path');
78
var resolveProjectPath = require('../../resolveProjectPath');
89
var RSVP = require('rsvp');
@@ -17,7 +18,7 @@ module.exports = function(context, options, payload) {
1718
if (payload.functions) {
1819
if (!_.has(payload.functions, '.source')) {
1920
return utils.reject('No .source directory specified under "functions", can\'t deploy Google Cloud Functions', {exit: 1});
20-
} else if (!fs.existsSync(resolveProjectPath(options.cwd, payload.functions['.source']))) {
21+
} else if (!fsutils.dirExistsSync(resolveProjectPath(options.cwd, payload.functions['.source']))) {
2122
return utils.reject('.source directory specified under "functions" does not exist, can\'t deploy Google Cloud Functions', {exit: 1});
2223
}
2324

@@ -33,17 +34,17 @@ module.exports = function(context, options, payload) {
3334
var sourceDirName = payload.functions['.source'];
3435
var sourceDir = options.config.path(sourceDirName);
3536
var packageJsonFile = path.join(sourceDir, 'package.json');
36-
if (fs.existsSync(packageJsonFile)) {
37+
if (fsutils.fileExistsSync(packageJsonFile)) {
3738
try {
3839
var data = cjson.load(packageJsonFile);
3940
var indexJsFile = path.join(sourceDir, data.main || 'index.js');
40-
if (!fs.existsSync(indexJsFile)) {
41+
if (!fsutils.fileExistsSync(indexJsFile)) {
4142
return utils.reject(path.relative(options.config.projectDir, indexJsFile) + ' does not exist, can\'t deploy Google Cloud Functions', {exit: 1});
4243
}
4344
} catch (e) {
4445
return utils.reject('There was an error reading ' + sourceDirName + path.sep + 'package.json:\n\n' + e.message, {exit: 1});
4546
}
46-
} else if (!fs.existsSync(path.join(sourceDir, 'function.js'))) {
47+
} else if (!fsutils.fileExistsSync(path.join(sourceDir, 'function.js'))) {
4748
return utils.reject('No npm package found in functions .source directory. Please run \'npm init\' inside ' + sourceDirName, {exit: 1});
4849
}
4950
}

lib/deploy/hosting/prepare.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var _ = require('lodash');
44
var RSVP = require('rsvp');
55
var resolveProjectPath = require('../../resolveProjectPath');
6-
var fs = require('fs');
6+
var fsutils = require('../../fsutils');
77
var utils = require('../../utils');
88

99
module.exports = function(context, options, payload) {
@@ -24,7 +24,7 @@ module.exports = function(context, options, payload) {
2424

2525
if (!_.has(payload, 'hosting.public')) {
2626
return utils.reject('No public directory specified, can\'t deploy hosting', {exit: 1});
27-
} else if (!fs.existsSync(resolveProjectPath(options.cwd, payload.hosting.public))) {
27+
} else if (!fsutils.dirExistsSync(resolveProjectPath(options.cwd, payload.hosting.public))) {
2828
return utils.reject('Specified public directory does not exist, can\'t deploy hosting', {exit: 1});
2929
}
3030

lib/detectProjectRoot.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
var fs = require('fs');
3+
var fsutils = require('./fsutils');
44
var path = require('path');
55

66
module.exports = function(cwd) {
77
var projectRootDir = cwd || process.cwd();
8-
while (!fs.existsSync(path.resolve(projectRootDir, './firebase.json'))) {
8+
while (!fsutils.fileExistsSync(path.resolve(projectRootDir, './firebase.json'))) {
99
var parentDir = path.dirname(projectRootDir);
1010
if (parentDir === projectRootDir) {
1111
return null;

lib/fsutils.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
5+
module.exports = {
6+
fileExistsSync: function(path) {
7+
try {
8+
var stats = fs.lstatSync(path);
9+
return stats.isFile();
10+
} catch (e) {
11+
return false;
12+
}
13+
},
14+
dirExistsSync: function(path) {
15+
try {
16+
var stats = fs.lstatSync(path);
17+
return stats.isDirectory();
18+
} catch (e) {
19+
return false;
20+
}
21+
}
22+
};

0 commit comments

Comments
 (0)