11import styleText from "node-style-text" ;
22import { fetchText , logPromise , processFile , runGit } from "../utils.js" ;
33
4- async function update ( { repo } ) {
4+ async function getNpmDependentsCount ( ) {
55 const npmPage = await logPromise (
66 "Fetching npm dependents count" ,
77 fetchText ( "https://www.npmjs.com/package/prettier" ) ,
88 ) ;
99 const dependentsCountNpm = Number (
10- npmPage . match ( / " d e p e n d e n t s C o u n t " : ( \d + ) , / u) [ 1 ] ,
10+ npmPage . match ( / " d e p e n d e n t s C o u n t " : " (?< dependentsCount > \d + ) " , / u) . groups
11+ . dependentsCount ,
1112 ) ;
1213 if ( Number . isNaN ( dependentsCountNpm ) ) {
1314 throw new TypeError (
1415 "Invalid data from https://www.npmjs.com/package/prettier" ,
1516 ) ;
1617 }
1718
19+ return dependentsCountNpm ;
20+ }
21+
22+ async function getGithubDependentsCount ( ) {
1823 const githubPage = await logPromise (
1924 "Fetching github dependents count" ,
2025 fetchText ( "https://github.com/prettier/prettier/network/dependents" ) ,
@@ -23,41 +28,69 @@ async function update({ repo }) {
2328 githubPage
2429 . replaceAll ( "\n" , "" )
2530 . match (
26- / < s v g .* ?o c t i c o n - c o d e - s q u a r e .* ?> .* ?< \/ s v g > \s * ( [ \d , ] + ) \s * R e p o s i t o r i e s \s * < \/ a > / u,
27- ) [ 1 ]
28- . replaceAll ( "," , "" ) ,
31+ / < s v g .* ?o c t i c o n - c o d e - s q u a r e .* ?> .* ?< \/ s v g > \s * (?< dependentsCount > [ \d , ] + ) \s * R e p o s i t o r i e s \s * < \/ a > / u,
32+ )
33+ . groups . dependentsCount . replaceAll ( "," , "" ) ,
2934 ) ;
30- if ( Number . isNaN ( dependentsCountNpm ) ) {
35+ if ( Number . isNaN ( dependentsCountGithub ) ) {
3136 throw new TypeError (
3237 "Invalid data from https://github.com/prettier/prettier/network/dependents" ,
3338 ) ;
3439 }
3540
36- processFile ( "website/src/pages/index.jsx" , ( content ) =>
37- content
38- . replace (
39- / ( < s t r o n g d a t a - p l a c e h o l d e r = " d e p e n d e n t - n p m " > ) ( .* ?) ( < \/ s t r o n g > ) / u,
40- `$1${ formatNumber ( dependentsCountNpm ) } $3` ,
41- )
42- . replace (
43- / ( < s t r o n g d a t a - p l a c e h o l d e r = " d e p e n d e n t - g i t h u b " > ) ( .* ?) ( < \/ s t r o n g > ) / u,
44- `$1${ formatNumber ( dependentsCountGithub ) } $3` ,
45- ) ,
46- ) ;
41+ return dependentsCountGithub ;
42+ }
4743
48- const isUpdated = await logPromise (
49- "Checking if dependents count has been updated" ,
50- async ( ) =>
51- ( await runGit ( [ "diff" , "--name-only" ] ) ) . stdout ===
52- "website/src/pages/index.jsx" ,
53- ) ;
44+ async function update ( { repo } ) {
45+ const [
46+ { value : dependentsCountNpm , reason : dependentsNpmError } ,
47+ { value : dependentsCountGithub , reason : dependentsGithubError } ,
48+ ] = await Promise . allSettled ( [
49+ getNpmDependentsCount ( ) ,
50+ getGithubDependentsCount ( ) ,
51+ ] ) ;
5452
55- if ( isUpdated ) {
56- await logPromise ( "Committing and pushing to remote" , async ( ) => {
57- await runGit ( [ "add" , "." ] ) ;
58- await runGit ( [ "commit" , "-m" , "Update dependents count" ] ) ;
59- await runGit ( [ "push" , "--repo" , repo ] ) ;
53+ if ( dependentsCountNpm || dependentsCountGithub ) {
54+ processFile ( "website/src/pages/index.jsx" , ( content ) => {
55+ if ( dependentsCountNpm ) {
56+ content = content . replace (
57+ / ( < s t r o n g d a t a - p l a c e h o l d e r = " d e p e n d e n t - n p m " > ) ( .* ?) ( < \/ s t r o n g > ) / u,
58+ `$1${ formatNumber ( dependentsCountNpm ) } $3` ,
59+ ) ;
60+ }
61+
62+ if ( dependentsCountGithub ) {
63+ content = content . replace (
64+ / ( < s t r o n g d a t a - p l a c e h o l d e r = " d e p e n d e n t - g i t h u b " > ) ( .* ?) ( < \/ s t r o n g > ) / u,
65+ `$1${ formatNumber ( dependentsCountGithub ) } $3` ,
66+ ) ;
67+ }
68+
69+ return content ;
6070 } ) ;
71+
72+ const isUpdated = await logPromise (
73+ "Checking if dependents count has been updated" ,
74+ async ( ) =>
75+ ( await runGit ( [ "diff" , "--name-only" ] ) ) . stdout ===
76+ "website/src/pages/index.jsx" ,
77+ ) ;
78+
79+ if ( isUpdated ) {
80+ await logPromise ( "Committing and pushing to remote" , async ( ) => {
81+ await runGit ( [ "add" , "." ] ) ;
82+ await runGit ( [ "commit" , "-m" , "Update dependents count" ] ) ;
83+ await runGit ( [ "push" , "--repo" , repo ] ) ;
84+ } ) ;
85+ }
86+ }
87+
88+ if ( dependentsNpmError ) {
89+ throw dependentsNpmError ;
90+ }
91+
92+ if ( dependentsGithubError ) {
93+ throw dependentsGithubError ;
6194 }
6295}
6396
0 commit comments