Skip to content

Commit ff29ccc

Browse files
Keen Yee Liaualxhub
authored andcommitted
fix(bazel): Use existing npm/yarn lock files (#30438)
This PR fixes Bazel builder to create yarn_install rule in WORKSPACE if yarn.lock is present, otherwise npm_install rule if package-lock.json is present. If none is present, default to yarn_install and create an empty yarn.lock file. PR closes #30164 PR closes #30359 PR Close #30438
1 parent c7f9a95 commit ff29ccc

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

‎packages/bazel/src/builders/bazel.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// <reference types='node'/>
1010

1111
import {spawn} from 'child_process';
12-
import {copyFileSync, existsSync, readdirSync, statSync, unlinkSync} from 'fs';
12+
import {copyFileSync, existsSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync} from 'fs';
1313
import {dirname, join, normalize} from 'path';
1414

1515
export type Executable = 'bazel' | 'ibazel';
@@ -106,6 +106,33 @@ function listR(dir: string): string[] {
106106
return list(dir, '', []);
107107
}
108108

109+
/**
110+
* Return the name of the lock file that is present in the specified 'root'
111+
* directory. If none exists, default to creating an empty yarn.lock file.
112+
*/
113+
function getOrCreateLockFile(root: string): 'yarn.lock'|'package-lock.json' {
114+
const yarnLock = join(root, 'yarn.lock');
115+
if (existsSync(yarnLock)) {
116+
return 'yarn.lock';
117+
}
118+
const npmLock = join(root, 'package-lock.json');
119+
if (existsSync(npmLock)) {
120+
return 'package-lock.json';
121+
}
122+
// Prefer yarn if no lock file exists
123+
writeFileSync(yarnLock, '');
124+
return 'yarn.lock';
125+
}
126+
127+
// Replace yarn_install rule with npm_install and copy from 'source' to 'dest'.
128+
function replaceYarnWithNpm(source: string, dest: string) {
129+
const srcContent = readFileSync(source, 'utf-8');
130+
const destContent = srcContent.replace(/yarn_install/g, 'npm_install')
131+
.replace('yarn_lock', 'package_lock_json')
132+
.replace('yarn.lock', 'package-lock.json');
133+
writeFileSync(dest, destContent);
134+
}
135+
109136
/**
110137
* Copy Bazel files (WORKSPACE, BUILD.bazel, etc) from the template directory to
111138
* the project `root` directory, and return the absolute paths of the files
@@ -117,14 +144,19 @@ export function copyBazelFiles(root: string, templateDir: string) {
117144
templateDir = normalize(templateDir);
118145
const bazelFiles: string[] = [];
119146
const templates = listR(templateDir);
147+
const useYarn = getOrCreateLockFile(root) === 'yarn.lock';
120148

121149
for (const template of templates) {
122150
const name = template.replace('__dot__', '.').replace('.template', '');
123151
const source = join(templateDir, template);
124152
const dest = join(root, name);
125153
try {
126154
if (!existsSync(dest)) {
127-
copyFileSync(source, dest);
155+
if (!useYarn && name === 'WORKSPACE') {
156+
replaceYarnWithNpm(source, dest);
157+
} else {
158+
copyFileSync(source, dest);
159+
}
128160
bazelFiles.push(dest);
129161
}
130162
} catch {

0 commit comments

Comments
 (0)