Skip to content

CB-12528 use fs instead of our own existsSync #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cordova-lib/spec-cordova/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('util module', function() {
process.chdir(cwd);
});
function removeDir(directory) {
shell.rm('-rf', directory);
shell.rm('-rf', directory);
}
it('Test 001 : should return false if it hits the home directory', function() {
var somedir = path.join(home, 'somedir');
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('util module', function() {
});

it('Test 020 : should pick buildConfig if no option is provided, but buildConfig.json exists', function() {
spyOn(util, 'existsSync').and.returnValue(true);
spyOn(fs, 'existsSync').and.returnValue(true);
// Using path.join below to normalize path separators
expect(util.preProcessOptions())
.toEqual(jasmine.objectContaining({options: {buildConfig: path.join('/fake/path/build.json')}}));
Expand Down
35 changes: 12 additions & 23 deletions cordova-lib/src/cordova/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var lib_path = path.join(global_config_path, 'lib');
exports.binname = 'cordova';
exports.globalConfig = global_config_path;

// defer defining libDirectory on exports so we don't create it if
// defer defining libDirectory on exports so we don't create it if
// someone simply requires this module
Object.defineProperty(exports,'libDirectory', {
configurable: true,
Expand Down Expand Up @@ -75,42 +75,31 @@ exports.isUrl = isUrl;
exports.getLatestMatchingNpmVersion = getLatestMatchingNpmVersion;
exports.getAvailableNpmVersions = getAvailableNpmVersions;
exports.getInstalledPlatformsWithVersions = getInstalledPlatformsWithVersions;
exports.existsSync = existsSync;


function isUrl(value) {
var u = value && url.parse(value);
return !!(u && u.protocol && u.protocol.length > 2); // Account for windows c:/ paths
}

function isRootDir(dir) {
if (exports.existsSync(path.join(dir, 'www'))) {
if (exports.existsSync(path.join(dir, 'config.xml'))) {
if (fs.existsSync(path.join(dir, 'www'))) {
if (fs.existsSync(path.join(dir, 'config.xml'))) {
// For sure is.
if (exports.existsSync(path.join(dir, 'platforms'))) {
if (fs.existsSync(path.join(dir, 'platforms'))) {
return 2;
} else {
return 1;
}
}
// Might be (or may be under platforms/).
if (exports.existsSync(path.join(dir, 'www', 'config.xml'))) {
if (fs.existsSync(path.join(dir, 'www', 'config.xml'))) {
return 1;
}
}
return 0;
}

function existsSync(fileSpec) {
// Since fs.existsSync() is deprecated
try {
fs.statSync(fileSpec);
return true;
} catch (error) {
return false;
}
}


// Runs up the directory chain looking for a .cordova directory.
// IF it is found we are in a Cordova project.
// Omit argument to use CWD.
Expand Down Expand Up @@ -181,7 +170,7 @@ function fixRelativePath(value, /* optional */ cwd) {

// Resolve any symlinks in order to avoid relative path issues. See https://issues.apache.org/jira/browse/CB-8757
function convertToRealPathSafe(path) {
if (path && exports.existsSync(path)) {
if (path && fs.existsSync(path)) {
return fs.realpathSync(path);
}

Expand All @@ -204,7 +193,7 @@ function deleteSvnFolders(dir) {
function listPlatforms(project_dir) {
var core_platforms = require('../platforms/platforms');
var platforms_dir = path.join(project_dir, 'platforms');
if ( !exports.existsSync(platforms_dir)) {
if ( !fs.existsSync(platforms_dir)) {
return [];
}
var subdirs = fs.readdirSync(platforms_dir);
Expand Down Expand Up @@ -235,7 +224,7 @@ function findPlugins(pluginPath) {
var plugins = [],
stats;

if (exports.existsSync(pluginPath)) {
if (fs.existsSync(pluginPath)) {
plugins = fs.readdirSync(pluginPath).filter(function (fileName) {
stats = fs.statSync(path.join(pluginPath, fileName));
return fileName != '.svn' && fileName != 'CVS' && stats.isDirectory();
Expand All @@ -256,9 +245,9 @@ function projectWww(projectDir) {
function projectConfig(projectDir) {
var rootPath = path.join(projectDir, 'config.xml');
var wwwPath = path.join(projectDir, 'www', 'config.xml');
if (exports.existsSync(rootPath)) {
if (fs.existsSync(rootPath)) {
return rootPath;
} else if (exports.existsSync(wwwPath)) {
} else if (fs.existsSync(wwwPath)) {
return wwwPath;
}
return false;
Expand Down Expand Up @@ -295,7 +284,7 @@ function preProcessOptions (inputOptions) {
result.platforms = projectPlatforms;
}

if (!result.options.buildConfig && exports.existsSync(path.join(projectRoot, 'build.json'))) {
if (!result.options.buildConfig && fs.existsSync(path.join(projectRoot, 'build.json'))) {
result.options.buildConfig = path.join(projectRoot, 'build.json');
}

Expand Down
3 changes: 2 additions & 1 deletion cordova-lib/src/util/npm-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

var npm = require('npm'),
path = require('path'),
fs = require('fs'),
Q = require('q'),
unpack = require('./unpack'),
util = require('../cordova/util'),
Expand Down Expand Up @@ -103,7 +104,7 @@ function cachePackage(packageName, packageVersion) {
// If already cached, use that rather than calling 'npm cache add' again.
var packageCacheDir = path.resolve(cacheDir, packageName, packageVersion);
var packageTGZ = path.resolve(packageCacheDir, 'package.tgz');
if (util.existsSync(packageTGZ)) {
if (fs.existsSync(packageTGZ)) {
return unpack.unpackTgz(packageTGZ, path.resolve(packageCacheDir, 'package'));
}

Expand Down