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