Skip to content

Commit 59b12d7

Browse files
authored
Merge pull request #11069 from Microsoft/FixNPMLocation
Fix npm location
2 parents 1fd0a8c + 311e285 commit 59b12d7

File tree

6 files changed

+107
-5
lines changed

6 files changed

+107
-5
lines changed

Jakefile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ task("generate-spec", [specMd]);
700700
// Makes a new LKG. This target does not build anything, but errors if not all the outputs are present in the built/local directory
701701
desc("Makes a new LKG out of the built js files");
702702
task("LKG", ["clean", "release", "local"].concat(libraryTargets), function () {
703-
var expectedFiles = [tscFile, servicesFile, serverFile, nodePackageFile, nodeDefinitionsFile, standaloneDefinitionsFile, tsserverLibraryFile, tsserverLibraryDefinitionFile].concat(libraryTargets);
703+
var expectedFiles = [tscFile, servicesFile, serverFile, nodePackageFile, nodeDefinitionsFile, standaloneDefinitionsFile, tsserverLibraryFile, tsserverLibraryDefinitionFile, cancellationTokenFile, typingsInstallerFile].concat(libraryTargets);
704704
var missingFiles = expectedFiles.filter(function (f) {
705705
return !fs.existsSync(f);
706706
});

src/harness/unittests/typingsInstaller.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,5 +627,97 @@ namespace ts.projectSystem {
627627
checkProjectActualFiles(p1, [lodashJs.path, commanderJs.path, file3.path, commander.path, jquery.path, lodash.path, cordova.path]);
628628
checkProjectActualFiles(p2, [file3.path, grunt.path, gulp.path ]);
629629
});
630+
631+
it("configured projects discover from node_modules", () => {
632+
const app = {
633+
path: "/app.js",
634+
content: ""
635+
};
636+
const jsconfig = {
637+
path: "/jsconfig.json",
638+
content: JSON.stringify({})
639+
};
640+
const jquery = {
641+
path: "/node_modules/jquery/index.js",
642+
content: ""
643+
};
644+
const jqueryPackage = {
645+
path: "/node_modules/jquery/package.json",
646+
content: JSON.stringify({ name: "jquery" })
647+
};
648+
const jqueryDTS = {
649+
path: "/tmp/node_modules/@types/jquery/index.d.ts",
650+
content: ""
651+
};
652+
const host = createServerHost([app, jsconfig, jquery, jqueryPackage]);
653+
const installer = new (class extends Installer {
654+
constructor() {
655+
super(host, { globalTypingsCacheLocation: "/tmp" });
656+
}
657+
runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) {
658+
const installedTypings = ["@types/jquery"];
659+
const typingFiles = [jqueryDTS];
660+
executeCommand(this, host, installedTypings, typingFiles, requestKind, cb);
661+
}
662+
})();
663+
664+
const projectService = createProjectService(host, { useSingleInferredProject: true, typingsInstaller: installer });
665+
projectService.openClientFile(app.path);
666+
667+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
668+
const p = projectService.configuredProjects[0];
669+
checkProjectActualFiles(p, [app.path]);
670+
671+
installer.installAll([TI.NpmViewRequest], [TI.NpmInstallRequest]);
672+
673+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
674+
checkProjectActualFiles(p, [app.path, jqueryDTS.path]);
675+
});
676+
677+
it("configured projects discover from bower.josn", () => {
678+
const app = {
679+
path: "/app.js",
680+
content: ""
681+
};
682+
const jsconfig = {
683+
path: "/jsconfig.json",
684+
content: JSON.stringify({})
685+
};
686+
const bowerJson = {
687+
path: "/bower.json",
688+
content: JSON.stringify({
689+
"dependencies": {
690+
"jquery": "^3.1.0"
691+
}
692+
})
693+
};
694+
const jqueryDTS = {
695+
path: "/tmp/node_modules/@types/jquery/index.d.ts",
696+
content: ""
697+
};
698+
const host = createServerHost([app, jsconfig, bowerJson]);
699+
const installer = new (class extends Installer {
700+
constructor() {
701+
super(host, { globalTypingsCacheLocation: "/tmp" });
702+
}
703+
runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) {
704+
const installedTypings = ["@types/jquery"];
705+
const typingFiles = [jqueryDTS];
706+
executeCommand(this, host, installedTypings, typingFiles, requestKind, cb);
707+
}
708+
})();
709+
710+
const projectService = createProjectService(host, { useSingleInferredProject: true, typingsInstaller: installer });
711+
projectService.openClientFile(app.path);
712+
713+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
714+
const p = projectService.configuredProjects[0];
715+
checkProjectActualFiles(p, [app.path]);
716+
717+
installer.installAll([TI.NpmViewRequest], [TI.NpmInstallRequest]);
718+
719+
checkNumberOfProjects(projectService, { configuredProjects: 1 });
720+
checkProjectActualFiles(p, [app.path, jqueryDTS.path]);
721+
});
630722
});
631723
}

