Skip to content

Commit

Permalink
Fix missing files in npm distribution (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Mar 10, 2020
1 parent 9b46ce3 commit 22e56e7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 15 deletions.
1 change: 0 additions & 1 deletion buildifier/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ exports_files(
exports_files(
[
"README.md",
"package.json",
],
visibility = ["//buildifier/npm:__pkg__"],
)
36 changes: 30 additions & 6 deletions buildifier/npm/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm", "nodejs_test")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

copy_file(
Expand All @@ -14,6 +14,28 @@ genrule(
cmd = "sed s/_TOOL_/buildifier/ $< > $@",
)

# npm rules live in this subdirectory to avoid a load() statement
# in the parent package leaking to users. This means we need
# to copy the output files so the pkg_npm will find them in the
# output directory for this package.
_PARENT_PACKAGE_FILES = [
"README.md",
"buildifier-darwin_amd64",
"buildifier-linux_amd64",
"buildifier-windows_amd64.exe",
]

[
copy_file(
name = "copy_%s" % s,
# go_binary doesn't give a predeclared output for
# the file in "out" so we have to construct a
# label to reference the go_binary rule itself.
src = "//buildifier:%s" % s.split("_")[0],
out = s,
) for s in _PARENT_PACKAGE_FILES
]

pkg_npm(
name = "buildifier",
srcs = [
Expand All @@ -22,9 +44,11 @@ pkg_npm(
deps = [
"LICENSE",
"buildifier.js",
"//buildifier:README.md",
"//buildifier:buildifier-darwin",
"//buildifier:buildifier-linux",
"//buildifier:buildifier-windows",
],
] + _PARENT_PACKAGE_FILES,
)

nodejs_test(
name = "integration_test",
data = [":buildifier"],
entry_point = "test.js",
)
12 changes: 12 additions & 0 deletions buildifier/npm/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const dir = require('path').join(
process.env['TEST_SRCDIR'],
process.env['BAZEL_WORKSPACE'],
'buildifier/npm/buildifier');
process.chdir(dir);
const {stdout} = require('child_process').spawnSync(
process.argv0,
['./buildifier.js', '--help'],
{encoding: 'utf-8'});
if (!/usage: buildifier/.test(stdout)) {
throw new Error('buildifier --help should include usage: buildifier');
}
1 change: 0 additions & 1 deletion buildozer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ go_binary(
exports_files(
[
"README.md",
"package.json",
],
visibility = ["//buildozer/npm:__pkg__"],
)
38 changes: 31 additions & 7 deletions buildozer/npm/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm", "nodejs_test")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@buildozer_npm_deps//typescript:index.bzl", "tsc")

Expand Down Expand Up @@ -34,6 +34,28 @@ genrule(
cmd = "sed s/_TOOL_/buildozer/ $< > $@",
)

# npm rules live in this subdirectory to avoid a load() statement
# in the parent package leaking to users. This means we need
# to copy the output files so the pkg_npm will find them in the
# output directory for this package.
_PARENT_PACKAGE_FILES = [
"README.md",
"buildozer-darwin_amd64",
"buildozer-linux_amd64",
"buildozer-windows_amd64.exe",
]

[
copy_file(
name = "copy_%s" % s,
# go_binary doesn't give a predeclared output for
# the file in "out" so we have to construct a
# label to reference the go_binary rule itself.
src = "//buildozer:%s" % s.split("_")[0],
out = s,
) for s in _PARENT_PACKAGE_FILES
]

pkg_npm(
name = "buildozer",
srcs = [
Expand All @@ -43,10 +65,12 @@ pkg_npm(
"LICENSE",
"buildozer.js",
"index.js",
"index.d.ts",
"//buildozer:README.md",
"//buildozer:buildozer-darwin",
"//buildozer:buildozer-linux",
"//buildozer:buildozer-windows",
],
"index.d.ts"
] + _PARENT_PACKAGE_FILES
)

nodejs_test(
name = "integration_test",
data = [":buildozer"],
entry_point = "test.js",
)
23 changes: 23 additions & 0 deletions buildozer/npm/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const dir = require('path').join(
process.env['TEST_SRCDIR'],
process.env['BAZEL_WORKSPACE'],
'buildozer/npm/buildozer');
process.chdir(dir);
const {stderr} = require('child_process').spawnSync(
process.argv0,
['./buildozer.js', '--help'],
{encoding: 'utf-8'});
if (!/Usage of .*buildozer/.test(stderr)) {
throw new Error('buildozer --help should include usage: buildifier');
}

process.chdir(process.env['TEST_TMPDIR']);
const buildozer = require(dir);
const fs = require('fs');
fs.mkdirSync('foo');
fs.writeFileSync('foo/BUILD', '');
buildozer.run({commands: ['new_load //:some.bzl some_rule'], targets: ['//foo:__pkg__']});
const content = fs.readFileSync('foo/BUILD', 'utf-8');
if (!content.includes('load("//:some.bzl", "some_rule")')) {
throw new Error('buildozer generated file should include load statement');
}

0 comments on commit 22e56e7

Please sign in to comment.