1+ #!/usr/bin/env node
2+ 'use strict' ;
3+
4+ /* eslint-disable no-console */
5+ const fs = require ( 'fs-extra' ) ;
6+ const path = require ( 'path' ) ;
7+ const util = require ( 'util' ) ;
8+ const exec = require ( 'child_process' ) . exec ;
9+ const version = require ( '../../package.json' ) . version ;
10+
11+ const documentationPath = './docs/documentation' ;
12+ const outputPath = '/tmp/documentation/' ;
13+
14+ function createTempDirectory ( ) {
15+ return new Promise ( ( resolve , reject ) => {
16+ fs . emptyDir ( outputPath , ( error ) => {
17+ if ( error ) {
18+ reject ( error ) ;
19+ }
20+ resolve ( ) ;
21+ } )
22+ } ) ;
23+ }
24+
25+ function execute ( command ) {
26+ return new Promise ( ( resolve , reject ) => {
27+ exec ( command , ( error , stdout ) => {
28+ if ( error ) {
29+ reject ( error ) ;
30+ }
31+ resolve ( stdout . trim ( ) )
32+ } ) ;
33+ } ) ;
34+ }
35+
36+ function gitClone ( url , directory ) {
37+ return execute ( util . format ( 'git clone %s %s' , url , directory ) ) ;
38+ }
39+
40+ function readFiles ( directory , filelist ) {
41+ if ( directory [ directory . length - 1 ] != '/' ) {
42+ directory = directory . concat ( '/' ) ;
43+ }
44+
45+ const files = fs . readdirSync ( directory ) ;
46+ filelist = filelist || [ ] ;
47+ files . forEach ( ( file ) => {
48+ if ( fs . statSync ( directory + file ) . isDirectory ( ) ) {
49+ filelist = readFiles ( directory + file + '/' , filelist ) ;
50+ } else {
51+ const originalPath = directory + file ;
52+ const newPath = outputPath + originalPath
53+ . replace ( documentationPath + '/' , '' )
54+ . replace ( '/' , '-' ) ;
55+ filelist . push ( {
56+ originalPath,
57+ newPath
58+ } ) ;
59+ }
60+ } ) ;
61+ return filelist ;
62+ }
63+
64+ function copyFile ( from , to , linksToReplace ) {
65+ from = path . relative ( process . cwd ( ) , from ) ;
66+ to = path . relative ( process . cwd ( ) , to ) ;
67+ const replaceHyphen = new RegExp ( '-' , 'gi' ) ;
68+ return new Promise ( ( resolve , reject ) => {
69+ fs . readFile ( from , ( error , data ) => {
70+ if ( error ) {
71+ reject ( error ) ;
72+ }
73+ let fileData = data . toString ( ) ;
74+ linksToReplace . forEach ( ( link ) => {
75+ const str = '\\(' + link . oldName . replace ( replaceHyphen , '\\-' ) + '\\)' ;
76+ const r = new RegExp ( str , 'gi' ) ;
77+ fileData = fileData . replace ( r , link . newName ) ;
78+ } ) ;
79+ fs . outputFile ( to , fileData , ( error2 ) => {
80+ if ( error2 ) {
81+ reject ( error2 ) ;
82+ }
83+ resolve ( ) ;
84+ } )
85+ } )
86+ } ) ;
87+ }
88+
89+ function createFiles ( ) {
90+ const files = readFiles ( documentationPath ) || [ ] ;
91+ const linksToReplace = checkNameLinks ( files ) ;
92+ const copies = files . map ( ( { originalPath, newPath } ) => copyFile ( originalPath , newPath , linksToReplace ) ) ;
93+ return Promise . all ( copies ) ;
94+ }
95+
96+ function checkNameLinks ( files ) {
97+ return files . reduce ( ( pValue , cValue ) => {
98+ const oldName = cValue . originalPath . split ( '/' ) . slice ( - 1 ) . pop ( ) . replace ( '.md' , '' ) ;
99+ const newName = '(' + cValue . newPath . split ( '/' ) . slice ( - 1 ) . pop ( ) . replace ( '.md' , '' ) + ')' ;
100+ if ( oldName !== newName ) {
101+ pValue . push ( {
102+ oldName,
103+ newName
104+ } ) ;
105+ }
106+ return pValue ;
107+ } , [ ] ) ;
108+ }
109+
110+ ( function ( ) {
111+ Promise . resolve ( )
112+ . then ( ( ) => createTempDirectory ( ) )
113+ . then ( ( ) => gitClone ( 'https://github.com/angular/angular-cli.wiki' , outputPath ) )
114+ . then ( ( ) => createFiles ( ) )
115+ . then ( ( ) => process . chdir ( outputPath ) )
116+ . then ( ( ) => execute ( 'git add .' ) )
117+ . then ( ( ) => execute ( `git commit -m ${ version } ` ) )
118+ . then ( ( ) => execute ( 'git status' ) )
119+ . then ( ( data ) => {
120+ console . log ( data ) ;
121+ } )
122+ . catch ( ( error ) => {
123+ console . log ( 'error' , error ) ;
124+ } )
125+ } ) ( ) ;
0 commit comments