src/server/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ts.server.typingsInstaller {
99
const path: {
1010
join(...parts: string[]): string;
1111
dirname(path: string): string;
12+
basename(path: string, extension?: string): string;
1213
} = require("path");
1314

1415
class FileLog implements Log {
@@ -23,6 +24,15 @@ namespace ts.server.typingsInstaller {
2324
}
2425
}
2526

27+
function getNPMLocation(processName: string) {
28+
if (path.basename(processName).indexOf("node") == 0) {
29+
return `"${path.join(path.dirname(process.argv[0]), "npm")}"`;
30+
}
31+
else {
32+
return "npm";
33+
}
34+
}
35+
2636
export class NodeTypingsInstaller extends TypingsInstaller {
2737
private readonly exec: { (command: string, options: { cwd: string }, callback?: (error: Error, stdout: string, stderr: string) => void): any };
2838

@@ -31,7 +41,7 @@ namespace ts.server.typingsInstaller {
3141
constructor(globalTypingsCacheLocation: string, throttleLimit: number, log: Log) {
3242
super(
3343
globalTypingsCacheLocation,
34-
/*npmPath*/ `"${path.join(path.dirname(process.argv[0]), "npm")}"`,
44+
/*npmPath*/ getNPMLocation(process.argv[0]),
3545
toPath("typingSafeList.json", __dirname, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)),
3646
throttleLimit,
3747
log);

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ namespace ts.server.typingsInstaller {
150150
if (this.installTypingHost.fileExists(packageJson)) {
151151
const npmConfig = <NpmConfig>JSON.parse(this.installTypingHost.readFile(packageJson));
152152
if (this.log.isEnabled()) {
153-
this.log.writeLine(`Loaded content of '${npmConfig}': ${JSON.stringify(npmConfig)}`);
153+
this.log.writeLine(`Loaded content of '${packageJson}': ${JSON.stringify(npmConfig)}`);
154154
}
155155
if (npmConfig.devDependencies) {
156156
for (const key in npmConfig.devDependencies) {

src/server/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace ts.server {
3535
function getProjectRootPath(project: Project): Path {
3636
switch (project.projectKind) {
3737
case ProjectKind.Configured:
38-
return <Path>project.getProjectName();
38+
return <Path>getDirectoryPath(project.getProjectName());
3939
case ProjectKind.Inferred:
4040
// TODO: fixme
4141
return <Path>"";

src/services/jsTyping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace ts.JsTyping {
187187
}
188188

189189
const typingNames: string[] = [];
190-
const fileNames = host.readDirectory(nodeModulesPath, ["*.json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
190+
const fileNames = host.readDirectory(nodeModulesPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
191191
for (const fileName of fileNames) {
192192
const normalizedFileName = normalizePath(fileName);
193193
if (getBaseFileName(normalizedFileName) !== "package.json") {

0 commit comments

Comments
 (0)