Skip to content
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

Type acquisition support for bower_components directory #14100

Merged
merged 2 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 48 additions & 2 deletions src/harness/unittests/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,53 @@ namespace ts.projectSystem {
checkProjectActualFiles(p, [app.path, jqueryDTS.path]);
});

it("configured projects discover from bower.josn", () => {
it("configured projects discover from bower_components", () => {
const app = {
path: "/app.js",
content: ""
};
const jsconfig = {
path: "/jsconfig.json",
content: JSON.stringify({})
};
const jquery = {
path: "/bower_components/jquery/index.js",
content: ""
};
const jqueryPackage = {
path: "/bower_components/jquery/package.json",
content: JSON.stringify({ name: "jquery" })
};
const jqueryDTS = {
path: "/tmp/node_modules/@types/jquery/index.d.ts",
content: ""
};
const host = createServerHost([app, jsconfig, jquery, jqueryPackage]);
const installer = new (class extends Installer {
constructor() {
super(host, { globalTypingsCacheLocation: "/tmp", typesRegistry: createTypesRegistry("jquery") });
}
installWorker(_requestId: number, _args: string[], _cwd: string, cb: server.typingsInstaller.RequestCompletedAction) {
const installedTypings = ["@types/jquery"];
const typingFiles = [jqueryDTS];
executeCommand(this, host, installedTypings, typingFiles, cb);
}
})();

const projectService = createProjectService(host, { useSingleInferredProject: true, typingsInstaller: installer });
projectService.openClientFile(app.path);

checkNumberOfProjects(projectService, { configuredProjects: 1 });
const p = projectService.configuredProjects[0];
checkProjectActualFiles(p, [app.path]);

installer.installAll(/*expectedCount*/ 1);

checkNumberOfProjects(projectService, { configuredProjects: 1 });
checkProjectActualFiles(p, [app.path, jqueryDTS.path]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check what paths are being watched?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea - will add this to the test

});

it("configured projects discover from bower.json", () => {
const app = {
path: "/app.js",
content: ""
Expand Down Expand Up @@ -975,7 +1021,7 @@ namespace ts.projectSystem {
}
});

it("should use cached locaitons", () => {
it("should use cached locations", () => {
const f = {
path: "/a/b/app.js",
content: ""
Expand Down
18 changes: 12 additions & 6 deletions src/services/jsTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ namespace ts.JsTyping {
const bowerJsonPath = combinePaths(searchDir, "bower.json");
getTypingNamesFromJson(bowerJsonPath, filesToWatch);

const bowerComponentsPath = combinePaths(searchDir, "bower_components");
getTypingNamesFromPackagesFolder(bowerComponentsPath);

const nodeModulesPath = combinePaths(searchDir, "node_modules");
getTypingNamesFromNodeModuleFolder(nodeModulesPath);
getTypingNamesFromPackagesFolder(nodeModulesPath);
}
getTypingNamesFromSourceFileNames(fileNames);

Expand Down Expand Up @@ -199,17 +202,20 @@ namespace ts.JsTyping {
}

/**
* Infer typing names from node_module folder
* @param nodeModulesPath is the path to the "node_modules" folder
* Infer typing names from packages folder (ex: node_module, bower_components)
* @param packagesFolderPath is the path to the packages folder

*/
function getTypingNamesFromNodeModuleFolder(nodeModulesPath: string) {
function getTypingNamesFromPackagesFolder(packagesFolderPath: string) {
filesToWatch.push(packagesFolderPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we watch the path after confirming it exists?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this before the path existence check as we want to watch these paths even if they haven't been created yet. (ex: User creates an empty ASP.NET project, adds a js file then uses Manage Bower Packages to install jQuery.)


// Todo: add support for ModuleResolutionHost too
if (!host.directoryExists(nodeModulesPath)) {
if (!host.directoryExists(packagesFolderPath)) {
return;
}

const typingNames: string[] = [];
const fileNames = host.readDirectory(nodeModulesPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
const fileNames = host.readDirectory(packagesFolderPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2);
for (const fileName of fileNames) {
const normalizedFileName = normalizePath(fileName);
if (getBaseFileName(normalizedFileName) !== "package.json") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For bower, should be checking bower.json instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, line 228 and below have some logics specifically targetting npm 3, now we should check if the json file name is package.json before applying these logics.

Copy link
Member Author

@jramsay jramsay Feb 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already retrieve the bower.json typings using getTypingsFromJson(bowerJsonPath, filesToWatch). This addresses the case when there is no bower.json present and just a bower_components folder that contains the packages. From my tests the bower_components folder is similar to the node_modules folder. It contains a directory for each package with a package.json (bower_components/jQuery/package.json). I've been testing using the ASP.NET UI. Let me try bower cmd line and see if this is consistent...
You're correct - from the cmd line install there isn't always a package.json generated. I'll have to check bower.json as well. thanks!

Expand Down