1
1
import path from 'path' ;
2
2
import url from 'url' ;
3
+ import fse from 'fs-extra' ;
3
4
4
5
/**
5
6
* Returns the full path of the root directory of this repository.
@@ -10,3 +11,32 @@ export function getWorkspaceRoot() {
10
11
const workspaceRoot = path . resolve ( currentDirectory , '..' ) ;
11
12
return workspaceRoot ;
12
13
}
14
+
15
+ /**
16
+ * Returns the version and destructured values of the version as env variables to be replaced.
17
+ */
18
+ export async function getVersionEnvVariables ( ) {
19
+ const packageJsonData = await fse . readFile ( path . resolve ( './package.json' ) , 'utf8' ) ;
20
+ const { version = null } = JSON . parse ( packageJsonData ) ;
21
+
22
+ if ( ! version ) {
23
+ throw new Error ( 'Could not find the version in the package.json' ) ;
24
+ }
25
+
26
+ const [ versionNumber , preReleaseInfo ] = version . split ( '-' ) ;
27
+ const [ major , minor , patch ] = versionNumber . split ( '.' ) ;
28
+ const [ preReleaseLabel , preReleaseNumber ] = preReleaseInfo ? preReleaseInfo . split ( '.' ) : [ ] ;
29
+
30
+ if ( ! major || ! minor || ! patch ) {
31
+ throw new Error ( `Couldn't parse version from package.json` ) ;
32
+ }
33
+
34
+ return {
35
+ MUI_VERSION : version ,
36
+ MUI_MAJOR_VERSION : major ,
37
+ MUI_MINOR_VERSION : minor ,
38
+ MUI_PATCH_VERSION : patch ,
39
+ MUI_PRERELEASE_LABEL : preReleaseLabel ,
40
+ MUI_PRERELEASE_NUMBER : preReleaseNumber ,
41
+ } ;
42
+ }
0 commit comments