From c8e61c5642d473da990c011f26a49a5b4f2d4965 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 12 Jun 2019 10:52:48 -0700 Subject: [PATCH] fix(create): run ts_setup_workspace in TypeScript workspaces This is required to install an extra node_modules for use by the ts_devserver rule. --- packages/create/index.js | 12 ++++++++++-- packages/create/test.js | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/create/index.js b/packages/create/index.js index 4432263692..350f8a414b 100644 --- a/packages/create/index.js +++ b/packages/create/index.js @@ -138,7 +138,7 @@ npm_install( package_lock_json = "//:package-lock.json", )`; - write('WORKSPACE', `# Bazel workspace created by @bazel/create 0.0.0-PLACEHOLDER + let workspaceContent = `# Bazel workspace created by @bazel/create 0.0.0-PLACEHOLDER # Declares that this directory is the root of a Bazel workspace. # See https://docs.bazel.build/versions/master/build-ref.html#workspace @@ -163,7 +163,15 @@ ${pkgMgr === 'yarn' ? yarnInstallCmd : npmInstallCmd} # Install any Bazel rules which were extracted earlier by the ${pkgMgr}_install rule. load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies") -install_bazel_dependencies()`); +install_bazel_dependencies()`; + if (args['typescript']) { + workspaceContent += ` + +# Setup TypeScript toolchain +load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace") +ts_setup_workspace()`; + } + write('WORKSPACE', workspaceContent); write('.bazelignore', `node_modules`); write( 'package.json', diff --git a/packages/create/test.js b/packages/create/test.js index 5f06335e14..5ac7a6f58e 100644 --- a/packages/create/test.js +++ b/packages/create/test.js @@ -3,7 +3,6 @@ * We don't use a test framework here since dependencies are awkward. */ const pkg = 'build_bazel_rules_nodejs/packages/create/npm_package'; -const path = require('path'); const fs = require('fs'); const {main} = require(pkg); @@ -12,6 +11,10 @@ function fail(...msg) { throw new Error('test failed'); } +function read(path) { + return fs.readFileSync(path, {encoding: 'utf-8'}); +} + let error, exitCode; function captureError(...msg) { error = error + '\n' + msg.join(' '); @@ -43,7 +46,7 @@ const projFiles = fs.readdirSync('some_project'); if (!projFiles.indexOf('.bazelrc') < 0) { fail('project should have .bazelrc'); } -let wkspContent = fs.readFileSync('some_project/WORKSPACE', {encoding: 'utf-8'}); +let wkspContent = read('some_project/WORKSPACE'); if (wkspContent.indexOf('npm_install(') < 0) { fail('should use npm by default'); } @@ -51,7 +54,7 @@ if (wkspContent.indexOf('npm_install(') < 0) { exitCode = main(['configure_pkgMgr', '--packageManager=yarn'], captureError); if (exitCode != 0) fail('should be success'); -wkspContent = fs.readFileSync('configure_pkgMgr/WORKSPACE', {encoding: 'utf-8'}); +wkspContent = read('configure_pkgMgr/WORKSPACE'); if (wkspContent.indexOf('yarn_install(') < 0) { fail('should use yarn when requested'); } @@ -59,15 +62,19 @@ if (wkspContent.indexOf('yarn_install(') < 0) { process.env['_'] = '/usr/bin/yarn'; exitCode = main(['default_to_yarn']); if (exitCode != 0) fail('should be success'); -wkspContent = fs.readFileSync('default_to_yarn/WORKSPACE', {encoding: 'utf-8'}); +wkspContent = read('default_to_yarn/WORKSPACE'); if (wkspContent.indexOf('yarn_install(') < 0) { - fail('should use yarn by default') + fail('should use yarn by default'); } // TODO: run bazel in the new directory to verify a build works exitCode = main(['--typescript', 'with_ts'], captureError); if (exitCode != 0) fail('should be success'); -let pkgContent = fs.readFileSync('with_ts/package.json', {encoding: 'utf-8'}); +let pkgContent = read('with_ts/package.json'); if (pkgContent.indexOf('"@bazel/typescript": "latest"') < 0) { fail('should install @bazel/typescript dependency', pkgContent); } +wkspContent = read('with_ts/WORKSPACE'); +if (wkspContent.indexOf('ts_setup_workspace(') < 0) { + fail('should install extra TS repositories'); +}