feat(dev-infra): create common config file loading util (#36091)

Create a common config file loading utility function and the
necessary util directory.  This util directory can provide common
utility functions for usage inside of the dev-infra package.

PR Close #36091
This commit is contained in:
Joey Perrott 2020-03-16 11:31:24 -07:00 committed by Andrew Kushnir
parent 61c37954c6
commit 52d66ea5a9
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,17 @@
load("@npm_bazel_typescript//:index.bzl", "ts_library")
ts_library(
name = "config",
srcs = [
"config.ts",
],
visibility = ["//dev-infra:__subpackages__"],
deps = [
"@npm//@types/json5",
"@npm//@types/node",
"@npm//@types/shelljs",
"@npm//json5",
"@npm//shelljs",
"@npm//tslib",
],
)

45
dev-infra/utils/config.ts Normal file
View File

@ -0,0 +1,45 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {parse} from 'json5';
import {readFileSync} from 'fs';
import {join} from 'path';
import {exec} from 'shelljs';
/**
* Gets the path of the directory for the repository base.
*/
function getRepoBaseDir() {
const baseRepoDir = exec(`git rev-parse --show-toplevel`, {silent: true});
if (baseRepoDir.code) {
throw Error(
`Unable to find the path to the base directory of the repository.\n` +
`Was the command run from inside of the repo?\n\n` +
`ERROR:\n ${baseRepoDir.stderr}`);
}
return baseRepoDir.trim();
}
/**
* Retrieve the configuration from the .dev-infra.json file.
*/
export function getAngularDevConfig(): DevInfraConfig {
const configPath = join(getRepoBaseDir(), '.dev-infra.json');
let rawConfig = '';
try {
rawConfig = readFileSync(configPath, 'utf8');
} catch {
throw Error(
`Unable to find config file at:\n` +
` ${configPath}`);
}
return parse(rawConfig);
}
// Interface exressing the expected structure of the DevInfraConfig.
export interface DevInfraConfig {}

View File

@ -148,6 +148,7 @@
"@bazel/buildifier": "^0.29.0",
"@bazel/ibazel": "^0.12.2",
"@octokit/graphql": "^4.3.1",
"@types/json5": "^0.0.30",
"@types/minimist": "^1.2.0",
"@yarnpkg/lockfile": "^1.1.0",
"browserstacktunnel-wrapper": "2.0.1",
@ -169,6 +170,7 @@
"gulp-tslint": "8.1.2",
"husky": "^0.14.3",
"jpm": "1.3.1",
"json5": "^2.1.2",
"karma-browserstack-launcher": "^1.3.0",
"karma-sauce-launcher": "^2.0.2",
"madge": "^3.6.0",

View File

@ -1538,6 +1538,11 @@
resolved "https://registry.yarnpkg.com/@types/jju/-/jju-1.4.0.tgz#ee074af79540c0e187426f46f12acbe8f6c31232"
integrity sha512-s6l49zLzFiXYHaTXbA+FNcDRo8ufZgC2/T5/jH+Wfr+ZV2tYbrBpEpN9oPkXXfug+y7pTZFkdeyQ0L/88Z34JA==
"@types/json5@^0.0.30":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.30.tgz#44cb52f32a809734ca562e685c6473b5754a7818"
integrity sha512-sqm9g7mHlPY/43fcSNrCYfOeX9zkTTK+euO5E6+CVijSMm5tTjkVdwdqRkY3ljjIAf8679vps5jKUoJBCLsMDA==
"@types/long@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef"
@ -8689,6 +8694,13 @@ json5@^2.1.0:
dependencies:
minimist "^1.2.0"
json5@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.2.tgz#43ef1f0af9835dd624751a6b7fa48874fb2d608e"
integrity sha512-MoUOQ4WdiN3yxhm7NEVJSJrieAo5hNSLQ5sj05OTRHPL9HOBy8u4Bu88jsC1jvqAdN+E1bJmsUcZH+1HQxliqQ==
dependencies:
minimist "^1.2.5"
jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
@ -10028,6 +10040,11 @@ minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=
minimist@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"