nx 20.5.0 → 20.6.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,12 +37,12 @@ export declare class ImportResult {
37
37
 
38
38
  export declare class NxCache {
39
39
  cacheDirectory: string
40
- constructor(workspaceRoot: string, cachePath: string, dbConnection: ExternalObject<NxDbConnection>, linkTaskDetails?: boolean | undefined | null)
40
+ constructor(workspaceRoot: string, cachePath: string, dbConnection: ExternalObject<NxDbConnection>, linkTaskDetails?: boolean | undefined | null, maxCacheSize?: number | undefined | null)
41
41
  get(hash: string): CachedResult | null
42
42
  put(hash: string, terminalOutput: string, outputs: Array<string>, code: number): void
43
- applyRemoteCacheResults(hash: string, result: CachedResult): void
43
+ applyRemoteCacheResults(hash: string, result: CachedResult, outputs: Array<string>): void
44
44
  getTaskOutputsPath(hash: string): string
45
- copyFilesFromCache(cachedResult: CachedResult, outputs: Array<string>): void
45
+ copyFilesFromCache(cachedResult: CachedResult, outputs: Array<string>): number
46
46
  removeOldCacheRecords(): void
47
47
  checkCacheFsInSync(): boolean
48
48
  }
@@ -112,13 +112,14 @@ export interface CachedResult {
112
112
  code: number
113
113
  terminalOutput: string
114
114
  outputsPath: string
115
+ size?: number
115
116
  }
116
117
 
117
118
  export declare export function closeDbConnection(connection: ExternalObject<NxDbConnection>): void
118
119
 
119
120
  export declare export function connectToNxDb(cacheDir: string, nxVersion: string, dbName?: string | undefined | null): ExternalObject<NxDbConnection>
120
121
 
121
- export declare export function copy(src: string, dest: string): void
122
+ export declare export function copy(src: string, dest: string): number
122
123
 
123
124
  export interface DepsOutputsInput {
124
125
  dependentTasksOutputFiles: string
Binary file
@@ -15,6 +15,7 @@ export declare function dbCacheEnabled(nxJson?: NxJsonConfiguration): boolean;
15
15
  export declare function getCache(options: DefaultTasksRunnerOptions): DbCache | Cache;
16
16
  export declare class DbCache {
17
17
  private readonly options;
18
+ private nxJson;
18
19
  private cache;
19
20
  private remoteCache;
20
21
  private remoteCachePromise;
@@ -27,16 +28,16 @@ export declare class DbCache {
27
28
  get(task: Task): Promise<CachedResult | null>;
28
29
  private applyRemoteCacheResults;
29
30
  put(task: Task, terminalOutput: string | null, outputs: string[], code: number): Promise<void>;
30
- copyFilesFromCache(_: string, cachedResult: CachedResult, outputs: string[]): Promise<void>;
31
+ copyFilesFromCache(_: string, cachedResult: CachedResult, outputs: string[]): Promise<number>;
31
32
  removeOldCacheRecords(): void;
32
33
  temporaryOutputPath(task: Task): string;
33
34
  private getRemoteCache;
34
35
  private _getRemoteCache;
35
- private getPowerpackS3Cache;
36
- private getPowerpackSharedCache;
37
- private getPowerpackGcsCache;
38
- private getPowerpackAzureCache;
39
- private getPowerpackCache;
36
+ private getS3Cache;
37
+ private getSharedCache;
38
+ private getGcsCache;
39
+ private getAzureCache;
40
+ private resolveRemoteCache;
40
41
  private resolvePackage;
41
42
  private assertCacheIsValid;
42
43
  }
@@ -66,3 +67,13 @@ export declare class Cache {
66
67
  private createCacheDir;
67
68
  private createTerminalOutputsDir;
68
69
  }
70
+ /**
71
+ * Converts a string representation of a max cache size to a number.
72
+ *
73
+ * e.g. '1GB' -> 1024 * 1024 * 1024
74
+ * '1MB' -> 1024 * 1024
75
+ * '1KB' -> 1024
76
+ *
77
+ * @param maxCacheSize Max cache size as specified in nx.json
78
+ */
79
+ export declare function parseMaxCacheSize(maxCacheSize: string): number | undefined;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Cache = exports.DbCache = void 0;
4
4
  exports.dbCacheEnabled = dbCacheEnabled;
5
5
  exports.getCache = getCache;
6
+ exports.parseMaxCacheSize = parseMaxCacheSize;
6
7
  const workspace_root_1 = require("../utils/workspace-root");
7
8
  const path_1 = require("path");
8
9
  const perf_hooks_1 = require("perf_hooks");
@@ -68,7 +69,8 @@ function getCache(options) {
68
69
  class DbCache {
69
70
  constructor(options) {
70
71
  this.options = options;
71
- this.cache = new native_1.NxCache(workspace_root_1.workspaceRoot, cache_directory_1.cacheDir, (0, db_connection_1.getDbConnection)());
72
+ this.nxJson = (0, nx_json_1.readNxJson)();
73
+ this.cache = new native_1.NxCache(workspace_root_1.workspaceRoot, cache_directory_1.cacheDir, (0, db_connection_1.getDbConnection)(), undefined, parseMaxCacheSize(this.nxJson.maxCacheSize));
72
74
  this.isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
73
75
  }
74
76
  async init() {
@@ -91,7 +93,7 @@ class DbCache {
91
93
  // attempt remote cache
92
94
  const res = await this.remoteCache.retrieve(task.hash, this.cache.cacheDirectory);
93
95
  if (res) {
94
- this.applyRemoteCacheResults(task.hash, res);
96
+ this.applyRemoteCacheResults(task.hash, res, task.outputs);
95
97
  return {
96
98
  ...res,
97
99
  remote: true,
@@ -105,8 +107,8 @@ class DbCache {
105
107
  return null;
106
108
  }
107
109
  }
108
- applyRemoteCacheResults(hash, res) {
109
- return this.cache.applyRemoteCacheResults(hash, res);
110
+ applyRemoteCacheResults(hash, res, outputs) {
111
+ return this.cache.applyRemoteCacheResults(hash, res, outputs);
110
112
  }
111
113
  async put(task, terminalOutput, outputs, code) {
112
114
  return tryAndRetry(async () => {
@@ -155,26 +157,38 @@ class DbCache {
155
157
  }
156
158
  }
157
159
  else {
158
- return ((await this.getPowerpackS3Cache()) ??
159
- (await this.getPowerpackSharedCache()) ??
160
- (await this.getPowerpackGcsCache()) ??
161
- (await this.getPowerpackAzureCache()) ??
160
+ return ((await this.getS3Cache()) ??
161
+ (await this.getSharedCache()) ??
162
+ (await this.getGcsCache()) ??
163
+ (await this.getAzureCache()) ??
162
164
  null);
163
165
  }
164
166
  }
165
- getPowerpackS3Cache() {
166
- return this.getPowerpackCache('@nx/powerpack-s3-cache');
167
- }
168
- getPowerpackSharedCache() {
169
- return this.getPowerpackCache('@nx/powerpack-shared-fs-cache');
170
- }
171
- getPowerpackGcsCache() {
172
- return this.getPowerpackCache('@nx/powerpack-gcs-cache');
173
- }
174
- getPowerpackAzureCache() {
175
- return this.getPowerpackCache('@nx/powerpack-azure-cache');
176
- }
177
- async getPowerpackCache(pkg) {
167
+ async getS3Cache() {
168
+ const cache = await this.resolveRemoteCache('@nx/s3-cache');
169
+ if (cache)
170
+ return cache;
171
+ return this.resolveRemoteCache('@nx/powerpack-s3-cache');
172
+ }
173
+ async getSharedCache() {
174
+ const cache = await this.resolveRemoteCache('@nx/shared-fs-cache');
175
+ if (cache)
176
+ return cache;
177
+ return this.resolveRemoteCache('@nx/powerpack-shared-fs-cache');
178
+ }
179
+ async getGcsCache() {
180
+ const cache = await this.resolveRemoteCache('@nx/gcs-cache');
181
+ if (cache)
182
+ return cache;
183
+ return this.resolveRemoteCache('@nx/powerpack-gcs-cache');
184
+ }
185
+ async getAzureCache() {
186
+ const cache = await this.resolveRemoteCache('@nx/azure-cache');
187
+ if (cache)
188
+ return cache;
189
+ return this.resolveRemoteCache('@nx/powerpack-azure-cache');
190
+ }
191
+ async resolveRemoteCache(pkg) {
178
192
  let getRemoteCache = null;
179
193
  try {
180
194
  getRemoteCache = (await Promise.resolve(`${this.resolvePackage(pkg)}`).then(s => require(s))).getRemoteCache;
@@ -455,3 +469,40 @@ function tryAndRetry(fn) {
455
469
  };
456
470
  return _try();
457
471
  }
472
+ /**
473
+ * Converts a string representation of a max cache size to a number.
474
+ *
475
+ * e.g. '1GB' -> 1024 * 1024 * 1024
476
+ * '1MB' -> 1024 * 1024
477
+ * '1KB' -> 1024
478
+ *
479
+ * @param maxCacheSize Max cache size as specified in nx.json
480
+ */
481
+ function parseMaxCacheSize(maxCacheSize) {
482
+ if (!maxCacheSize) {
483
+ return undefined;
484
+ }
485
+ let regexResult = maxCacheSize.match(/^(?<size>[\d|.]+)\s?((?<unit>[KMG]?B)?)$/);
486
+ if (!regexResult) {
487
+ throw new Error(`Invalid max cache size specified in nx.json: ${maxCacheSize}. Must be a number followed by an optional unit (KB, MB, GB)`);
488
+ }
489
+ let sizeString = regexResult.groups.size;
490
+ let unit = regexResult.groups.unit;
491
+ if ([...sizeString].filter((c) => c === '.').length > 1) {
492
+ throw new Error(`Invalid max cache size specified in nx.json: ${maxCacheSize} (multiple decimal points in size)`);
493
+ }
494
+ let size = parseFloat(sizeString);
495
+ if (isNaN(size)) {
496
+ throw new Error(`Invalid max cache size specified in nx.json: ${maxCacheSize} (${sizeString} is not a number)`);
497
+ }
498
+ switch (unit) {
499
+ case 'KB':
500
+ return size * 1024;
501
+ case 'MB':
502
+ return size * 1024 * 1024;
503
+ case 'GB':
504
+ return size * 1024 * 1024 * 1024;
505
+ default:
506
+ return size;
507
+ }
508
+ }
@@ -36,7 +36,7 @@ const task_results_life_cycle_1 = require("./life-cycles/task-results-life-cycle
36
36
  const task_graph_utils_1 = require("./task-graph-utils");
37
37
  const utils_1 = require("./utils");
38
38
  const chalk = require("chalk");
39
- const powerpack_1 = require("../utils/powerpack");
39
+ const nx_key_1 = require("../utils/nx-key");
40
40
  const tasks_execution_hooks_1 = require("../project-graph/plugins/tasks-execution-hooks");
41
41
  async function getTerminalOutputLifeCycle(initiatingProject, projectNames, tasks, nxArgs, nxJson, overrides) {
42
42
  const { runnerOptions } = getRunner(nxArgs, nxJson);
@@ -141,7 +141,7 @@ async function runCommandForTasks(projectsToRun, currentProjectGraph, { nxJson }
141
141
  initiatingProject,
142
142
  });
143
143
  await renderIsDone;
144
- await (0, powerpack_1.printPowerpackLicense)();
144
+ await (0, nx_key_1.printNxKey)();
145
145
  return taskResults;
146
146
  }
147
147
  async function ensureWorkspaceIsInSyncAndGetGraphs(projectGraph, nxJson, projectNames, nxArgs, overrides, extraTargetDependencies, extraOptions) {
@@ -131,7 +131,12 @@ class TaskOrchestrator {
131
131
  if (!cachedResult || cachedResult.code !== 0)
132
132
  return null;
133
133
  const outputs = task.outputs;
134
- const shouldCopyOutputsFromCache = !!outputs.length &&
134
+ const shouldCopyOutputsFromCache =
135
+ // No output files to restore
136
+ !!outputs.length &&
137
+ // Remote caches are restored to output dirs when applied
138
+ !cachedResult.remote &&
139
+ // Output files have not been touched since last run
135
140
  (await this.shouldCopyOutputsFromCache(outputs, task.hash));
136
141
  if (shouldCopyOutputsFromCache) {
137
142
  await this.cache.copyFilesFromCache(task.hash, cachedResult, outputs);
@@ -0,0 +1,7 @@
1
+ import type { NxKey } from '@nx/key';
2
+ export declare function createNxKeyLicenseeInformation(nxKey: NxKey): string;
3
+ export declare function printNxKey(): Promise<void>;
4
+ export declare function getNxKeyInformation(): Promise<NxKey | null>;
5
+ export declare class NxKeyNotInstalledError extends Error {
6
+ constructor(e: Error);
7
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NxKeyNotInstalledError = void 0;
4
+ exports.createNxKeyLicenseeInformation = createNxKeyLicenseeInformation;
5
+ exports.printNxKey = printNxKey;
6
+ exports.getNxKeyInformation = getNxKeyInformation;
7
+ const logger_1 = require("./logger");
8
+ const package_manager_1 = require("./package-manager");
9
+ const workspace_root_1 = require("./workspace-root");
10
+ function createNxKeyLicenseeInformation(nxKey) {
11
+ if ('isPowerpack' in nxKey && nxKey.isPowerpack) {
12
+ return `Licensed to ${nxKey.organizationName} for ${nxKey.seatCount} user${nxKey.seatCount > 1 ? 's' : ''} in ${nxKey.workspaceCount === 9999
13
+ ? 'an unlimited number of'
14
+ : nxKey.workspaceCount} workspace${nxKey.workspaceCount > 1 ? 's' : ''}.`;
15
+ }
16
+ else {
17
+ return `Licensed to ${nxKey.organizationName}.`;
18
+ }
19
+ }
20
+ async function printNxKey() {
21
+ try {
22
+ const key = await getNxKeyInformation();
23
+ if (key) {
24
+ logger_1.logger.log(createNxKeyLicenseeInformation(key));
25
+ }
26
+ }
27
+ catch { }
28
+ }
29
+ async function getNxKeyInformation() {
30
+ try {
31
+ const { getPowerpackLicenseInformation, getPowerpackLicenseInformationAsync, } = (await Promise.resolve().then(() => require('@nx/powerpack-license')));
32
+ return (getPowerpackLicenseInformationAsync ?? getPowerpackLicenseInformation)(workspace_root_1.workspaceRoot);
33
+ }
34
+ catch (e) {
35
+ try {
36
+ const { getNxKeyInformationAsync } = (await Promise.resolve().then(() => require('@nx/key')));
37
+ return getNxKeyInformationAsync(workspace_root_1.workspaceRoot);
38
+ }
39
+ catch (e) {
40
+ if ('code' in e && e.code === 'MODULE_NOT_FOUND') {
41
+ throw new NxKeyNotInstalledError(e);
42
+ }
43
+ throw e;
44
+ }
45
+ }
46
+ }
47
+ class NxKeyNotInstalledError extends Error {
48
+ constructor(e) {
49
+ super(`The "@nx/key" package is needed to use Nx key enabled features. Please install it with ${(0, package_manager_1.getPackageManagerCommand)().addDev} @nx/key`, { cause: e });
50
+ }
51
+ }
52
+ exports.NxKeyNotInstalledError = NxKeyNotInstalledError;
@@ -0,0 +1 @@
1
+ export declare function requireNxKey(): Promise<typeof import('@nx/key')>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requireNxKey = requireNxKey;
4
+ const child_process_1 = require("child_process");
5
+ const package_manager_1 = require("./package-manager");
6
+ async function requireNxKey() {
7
+ // @ts-ignore
8
+ return Promise.resolve().then(() => require('@nx/key')).catch(async (e) => {
9
+ if ('code' in e && e.code === 'MODULE_NOT_FOUND') {
10
+ try {
11
+ (0, child_process_1.execSync)(`${(0, package_manager_1.getPackageManagerCommand)().addDev} @nx/key@latest`, {
12
+ windowsHide: false,
13
+ });
14
+ // @ts-ignore
15
+ return await Promise.resolve().then(() => require('@nx/key'));
16
+ }
17
+ catch (e) {
18
+ throw new Error('Failed to install @nx/key. Please install @nx/key and try again.');
19
+ }
20
+ }
21
+ });
22
+ }
@@ -1,2 +0,0 @@
1
- import { ActivatePowerpackOptions } from './command-object';
2
- export declare function handleActivatePowerpack(options: ActivatePowerpackOptions): Promise<void>;
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleActivatePowerpack = handleActivatePowerpack;
4
- const workspace_root_1 = require("../../utils/workspace-root");
5
- const enquirer_1 = require("enquirer");
6
- const child_process_1 = require("child_process");
7
- const package_manager_1 = require("../../utils/package-manager");
8
- async function handleActivatePowerpack(options) {
9
- const license = options.license ??
10
- (await (0, enquirer_1.prompt)({
11
- type: 'input',
12
- name: 'license',
13
- message: 'Enter your License Key',
14
- }));
15
- const { activatePowerpack } = await requirePowerpack();
16
- activatePowerpack(workspace_root_1.workspaceRoot, license);
17
- }
18
- async function requirePowerpack() {
19
- // @ts-ignore
20
- return Promise.resolve().then(() => require('@nx/powerpack-license')).catch(async (e) => {
21
- if ('code' in e && e.code === 'MODULE_NOT_FOUND') {
22
- try {
23
- (0, child_process_1.execSync)(`${(0, package_manager_1.getPackageManagerCommand)().addDev} @nx/powerpack-license@latest`, {
24
- windowsHide: false,
25
- });
26
- // @ts-ignore
27
- return await Promise.resolve().then(() => require('@nx/powerpack-license'));
28
- }
29
- catch (e) {
30
- throw new Error('Failed to install @nx/powerpack-license. Please install @nx/powerpack-license and try again.');
31
- }
32
- }
33
- });
34
- }
@@ -1,6 +0,0 @@
1
- import { CommandModule } from 'yargs';
2
- export interface ActivatePowerpackOptions {
3
- license: string;
4
- verbose: boolean;
5
- }
6
- export declare const yargsActivatePowerpackCommand: CommandModule<{}, ActivatePowerpackOptions>;
@@ -1,5 +0,0 @@
1
- export declare function printPowerpackLicense(): Promise<void>;
2
- export declare function getPowerpackLicenseInformation(): Promise<import("@nx/powerpack-license").PowerpackLicense>;
3
- export declare class NxPowerpackNotInstalledError extends Error {
4
- constructor(e: Error);
5
- }
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NxPowerpackNotInstalledError = void 0;
4
- exports.printPowerpackLicense = printPowerpackLicense;
5
- exports.getPowerpackLicenseInformation = getPowerpackLicenseInformation;
6
- const logger_1 = require("./logger");
7
- const package_manager_1 = require("./package-manager");
8
- const workspace_root_1 = require("./workspace-root");
9
- async function printPowerpackLicense() {
10
- try {
11
- const { organizationName, seatCount, workspaceCount } = await getPowerpackLicenseInformation();
12
- logger_1.logger.log(`Nx Powerpack Licensed to ${organizationName} for ${seatCount} user${seatCount > 1 ? 's' : ''} in ${workspaceCount === 9999 ? 'an unlimited number of' : workspaceCount} workspace${workspaceCount > 1 ? 's' : ''}`);
13
- }
14
- catch { }
15
- }
16
- async function getPowerpackLicenseInformation() {
17
- try {
18
- const { getPowerpackLicenseInformation, getPowerpackLicenseInformationAsync, } = (await Promise.resolve().then(() => require('@nx/powerpack-license')));
19
- return (getPowerpackLicenseInformationAsync ?? getPowerpackLicenseInformation)(workspace_root_1.workspaceRoot);
20
- }
21
- catch (e) {
22
- if ('code' in e && e.code === 'MODULE_NOT_FOUND') {
23
- throw new NxPowerpackNotInstalledError(e);
24
- }
25
- throw e;
26
- }
27
- }
28
- class NxPowerpackNotInstalledError extends Error {
29
- constructor(e) {
30
- super(`The "@nx/powerpack-license" package is needed to use Nx Powerpack enabled features. Please install the @nx/powerpack-license with ${(0, package_manager_1.getPackageManagerCommand)().addDev} @nx/powerpack-license`, { cause: e });
31
- }
32
- }
33
- exports.NxPowerpackNotInstalledError = NxPowerpackNotInstalledError;