@@ -3,7 +3,6 @@ import fs from 'fs-extra';
3
3
import R from 'ramda' ;
4
4
import parents from 'parents' ;
5
5
import path from 'path' ;
6
- import { PackageJsonAlreadyExists , PackageJsonNotFound } from '../exceptions' ;
7
6
import { PACKAGE_JSON } from '../constants' ;
8
7
9
8
function composePath ( componentRootFolder : string ) {
@@ -12,164 +11,8 @@ function composePath(componentRootFolder: string) {
12
11
function convertComponentsIdToValidPackageName ( registryPrefix : string , id : string ) : string {
13
12
return `${ registryPrefix } /${ id . replace ( / \/ / g, '.' ) } ` ;
14
13
}
15
- function convertComponentsToValidPackageNames (
16
- registryPrefix : string ,
17
- bitDependencies : Record < string , any >
18
- ) : Record < string , any > {
19
- const obj = { } ;
20
- if ( R . isEmpty ( bitDependencies ) || R . isNil ( bitDependencies ) ) return obj ;
21
- Object . keys ( bitDependencies ) . forEach ( key => {
22
- const name = convertComponentsIdToValidPackageName ( registryPrefix , key ) ;
23
- obj [ name ] = bitDependencies [ key ] ;
24
- } ) ;
25
- return obj ;
26
- }
27
-
28
- const PackageJsonPropsNames = [
29
- 'name' ,
30
- 'version' ,
31
- 'homepage' ,
32
- 'main' ,
33
- 'dependencies' ,
34
- 'devDependencies' ,
35
- 'peerDependencies' ,
36
- 'license' ,
37
- 'scripts' ,
38
- 'workspaces' ,
39
- 'private'
40
- ] ;
41
-
42
- export type PackageJsonProps = {
43
- name ?: string ;
44
- version ?: string ;
45
- homepage ?: string ;
46
- main ?: string ;
47
- dependencies ?: Record < string , any > ;
48
- devDependencies ?: Record < string , any > ;
49
- peerDependencies ?: Record < string , any > ;
50
- license ?: string ;
51
- scripts ?: Record < string , any > ;
52
- workspaces ?: string [ ] ;
53
- private ?: boolean ;
54
- } ;
55
14
56
15
export default class PackageJson {
57
- name : string ;
58
- version : string ;
59
- homepage : string ;
60
- main : string ;
61
- dependencies : Record < string , any > ;
62
- devDependencies : Record < string , any > ;
63
- peerDependencies : Record < string , any > ;
64
- componentRootFolder : string ; // path where to write the package.json
65
- license : string ;
66
- scripts : Record < string , any > ;
67
- workspaces : string [ ] ;
68
-
69
- constructor (
70
- componentRootFolder : string ,
71
- {
72
- name,
73
- version,
74
- homepage,
75
- main,
76
- dependencies,
77
- devDependencies,
78
- peerDependencies,
79
- license,
80
- scripts,
81
- workspaces
82
- } : PackageJsonProps
83
- ) {
84
- this . name = name ;
85
- this . version = version ;
86
- this . homepage = homepage ;
87
- this . main = main ;
88
- this . dependencies = dependencies ;
89
- this . devDependencies = devDependencies ;
90
- this . peerDependencies = peerDependencies ;
91
- this . componentRootFolder = componentRootFolder ;
92
- this . license = license ;
93
- this . scripts = scripts ;
94
- this . workspaces = workspaces ;
95
- }
96
-
97
- toPlainObject ( ) : Record < string , any > {
98
- const result = { } ;
99
- const addToResult = propName => {
100
- result [ propName ] = this [ propName ] ;
101
- } ;
102
-
103
- R . forEach ( addToResult , PackageJsonPropsNames ) ;
104
- // @ts -ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
105
- if ( this . workspaces ) result . private = true ;
106
- return result ;
107
- }
108
-
109
- toJson ( readable = true ) {
110
- if ( ! readable ) return JSON . stringify ( this . toPlainObject ( ) ) ;
111
- return JSON . stringify ( this . toPlainObject ( ) , null , 4 ) ;
112
- }
113
-
114
- addDependencies ( bitDependencies : Record < string , any > , registryPrefix : string ) {
115
- this . dependencies = Object . assign (
116
- { } ,
117
- this . dependencies ,
118
- convertComponentsToValidPackageNames ( registryPrefix , bitDependencies )
119
- ) ;
120
- }
121
-
122
- addDevDependencies ( bitDevDependencies : Record < string , any > , registryPrefix : string ) {
123
- this . devDependencies = Object . assign (
124
- { } ,
125
- this . devDependencies ,
126
- convertComponentsToValidPackageNames ( registryPrefix , bitDevDependencies )
127
- ) ;
128
- }
129
-
130
- static hasExisting ( componentRootFolder : string , throws = false ) : boolean {
131
- const packageJsonPath = composePath ( componentRootFolder ) ;
132
- const exists = fs . pathExistsSync ( packageJsonPath ) ;
133
- if ( ! exists && throws ) {
134
- throw new PackageJsonNotFound ( packageJsonPath ) ;
135
- }
136
- return exists ;
137
- }
138
-
139
- async write ( { override = true } : { override ?: boolean } ) : Promise < boolean > {
140
- if ( ! override && PackageJson . hasExisting ( this . componentRootFolder ) ) {
141
- return Promise . reject ( new PackageJsonAlreadyExists ( this . componentRootFolder ) ) ;
142
- }
143
- const plain = this . toPlainObject ( ) ;
144
- return fs . outputJSON ( composePath ( this . componentRootFolder ) , plain , { spaces : 2 } ) ;
145
- }
146
-
147
- static create ( componentRootFolder : string ) : PackageJson {
148
- return new PackageJson ( componentRootFolder , { } ) ;
149
- }
150
-
151
- static ensure ( componentRootFolder ) : Promise < PackageJson > {
152
- return this . load ( componentRootFolder ) ;
153
- }
154
-
155
- static fromPlainObject ( componentRootFolder : string , object : Record < string , any > ) {
156
- return new PackageJson ( componentRootFolder , object ) ;
157
- }
158
-
159
- static async load ( componentRootFolder : string , throwError = true ) : Promise < PackageJson > {
160
- const composedPath = composePath ( componentRootFolder ) ;
161
- if ( ! PackageJson . hasExisting ( componentRootFolder , throwError ) ) return null ;
162
- const componentJsonObject = await fs . readJson ( composedPath ) ;
163
- return new PackageJson ( componentRootFolder , componentJsonObject ) ;
164
- }
165
-
166
- static loadSync ( componentRootFolder : string , throwError = true ) : PackageJson {
167
- const composedPath = composePath ( componentRootFolder ) ;
168
- if ( ! PackageJson . hasExisting ( componentRootFolder , throwError ) ) return null ;
169
- const componentJsonObject = fs . readJsonSync ( composedPath ) ;
170
- return new PackageJson ( componentRootFolder , componentJsonObject ) ;
171
- }
172
-
173
16
/**
174
17
* Taken from this package (with some minor changes):
175
18
* https://www.npmjs.com/package/find-package
@@ -230,26 +73,6 @@ export default class PackageJson {
230
73
return fs . outputJSON ( composePath ( pathStr ) , obj , { spaces : 2 } ) ;
231
74
}
232
75
233
- /*
234
- * For an existing package.json file of the root project, we don't want to do any change, other than what needed.
235
- * That's why this method doesn't use the 'load' and 'write' methods of this class. Otherwise, it'd write only the
236
- * PackageJsonPropsNames attributes.
237
- * Also, in case there is no package.json file in this project, it generates a new one with only the 'dependencies'
238
- * attribute. Nothing more, nothing less.
239
- */
240
- static async addComponentsIntoExistingPackageJson (
241
- rootDir : string ,
242
- components : Record < string , any > ,
243
- registryPrefix : string
244
- ) {
245
- const packageJson = ( await PackageJson . getPackageJson ( rootDir ) ) || { dependencies : { } } ;
246
- packageJson . dependencies = Object . assign (
247
- { } ,
248
- packageJson . dependencies ,
249
- convertComponentsToValidPackageNames ( registryPrefix , components )
250
- ) ;
251
- await PackageJson . saveRawObject ( rootDir , packageJson ) ;
252
- }
253
76
/*
254
77
* For an existing package.json file of the root project, we don't want to do any change, other than what needed.
255
78
* That's why this method doesn't use the 'load' and 'write' methods of this class. Otherwise, it'd write only the
0 commit comments