11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See the @microsoft /rush package's LICENSE file for license information.
33
4- import {
5- CommandLineAction
6- } from '@microsoft/ts-command-line' ;
4+ import * as path from 'path' ;
5+ import { StringBuilder , Text , Sort , FileSystem } from '@microsoft/node-core-library' ;
6+ import { RushConfiguration , RushConfigurationProject } from '@microsoft/rush-lib' ;
7+ import { CommandLineAction } from '@microsoft/ts-command-line' ;
78
89export class ReadmeAction extends CommandLineAction {
10+ private static _isPublished ( project : RushConfigurationProject ) : boolean {
11+ return project . shouldPublish || ! ! project . versionPolicyName ;
12+ }
13+
914 public constructor ( ) {
1015 super ( {
1116 actionName : 'readme' ,
@@ -15,7 +20,73 @@ export class ReadmeAction extends CommandLineAction {
1520 }
1621
1722 protected onExecute ( ) : Promise < void > { // abstract
18- console . log ( 'hi' ) ;
23+
24+ const rushConfiguration : RushConfiguration = RushConfiguration . loadFromDefaultLocation ( ) ;
25+
26+ const builder : StringBuilder = new StringBuilder ( ) ;
27+ const orderedProjects : RushConfigurationProject [ ] = [ ...rushConfiguration . projects ] ;
28+ Sort . sortBy ( orderedProjects , x => x . projectRelativeFolder ) ;
29+
30+ builder . append ( '## Published Packages\n\n' ) ;
31+ builder . append ( '| Folder | Version | Changelog | Package |\n' ) ;
32+ builder . append ( '| ------ | ------- | --------- | ------- |\n' ) ;
33+ for ( const project of orderedProjects . filter ( x => ReadmeAction . _isPublished ( x ) ) ) {
34+
35+ // Example:
36+ //
37+ // | [/apps/api-extractor](./apps/api-extractor/)
38+ // | [](https://badge.fury.io/js/%40microsoft%2Fapi-extractor)
40+ // | [changelog](./apps/api-extractor/CHANGELOG.md)
41+ // | [@microsoft/api-extractor](https://www.npmjs.com/package/@microsoft/api-extractor)
42+ // |
43+
44+ const scopedName : string = project . packageName ; // "@microsoft/api-extractor"
45+ const folderPath : string = project . projectRelativeFolder ; // "apps/api-extractor"
46+ let escapedScopedName : string = scopedName ; // "%40microsoft%2Fapi-extractor"
47+ escapedScopedName = Text . replaceAll ( escapedScopedName , '/' , '%2F' ) ;
48+ escapedScopedName = Text . replaceAll ( escapedScopedName , '@' , '%40' ) ;
49+
50+ // | [/apps/api-extractor](./apps/api-extractor/)
51+ builder . append ( `| [/${ folderPath } ](./${ folderPath } /) ` ) ;
52+
53+ // | [](https://badge.fury.io/js/%40microsoft%2Fapi-extractor)
55+ builder . append ( `| []`
56+ + `(https://badge.fury.io/js/${ escapedScopedName } ) ` ) ;
57+
58+ // | [changelog](./apps/api-extractor/CHANGELOG.md)
59+ builder . append ( `| [changelog](./${ folderPath } /CHANGELOG.md) ` ) ;
60+
61+ // | [@microsoft/api-extractor](https://www.npmjs.com/package/@microsoft/api-extractor)
62+ builder . append ( `| [${ scopedName } ](https://www.npmjs.com/package/${ scopedName } ) ` ) ;
63+
64+ builder . append ( `|\n` ) ;
65+ }
66+
67+ builder . append ( '\n\n## Unpublished Local Projects\n\n' ) ;
68+ builder . append ( '| Folder | Description |\n' ) ;
69+ builder . append ( '| ------ | -----------|\n' ) ;
70+ for ( const project of orderedProjects . filter ( x => ! ReadmeAction . _isPublished ( x ) ) ) {
71+ const folderPath : string = project . projectRelativeFolder ; // "apps/api-extractor"
72+
73+ // | [/apps/api-extractor](./apps/api-extractor/)
74+ builder . append ( `| [/${ folderPath } ](./${ folderPath } /) ` ) ;
75+
76+ const description : string = ( project . packageJson . description || '' ) . replace ( / [ \n \r | ] + / g, '' ) ;
77+
78+ builder . append ( `| ${ description } ` ) ;
79+
80+ builder . append ( `|\n` ) ;
81+ }
82+
83+ const outputFilePath : string = path . resolve ( './dist/README.md' ) ;
84+
85+ console . log ( 'Writing ' + outputFilePath ) ;
86+ FileSystem . writeFile ( outputFilePath , builder . toString ( ) , { ensureFolderExists : true } ) ;
87+
88+ console . log ( '\nSuccess.' ) ;
89+
1990 return Promise . resolve ( ) ;
2091 }
2192
0 commit comments