1 | 'use strict';
|
2 |
|
3 | var child_process = require('child_process');
|
4 | var fs = require('fs');
|
5 | var fse = require('fs-extra');
|
6 | var glob = require('glob');
|
7 | var path = require('path');
|
8 | var yaml = require('js-yaml');
|
9 |
|
10 | var src = 'source_repo';
|
11 | var packageMappingFileRelativePath = 'package_service_mapping.json';
|
12 | var repoRelativePath = 'repo.json';
|
13 | var dest = 'target_repo/docs-ref-autogen';
|
14 | var configPath = 'node2docfx.json';
|
15 | var tempConfigPath = '_node2docfx_temp.json';
|
16 | var filenameMaxLength = 100;
|
17 | var packagesToFilter = ['azure-arm-datalake-store'];
|
18 |
|
19 | function itemsByType(type) {
|
20 | return packageNames.filter(function (value) {
|
21 | return value.indexOf(type) > -1;
|
22 | });
|
23 | }
|
24 |
|
25 | function buildTocItems(keys, relativePathToRootFolder) {
|
26 | return keys.sort().map(function (key) {
|
27 | var packageToc = path.join(dest, key, 'toc.yml');
|
28 | var href, topicHref;
|
29 | if (fs.existsSync(packageToc)) {
|
30 | href = path.join(relativePathToRootFolder, key, 'toc.yml');
|
31 | } else {
|
32 | href = key + '/';
|
33 | }
|
34 | var packageIndex = path.join(dest, key, 'index.md');
|
35 | if (fs.existsSync(packageIndex)) {
|
36 | topicHref = path.join(relativePathToRootFolder, key, 'index.md');
|
37 | } else {
|
38 | topicHref = undefined;
|
39 | }
|
40 | return {
|
41 | name: key,
|
42 | href: href,
|
43 | topicHref: topicHref
|
44 | };
|
45 | });
|
46 | }
|
47 |
|
48 | function generatePackageDoc(packagePath, configPath, dest, rootPackage, whiteList, repo, repoName) {
|
49 | var config = fse.readJsonSync(configPath);
|
50 | var dir = path.dirname(packagePath);
|
51 | var packageName = fse.readJsonSync(packagePath).name;
|
52 |
|
53 | if (whiteList && whiteList.indexOf(packageName) == -1) {
|
54 | return;
|
55 | }
|
56 |
|
57 | if (rootPackage) {
|
58 | if (whiteList){
|
59 | config.source.include = path.join(dir, 'lib', packageName + '.js');
|
60 | }
|
61 | else{
|
62 |
|
63 | config.source.include = path.join(dir, 'lib');
|
64 | }
|
65 | }
|
66 | else{
|
67 | config.source.include = [dir];
|
68 | }
|
69 | config.package = packagePath;
|
70 | config.readme = path.join(dir, 'README.md');
|
71 | config.destination = path.join(dest, packageName);
|
72 | if (repo) {
|
73 | config.repo = [repo[repoName]];
|
74 | }
|
75 | fse.writeJsonSync(tempConfigPath, config);
|
76 | child_process.execFileSync('node', ['node_modules/node2docfx/node2docfx.js', tempConfigPath]);
|
77 | console.log('Finish generating YAML files for ' + packageName);
|
78 | }
|
79 |
|
80 | function getWhiteListFromPackageMappingFile(sourcePath, packageMappingFileRelativePath) {
|
81 | var mapping = fse.readJsonSync(sourcePath + '/' + packageMappingFileRelativePath);
|
82 | var whiteList = [];
|
83 | Object.keys(mapping).forEach(function(element) {
|
84 | whiteList.push(element);
|
85 | }, this);
|
86 | return whiteList;
|
87 | }
|
88 |
|
89 |
|
90 | fse.removeSync(dest);
|
91 | var repoConfig = fse.readJsonSync(repoRelativePath);
|
92 | var repo = null;
|
93 | if (repoConfig && repoConfig.repo) {
|
94 | repo = repoConfig.repo;
|
95 | }
|
96 |
|
97 | var globalWhiteList = [];
|
98 | Object.keys(repo).forEach(function (repoName){
|
99 | if(!repo[repoName]['onePackage']){
|
100 | var whiteList = getWhiteListFromPackageMappingFile(src + '/' + repoName, packageMappingFileRelativePath);
|
101 | globalWhiteList = globalWhiteList.concat(whiteList);
|
102 | }
|
103 | });
|
104 |
|
105 |
|
106 | var rootConfig = fse.readJsonSync(configPath);
|
107 | Object.keys(repo).forEach(function (repoName){
|
108 | var packagePath = path.join(src, repoName, 'package.json');
|
109 | var whiteList = globalWhiteList;
|
110 | if(repo[repoName]['onePackage'] && repo[repoName]['onePackage'] === 'true'){
|
111 | whiteList = null;
|
112 | }
|
113 | generatePackageDoc(packagePath, configPath, rootConfig.destination, true, whiteList, repo, repoName);
|
114 | });
|
115 |
|
116 |
|
117 | Object.keys(repo).forEach(function (repoName){
|
118 | if (!repo[repoName]['onePackage']){
|
119 | var packageJsons = glob.sync(path.join(src, repoName, 'lib/**/package.json'));
|
120 | packageJsons.forEach(function (packagePath) {
|
121 | generatePackageDoc(packagePath, configPath, dest, false, globalWhiteList, repo, repoName);
|
122 | });
|
123 | }
|
124 | });
|
125 | fs.unlink(tempConfigPath);
|
126 |
|
127 |
|
128 | packagesToFilter.forEach(function (p) {
|
129 | var uidsToFilter = [];
|
130 | fs.readdirSync(path.join(dest, p)).forEach(function (f) {
|
131 | if (f.length > filenameMaxLength) {
|
132 | var filePath = path.join(dest, p, f);
|
133 | uidsToFilter.push(yaml.safeLoad(fs.readFileSync(filePath)).items[0].uid);
|
134 | fs.unlinkSync(filePath);
|
135 | console.log('File with too long name is removed from ref folder: ' + filePath);
|
136 | }
|
137 | });
|
138 | var tocPath = path.join(dest, p, 'toc.yml');
|
139 | if (fs.existsSync(tocPath)) {
|
140 | var toc = yaml.safeLoad(fs.readFileSync(tocPath)).filter(function(i){
|
141 | return uidsToFilter.indexOf(i.uid) == -1;
|
142 | });
|
143 | fs.writeFileSync(tocPath, yaml.safeDump(toc));
|
144 | console.log('TOC updated after removing files: ' + tocPath);
|
145 | }
|
146 | });
|
147 |
|
148 |
|
149 | var rootToc = [];
|
150 | var rootTocPath = path.join(dest, 'toc.yml');
|
151 | var subTocs = glob.sync(path.join(dest, '**/toc.yml'));
|
152 | subTocs.forEach(function (subTocPath) {
|
153 | var tocContent = yaml.safeLoad(fs.readFileSync(subTocPath));
|
154 | var packageName = subTocPath.split('/')[1];
|
155 | var topicHref = path.join(packageName, 'index.md');
|
156 | tocContent = { name: packageName, uid: packageName, landingPageType: 'Service', items: tocContent };
|
157 | rootToc.push(tocContent);
|
158 | });
|
159 | fs.writeFileSync(rootTocPath, yaml.safeDump(rootToc));
|
160 | console.log('Finish combining sub TOCs to root TOC');
|