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' ;
22
33import { JSONObject } from '../types' ;
4- import { SpawnError , spawnProcess } from '../utils' ;
4+ import { safeJsonParse , SpawnError , spawnProcess , splitLines } from '../utils' ;
55import { Packager } from './packager' ;
66
77/**
@@ -36,7 +36,7 @@ export class Yarn implements Packager {
3636 }
3737 }
3838
39- getProdDependencies ( cwd , depth ) {
39+ getProdDependencies ( cwd : string , depth ?: number ) {
4040 const command = / ^ w i n / . test ( process . platform ) ? 'yarn.cmd' : 'yarn' ;
4141 const args = [ 'list' , `--depth=${ depth || 1 } ` , '--json' , '--production' ] ;
4242
@@ -47,33 +47,36 @@ export class Yarn implements Packager {
4747 try {
4848 processOutput = spawnProcess ( command , args , { cwd } ) ;
4949 } 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 ;
6559 }
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 ;
6668 }
6769
68- throw err ;
70+ processOutput = { stdout : err . stdout } ;
6971 }
7072
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 ) => {
7477 const splitModule = split ( '@' , tree . name ) ;
7578 // If we have a scoped module we have to re-add the @
76- if ( startsWith ( '@' , tree . name ) ) {
79+ if ( tree . name . startsWith ( '@' ) ) {
7780 splitModule . splice ( 0 , 1 ) ;
7881 splitModule [ 0 ] = '@' + splitModule [ 0 ] ;
7982 }
@@ -101,28 +104,34 @@ export class Yarn implements Packager {
101104 while ( ( match = fileVersionMatcher . exec ( lockfile ) ) !== null ) {
102105 replacements . push ( {
103106 oldRef : match [ 1 ] ,
104- newRef : replace ( / \\ / g , '/' , `${ pathToPackageRoot } /${ match [ 1 ] } ` )
107+ newRef : `${ pathToPackageRoot } /${ match [ 1 ] } ` . replace ( / \\ / g , '/' )
105108 } ) ;
106109 }
107110
108111 // 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 ) ;
110113 }
111114
112- install ( cwd ) {
115+ install ( cwd : string , packagerOptions ? ) {
113116 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+ }
115123
116124 spawnProcess ( command , args , { cwd } ) ;
117125 }
118126
119127 // "Yarn install" prunes automatically
120- prune ( cwd ) {
121- return this . install ( cwd ) ;
128+ prune ( cwd : string , packagerOptions ? ) {
129+ return this . install ( cwd , packagerOptions ) ;
122130 }
123131
124132 runScripts ( cwd , scriptNames : string [ ] ) {
125133 const command = / ^ w i n / . test ( process . platform ) ? 'yarn.cmd' : 'yarn' ;
134+
126135 scriptNames . forEach ( scriptName => spawnProcess ( command , [ 'run' , scriptName ] , { cwd } ) ) ;
127136 }
128137}
0 commit comments