Skip to content

Commit

Permalink
fix(create): run ts_setup_workspace in TypeScript workspaces
Browse files Browse the repository at this point in the history
This is required to install an extra node_modules for use by the
ts_devserver rule.
  • Loading branch information
alexeagle committed Jun 12, 2019
1 parent a522613 commit c8e61c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 10 additions & 2 deletions packages/create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down
19 changes: 13 additions & 6 deletions packages/create/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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(' ');
Expand Down Expand Up @@ -43,31 +46,35 @@ 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');
}
// TODO: run bazel in the new directory to verify a build works

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');
}

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');
}

0 comments on commit c8e61c5

Please sign in to comment.