UNPKG

5.64 kBJavaScriptView Raw
1'use strict';
2
3var child_process = require('child_process');
4var fs = require('fs');
5var fse = require('fs-extra');
6var glob = require('glob');
7var path = require('path');
8var yaml = require('js-yaml');
9
10var src = 'source_repo';
11var packageMappingFileRelativePath = 'package_service_mapping.json';
12var repoRelativePath = 'repo.json';
13var dest = 'target_repo/docs-ref-autogen';
14var configPath = 'node2docfx.json';
15var tempConfigPath = '_node2docfx_temp.json';
16var filenameMaxLength = 100;
17var packagesToFilter = ['azure-arm-datalake-store'];
18
19function itemsByType(type) {
20 return packageNames.filter(function (value) {
21 return value.indexOf(type) > -1;
22 });
23}
24
25function 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
48function 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 // null whiteList means this root package is the only one package
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
80function 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// 1. prepare
90fse.removeSync(dest);
91var repoConfig = fse.readJsonSync(repoRelativePath);
92var repo = null;
93if (repoConfig && repoConfig.repo) {
94 repo = repoConfig.repo;
95}
96// get globalWhiteList from all repo package_service_mapping files, except one package repo
97var globalWhiteList = [];
98Object.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// 2. generate yml and copy readme.md for root package of repo
106var rootConfig = fse.readJsonSync(configPath);
107Object.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// 3. generate yml and copy readme.md for all sub packages
117Object.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});
125fs.unlink(tempConfigPath);
126
127// 4. remove files with too long filename that breaks DocFX
128packagesToFilter.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// 5. generate root toc
149var rootToc = [];
150var rootTocPath = path.join(dest, 'toc.yml');
151var subTocs = glob.sync(path.join(dest, '**/toc.yml'));
152subTocs.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});
159fs.writeFileSync(rootTocPath, yaml.safeDump(rootToc));
160console.log('Finish combining sub TOCs to root TOC');