Skip to content

Ensure direnv runs before everything else #12

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1,075 changes: 1,075 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publisher": "Rubymaniac",
"license": "MIT",
"engines": {
"vscode": "^1.0.0"
"vscode": "^1.1.34"
},
"bugs": {
"url": "https://github.com/rubymaniac/vscode-direnv/issues"
Expand All @@ -16,22 +16,26 @@
"url": "https://github.com/rubymaniac/vscode-direnv.git"
},
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
"vscode:prepublish": "./node_modules/typescript/bin/tsc -p ./",
"compile": "./node_modules/typescript/bin/tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"lint": "node ./node_modules/tslint/bin/tslint ./src/*.ts"
},
"devDependencies": {
"typescript": "^1.8.5",
"typescript": "^2.0.3",
"tslint": "^3.15.1",
"vscode": "^0.11.0"
"vsce": "^1.75.0",
"vscode": "^1.1.34",
"mocha": "^2.3.3",
"@types/node": "^6.0.40",
"@types/mocha": "^2.2.32"
},
"dependencies": {},
"categories": [
"Other"
],
"activationEvents": [
"*"
"onFileSystem:file"
],
"main": "./out/src/main",
"contributes": {
Expand All @@ -53,4 +57,4 @@
}
]
}
}
}
22 changes: 9 additions & 13 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import * as path from 'path';
import { exec, ExecOptions } from 'child_process';
import { execSync, ExecOptions } from 'child_process';
import * as constants from './constants';
import * as utils from './utils';

Expand All @@ -16,31 +16,27 @@ interface CommandExecOptions {
export class Command {
rootPath: string;
rcPath: string;
direnvExe: any;
constructor(rootPath: string) {
this.rootPath = rootPath;
this.rcPath = path.join(rootPath, `${constants.direnv.rc}`);
}
// Private methods
private exec(options: CommandExecOptions): Thenable<string> {
private exec(options: CommandExecOptions): string {
let direnvCmd = [constants.direnv.cmd, options.cmd].join(' ');
let execOptions: ExecOptions = {};
if (options.cwd == null || options.cwd) {
execOptions.cwd = this.rootPath;
}
return new Promise((resolve, reject) => {
exec(direnvCmd, execOptions, (err, stdout, stderr) => {
if (err) {
err.message = stderr;
reject(err);
} else {
resolve(stdout);
}
});
});
let result = execSync(direnvCmd, execOptions);
return result.toString();
}
// Public methods
version = () => this.exec({ cmd: 'version' });
allow = () => this.exec({ cmd: 'allow' });
deny = () => this.exec({ cmd: 'deny' });
exportJSON = () => this.exec({ cmd: 'export json' }).then((o) => o ? JSON.parse(o) : {});
exportJSON = () => {
let o = this.exec({ cmd: 'export json' });
return o ? JSON.parse(o) : {};
}
}
30 changes: 17 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,53 @@ let displayError = (e) =>
vscode.window.showErrorMessage(constants.messages.error(e));
let handleError = (t: Thenable<any>) => t.then(undefined, displayError);
let version = () =>
command.version().then(v => vscode.window.showInformationMessage(constants.messages.version(v)), displayError);
vscode.window.showInformationMessage(constants.messages.version(command.version()));
let revertFromOption = (option) => {
if (option === constants.vscode.extension.actions.revert) {
utils.assign(process.env, oldEnvDiff);
oldEnvDiff = {};
vscode.window.showInformationMessage(constants.messages.reverted);
}
};
let assignEnvDiff = (options: { showSuccess: boolean }) => {
return command.exportJSON().then((envDiff) => {
let assignEnvDiff = async (options: { showSuccess: boolean }) => {
try {
let envDiff = command.exportJSON()
Object.keys(envDiff).forEach((key) => {
if (key.indexOf('DIRENV_') === -1 && oldEnvDiff[key] !== envDiff[key]) {
oldEnvDiff[key] = process.env[key];
}
});
return utils.assign(process.env, envDiff);
}).then(() => {
utils.assign(process.env, envDiff);
if (options.showSuccess) {
return vscode.window.showInformationMessage(constants.messages.assign.success);
}
}, (err) => {
} catch (err) {
let optionP;
if (err.message.indexOf(`${constants.direnv.rc} is blocked`) !== -1) {
return vscode.window.showWarningMessage(constants.messages.assign.warn,
optionP = vscode.window.showWarningMessage(constants.messages.assign.warn,
constants.vscode.extension.actions.allow, constants.vscode.extension.actions.view);
} else {
return displayError(err);
optionP = displayError(err);
}
}).then((option) => {
let option = await optionP
if (option === constants.vscode.extension.actions.allow) {
return allow();
} else if (option === constants.vscode.extension.actions.view) {
return viewThenAllow();
}
});
}
};
let allow = () => {
return command.allow().then(() => assignEnvDiff({ showSuccess: true }), (err) => {
try {
command.allow();
return assignEnvDiff({ showSuccess: true });
} catch (err) {
if (err.message.indexOf(`${constants.direnv.rc} file not found`) !== -1) {
return vscode.commands.executeCommand(constants.vscode.commands.open, vscode.Uri.file(command.rcPath));
} else {
displayError(err);
return displayError(err);
}
});
};
};
let allowFromOption = (option) => {
if (option === constants.vscode.extension.actions.allow) {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"target": "es6",
"outDir": "out",
"noLib": true,
"sourceMap": true,
"lib": ["es6"],
"rootDir": "."
},
"exclude": [
Expand Down
1 change: 0 additions & 1 deletion typings/node.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion typings/vscode-typings.d.ts

This file was deleted.

Loading