1
- import { any , head , isEmpty , join , pathOr , reduce , replace , split , startsWith , tail } from 'ramda' ;
1
+ import { any , head , isEmpty , join , pathOr , split , tail } from 'ramda' ;
2
2
3
3
import { JSONObject } from '../types' ;
4
- import { SpawnError , spawnProcess } from '../utils' ;
4
+ import { safeJsonParse , SpawnError , spawnProcess , splitLines } from '../utils' ;
5
5
import { Packager } from './packager' ;
6
6
7
7
/**
@@ -36,7 +36,7 @@ export class Yarn implements Packager {
36
36
}
37
37
}
38
38
39
- getProdDependencies ( cwd , depth ) {
39
+ getProdDependencies ( cwd : string , depth ?: number ) {
40
40
const command = / ^ w i n / . test ( process . platform ) ? 'yarn.cmd' : 'yarn' ;
41
41
const args = [ 'list' , `--depth=${ depth || 1 } ` , '--json' , '--production' ] ;
42
42
@@ -47,33 +47,36 @@ export class Yarn implements Packager {
47
47
try {
48
48
processOutput = spawnProcess ( command , args , { cwd } ) ;
49
49
} catch ( err ) {
50
- if ( err instanceof SpawnError ) {
51
- // Only exit with an error if we have critical npm errors for 2nd level inside
52
- const errors = err . stderr ?. split ( '\n' ) ?? [ ] ;
53
- const failed = errors . reduce ( ( f , error ) => {
54
- if ( f ) {
55
- return true ;
56
- }
57
- return (
58
- ! isEmpty ( error ) &&
59
- ! any ( ignoredError => error . startsWith ( `npm ERR! ${ ignoredError . npmError } ` ) , ignoredYarnErrors )
60
- ) ;
61
- } , false ) ;
62
-
63
- if ( ! failed && ! isEmpty ( err . stdout ) ) {
64
- return { stdout : err . stdout } ;
50
+ if ( ! ( err instanceof SpawnError ) ) {
51
+ throw err ;
52
+ }
53
+
54
+ // Only exit with an error if we have critical npm errors for 2nd level inside
55
+ const errors = err . stderr ?. split ( '\n' ) ?? [ ] ;
56
+ const failed = errors . reduce ( ( f , error ) => {
57
+ if ( f ) {
58
+ return true ;
65
59
}
60
+ return (
61
+ ! isEmpty ( error ) &&
62
+ ! any ( ignoredError => error . startsWith ( `npm ERR! ${ ignoredError . npmError } ` ) , ignoredYarnErrors )
63
+ ) ;
64
+ } , false ) ;
65
+
66
+ if ( failed || isEmpty ( err . stdout ) ) {
67
+ throw err ;
66
68
}
67
69
68
- throw err ;
70
+ processOutput = { stdout : err . stdout } ;
69
71
}
70
72
71
- const depJson = processOutput . stdout ;
72
- const parsedTree = JSON . parse ( depJson ) ;
73
- const convertTrees = reduce ( ( __ , tree : JSONObject ) => {
73
+ const lines = splitLines ( processOutput . stdout ) ;
74
+ const parsedLines = lines . map ( safeJsonParse ) ;
75
+ const parsedTree = parsedLines . find ( line => line && line . type === 'tree' ) ;
76
+ const convertTrees = ts => ts . reduce ( ( __ , tree : JSONObject ) => {
74
77
const splitModule = split ( '@' , tree . name ) ;
75
78
// If we have a scoped module we have to re-add the @
76
- if ( startsWith ( '@' , tree . name ) ) {
79
+ if ( tree . name . startsWith ( '@' ) ) {
77
80
splitModule . splice ( 0 , 1 ) ;
78
81
splitModule [ 0 ] = '@' + splitModule [ 0 ] ;
79
82
}
@@ -101,28 +104,34 @@ export class Yarn implements Packager {
101
104
while ( ( match = fileVersionMatcher . exec ( lockfile ) ) !== null ) {
102
105
replacements . push ( {
103
106
oldRef : match [ 1 ] ,
104
- newRef : replace ( / \\ / g , '/' , `${ pathToPackageRoot } /${ match [ 1 ] } ` )
107
+ newRef : `${ pathToPackageRoot } /${ match [ 1 ] } ` . replace ( / \\ / g , '/' )
105
108
} ) ;
106
109
}
107
110
108
111
// Replace all lines in lockfile
109
- return reduce ( ( __ , replacement ) => replace ( __ , replacement . oldRef , replacement . newRef ) , lockfile , replacements ) ;
112
+ return replacements . reduce ( ( __ , replacement ) => __ . replace ( replacement . oldRef , replacement . newRef ) , lockfile ) ;
110
113
}
111
114
112
- install ( cwd ) {
115
+ install ( cwd : string , packagerOptions ? ) {
113
116
const command = / ^ w i n / . test ( process . platform ) ? 'yarn.cmd' : 'yarn' ;
114
- const args = [ 'install' , '--frozen-lockfile' , '--non-interactive' ] ;
117
+ const args = [ 'install' , '--frozen-lockfile' , '--non-interactive' ] ;
118
+
119
+ // Convert supported packagerOptions
120
+ if ( packagerOptions . ignoreScripts ) {
121
+ args . push ( '--ignore-scripts' ) ;
122
+ }
115
123
116
124
spawnProcess ( command , args , { cwd } ) ;
117
125
}
118
126
119
127
// "Yarn install" prunes automatically
120
- prune ( cwd ) {
121
- return this . install ( cwd ) ;
128
+ prune ( cwd : string , packagerOptions ? ) {
129
+ return this . install ( cwd , packagerOptions ) ;
122
130
}
123
131
124
132
runScripts ( cwd , scriptNames : string [ ] ) {
125
133
const command = / ^ w i n / . test ( process . platform ) ? 'yarn.cmd' : 'yarn' ;
134
+
126
135
scriptNames . forEach ( scriptName => spawnProcess ( command , [ 'run' , scriptName ] , { cwd } ) ) ;
127
136
}
128
137
}
0 commit comments