Skip to content

Commit c8e61c5

Browse files
committed
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.
1 parent a522613 commit c8e61c5

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

packages/create/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ npm_install(
138138
package_lock_json = "//:package-lock.json",
139139
)`;
140140

141-
write('WORKSPACE', `# Bazel workspace created by @bazel/create 0.0.0-PLACEHOLDER
141+
let workspaceContent = `# Bazel workspace created by @bazel/create 0.0.0-PLACEHOLDER
142142
143143
# Declares that this directory is the root of a Bazel workspace.
144144
# See https://docs.bazel.build/versions/master/build-ref.html#workspace
@@ -163,7 +163,15 @@ ${pkgMgr === 'yarn' ? yarnInstallCmd : npmInstallCmd}
163163
164164
# Install any Bazel rules which were extracted earlier by the ${pkgMgr}_install rule.
165165
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
166-
install_bazel_dependencies()`);
166+
install_bazel_dependencies()`;
167+
if (args['typescript']) {
168+
workspaceContent += `
169+
170+
# Setup TypeScript toolchain
171+
load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
172+
ts_setup_workspace()`;
173+
}
174+
write('WORKSPACE', workspaceContent);
167175
write('.bazelignore', `node_modules`);
168176
write(
169177
'package.json',

packages/create/test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* We don't use a test framework here since dependencies are awkward.
44
*/
55
const pkg = 'build_bazel_rules_nodejs/packages/create/npm_package';
6-
const path = require('path');
76
const fs = require('fs');
87
const {main} = require(pkg);
98

@@ -12,6 +11,10 @@ function fail(...msg) {
1211
throw new Error('test failed');
1312
}
1413

14+
function read(path) {
15+
return fs.readFileSync(path, {encoding: 'utf-8'});
16+
}
17+
1518
let error, exitCode;
1619
function captureError(...msg) {
1720
error = error + '\n' + msg.join(' ');
@@ -43,31 +46,35 @@ const projFiles = fs.readdirSync('some_project');
4346
if (!projFiles.indexOf('.bazelrc') < 0) {
4447
fail('project should have .bazelrc');
4548
}
46-
let wkspContent = fs.readFileSync('some_project/WORKSPACE', {encoding: 'utf-8'});
49+
let wkspContent = read('some_project/WORKSPACE');
4750
if (wkspContent.indexOf('npm_install(') < 0) {
4851
fail('should use npm by default');
4952
}
5053
// TODO: run bazel in the new directory to verify a build works
5154

5255
exitCode = main(['configure_pkgMgr', '--packageManager=yarn'], captureError);
5356
if (exitCode != 0) fail('should be success');
54-
wkspContent = fs.readFileSync('configure_pkgMgr/WORKSPACE', {encoding: 'utf-8'});
57+
wkspContent = read('configure_pkgMgr/WORKSPACE');
5558
if (wkspContent.indexOf('yarn_install(') < 0) {
5659
fail('should use yarn when requested');
5760
}
5861

5962
process.env['_'] = '/usr/bin/yarn';
6063
exitCode = main(['default_to_yarn']);
6164
if (exitCode != 0) fail('should be success');
62-
wkspContent = fs.readFileSync('default_to_yarn/WORKSPACE', {encoding: 'utf-8'});
65+
wkspContent = read('default_to_yarn/WORKSPACE');
6366
if (wkspContent.indexOf('yarn_install(') < 0) {
64-
fail('should use yarn by default')
67+
fail('should use yarn by default');
6568
}
6669
// TODO: run bazel in the new directory to verify a build works
6770

6871
exitCode = main(['--typescript', 'with_ts'], captureError);
6972
if (exitCode != 0) fail('should be success');
70-
let pkgContent = fs.readFileSync('with_ts/package.json', {encoding: 'utf-8'});
73+
let pkgContent = read('with_ts/package.json');
7174
if (pkgContent.indexOf('"@bazel/typescript": "latest"') < 0) {
7275
fail('should install @bazel/typescript dependency', pkgContent);
7376
}
77+
wkspContent = read('with_ts/WORKSPACE');
78+
if (wkspContent.indexOf('ts_setup_workspace(') < 0) {
79+
fail('should install extra TS repositories');
80+
}

0 commit comments

Comments
 (0)