@@ -186,24 +186,63 @@ node_module_library(
186186 * Generates all BUILD & bzl files for a package.
187187 */
188188function generatePackageBuildFiles ( pkg : Dep ) {
189+ // If a BUILD file was shipped with the package, append its contents to the end of
190+ // what we generate for the package.
191+ let buildFilePath : string | undefined ;
192+ if ( pkg . _files . includes ( 'BUILD' ) ) buildFilePath = 'BUILD' ;
193+ if ( pkg . _files . includes ( 'BUILD.bazel' ) ) buildFilePath = 'BUILD.bazel' ;
189194 let buildFile = printPackage ( pkg ) ;
195+ if ( buildFilePath ) {
196+ buildFile = buildFile + '\n' +
197+ fs . readFileSync ( path . join ( 'node_modules' , pkg . _dir , buildFilePath ) , 'utf-8' ) ;
198+ } else {
199+ buildFilePath = 'BUILD.bazel'
200+ }
190201
191- const binBuildFile = printPackageBin ( pkg ) ;
192- if ( binBuildFile . length ) {
193- writeFileSync (
194- path . posix . join ( pkg . _dir , 'bin' , 'BUILD.bazel' ) , BUILD_FILE_HEADER + binBuildFile ) ;
202+ // If the package didn't ship a bin/BUILD file, generate one.
203+ if ( ! pkg . _files . includes ( 'bin/BUILD.bazel' ) && ! pkg . _files . includes ( 'bin/BUILD' ) ) {
204+ const binBuildFile = printPackageBin ( pkg ) ;
205+ if ( binBuildFile . length ) {
206+ writeFileSync (
207+ path . posix . join ( pkg . _dir , 'bin' , 'BUILD.bazel' ) , BUILD_FILE_HEADER + binBuildFile ) ;
208+ }
195209 }
196210
197- const indexFile = printIndexBzl ( pkg ) ;
198- if ( indexFile . length ) {
199- writeFileSync ( path . posix . join ( pkg . _dir , 'index.bzl' ) , indexFile ) ;
200- buildFile = `${ buildFile }
211+ // If there's an index.bzl in the package then copy all the package's files
212+ // other than the BUILD file which we'll write below.
213+ // (maybe we shouldn't copy .js though, since it belongs under node_modules?)
214+ if ( pkg . _files . includes ( 'index.bzl' ) ) {
215+ pkg . _files . filter ( f => f !== 'BUILD' && f !== 'BUILD.bazel' ) . forEach ( file => {
216+ if ( / ^ n o d e _ m o d u l e s [ / \\ ] / . test ( file ) ) {
217+ // don't copy over nested node_modules
218+ return ;
219+ }
220+ // don't support rootPath here?
221+ let destFile = path . posix . join ( pkg . _dir , file ) ;
222+ const basename = path . basename ( file ) ;
223+ const basenameUc = basename . toUpperCase ( ) ;
224+ // Bazel BUILD files from npm distribution would have been renamed earlier with a _ prefix so
225+ // we restore the name on the copy
226+ if ( basenameUc === '_BUILD' || basenameUc === '_BUILD.BAZEL' ) {
227+ destFile = path . posix . join ( path . dirname ( destFile ) , basename . substr ( 1 ) ) ;
228+ }
229+ const src = path . posix . join ( 'node_modules' , pkg . _dir , file ) ;
230+
231+ mkdirp ( path . dirname ( destFile ) ) ;
232+ fs . copyFileSync ( src , destFile ) ;
233+ } ) ;
234+ } else {
235+ const indexFile = printIndexBzl ( pkg ) ;
236+ if ( indexFile . length ) {
237+ writeFileSync ( path . posix . join ( pkg . _dir , 'index.bzl' ) , indexFile ) ;
238+ buildFile += `
201239# For integration testing
202240exports_files(["index.bzl"])
203241` ;
242+ }
204243 }
205244
206- writeFileSync ( path . posix . join ( pkg . _dir , 'BUILD.bazel' ) , BUILD_FILE_HEADER + buildFile ) ;
245+ writeFileSync ( path . posix . join ( pkg . _dir , buildFilePath ) , BUILD_FILE_HEADER + buildFile ) ;
207246}
208247
209248/**
0 commit comments