Skip to content

Commit

Permalink
Vendor in removeNPMAbsolutePaths (bazel-contrib#763)
Browse files Browse the repository at this point in the history
Fixes npm_install target cycle Bazel crash
  • Loading branch information
gregmagolan committed May 27, 2019
1 parent 0102fed commit 9817704
Show file tree
Hide file tree
Showing 14 changed files with 650 additions and 42 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ npm_package(
"//third_party/github.com/inikulin/parse5:package_contents",
"//third_party/github.com/gjtorikian/isBinaryFile:package_contents",
"//third_party/github.com/jhermsmeier/browserify-named-amd:package_contents",
"//third_party/github.com/juanjoDiaz/removeNPMAbsolutePaths:package_contents",
"//third_party/github.com/source-map:package_contents",
"//third_party/github.com/source-map-support:package_contents",
"//internal:package_contents",
Expand Down
13 changes: 0 additions & 13 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -563,19 +563,6 @@ def node_repositories(
package_json = package_json,
)

_maybe(
yarn_install,
name = "build_bazel_rules_nodejs_npm_install_deps",
package_json = "@build_bazel_rules_nodejs//internal/npm_install:package.json",
yarn_lock = "@build_bazel_rules_nodejs//internal/npm_install:yarn.lock",
# Just here as a smoke test for this attribute
prod_only = True,
# Do not symlink node_modules as when used in downstream repos we should not create
# node_modules folders in the @build_bazel_rules_nodejs external repository. This is
# not supported by managed_directories.
symlink_node_modules = False,
)

_maybe(
yarn_install,
name = "build_bazel_rules_nodejs_rollup_deps",
Expand Down
13 changes: 0 additions & 13 deletions internal/node/yarn.lock

This file was deleted.

2 changes: 1 addition & 1 deletion internal/npm_install/npm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ cd "{root}" && "{npm}" {npm_args}
if result.return_code:
fail("npm_install failed: %s (%s)" % (result.stdout, result.stderr))

remove_npm_absolute_paths = Label("@build_bazel_rules_nodejs_npm_install_deps//:node_modules/removeNPMAbsolutePaths/bin/removeNPMAbsolutePaths")
remove_npm_absolute_paths = Label("//third_party/github.com/juanjoDiaz/removeNPMAbsolutePaths:bin/removeNPMAbsolutePaths")

# removeNPMAbsolutePaths is run on node_modules after npm install as the package.json files
# generated by npm are non-deterministic. They contain absolute install paths and other private
Expand Down
7 changes: 0 additions & 7 deletions internal/npm_install/package.json

This file was deleted.

7 changes: 0 additions & 7 deletions internal/npm_install/yarn.lock

This file was deleted.

1 change: 0 additions & 1 deletion internal/npm_package/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ nodejs_binary(
],
entry_point = "build_bazel_rules_nodejs/internal/npm_package/packager.js",
install_source_map_support = False,
node_modules = "@build_bazel_rules_nodejs_npm_install_deps//:node_modules",
visibility = ["//visibility:public"],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

# Downloaded from https://github.com/juanjoDiaz/removeNPMAbsolutePaths/archive/v1.0.4.tar.gz
# Timestamp: 2019-05-27
# SHA256: 3e8309c823660df6edeb4580f1d2df06ebfec0d65e196c46b488f53e644bd7aa
exports_files(["LICENSE"])

filegroup(
name = "package_contents",
srcs = glob(["**/*"]),
)
21 changes: 21 additions & 0 deletions third_party/github.com/juanjoDiaz/removeNPMAbsolutePaths/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Juanjo Diaz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions third_party/github.com/juanjoDiaz/removeNPMAbsolutePaths/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Downloads][downloads-image]][downloads-url]

# removeNPMAbsolutePaths

removeNPMAbsolutePaths is a small utility to remove the fields that npm adds to the modules in `node_modules` containing local aboslute paths.

It has been noted that the `package.json` of modules in the `node_modules` folder contain some extra fields like `_args` and `where` which contain the absolute path of the module. According to NPM those fields are not even used.

The problem comes when you are planning to package your application using electron, NW.js or similar and distribute it. You might not want to distribute files containing absolute paths within your computer.

A feature request has been raised to NPM to fix this issue but they have made clear they don't plan to fix this.
- https://github.com/npm/npm/issues/12110 (feature request)
- https://github.com/npm/npm/issues/10393 (discussion about the topic)

## Using removeNPMAbsolutePaths

removeNPMAbsolutePaths simply loop through all the files in the given folder, open the files called `package.json` and remove all the fields starting with an underscore (`_`).

You can install removeNPMAbsolutePaths globally and use it from the command line
```Javascript
npm install -g removeNPMAbsolutePaths
removeNPMAbsolutePaths '<PROJECT_FOLDER>'
```
or use it from whithin your code
```Javascript
var removeNPMAbsolutePaths = require('removeNPMAbsolutePaths');
removeNPMAbsolutePaths('<PROJECT_FOLDER>')
.then(results => results.forEach(result => {
// Print only information about files that couldn't be processed
if (!result.success) {
console.log(result.err.message);
}
}))
.catch(err => console.log(err.message));
```
Using `removeNPMAbsolutePaths` from within Javascript returns a promise containing information about all the folders and files processed and whether they where successfully processed and rewritten or not.

### Options
removeNPMAbsolutePaths can be configured using tags. Tags can be added to the command line commands:
```Javascript
removeNPMAbsolutePaths '<PROJECT_FOLDER>' --tag1 --tag2
```
or passed programmatically in an options object
```Javascript
removeNPMAbsolutePaths('<PROJECT_FOLDER>', { tag1: true, tag2: false});
```

#### force
removeNPMAbsolutePaths only rewrite to disk the files that it modifies. Passing the `--force` tag will rewritte all the files even if they haven't been modfied. This might be useful if you want all the package.json files to have always exactly the same styling for example for hashing.

#### fields
removeNPMAbsolutePaths by default removes all fields starting with `_`. Passing the `--fields` tag followed by a list of field names you want removed will cause it to remove only those ones you list. This might be useful if only some of the fields in package.json are bothering you.

```Javascript
removeNPMAbsolutePaths '<PROJECT_FOLDER>' --fields _where _args
```

```Javascript
removeNPMAbsolutePaths('<PROJECT_FOLDER>', { tag1: true, fields: ['_where', '_args']});
```


## License
MIT


[npm-image]: https://img.shields.io/npm/v/removeNPMAbsolutePaths.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/removeNPMAbsolutePaths
[travis-image]: https://img.shields.io/travis/juanjoDiaz/removeNPMAbsolutePaths/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/juanjoDiaz/removeNPMAbsolutePaths
[coveralls-image]: https://img.shields.io/coveralls/juanjoDiaz/removeNPMAbsolutePaths/master.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/juanjoDiaz/removeNPMAbsolutePaths?branch=master
[downloads-image]: https://img.shields.io/npm/dm/removeNPMAbsolutePaths.svg?style=flat-square
[downloads-url]: https://www.npmjs.com/package/removeNPMAbsolutePaths
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env node

/* eslint-disable no-console */

'use strict';

const removeNPMAbsolutePaths = require('../src/removeNPMAbsolutePaths');

if (process.argv < 1) {
console.log('Invalid argument.', 'The first argument should be the path to a directory or a package.json file.');
}

const args = process.argv.slice(2);
const folder = args[0];
const opts = {
force: false,
};

const ignoredOptions = [];

for (let i = 1; i < args.length; i += 1) {
const arg = args[i];
switch (arg) {
case '--force':
opts.force = true;
break;
case '--fields':
opts.fields = opts.fields || [];
while (args[i + 1] && args[i + 1].slice(0, 2) !== '--') {
opts.fields.push(args[i += 1]);
}
break;
default:
ignoredOptions.push(arg);
break;
}
}

if (ignoredOptions.length) {
console.warn(`The following options are unknown and will be ignored:\n${ignoredOptions.join('\n')}`);
}

removeNPMAbsolutePaths(folder, opts)
.then(results => results.forEach((result) => {
if (!result.success) {
console.log(result.err.message);
}
}))
.catch(err => console.log(err.message));
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "removeNPMAbsolutePaths",
"version": "1.0.4",
"description": "Remove the fields containing local aboslute paths created by NPM",
"keywords": [
"npm",
"modules"
],
"main": "src/removeNPMAbsolutePaths.js",
"scripts": {
"lint": "eslint bin/removeNPMAbsolutePaths src test",
"test": "mocha test",
"test-with-coverage": "nyc --reporter=text mocha test",
"travis": "npm run lint && npm run test-with-coverage",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"author": "Juanjo Diaz <[email protected]>",
"repository": {
"type": "git",
"url": "git://github.com/juanjoDiaz/removeNPMAbsolutePaths"
},
"bugs": {
"url": "https://github.com/removeNPMAbsolutePaths/issues",
"email": "[email protected]"
},
"license": "MIT",
"preferGlobal": "true",
"bin": {
"removeNPMAbsolutePaths": "bin/removeNPMAbsolutePaths"
},
"devDependencies": {
"chai": "^4.1.1",
"chai-as-promised": "^7.1.1",
"coveralls": "^2.13.1",
"eslint": "^4.4.1",
"eslint-config-airbnb-base": "^11.3.1",
"eslint-plugin-import": "^2.7.0",
"mocha": "^3.5.0",
"nyc": "^11.1.0",
"sinon": "^3.2.0",
"sinon-chai": "^2.12.0"
},
"engines": {
"node": ">=4.0.0"
}
}
Loading

0 comments on commit 9817704

Please sign in to comment.