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 https://github.com/angular/angular/issues/30164
PR closes https://github.com/angular/angular/pull/30359

PR Close #30438
This commit is contained in:
Keen Yee Liau 2019-05-13 12:47:58 -07:00 committed by Alex Rickabaugh
parent c7f9a95a3f
commit ff29cccb77
1 changed files with 34 additions and 2 deletions

View File

@ -9,7 +9,7 @@
/// <reference types='node'/> /// <reference types='node'/>
import {spawn} from 'child_process'; import {spawn} from 'child_process';
import {copyFileSync, existsSync, readdirSync, statSync, unlinkSync} from 'fs'; import {copyFileSync, existsSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync} from 'fs';
import {dirname, join, normalize} from 'path'; import {dirname, join, normalize} from 'path';
export type Executable = 'bazel' | 'ibazel'; export type Executable = 'bazel' | 'ibazel';
@ -106,6 +106,33 @@ function listR(dir: string): string[] {
return list(dir, '', []); return list(dir, '', []);
} }
/**
* Return the name of the lock file that is present in the specified 'root'
* directory. If none exists, default to creating an empty yarn.lock file.
*/
function getOrCreateLockFile(root: string): 'yarn.lock'|'package-lock.json' {
const yarnLock = join(root, 'yarn.lock');
if (existsSync(yarnLock)) {
return 'yarn.lock';
}
const npmLock = join(root, 'package-lock.json');
if (existsSync(npmLock)) {
return 'package-lock.json';
}
// Prefer yarn if no lock file exists
writeFileSync(yarnLock, '');
return 'yarn.lock';
}
// Replace yarn_install rule with npm_install and copy from 'source' to 'dest'.
function replaceYarnWithNpm(source: string, dest: string) {
const srcContent = readFileSync(source, 'utf-8');
const destContent = srcContent.replace(/yarn_install/g, 'npm_install')
.replace('yarn_lock', 'package_lock_json')
.replace('yarn.lock', 'package-lock.json');
writeFileSync(dest, destContent);
}
/** /**
* Copy Bazel files (WORKSPACE, BUILD.bazel, etc) from the template directory to * Copy Bazel files (WORKSPACE, BUILD.bazel, etc) from the template directory to
* the project `root` directory, and return the absolute paths of the files * the project `root` directory, and return the absolute paths of the files
@ -117,6 +144,7 @@ export function copyBazelFiles(root: string, templateDir: string) {
templateDir = normalize(templateDir); templateDir = normalize(templateDir);
const bazelFiles: string[] = []; const bazelFiles: string[] = [];
const templates = listR(templateDir); const templates = listR(templateDir);
const useYarn = getOrCreateLockFile(root) === 'yarn.lock';
for (const template of templates) { for (const template of templates) {
const name = template.replace('__dot__', '.').replace('.template', ''); const name = template.replace('__dot__', '.').replace('.template', '');
@ -124,7 +152,11 @@ export function copyBazelFiles(root: string, templateDir: string) {
const dest = join(root, name); const dest = join(root, name);
try { try {
if (!existsSync(dest)) { if (!existsSync(dest)) {
copyFileSync(source, dest); if (!useYarn && name === 'WORKSPACE') {
replaceYarnWithNpm(source, dest);
} else {
copyFileSync(source, dest);
}
bazelFiles.push(dest); bazelFiles.push(dest);
} }
} catch { } catch {