Allow configurable npmLocation for typingsInstaller (#16084)

* Allow configurable npmLocation for typingsInstaller

* Undo "export class" changes

* Add log for npmLocation

* Log whether '--npmLocation' was provided
This commit is contained in:
Andy 2017-05-26 07:06:11 -07:00 committed by GitHub
parent 068256b8b0
commit 2412f8c6cf
3 changed files with 34 additions and 20 deletions

View file

@ -13,6 +13,7 @@ namespace ts.server {
globalTypingsCacheLocation: string; globalTypingsCacheLocation: string;
logger: Logger; logger: Logger;
typingSafeListLocation: string; typingSafeListLocation: string;
npmLocation: string | undefined;
telemetryEnabled: boolean; telemetryEnabled: boolean;
globalPlugins: string[]; globalPlugins: string[];
pluginProbeLocations: string[]; pluginProbeLocations: string[];
@ -234,6 +235,7 @@ namespace ts.server {
eventPort: number, eventPort: number,
readonly globalTypingsCacheLocation: string, readonly globalTypingsCacheLocation: string,
readonly typingSafeListLocation: string, readonly typingSafeListLocation: string,
private readonly npmLocation: string | undefined,
private newLine: string) { private newLine: string) {
this.throttledOperations = new ThrottledOperations(host); this.throttledOperations = new ThrottledOperations(host);
if (eventPort) { if (eventPort) {
@ -278,19 +280,21 @@ namespace ts.server {
if (this.typingSafeListLocation) { if (this.typingSafeListLocation) {
args.push(Arguments.TypingSafeListLocation, this.typingSafeListLocation); args.push(Arguments.TypingSafeListLocation, this.typingSafeListLocation);
} }
if (this.npmLocation) {
args.push(Arguments.NpmLocation, this.npmLocation);
}
const execArgv: string[] = []; const execArgv: string[] = [];
{ for (const arg of process.execArgv) {
for (const arg of process.execArgv) { const match = /^--(debug|inspect)(=(\d+))?$/.exec(arg);
const match = /^--(debug|inspect)(=(\d+))?$/.exec(arg); if (match) {
if (match) { // if port is specified - use port + 1
// if port is specified - use port + 1 // otherwise pick a default port depending on if 'debug' or 'inspect' and use its value + 1
// otherwise pick a default port depending on if 'debug' or 'inspect' and use its value + 1 const currentPort = match[3] !== undefined
const currentPort = match[3] !== undefined ? +match[3]
? +match[3] : match[1] === "debug" ? 5858 : 9229;
: match[1] === "debug" ? 5858 : 9229; execArgv.push(`--${match[1]}=${currentPort + 1}`);
execArgv.push(`--${match[1]}=${currentPort + 1}`); break;
break;
}
} }
} }
@ -389,10 +393,10 @@ namespace ts.server {
class IOSession extends Session { class IOSession extends Session {
constructor(options: IOSessionOptions) { constructor(options: IOSessionOptions) {
const { host, installerEventPort, globalTypingsCacheLocation, typingSafeListLocation, canUseEvents } = options; const { host, installerEventPort, globalTypingsCacheLocation, typingSafeListLocation, npmLocation, canUseEvents } = options;
const typingsInstaller = disableAutomaticTypingAcquisition const typingsInstaller = disableAutomaticTypingAcquisition
? undefined ? undefined
: new NodeTypingsInstaller(telemetryEnabled, logger, host, installerEventPort, globalTypingsCacheLocation, typingSafeListLocation, host.newLine); : new NodeTypingsInstaller(telemetryEnabled, logger, host, installerEventPort, globalTypingsCacheLocation, typingSafeListLocation, npmLocation, host.newLine);
super({ super({
host, host,
@ -741,7 +745,8 @@ namespace ts.server {
validateLocaleAndSetLanguage(localeStr, sys); validateLocaleAndSetLanguage(localeStr, sys);
} }
const typingSafeListLocation = findArgument("--typingSafeListLocation"); const typingSafeListLocation = findArgument(Arguments.TypingSafeListLocation);
const npmLocation = findArgument(Arguments.NpmLocation);
const globalPlugins = (findArgument("--globalPlugins") || "").split(","); const globalPlugins = (findArgument("--globalPlugins") || "").split(",");
const pluginProbeLocations = (findArgument("--pluginProbeLocations") || "").split(","); const pluginProbeLocations = (findArgument("--pluginProbeLocations") || "").split(",");
@ -760,6 +765,7 @@ namespace ts.server {
disableAutomaticTypingAcquisition, disableAutomaticTypingAcquisition,
globalTypingsCacheLocation: getGlobalTypingsCacheLocation(), globalTypingsCacheLocation: getGlobalTypingsCacheLocation(),
typingSafeListLocation, typingSafeListLocation,
npmLocation,
telemetryEnabled, telemetryEnabled,
logger, logger,
globalPlugins, globalPlugins,

View file

@ -12,13 +12,18 @@ namespace ts.server {
export const LogFile = "--logFile"; export const LogFile = "--logFile";
export const EnableTelemetry = "--enableTelemetry"; export const EnableTelemetry = "--enableTelemetry";
export const TypingSafeListLocation = "--typingSafeListLocation"; export const TypingSafeListLocation = "--typingSafeListLocation";
/**
* This argument specifies the location of the NPM executable.
* typingsInstaller will run the command with `${npmLocation} install ...`.
*/
export const NpmLocation = "--npmLocation";
} }
export function hasArgument(argumentName: string) { export function hasArgument(argumentName: string) {
return sys.args.indexOf(argumentName) >= 0; return sys.args.indexOf(argumentName) >= 0;
} }
export function findArgument(argumentName: string) { export function findArgument(argumentName: string): string | undefined {
const index = sys.args.indexOf(argumentName); const index = sys.args.indexOf(argumentName);
return index >= 0 && index < sys.args.length - 1 return index >= 0 && index < sys.args.length - 1
? sys.args[index + 1] ? sys.args[index + 1]

View file

@ -30,7 +30,8 @@ namespace ts.server.typingsInstaller {
} }
} }
function getNPMLocation(processName: string) { /** Used if `--npmLocation` is not passed. */
function getDefaultNPMLocation(processName: string) {
if (path.basename(processName).indexOf("node") === 0) { if (path.basename(processName).indexOf("node") === 0) {
return `"${path.join(path.dirname(process.argv[0]), "npm")}"`; return `"${path.join(path.dirname(process.argv[0]), "npm")}"`;
} }
@ -76,17 +77,18 @@ namespace ts.server.typingsInstaller {
private delayedInitializationError: InitializationFailedResponse; private delayedInitializationError: InitializationFailedResponse;
constructor(globalTypingsCacheLocation: string, typingSafeListLocation: string, throttleLimit: number, log: Log) { constructor(globalTypingsCacheLocation: string, typingSafeListLocation: string, npmLocation: string | undefined, throttleLimit: number, log: Log) {
super( super(
sys, sys,
globalTypingsCacheLocation, globalTypingsCacheLocation,
typingSafeListLocation ? toPath(typingSafeListLocation, "", createGetCanonicalFileName(sys.useCaseSensitiveFileNames)) : toPath("typingSafeList.json", __dirname, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)), typingSafeListLocation ? toPath(typingSafeListLocation, "", createGetCanonicalFileName(sys.useCaseSensitiveFileNames)) : toPath("typingSafeList.json", __dirname, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)),
throttleLimit, throttleLimit,
log); log);
this.npmPath = npmLocation !== undefined ? npmLocation : getDefaultNPMLocation(process.argv[0]);
if (this.log.isEnabled()) { if (this.log.isEnabled()) {
this.log.writeLine(`Process id: ${process.pid}`); this.log.writeLine(`Process id: ${process.pid}`);
this.log.writeLine(`NPM location: ${this.npmPath} (explicit '${Arguments.NpmLocation}' ${npmLocation === undefined ? "not " : ""} provided)`);
} }
this.npmPath = getNPMLocation(process.argv[0]);
({ execSync: this.execSync } = require("child_process")); ({ execSync: this.execSync } = require("child_process"));
this.ensurePackageDirectoryExists(globalTypingsCacheLocation); this.ensurePackageDirectoryExists(globalTypingsCacheLocation);
@ -168,6 +170,7 @@ namespace ts.server.typingsInstaller {
const logFilePath = findArgument(server.Arguments.LogFile); const logFilePath = findArgument(server.Arguments.LogFile);
const globalTypingsCacheLocation = findArgument(server.Arguments.GlobalCacheLocation); const globalTypingsCacheLocation = findArgument(server.Arguments.GlobalCacheLocation);
const typingSafeListLocation = findArgument(server.Arguments.TypingSafeListLocation); const typingSafeListLocation = findArgument(server.Arguments.TypingSafeListLocation);
const npmLocation = findArgument(server.Arguments.NpmLocation);
const log = new FileLog(logFilePath); const log = new FileLog(logFilePath);
if (log.isEnabled()) { if (log.isEnabled()) {
@ -181,6 +184,6 @@ namespace ts.server.typingsInstaller {
} }
process.exit(0); process.exit(0);
}); });
const installer = new NodeTypingsInstaller(globalTypingsCacheLocation, typingSafeListLocation, /*throttleLimit*/5, log); const installer = new NodeTypingsInstaller(globalTypingsCacheLocation, typingSafeListLocation, npmLocation, /*throttleLimit*/5, log);
installer.listen(); installer.listen();
} }