From 52d66ea5a9ea908fcfff4c811bebf030a5254123 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Mon, 16 Mar 2020 11:31:24 -0700 Subject: [PATCH] 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 --- dev-infra/utils/BUILD.bazel | 17 ++++++++++++++ dev-infra/utils/config.ts | 45 +++++++++++++++++++++++++++++++++++++ package.json | 2 ++ yarn.lock | 17 ++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 dev-infra/utils/BUILD.bazel create mode 100644 dev-infra/utils/config.ts diff --git a/dev-infra/utils/BUILD.bazel b/dev-infra/utils/BUILD.bazel new file mode 100644 index 0000000000..44785fa1b8 --- /dev/null +++ b/dev-infra/utils/BUILD.bazel @@ -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", + ], +) diff --git a/dev-infra/utils/config.ts b/dev-infra/utils/config.ts new file mode 100644 index 0000000000..ad92b8946f --- /dev/null +++ b/dev-infra/utils/config.ts @@ -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 {} \ No newline at end of file diff --git a/package.json b/package.json index b6cc0ecadb..8485b40504 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index 79a7f24a70..60fd93be0a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"