Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion goldens/public-api/angular_devkit/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ interface SimpleMemoryHostStats {
// @public (undocumented)
interface SmartDefaultProvider<T> {
// (undocumented)
(schema: JsonObject): T | Observable<T>;
(schema: JsonObject): T | Observable<T> | Promise<T>;
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export abstract class ArchitectBaseCommandModule<T extends object>
} catch (e) {
assertIsError(e);
if (e.code === 'MODULE_NOT_FOUND') {
this.warnOnMissingNodeModules();
await this.warnOnMissingNodeModules();
throw new CommandModuleError(`Could not find the '${builderConf}' builder's node package.`);
}

Expand All @@ -203,7 +203,7 @@ export abstract class ArchitectBaseCommandModule<T extends object>
);
}

private warnOnMissingNodeModules(): void {
private async warnOnMissingNodeModules(): Promise<void> {
const basePath = this.context.workspace?.basePath;
if (!basePath) {
return;
Expand All @@ -218,7 +218,9 @@ export abstract class ArchitectBaseCommandModule<T extends object>
} catch {}

this.context.logger.warn(
`Node packages may not be installed. Try installing with '${this.context.packageManager.name} install'.`,
`Node packages may not be installed. Try installing with '${
(await this.context.packageManager).name
} install'.`,
);
}

Expand Down
6 changes: 4 additions & 2 deletions packages/angular/cli/src/command-builder/command-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
['version', 'update', 'analytics'].includes(this.commandName),
);

const packageManager = await this.context.packageManager;

return userId
? new AnalyticsCollector(this.context.logger, userId, {
name: this.context.packageManager.name,
version: await this.context.packageManager.getVersion(),
name: packageManager.name,
version: await packageManager.getVersion(),
})
: undefined;
}
Expand Down
34 changes: 19 additions & 15 deletions packages/angular/cli/src/command-builder/command-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
RootCommands,
RootCommandsAliases,
} from '../commands/command-config';
import { createPackageManager } from '../package-managers';
import { PackageManager, createPackageManager } from '../package-managers';
import { ConfiguredPackageManagerInfo } from '../package-managers/factory';
import { colors } from '../utilities/color';
import { AngularWorkspace, getProjectByCwd, getWorkspace } from '../utilities/config';
Expand Down Expand Up @@ -65,28 +65,32 @@ export async function runCommand(args: string[], logger: logging.Logger): Promis
}

const root = workspace?.basePath ?? process.cwd();
const cacheConfig = workspace && getCacheConfig(workspace);
const packageManager = await createPackageManager({
cwd: root,
logger,
dryRun: dryRun || help || jsonHelp || getYargsCompletions,
tempDirectory: cacheConfig?.enabled ? cacheConfig.path : undefined,
configuredPackageManager: await getConfiguredPackageManager(
root,
workspace,
globalConfiguration,
),
});

