Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
Add option to resolve elm-format binary from local project directory
Browse files Browse the repository at this point in the history
  • Loading branch information
MethodGrab committed Oct 9, 2018
1 parent 65c9824 commit 766a8fb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use babel';

import fs from 'fs';
import path from 'path';
import open from 'opn';

export const installInstructions = {
Expand All @@ -11,3 +13,27 @@ export const installInstructions = {
text: 'Open instructions',
}],
};

const findElmFormatBinary = (dir) => {
const elmFormatFile = path.join(dir, 'node_modules', '.bin', 'elm-format');

try {
fs.accessSync(elmFormatFile, fs.constants.F_OK);
return elmFormatFile;
} catch (e) {
return false;
}
};

export const resolveLocalBinary = () => {
const projectPaths = atom.project.getPaths();
const results = projectPaths
.map(findElmFormatBinary)
.filter(a => a !== false);

if (results && results[0]) {
return results[0];
}

return false;
};
23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
import childProcess from 'child_process';
import fs from 'fs';
import config from './settings';
import { installInstructions } from './helpers';
import { installInstructions, resolveLocalBinary } from './helpers';

export default {
config,
Expand Down Expand Up @@ -82,12 +82,27 @@ export default {
try {
// Reset the error tracker.
this.errorLineNum = null;
const binary = atom.config.get('elm-format.binary');
const elmVersion = atom.config.get('elm-format.elmVersion') || '0.19';
let binary = atom.config.get('elm-format.binary');
let elmVersion = atom.config.get('elm-format.elmVersion') || '0.19';

if (atom.config.get('elm-format.preferLocalBinary')) {
const localBinary = resolveLocalBinary();

if (localBinary) {
binary = localBinary;
elmVersion = false;
}
}

const args = ['--stdin'];

if (elmVersion) {
args.push('--elm-version', elmVersion);
}

const { status, stdout, stderr } = childProcess.spawnSync(
binary,
['--elm-version', elmVersion, '--stdin'], { input: editor.getText() });
args, { input: editor.getText() });

switch (status) {
case 0: {
Expand Down
7 changes: 7 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export default {
description: 'Path for elm-format',
type: 'string',
default: 'elm-format',
order: 7,
},
preferLocalBinary: {
title: 'Prefer local binary',
description: 'Try to resolve the binary from the current project\'s directories if possible',
type: 'boolean',
default: false,
order: 6,
},
elmVersion: {
Expand Down

0 comments on commit 766a8fb

Please sign in to comment.