forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-docs.ts
101 lines (80 loc) · 2.82 KB
/
publish-docs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable: no-console
import { exec } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { packages } from '../lib/packages';
const temp = require('temp');
function die(message = 'Unknown error.') {
throw new Error(message);
}
const version = packages['@angular/cli'].version || die('Cannot find @angular/cli.');
const docsRoot = path.join(__dirname, '../docs/documentation');
const outputPath = temp.mkdirSync('angular-cli-docs');
// Execute a process and returns its stdout.
function execute(command: string): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout) => {
if (error) {
reject(error);
}
resolve(stdout.trim());
});
});
}
// List all files in a directory.
function listAllFiles(directory: string) {
const list: string[] = [];
function _listRecurse(p: string) {
const files = fs.readdirSync(path.join(directory, p));
files.forEach(name => {
const fileName = path.join(p, name);
const stat = fs.statSync(path.join(directory, fileName));
if (stat.isDirectory()) {
_listRecurse(fileName);
} else {
list.push(fileName);
}
});
}
_listRecurse('');
return list;
}
export default async function() {
console.log(`Documentation Path: ${docsRoot}`);
console.log(`Wiki path: ${outputPath}`);
console.log('Cloning...');
await execute(`git clone "https://github.com/angular/angular-cli.wiki" "${outputPath}"`);
console.log('Listing all files...');
const allFiles = listAllFiles(docsRoot);
const allFilesInfo = allFiles.map(fileName => {
const wikiFileName = fileName.split(path.sep).join('-');
const src = path.join(docsRoot, fileName);
const dest = path.join(outputPath, wikiFileName);
return { fileName, wikiFileName, src, dest };
});
// For each files, read its content, replace all links from 'a/b/c.md' to 'a-b-c' and write it.
allFilesInfo.forEach(({ src, dest }) => {
let content = fs.readFileSync(src, 'utf-8');
content = allFilesInfo.reduce((acc, { fileName, wikiFileName }) => {
const replace = fileName.replace(/\.md$/, '');
const replaceWith = wikiFileName.replace(/\.md$/, '');
const text = replace.replace(/[\-\[\]{}()+?.^$|]/g, '\\$&');
return acc.replace(new RegExp(text, 'g'), replaceWith);
}, content);
fs.writeFileSync(dest, content);
});
console.log(`Done ${allFiles.length} files...`);
process.chdir(outputPath);
console.log('Committing...');
await execute('git add .');
await execute(`git commit -m "${version}"`);
await execute('git push');
console.log('Done');
}