1- const assert = require ( 'assert' ) ;
2- const { readFile, writeFile } = require ( 'fs/promises' ) ;
1+ const { readFile } = require ( 'fs/promises' ) ;
32const { resolve } = require ( 'path' ) ;
43
54const { memoize, cloneDeep, defaultsDeep, uniq } = require ( 'lodash' ) ;
65const semver = require ( 'semver' ) ;
7- const { normalizePath } = require ( 'typedoc' ) ;
86
9- const { readProjectPackageJson , getDocsUrl , assertDiffFile } = require ( './utils' ) ;
10- const { formatPackages , resolveRoot } = require ( '../utils' ) ;
7+ const { syncFile , formatPackage , getDocsUrl } = require ( './utils' ) ;
8+ const { resolveRoot } = require ( '../utils' ) ;
119
1210/**
1311 * @param {boolean } checkOnly
14- * @returns {import('./utils').ProtoHandler<{getProtoPkg: (v: string) => string, rootJson: any, rootJsonStr: string, rootPath: string} }
12+ * @returns {import('./utils').ProtoHandler<{getProtoPkg: (v: string) => Promise< string> , rootJson: any, rootJsonStr: string, rootPath: string} }
1513 */
1614module . exports . packageJson = async checkOnly => ( {
1715 setup : async ( ) => {
@@ -21,27 +19,22 @@ module.exports.packageJson = async checkOnly => ( {
2119 const rootJson = JSON . parse ( rootJsonStr ) ;
2220 return { getProtoPkg, rootJson, rootJsonStr, rootPath } ;
2321 } ,
24- run : async ( proto , { path : projectPath } , projects , _ , { getProtoPkg, rootJson : rootPackageJson } ) => {
25- const { packageContent = { } , path : packagePath } = await readProjectPackageJson ( projectPath ) ?? { } ;
22+ run : async ( proto , { path : projectPath , pkgJsonPath, pkgJson } , projects , _ , { getProtoPkg, rootJson : rootPackageJson } ) => {
2623 const protoPkgContent = await getProtoPkg ( proto ) ;
2724 const protoPkg = JSON . parse ( protoPkgContent
2825 . replace ( / \{ p r o j e c t R e l D i r \} / g, projectPath )
29- . replace ( / \{ p r o j e c t T y p e D o c U r l \} / g, getDocsUrl ( packageContent ) ) ) ;
30- const newProjectPkg = defaultsDeep ( cloneDeep ( protoPkg ) , packageContent ) ;
26+ . replace ( / \{ p r o j e c t T y p e D o c U r l \} / g, getDocsUrl ( pkgJson ) ) ) ;
27+ const newProjectPkg = defaultsDeep ( cloneDeep ( protoPkg ) , pkgJson ) ;
3128 [ 'keywords' , 'files' ] . forEach ( prop => newProjectPkg [ prop ] = uniq ( [
3229 ...( protoPkg [ prop ] ?? [ ] ) ,
33- ...( packageContent [ prop ] ?? [ ] ) ,
30+ ...( pkgJson [ prop ] ?? [ ] ) ,
3431 ]
3532 . map ( k => k . toLowerCase ( ) ) )
3633 . sort ( ) ) ;
37- if ( checkOnly ) {
38- assert . deepStrictEqual ( newProjectPkg , packageContent ) ;
39- } else {
40- await writeFile ( packagePath , JSON . stringify ( newProjectPkg , null , 2 ) ) ;
41- }
34+ await syncFile ( checkOnly , pkgJsonPath , await formatPackage ( newProjectPkg ) ) ;
4235
4336 rootPackageJson [ 'devDependencies' ] = rootPackageJson [ 'devDependencies' ] ?? { } ;
44- Object . entries ( packageContent )
37+ Object . entries ( syncFile )
4538 . filter ( ( [ k ] ) => k . toLowerCase ( ) . includes ( 'dependencies' ) )
4639 . forEach ( ( [ k , v ] ) => {
4740 const rootPkgDeps = rootPackageJson [ 'devDependencies' ] ?? { } ;
@@ -64,22 +57,12 @@ module.exports.packageJson = async checkOnly => ( {
6457 } ;
6558 } ) ;
6659 } ,
67- tearDown : async ( proto , projects , _ , { rootJson : rootJson , rootJsonStr : rootJsonStr , rootPath } ) => {
60+ tearDown : async ( proto , projects , _ , { rootJson, rootPath } ) => {
6861 rootJson [ 'devDependencies' ] = {
6962 ...rootJson [ 'devDependencies' ] ,
7063 ...Object . fromEntries ( projects . map ( p => [ p . pkgName , `file:${ p . path } ` ] ) ) ,
7164 } ;
72- await writeFile ( rootPath , `${ JSON . stringify ( rootJson , null , 2 ) } \n` ) ;
73- if ( checkOnly ) {
74- try {
75- await formatPackages ( rootPath ) ;
76- await assertDiffFile ( rootPath , rootJsonStr , true ) ;
77- } finally {
78- await writeFile ( rootPath , rootJsonStr ) ;
79- }
80- } else {
81- await formatPackages ( rootPath , ...projects . map ( p => normalizePath ( resolve ( p . path , 'package.json' ) ) ) ) ;
82- }
65+ await syncFile ( checkOnly , rootPath , await formatPackage ( rootJson ) ) ;
8366 } ,
8467 handleFile : filename => / ( \/ | ^ ) p a c k a g e \. j s o n $ / . test ( filename ) ,
8568} ) ;
0 commit comments