Skip to content

Commit

Permalink
Closes #15. Support loading .jsbeautifyrc from Project directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Jun 13, 2014
1 parent db8a606 commit 4fdab97
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 37 deletions.
118 changes: 82 additions & 36 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var path = require('path');
var nopt = require('nopt');
var extend = require('extend');
var _ = require('lodash');
var shjs = require('shelljs');

// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
Expand Down Expand Up @@ -49,24 +50,8 @@ var Subscriber = require('emissary').Subscriber;
var plugin = module.exports;
Subscriber.extend(plugin);

function verifyExists(fullPath) {
return fs.existsSync(fullPath) ? fullPath : null;
}

function findRecursive(dir, fileName) {
var fullPath = path.join(dir, fileName);
var nextDir = path.dirname(dir);
var result = verifyExists(fullPath);

if (!result && (nextDir !== dir)) {
result = findRecursive(nextDir, fileName);
}

return result;
}

function getUserHome() {
return process.env.HOME || process.env.USERPROFILE;
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}

function cleanOptions(data, types) {
Expand Down Expand Up @@ -96,7 +81,75 @@ function setCursors(editor, posArray) {
}
}

// Storage for memoized results from find file
// Should prevent lots of directory traversal &
// lookups when liniting an entire project
var findFileResults = {};

/**
* Searches for a file with a specified name starting with
* 'dir' and going all the way up either until it finds the file
* or hits the root.
*
* @param {string} name filename to search for (e.g. .jshintrc)
* @param {string} dir directory to start search from (default:
* current working directory)
*
* @returns {string} normalized filename
*/
function findFile(name, dir) {
dir = dir || process.cwd();

var filename = path.normalize(path.join(dir, name));
if (findFileResults[filename] !== undefined) {
return findFileResults[filename];
}

var parent = path.resolve(dir, '../');

if (shjs.test('-e', filename)) {
findFileResults[filename] = filename;
return filename;
}

if (dir === parent) {
findFileResults[filename] = null;
return null;
}

return findFile(name, parent);
}

/**
* Tries to find a configuration file in either project directory
* or in the home directory. Configuration files are named
* '.jshintrc'.
*
* @param {string} config name of the configuration file
* @param {string} file path to the file to be linted
* @returns {string} a path to the config file
*/
function findConfig(config, file) {
var dir = path.dirname(path.resolve(file));
var envs = getUserHome();
var home = path.normalize(path.join(envs, config));

var proj = findFile(config, dir);
if (proj) {
return proj;
}

if (shjs.test('-e', home)) {
return home;
}

return null;
}

function beautify() {

console.log('Beautify!!!');

var text;
var editor = atom.workspace.getActiveEditor();
var isSelection = !! editor.getSelectedText();
Expand All @@ -111,27 +164,20 @@ function beautify() {

// Look for .jsbeautifierrc in file and home path, check env variables
var editedFilePath = editor.getPath();
var rcInRecursiveCwd;
if (editedFilePath && (rcInRecursiveCwd = findRecursive(path.dirname(
editedFilePath), '.jsbeautifyrc')) === editedFilePath) {
rcInRecursiveCwd = null;
}
var rcInHomePath;
if (editedFilePath && (rcInHomePath = verifyExists(path.join(getUserHome() ||
'', '.jsbeautifyrc'))) === editedFilePath) {
rcInHomePath = null;
}

var externalOptions;
// Get the path to the config file
var configPath = findConfig('.jsbeautifyrc', editedFilePath);

if (rcInRecursiveCwd) {
externalOptions = JSON.parse(fs.readFileSync(rcInRecursiveCwd, {
encoding: 'utf8'
}));
} else if (rcInHomePath) {
externalOptions = JSON.parse(fs.readFileSync(rcInHomePath, {
encoding: 'utf8'
}));
var externalOptions;
if (configPath) {
var strip = require('strip-json-comments');
try {
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
encoding: 'utf8'
})));
} catch (e) {
externalOptions = {};
}
} else {
externalOptions = {};
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"extend": "^1.2.1",
"js-beautify": "~1.4.2",
"nopt": "^2.2.1",
"lodash": "2.4.1"
"lodash": "2.4.1",
"shelljs": "^0.3.0",
"strip-json-comments": "^0.1.3"
}
}

0 comments on commit 4fdab97

Please sign in to comment.