forked from bazel-contrib/rules_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes bazel-contrib#10
- Loading branch information
Showing
5 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") | ||
|
||
nodejs_binary( | ||
name = "packager", | ||
data = [ | ||
"packager.js", | ||
"@nodejs//:run_npm.sh.template", | ||
], | ||
entry_point = "build_bazel_rules_nodejs/internal/npm_package/packager.js", | ||
node_modules = "@build_bazel_rules_nodejs_npm_install_deps//:node_modules", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""The npm_package rule creates a directory containing a publishable npm artifact. | ||
It also produces two named outputs: | ||
:label.pack | ||
:label.publish | ||
These can be used with `bazel run` to create a .tgz of the package and to publish | ||
the package to the npm registry, respectively. | ||
""" | ||
|
||
load("//internal:node.bzl", "sources_aspect") | ||
|
||
def create_package(ctx, devmode_sources): | ||
"""Creates an action that produces the npm package. | ||
It copies srcs and deps into the artifact and produces the .pack and .publish | ||
scripts. | ||
Args: | ||
ctx: the skylark rule context | ||
devmode_sources: the .js files which belong in the package | ||
Returns: | ||
The tree artifact which is the publishable directory. | ||
""" | ||
|
||
package_dir = ctx.actions.declare_directory(ctx.label.name) | ||
|
||
args = ctx.actions.args() | ||
args.add(package_dir.path) | ||
args.add([s.path for s in ctx.files.srcs], join_with=",") | ||
args.add(ctx.bin_dir.path) | ||
args.add([s.path for s in devmode_sources], join_with=",") | ||
args.add([ctx.outputs.pack.path, ctx.outputs.publish.path]) | ||
|
||
ctx.action( | ||
executable = ctx.executable._packager, | ||
inputs = ctx.files.srcs + devmode_sources + [ctx.file._run_npm_template], | ||
outputs = [package_dir, ctx.outputs.pack, ctx.outputs.publish], | ||
arguments = [args], | ||
) | ||
return package_dir | ||
|
||
def _npm_package(ctx): | ||
files = depset() | ||
for d in ctx.attr.deps: | ||
files = depset(transitive = [files, d.files, d.node_sources]) | ||
|
||
package_dir = create_package(ctx, files.to_list()) | ||
|
||
return [DefaultInfo( | ||
files = depset([package_dir]), | ||
)] | ||
|
||
NPM_PACKAGE_ATTRS = { | ||
"srcs": attr.label_list(allow_files = True), | ||
"deps": attr.label_list(aspects = [sources_aspect]), | ||
"_packager": attr.label( | ||
default = Label("//internal/npm_package:packager"), | ||
cfg = "host", executable = True), | ||
"_run_npm_template": attr.label( | ||
default = Label("@nodejs//:run_npm.sh.template"), | ||
allow_single_file = True), | ||
} | ||
|
||
NPM_PACKAGE_OUTPUTS = { | ||
"pack": "%{name}.pack", | ||
"publish": "%{name}.publish", | ||
} | ||
|
||
npm_package = rule( | ||
implementation = _npm_package, | ||
attrs = NPM_PACKAGE_ATTRS, | ||
outputs = NPM_PACKAGE_OUTPUTS, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 The Bazel Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function mkdirp(p) { | ||
if (!fs.existsSync(p)) { | ||
mkdirp(path.dirname(p)); | ||
fs.mkdirSync(p); | ||
} | ||
} | ||
|
||
function write(p, content) { | ||
mkdirp(path.dirname(p)); | ||
fs.writeFileSync(p, content); | ||
} | ||
|
||
// TODO(alexeagle): add support for version stamping, we might want to replace | ||
// the version number in some of the files (eg. take the latest git tag and | ||
// overwrite the version in package.json) | ||
|
||
function main(args) { | ||
const [outDir, srcsArg, binDir, depsArg, packPath, publishPath] = args; | ||
|
||
// src like my/path is just copied to outDir/my/path | ||
for (src of srcsArg.split(',').filter(s => !!s)) { | ||
const content = fs.readFileSync(src, {encoding: 'utf-8'}); | ||
const outPath = path.join(outDir, src); | ||
write(outPath, content); | ||
} | ||
|
||
// deps like bazel-bin/my/path is copied to outDir/my/path | ||
for (dep of depsArg.split(',').filter(s => !!s)) { | ||
const content = fs.readFileSync(dep, {encoding: 'utf-8'}) | ||
const outPath = path.join(outDir, path.relative(binDir, dep)); | ||
write(outPath, content); | ||
} | ||
|
||
const npmTemplate = | ||
fs.readFileSync(require.resolve('nodejs/run_npm.sh.template'), {encoding: 'utf-8'}); | ||
fs.writeFileSync(packPath, npmTemplate.replace('TMPL_args', `pack ${outDir}`)); | ||
fs.writeFileSync(publishPath, npmTemplate.replace('TMPL_args', `publish ${outDir}`)); | ||
} | ||
|
||
if (require.main === module) { | ||
process.exitCode = main(process.argv.slice(2)); | ||
} |