Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev-infra): register ts-node when reading configuration #37196

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions dev-infra/tmpl-package.json
Expand Up @@ -24,7 +24,13 @@
"peerDependencies": {
"@bazel/buildifier": "<from-root>",
"clang-format": "<from-root>",
"ts-node": "<from-root>",
"tslib": "<from-root>",
"typescript": "<from-root>"
},
"peerDependenciesMeta": {
"ts-node": {
"optional": true
}
}
}
31 changes: 25 additions & 6 deletions dev-infra/utils/config.ts
Expand Up @@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {existsSync} from 'fs';
import {join} from 'path';
import {exec} from 'shelljs';
import {isTsNodeAvailable} from './ts-node';

/**
* Describes the Github configuration for dev-infra. This configuration is
Expand Down Expand Up @@ -41,17 +43,16 @@ const CONFIG_FILE_NAME = '.ng-dev-config';
let CONFIG: {}|null = null;

/**
* Get the configuration from the file system, returning the already loaded copy if it
* is defined.
* Get the configuration from the file system, returning the already loaded
* copy if it is defined.
*/
export function getConfig(): NgDevConfig {
// If the global config is not defined, load it from the file system.
if (CONFIG === null) {
// The full path to the configuration file.
const configPath = join(getRepoBaseDir(), CONFIG_FILE_NAME);
// Set the global config object to a clone of the configuration loaded through default exports
// from the config file.
CONFIG = {...require(configPath)};
// Set the global config object.
CONFIG = readConfigFile(configPath);
}
// Return a clone of the global config to ensure that a new instance of the config is returned
// each time, preventing unexpected effects of modifications to the config object.
Expand All @@ -72,10 +73,28 @@ function validateCommonConfig(config: Partial<NgDevConfig>) {
errors.push(`"github.owner" is not defined`);
}
}

assertNoErrors(errors);
return config as NgDevConfig;
}

/** Resolves and reads the specified configuration file. */
function readConfigFile(configPath: string): object {
// If the the `.ts` extension has not been set up already, and a TypeScript based
// version of the given configuration seems to exist, set up `ts-node` if available.
if (require.extensions['.ts'] === undefined && existsSync(`${configPath}.ts`) &&
isTsNodeAvailable()) {
require('ts-node').register({skipProject: true, transpileOnly: true});
}

try {
return require(configPath)
} catch (e) {
console.error('Could not read configuration file.');
console.error(e);
process.exit(1);
}
}

/**
* Asserts the provided array of error messages is empty. If any errors are in the array,
* logs the errors and exit the process as a failure.
Expand Down
17 changes: 17 additions & 0 deletions dev-infra/utils/ts-node.ts
@@ -0,0 +1,17 @@
/**
* @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
*/

/** Whether ts-node has been installed and is available to ng-dev. */
export function isTsNodeAvailable(): boolean {
try {
require.resolve('ts-node');
return true;
} catch {
return false;
}
}