@@ -12,7 +12,68 @@ function convertComponentsIdToValidPackageName(registryPrefix: string, id: strin
12
12
return `${ registryPrefix } /${ id . replace ( / \/ / g, '.' ) } ` ;
13
13
}
14
14
15
+ export type PackageJsonProps = {
16
+ name ?: string ;
17
+ version ?: string ;
18
+ homepage ?: string ;
19
+ main ?: string ;
20
+ dependencies ?: Record < string , any > ;
21
+ devDependencies ?: Record < string , any > ;
22
+ peerDependencies ?: Record < string , any > ;
23
+ license ?: string ;
24
+ scripts ?: Record < string , any > ;
25
+ workspaces ?: string [ ] ;
26
+ private ?: boolean ;
27
+ } ;
28
+
15
29
export default class PackageJson {
30
+ name ?: string ;
31
+ version ?: string ;
32
+ homepage ?: string ;
33
+ main ?: string ;
34
+ dependencies ?: Record < string , any > ;
35
+ devDependencies ?: Record < string , any > ;
36
+ peerDependencies ?: Record < string , any > ;
37
+ componentRootFolder ?: string ; // path where to write the package.json
38
+ license ?: string ;
39
+ scripts ?: Record < string , any > ;
40
+ workspaces ?: string [ ] ;
41
+
42
+ constructor (
43
+ componentRootFolder : string ,
44
+ {
45
+ name,
46
+ version,
47
+ homepage,
48
+ main,
49
+ dependencies,
50
+ devDependencies,
51
+ peerDependencies,
52
+ license,
53
+ scripts,
54
+ workspaces
55
+ } : PackageJsonProps
56
+ ) {
57
+ this . name = name ;
58
+ this . version = version ;
59
+ this . homepage = homepage ;
60
+ this . main = main ;
61
+ this . dependencies = dependencies ;
62
+ this . devDependencies = devDependencies ;
63
+ this . peerDependencies = peerDependencies ;
64
+ this . componentRootFolder = componentRootFolder ;
65
+ this . license = license ;
66
+ this . scripts = scripts ;
67
+ this . workspaces = workspaces ;
68
+ }
69
+
70
+ static loadSync ( componentRootFolder : string ) : PackageJson | null {
71
+ const composedPath = composePath ( componentRootFolder ) ;
72
+ if ( ! PackageJson . hasExisting ( componentRootFolder ) ) return null ;
73
+ const componentJsonObject = fs . readJsonSync ( composedPath ) ;
74
+ return new PackageJson ( componentRootFolder , componentJsonObject ) ;
75
+ }
76
+
16
77
static hasExisting ( componentRootFolder : string ) : boolean {
17
78
const packageJsonPath = composePath ( componentRootFolder ) ;
18
79
return fs . pathExistsSync ( packageJsonPath ) ;
@@ -72,16 +133,6 @@ export default class PackageJson {
72
133
return null ;
73
134
}
74
135
75
- /*
76
- * load package.json from path
77
- */
78
- static async getPackageJsonSync ( pathStr : string ) {
79
- const getRawObject = ( ) => fs . readJsonSync ( composePath ( pathStr ) ) ;
80
- const exist = PackageJson . hasExisting ( pathStr ) ;
81
- if ( exist ) return getRawObject ( ) ;
82
- return null ;
83
- }
84
-
85
136
/*
86
137
* save package.json in path
87
138
*/
0 commit comments