Skip to content

Commit

Permalink
Workaround requireg usage from vscode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Mar 21, 2019
1 parent 7ed1576 commit dff3734
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
import { workspace, Uri } from 'vscode';
import { basename } from 'path';
import { basename, dirname, join } from 'path';
import {
PrettierVSCodeConfig,
Prettier,
PrettierSupportInfo,
ParserOption,
} from './types.d';
import { requireLocalPkg } from './requirePkg';
import { execSync } from 'child_process';

const requireGlobal = require('requireg');
const bundledPrettier = require('prettier') as Prettier;

let globalNodeExecPath: string | undefined;

try {
globalNodeExecPath = execSync(
'node --eval "process.stdout.write(process.execPath)"'
).toString();
} catch (error) {
// Not installed
}

/**
* Require global prettier or undefined if none is installed
*/
export function requireGlobalPrettier(): Prettier | undefined {
if (!globalNodeExecPath) {
return;
}

// The global node is different from the one vscode extensions executes in.
// So workaround it by setting NODE_PATH using the process.execPath from the
// global node installation
const origNodePath = process.env.NODE_PATH;
process.env.NODE_PATH = join(dirname(globalNodeExecPath), 'node_modules');

try {
return requireGlobal('prettier', true);
} catch (error) {}
} catch (error) {
// No global installed
} finally {
process.env.NODE_PATH = origNodePath;
}
}

export function getConfig(uri?: Uri): PrettierVSCodeConfig {
Expand Down

0 comments on commit dff3734

Please sign in to comment.