From 4d5625bf21256e2bb0e9e2491d7262967ee472a7 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Wed, 7 Nov 2018 18:05:09 +0000 Subject: [PATCH] build: add check when running npm install (#14021) Even though we're using `yarn` for everything, it's still easy to fall into the old habit of running `npm i` which could mess up the `node_modules` and generate a new lock file. These changes add a script that throws an error when trying to `npm install`, similar to angular/angular. --- package.json | 3 ++- tools/angular_material_setup_workspace.bzl | 4 +++- tools/npm/check-npm.js | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tools/npm/check-npm.js diff --git a/package.json b/package.json index 71573b4b188..b4ba8070225 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "api": "gulp api-docs", "breaking-changes": "gulp breaking-changes", "gulp": "gulp", - "stage-release": "bash ./tools/release/stage-release-bin.sh" + "stage-release": "bash ./tools/release/stage-release-bin.sh", + "preinstall": "node ./tools/npm/check-npm.js" }, "version": "7.0.2", "requiredAngularVersion": ">=7.0.0", diff --git a/tools/angular_material_setup_workspace.bzl b/tools/angular_material_setup_workspace.bzl index 1519ee625ab..cf5ea5e72f7 100644 --- a/tools/angular_material_setup_workspace.bzl +++ b/tools/angular_material_setup_workspace.bzl @@ -13,7 +13,7 @@ def angular_material_setup_workspace(): It creates some additional Bazel external repositories that are used internally to build Angular Material """ - # Use Bazel managed node modules. See more below: + # Use Bazel managed node modules. See more below: # https://github.com/bazelbuild/rules_nodejs#bazel-managed-vs-self-managed-dependencies # Note: The repository_rule name is `@matdeps` so it does not conflict with the `@npm` repository # name downstream when building Angular Material from source. In the future when Angular + Bazel @@ -22,5 +22,7 @@ def angular_material_setup_workspace(): yarn_install( name = "matdeps", package_json = "@angular_material//:package.json", + # Ensure that the script is available when running `postinstall` in the Bazel sandbox. + data = ["@angular_material//:tools/npm/check-npm.js"], yarn_lock = "@angular_material//:yarn.lock", ) diff --git a/tools/npm/check-npm.js b/tools/npm/check-npm.js new file mode 100644 index 00000000000..2ad3caaf057 --- /dev/null +++ b/tools/npm/check-npm.js @@ -0,0 +1,13 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +if (process.env.npm_execpath.indexOf('yarn') === -1) { + console.error('Please use Yarn instead of NPM to install dependencies. ' + + 'See: https://yarnpkg.com/lang/en/docs/install/'); + process.exit(1); +}