1+ const
2+ fs = require ( 'fs' ) ,
3+ path = require ( 'path' ) ,
4+ root = path . resolve ( __dirname , '../..' ) ,
5+ resolvePath = file => path . resolve ( root , file ) ,
6+ { blue } = require ( 'chalk' )
7+
8+ const writeJson = function ( file , json ) {
9+ return fs . writeFileSync ( file , JSON . stringify ( json , null , 2 ) + '\n' , 'utf-8' )
10+ }
11+
12+ module . exports . syncAppExt = function ( both = true ) {
13+ // make sure this project has an app-extension project
14+ const appExtDir = resolvePath ( 'app-extension' )
15+ if ( ! fs . existsSync ( appExtDir ) ) {
16+ return
17+ }
18+
19+ // make sure this project has an ui project
20+ const uiDir = resolvePath ( 'ui' )
21+ if ( ! fs . existsSync ( uiDir ) ) {
22+ return
23+ }
24+
25+ // get version and name from ui package.json
26+ const { name, version } = require ( resolvePath ( resolvePath ( 'ui/package.json' ) ) )
27+
28+ // read app-ext package.json
29+ const appExtFile = resolvePath ( 'app-extension/package.json' )
30+ let appExtJson = require ( appExtFile ) ,
31+ finished = false
32+
33+ // sync version numbers
34+ if ( both === true ) {
35+ appExtJson . version = version
36+ }
37+
38+ // check dependencies
39+ if ( appExtJson . dependencies !== void 0 ) {
40+ if ( appExtJson . dependencies [ name ] !== void 0 ) {
41+ appExtJson . dependencies [ name ] = '^' + version
42+ finished = true
43+ }
44+ }
45+ // check devDependencies, if not finished
46+ if ( finished === false && appExtJson . devDependencies !== void 0 ) {
47+ if ( appExtJson . devDependencies [ name ] !== void 0 ) {
48+ appExtJson . devDependencies [ name ] = '^' + version
49+ finished = true
50+ }
51+ }
52+
53+ if ( finished === true ) {
54+ writeJson ( appExtFile , appExtJson )
55+ console . log ( ` ⭐️ App Extension ${ blue ( appExtJson . name ) } synced.\n` )
56+ return
57+ }
58+
59+ console . error ( ` App Extension version and dependency NOT synced.\n` )
60+ }
0 commit comments