1
1
#!/usr/bin/env node
2
2
const childProcess = require ( "child_process" ) ;
3
3
const path = require ( "path" ) ;
4
- const opticssDir = path . resolve ( process . argv [ 2 ] ) ;
4
+ const fs = require ( "fs" )
5
+ let args = process . argv . slice ( 2 ) ;
6
+ let opts = {
7
+ hard : false ,
8
+ opticssDir : null ,
9
+ } ;
10
+ function processArg ( arg ) {
11
+ if ( arg === "--hard" ) {
12
+ opts . hard = true ;
13
+ } else if ( arg . startsWith ( "-" ) ) {
14
+ throw new Error ( `unrecognize option: ${ arg } ` ) ;
15
+ } else if ( opts . opticssDir === null ) {
16
+ opts . opticssDir = path . resolve ( arg ) ;
17
+ } else {
18
+ throw new Error ( `unrecognize argument: ${ arg } ` ) ;
19
+ }
20
+ }
21
+
22
+ while ( args . length > 0 ) {
23
+ processArg ( args . shift ( ) ) ;
24
+ }
25
+
26
+ if ( ! opts . opticssDir ) {
27
+ throw new Error ( "No directory for opticss provided." ) ;
28
+ }
5
29
6
30
function getLernaPackageDirs ( monoRepoDir ) {
7
31
let lernaSpec = require ( path . join ( monoRepoDir , "lerna.json" ) ) ;
@@ -17,6 +41,7 @@ const blocksDir = path.resolve(__dirname, "..");
17
41
const blocksPackageDirs = getLernaPackageDirs ( blocksDir ) ;
18
42
19
43
let depToPackages = { } ;
44
+ let dirToJSON = { }
20
45
21
46
function recordDependencies ( deps , dir ) {
22
47
if ( ! deps ) return ;
@@ -30,26 +55,63 @@ function recordDependencies(deps, dir) {
30
55
31
56
for ( let dir of blocksPackageDirs ) {
32
57
let pkg = getPackageJson ( dir ) ;
58
+ dirToJSON [ dir ] = pkg ;
33
59
recordDependencies ( pkg . dependencies , dir ) ;
34
60
recordDependencies ( pkg . devDependencies , dir ) ;
35
61
}
36
62
37
- for ( let dir of getLernaPackageDirs ( opticssDir ) ) {
38
- let pkg = getPackageJson ( dir ) ;
39
- let name = pkg . name ;
40
- let depDirs = depToPackages [ name ] ;
41
- if ( depDirs ) {
42
- try {
43
- console . log ( childProcess . execSync ( `cd ${ dir } && yarn unlink` , { encoding : "utf8" } ) ) ;
44
- } catch ( e ) {
45
- //ignore
63
+ function symlink ( ) {
64
+ for ( let dir of getLernaPackageDirs ( opts . opticssDir ) ) {
65
+ let pkg = getPackageJson ( dir ) ;
66
+ let name = pkg . name ;
67
+ let depDirs = depToPackages [ name ] ;
68
+ if ( depDirs ) {
69
+ try {
70
+ console . log ( childProcess . execSync ( `cd ${ dir } && yarn unlink` , { encoding : "utf8" } ) ) ;
71
+ } catch ( e ) {
72
+ //ignore
73
+ }
74
+ console . log ( childProcess . execSync ( `cd ${ dir } && yarn link` , { encoding : "utf8" } ) ) ;
75
+ console . log ( `linking ${ name } @${ pkg . version } to shared packages` ) ;
76
+ console . log ( childProcess . execSync ( `cd ${ blocksDir } && yarn link ${ name } ` , { encoding : "utf8" } ) ) ;
77
+ for ( let depDir of depDirs ) {
78
+ console . log ( `linking ${ name } @${ pkg . version } to ${ path . relative ( path . resolve ( "." ) , depDir ) } ` ) ;
79
+ console . log ( childProcess . execSync ( `cd ${ depDir } && yarn link ${ name } ` , { encoding : "utf8" } ) ) ;
80
+ }
46
81
}
47
- console . log ( childProcess . execSync ( `cd ${ dir } && yarn link` , { encoding : "utf8" } ) ) ;
48
- console . log ( `linking ${ name } @${ pkg . version } to shared packages` ) ;
49
- console . log ( childProcess . execSync ( `cd ${ blocksDir } && yarn link ${ name } ` , { encoding : "utf8" } ) ) ;
50
- for ( let depDir of depDirs ) {
51
- console . log ( `linking ${ name } @${ pkg . version } to ${ path . relative ( path . resolve ( "." ) , depDir ) } ` ) ;
52
- console . log ( childProcess . execSync ( `cd ${ depDir } && yarn link ${ name } ` , { encoding : "utf8" } ) ) ;
82
+ }
83
+ }
84
+
85
+ function setDependencyToFile ( name , dependencyDir , dependentDir ) {
86
+ let pkg = dirToJSON [ dependentDir ] ;
87
+ if ( pkg . dependencies [ name ] ) {
88
+ pkg . dependencies [ name ] = `file:${ dependencyDir } ` ;
89
+ }
90
+ if ( pkg . devDependencies [ name ] ) {
91
+ pkg . devDependencies [ name ] = `file:${ dependencyDir } ` ;
92
+ }
93
+ }
94
+
95
+ function hardlink ( ) {
96
+ for ( let depDir of getLernaPackageDirs ( opts . opticssDir ) ) {
97
+ let pkg = getPackageJson ( depDir ) ;
98
+ let name = pkg . name ;
99
+ let depDirs = depToPackages [ name ] ;
100
+ if ( depDirs ) {
101
+ for ( let dir of depDirs ) {
102
+ setDependencyToFile ( name , depDir , dir )
103
+ }
53
104
}
54
105
}
106
+ for ( let dir of blocksPackageDirs ) {
107
+ let pkg = dirToJSON [ dir ] ;
108
+ let updated = JSON . stringify ( pkg , null , 2 ) ;
109
+ fs . writeFileSync ( path . join ( dir , "package.json" ) , updated ) ;
110
+ }
111
+ }
112
+
113
+ if ( opts . hard ) {
114
+ hardlink ( ) ;
115
+ } else {
116
+ symlink ( ) ;
55
117
}
0 commit comments