Skip to content

Commit

Permalink
Merge pull request #86 from AtomLinter/steelbrain/use-consistent-path
Browse files Browse the repository at this point in the history
Use consistent-path
  • Loading branch information
steelbrain committed Dec 30, 2015
2 parents 02a08bd + 970370f commit 662544a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Upcoming

* Use `consistent-path` package to determine `$PATH` correctly on OSX

### 4.1.1

* Export `FindCache`, now you can do `Helper.FindCache.clear()` to clear find cache
Expand Down
48 changes: 25 additions & 23 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ var _atom = require('atom');

var _path = require('path');

var _path2 = _interopRequireDefault(_path);
var Path = _interopRequireWildcard(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);
var FS = _interopRequireWildcard(_fs);

var _tmp = require('tmp');

var _tmp2 = _interopRequireDefault(_tmp);
var TMP = _interopRequireWildcard(_tmp);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _consistentPath = require('consistent-path');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

let XRegExp = null;
const EventsCache = new WeakMap();
Expand All @@ -46,15 +48,15 @@ const assign = Object.assign || function (target, source) {

function _exec(command, args, opts, isNode) {
const options = assign({
env: process.env,
stream: 'stdout',
throwOnStdErr: true
}, opts);

if (isNode) {
const env = assign({}, process.env);
delete env.OS;
assign(options, { env: env });
delete options.env.OS;
}
assign(options.env, { PATH: (0, _consistentPath.getPath)() });

return new Promise(function (resolve, reject) {
const data = { stdout: [], stderr: [] };
Expand Down Expand Up @@ -204,26 +206,26 @@ function createElement(name) {
function findAsync(directory, name) {
validate_find(directory, name);
const names = name instanceof Array ? name : [name];
const chunks = directory.split(_path2.default.sep);
const chunks = directory.split(Path.sep);
let promise = Promise.resolve(null);

while (chunks.length) {
let currentDir = chunks.join(_path2.default.sep);
let currentDir = chunks.join(Path.sep);
if (currentDir === '') {
currentDir = _path2.default.resolve(directory, '/');
currentDir = Path.resolve(directory, '/');
}
promise = promise.then(function (filePath) {
if (filePath !== null) {
return filePath;
}
return names.reduce(function (promise, name) {
const currentFile = _path2.default.join(currentDir, name);
const currentFile = Path.join(currentDir, name);
return promise.then(function (filePath) {
if (filePath !== null) {
return filePath;
}
return new Promise(function (resolve) {
_fs2.default.access(currentFile, _fs2.default.R_OK, function (error) {
FS.access(currentFile, FS.R_OK, function (error) {
if (error) {
resolve(null);
} else resolve(currentFile);
Expand All @@ -245,7 +247,7 @@ function findCachedAsync(directory, name) {
if (FindCache.has(cacheKey)) {
const cachedFilePath = FindCache.get(cacheKey);
return new Promise(function (resolve, reject) {
_fs2.default.access(cachedFilePath, _fs2.default.R_OK, function (error) {
FS.access(cachedFilePath, FS.R_OK, function (error) {
if (error) {
FindCache.delete(cacheKey);
resolve(findCachedAsync(directory, names));
Expand All @@ -263,18 +265,18 @@ function findCachedAsync(directory, name) {
function find(directory, name) {
validate_find(directory, name);
const names = name instanceof Array ? name : [name];
const chunks = directory.split(_path2.default.sep);
const chunks = directory.split(Path.sep);

while (chunks.length) {
let currentDir = chunks.join(_path2.default.sep);
let currentDir = chunks.join(Path.sep);
if (currentDir === '') {
currentDir = _path2.default.resolve(directory, '/');
currentDir = Path.resolve(directory, '/');
}
for (const fileName of names) {
const filePath = _path2.default.join(currentDir, fileName);
const filePath = Path.join(currentDir, fileName);

try {
_fs2.default.accessSync(filePath, _fs2.default.R_OK);
FS.accessSync(filePath, FS.R_OK);
return filePath;
} catch (_) {}
}
Expand All @@ -291,7 +293,7 @@ function findCached(directory, name) {
if (FindCache.has(cacheKey)) {
const cachedFilePath = FindCache.get(cacheKey);
try {
_fs2.default.accessSync(cachedFilePath, _fs2.default.R_OK);
FS.accessSync(cachedFilePath, FS.R_OK);
return cachedFilePath;
} catch (_) {
FindCache.delete(cacheKey);
Expand All @@ -314,20 +316,20 @@ function tempFile(fileName, fileContents, callback) {
}

return new Promise(function (resolve, reject) {
_tmp2.default.dir({
TMP.dir({
prefix: 'atom-linter_'
}, function (error, directory, directoryCleanup) {
if (error) {
return reject(error);
}
const filePath = _path2.default.join(directory, fileName);
_fs2.default.writeFile(filePath, fileContents, function (error) {
const filePath = Path.join(directory, fileName);
FS.writeFile(filePath, fileContents, function (error) {
if (error) {
directoryCleanup();
return reject(error);
}
function fileCleanup() {
_fs2.default.unlink(filePath, function () {
FS.unlink(filePath, function () {
directoryCleanup();
});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"homepage": "https://github.com/AtomLinter/atom-linter#readme",
"dependencies": {
"xregexp": "^3.0.0",
"tmp": "latest"
"tmp": "latest",
"consistent-path": "^1.0.1"
},
"devDependencies": {
"babel-preset-steelbrain": "^1.0.0"
Expand Down
13 changes: 7 additions & 6 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use babel'

import {BufferedProcess, BufferedNodeProcess} from 'atom'
import Path from 'path'
import FS from 'fs'
import TMP from 'tmp'
import * as Path from 'path'
import * as FS from 'fs'
import * as TMP from 'tmp'
import {getPath} from 'consistent-path'

let XRegExp = null
const EventsCache = new WeakMap()
Expand All @@ -19,15 +20,15 @@ const assign = Object.assign || function(target, source) {

function _exec(command, args, opts, isNode) {
const options = assign({
env: process.env,
stream: 'stdout',
throwOnStdErr: true
}, opts)

if (isNode) {
const env = assign({}, process.env)
delete env.OS
assign(options, {env})
delete options.env.OS
}
assign(options.env, {PATH: getPath()})

return new Promise(function(resolve, reject) {
const data = {stdout: [], stderr: []}
Expand Down

0 comments on commit 662544a

Please sign in to comment.