const localYargs = yargs(args);
let packageManager: Promise<PackageManager> | undefined;
const context: CommandContext = {
globalConfiguration,
workspace,
logger,
currentDirectory: process.cwd(),
yargsInstance: localYargs,
root,
packageManager,
get packageManager() {
return (packageManager ??= (async () => {
const cacheConfig = workspace && getCacheConfig(workspace);

return createPackageManager({
cwd: root,
logger,
dryRun: dryRun || help || jsonHelp || getYargsCompletions,
tempDirectory: cacheConfig?.enabled ? cacheConfig.path : undefined,
configuredPackageManager: await getConfiguredPackageManager(
root,
workspace,
globalConfiguration,
),
});
})());
},
args: {
positional: positional.map((v) => v.toString()),
options: {
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/src/command-builder/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface CommandContext {
workspace?: AngularWorkspace;
globalConfiguration: AngularWorkspace;
logger: logging.Logger;
packageManager: PackageManager;
readonly packageManager: Promise<PackageManager>;
yargsInstance: Argv<{}>;

/** Arguments parsed in free-from without parser configuration. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export abstract class SchematicsCommandModule
collectionName: string,
options: SchematicsExecutionOptions,
): Promise<NodeWorkflow> {
const { logger, root, packageManager } = this.context;
const { logger, root } = this.context;
const packageManager = await this.context.packageManager;
const { force, dryRun, packageRegistry } = options;

const workflow = new NodeWorkflow(root, {
force,
dryRun,
Expand Down
14 changes: 8 additions & 6 deletions packages/angular/cli/src/commands/add/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export default class AddCommandModule
[
{
title: 'Determining Package Manager',
task: (_context, task) =>
(task.output = `Using package manager: ${color.dim(this.context.packageManager.name)}`),
task: async (_context, task) =>
(task.output = `Using package manager: ${color.dim((await this.context.packageManager).name)}`),
rendererOptions: { persistentOutput: true },
},
{
Expand Down Expand Up @@ -318,7 +318,7 @@ export default class AddCommandModule
): Promise<void> {
const { registry, verbose } = options;
const { packageIdentifier } = context;
const { packageManager } = this.context;
const packageManager = await this.context.packageManager;
const packageName = packageIdentifier.name;

assert(packageName, 'Registry package identifiers should always have a name.');
Expand Down Expand Up @@ -416,7 +416,7 @@ export default class AddCommandModule
},
): Promise<PackageManifest | null> {
const { packageIdentifier } = context;
const { packageManager } = this.context;
const packageManager = await this.context.packageManager;
const { registry, verbose, rejectionReasons } = options;
const packageName = packageIdentifier.name;
assert(packageName, 'Package name must be defined.');
Expand Down Expand Up @@ -494,7 +494,9 @@ export default class AddCommandModule

let manifest;
try {
manifest = await this.context.packageManager.getManifest(context.packageIdentifier, {
manifest = await (
await this.context.packageManager
).getManifest(context.packageIdentifier, {
registry,
});
Comment on lines +497 to 501

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability, you could await the packageManager promise and store it in a local variable before calling getManifest.

      const packageManager = await this.context.packageManager;
      manifest = await packageManager.getManifest(context.packageIdentifier, {
        registry,
      });

} catch (e) {
Expand Down Expand Up @@ -567,7 +569,7 @@ export default class AddCommandModule
): Promise<void> {
const { registry } = options;
const { packageIdentifier, savePackage } = context;
const { packageManager } = this.context;
const packageManager = await this.context.packageManager;

// Only show if installation will actually occur
task.title = 'Installing package';
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/src/commands/new/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class NewCommandModule
workflow.registry.addSmartDefaultProvider('ng-cli-version', () => VERSION.full);
workflow.registry.addSmartDefaultProvider(
'packageManager',
() => this.context.packageManager.name,
async () => (await this.context.packageManager).name,
);

return this.runSchematic({
Expand Down
5 changes: 3 additions & 2 deletions packages/angular/cli/src/commands/update/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
}

async run(options: Options<UpdateCommandArgs>): Promise<number | void> {
const { logger, packageManager } = this.context;
const { logger } = this.context;
const packageManager = await this.context.packageManager;

// Check if the current installed CLI version is older than the latest compatible version.
// Skip when running `ng update` without a package name as this will not trigger an actual update.
Expand Down Expand Up @@ -517,7 +518,7 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
verbose: options.verbose,
force: options.force,
next: options.next,
packageManager: this.context.packageManager.name,
packageManager: (await this.context.packageManager).name,
packages: packagesToUpdate,
},
);
Expand Down
5 changes: 3 additions & 2 deletions packages/angular/cli/src/commands/version/version-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export async function gatherVersionInfo(context: CommandContext): Promise<Versio
}

const angularCoreVersion = packages['@angular/core'];
const packageManager = await context.packageManager;

return {
cli: {
Expand All @@ -121,8 +122,8 @@ export async function gatherVersionInfo(context: CommandContext): Promise<Versio
architecture: process.arch,
},
packageManager: {
name: context.packageManager.name,
version: await context.packageManager.getVersion(),
name: packageManager.name,
version: await packageManager.getVersion(),
},
},
packages,
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/core/src/json/schema/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface SchemaFormat {
}

export interface SmartDefaultProvider<T> {
(schema: JsonObject): T | Observable<T>;
(schema: JsonObject): T | Observable<T> | Promise<T>;
}

export interface SchemaKeywordValidator {
Expand Down
12 changes: 6 additions & 6 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {

private _smartDefaultKeyword = false;
private _promptProvider?: PromptProvider;
private _sourceMap = new Map<string, SmartDefaultProvider<{}>>();
private _sourceProvider = new Map<string, SmartDefaultProvider<{}>>();

constructor(formats: SchemaFormat[] = []) {
this._ajv = new Ajv({
Expand Down Expand Up @@ -387,11 +387,11 @@ export class CoreSchemaRegistry implements SchemaRegistry {
}

addSmartDefaultProvider<T>(source: string, provider: SmartDefaultProvider<T>): void {
if (this._sourceMap.has(source)) {
if (this._sourceProvider.has(source)) {
throw new Error(source);
}

this._sourceMap.set(source, provider as unknown as SmartDefaultProvider<{}>);
this._sourceProvider.set(source, provider as unknown as SmartDefaultProvider<{}>);

if (!this._smartDefaultKeyword) {
this._smartDefaultKeyword = true;
Expand Down Expand Up @@ -632,17 +632,17 @@ export class CoreSchemaRegistry implements SchemaRegistry {
): Promise<void> {
for (const [pointer, schema] of smartDefaults.entries()) {
const fragments = JSON.parse(pointer) as string[];
const source = this._sourceMap.get(schema.$source as string);
const source = this._sourceProvider.get(schema.$source as string);
if (!source) {
continue;
}

let value = source(schema);
if (isObservable(value)) {
value = (await lastValueFrom(value)) as {};
value = lastValueFrom(value) as {};
}

CoreSchemaRegistry._set(data, fragments, value);
CoreSchemaRegistry._set(data, fragments, await value);
}
}

Expand Down
Loading