|
1 | 1 | var _ = require('lodash'); |
| 2 | +var path = require('canonical-path'); |
2 | 3 |
|
3 | | -module.exports = function createTypeDefinitionFile() { |
| 4 | +module.exports = function createTypeDefinitionFile(log) { |
4 | 5 |
|
5 | 6 | return { |
6 | 7 | $runAfter: ['processing-docs'], |
7 | 8 | $runBefore: ['docs-processed'], |
| 9 | + $validate: { |
| 10 | + dtsPath: { presence: true }, |
| 11 | + dtsExtension: { presence: true }, |
| 12 | + typeDefinitions: { presence: true } |
| 13 | + }, |
| 14 | + dtsPath: 'typings', |
| 15 | + dtsExtension: '.d.ts', |
| 16 | + typeDefinitions: [ |
| 17 | + { |
| 18 | + id: 'angular2/angular2', |
| 19 | + modules: { |
| 20 | + // The shape of the public API is determined by what is reexported into |
| 21 | + // angular2/angular2, with hacks layered into angular2.api.ts |
| 22 | + 'angular2/angular2': 'angular2/angular2.api', |
| 23 | + } |
| 24 | + }, |
| 25 | + { |
| 26 | + id: 'angular2/router', |
| 27 | + modules: { |
| 28 | + 'angular2/router': 'angular2/router' |
| 29 | + } |
| 30 | + } |
| 31 | + ], |
8 | 32 | $process: function(docs) { |
9 | | - var typeDefDoc = { |
10 | | - id: 'type-definition', |
11 | | - aliases: ['type-definition'], |
12 | | - path: 'type-definition', |
13 | | - outputPath: 'typings/angular2/angular2.d.ts', |
14 | | - modules: [] |
15 | | - }; |
| 33 | + var dtsPath = this.dtsPath; |
| 34 | + var dtsExtension = this.dtsExtension; |
| 35 | + |
| 36 | + // For each type definition that we wish to create we define a dgeni "doc" for it |
| 37 | + var typeDefDocs = _.map(this.typeDefinitions, function(def) { |
| 38 | + |
| 39 | + var id = def.id + dtsExtension; |
| 40 | + var docPath = path.join(dtsPath, id); |
| 41 | + |
| 42 | + return { |
| 43 | + docType: 'type-definition', |
| 44 | + id: id, |
| 45 | + aliases: [id], |
| 46 | + path: docPath, |
| 47 | + outputPath: docPath, |
| 48 | + // A type definition may include a number of top level modules |
| 49 | + // And those modules could be aliased (such as 'angular2/angular2.api' -> 'angular2/angular2') |
| 50 | + moduleDocs: _.transform(def.modules, function(moduleDocs, id, alias) { |
| 51 | + moduleDocs[id] = { id: alias, doc: null }; |
| 52 | + }) |
| 53 | + }; |
| 54 | + }); |
| 55 | + |
| 56 | + // Now add all the module docs to their corresponding type definition doc |
16 | 57 | _.forEach(docs, function(doc) { |
17 | | - // The shape of the public API is determined by what is reexported into |
18 | | - // angular2/angular2, with hacks layered into angular2.api.ts |
19 | | - if (doc.id === 'angular2/angular2.api') { |
20 | | - doc.id = 'angular2/angular2'; |
21 | | - typeDefDoc.modules.push(doc); |
22 | | - } |
| 58 | + _.forEach(typeDefDocs, function(typeDefDoc) { |
| 59 | + if(typeDefDoc.moduleDocs[doc.id]) { |
| 60 | + typeDefDoc.moduleDocs[doc.id].doc = doc; |
| 61 | + } |
| 62 | + }); |
23 | 63 | }); |
24 | | - docs.push(typeDefDoc); |
| 64 | + |
| 65 | + _.forEach(typeDefDocs, function(doc) { |
| 66 | + _.forEach(doc.moduleDocs, function(modDoc, alias) { |
| 67 | + if (!modDoc.doc) { |
| 68 | + log.error('createTypeDefinitionFile processor: no such module "' + alias + '" (Did you forget to add it to the modules to load?)'); |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + // Add all the type definition docs to the docs collection so that dgeni can process them |
| 74 | + return docs.concat(typeDefDocs); |
25 | 75 | } |
26 | 76 | }; |
27 | 77 | }; |
0 commit comments