1
- import { execUtils } from '@yarnpkg/core' ;
2
- import { Workspace , structUtils } from '@yarnpkg/core' ;
3
- import { PortablePath } from '@yarnpkg/fslib' ;
4
- import { packUtils } from '@yarnpkg/plugin-pack' ;
5
- import { createHash } from 'crypto' ;
6
- import ssri from 'ssri' ;
7
- import { URL } from 'url' ;
8
-
9
- import { normalizeRegistry } from './npmConfigUtils' ;
10
-
11
- export async function makePublishBody ( workspace : Workspace , buffer : Buffer , { access, tag, registry, gitHead} : { access : string | undefined , tag : string , registry : string , gitHead ?: string } ) {
12
- const configuration = workspace . project . configuration ;
13
-
1
+ import { execUtils , Ident } from '@yarnpkg/core' ;
2
+ import { Workspace , structUtils } from '@yarnpkg/core' ;
3
+ import { PortablePath , xfs , npath } from '@yarnpkg/fslib' ;
4
+ import { packUtils } from '@yarnpkg/plugin-pack' ;
5
+ import { createHash } from 'crypto' ;
6
+ import ssri from 'ssri' ;
7
+ import { URL } from 'url' ;
8
+
9
+ import { normalizeRegistry } from './npmConfigUtils' ;
10
+
11
+ type PublishAdditionalParams = {
12
+ access : string | undefined ;
13
+ tag : string ;
14
+ registry : string ;
15
+ gitHead ?: string ;
16
+ } ;
17
+
18
+ export async function makePublishBody ( workspace : Workspace , buffer : Buffer , { access, tag, registry, gitHead} : PublishAdditionalParams ) {
14
19
const ident = workspace . manifest . name ! ;
15
20
const version = workspace . manifest . version ! ;
16
21
@@ -19,17 +24,8 @@ export async function makePublishBody(workspace: Workspace, buffer: Buffer, {acc
19
24
const shasum = createHash ( `sha1` ) . update ( buffer ) . digest ( `hex` ) ;
20
25
const integrity = ssri . fromData ( buffer ) . toString ( ) ;
21
26
22
- if ( typeof access === `undefined` ) {
23
- if ( workspace . manifest . publishConfig && typeof workspace . manifest . publishConfig . access === `string` ) {
24
- access = workspace . manifest . publishConfig . access ;
25
- } else if ( configuration . get ( `npmPublishAccess` ) !== null ) {
26
- access = configuration . get ( `npmPublishAccess` ) ! ;
27
- } else if ( ident . scope ) {
28
- access = `restricted` ;
29
- } else {
30
- access = `public` ;
31
- }
32
- }
27
+ const publishAccess = access ?? getPublishAccess ( workspace , ident ) ;
28
+ const readmeContent = await getReadmeContent ( workspace ) ;
33
29
34
30
const raw = await packUtils . genPackageManifest ( workspace ) ;
35
31
@@ -50,7 +46,7 @@ export async function makePublishBody(workspace: Workspace, buffer: Buffer, {acc
50
46
} ,
51
47
} ,
52
48
name,
53
- access,
49
+ access : publishAccess ,
54
50
55
51
[ `dist-tags` ] : {
56
52
[ tag ] : version ,
@@ -75,6 +71,7 @@ export async function makePublishBody(workspace: Workspace, buffer: Buffer, {acc
75
71
} ,
76
72
} ,
77
73
} ,
74
+ readme : readmeContent ,
78
75
} ;
79
76
}
80
77
@@ -88,3 +85,39 @@ export async function getGitHead(workingDir: PortablePath) {
88
85
return undefined ;
89
86
}
90
87
}
88
+
89
+ export function getPublishAccess ( workspace : Workspace , ident : Ident ) : string {
90
+ const configuration = workspace . project . configuration ;
91
+
92
+ if ( workspace . manifest . publishConfig && typeof workspace . manifest . publishConfig . access === `string` )
93
+ return workspace . manifest . publishConfig . access ;
94
+
95
+ if ( configuration . get ( `npmPublishAccess` ) !== null )
96
+ return configuration . get ( `npmPublishAccess` ) ! ;
97
+
98
+ const access = ident . scope
99
+ ? `restricted`
100
+ : `public` ;
101
+
102
+ return access ;
103
+ }
104
+
105
+ export async function getReadmeContent ( workspace : Workspace ) : Promise < string > {
106
+ const readmePath = npath . toPortablePath ( `${ workspace . cwd } /README.md` ) ;
107
+
108
+ const ident = workspace . manifest . name ! ;
109
+ const packageName = structUtils . stringifyIdent ( ident ) ;
110
+
111
+ let readme = `# ${ packageName } \n` ;
112
+ try {
113
+ readme = await xfs . readFilePromise ( readmePath , `utf8` ) ;
114
+ } catch ( err ) {
115
+ if ( err . code === `ENOENT` ) {
116
+ return readme ;
117
+ } else {
118
+ throw err ;
119
+ }
120
+ }
121
+
122
+ return readme ;
123
+ }
0 commit comments