Socket
Socket
Sign inDemoInstall

nx

Package Overview
Dependencies
Maintainers
8
Versions
1371
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nx - npm Package Compare versions

Comparing version 19.7.0-canary.20240829-0ef6892 to 19.7.0-canary.20240830-83a387a

24

package.json
{
"name": "nx",
"version": "19.7.0-canary.20240829-0ef6892",
"version": "19.7.0-canary.20240830-83a387a",
"private": false,

@@ -74,3 +74,3 @@ "description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",

"ora": "5.3.0",
"@nrwl/tao": "19.7.0-canary.20240829-0ef6892"
"@nrwl/tao": "19.7.0-canary.20240830-83a387a"
},

@@ -90,12 +90,12 @@ "peerDependencies": {

"optionalDependencies": {
"@nx/nx-darwin-x64": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-darwin-arm64": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-linux-x64-gnu": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-linux-x64-musl": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-win32-x64-msvc": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-linux-arm64-gnu": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-linux-arm64-musl": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-linux-arm-gnueabihf": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-win32-arm64-msvc": "19.7.0-canary.20240829-0ef6892",
"@nx/nx-freebsd-x64": "19.7.0-canary.20240829-0ef6892"
"@nx/nx-darwin-x64": "19.7.0-canary.20240830-83a387a",
"@nx/nx-darwin-arm64": "19.7.0-canary.20240830-83a387a",
"@nx/nx-linux-x64-gnu": "19.7.0-canary.20240830-83a387a",
"@nx/nx-linux-x64-musl": "19.7.0-canary.20240830-83a387a",
"@nx/nx-win32-x64-msvc": "19.7.0-canary.20240830-83a387a",
"@nx/nx-linux-arm64-gnu": "19.7.0-canary.20240830-83a387a",
"@nx/nx-linux-arm64-musl": "19.7.0-canary.20240830-83a387a",
"@nx/nx-linux-arm-gnueabihf": "19.7.0-canary.20240830-83a387a",
"@nx/nx-win32-arm64-msvc": "19.7.0-canary.20240830-83a387a",
"@nx/nx-freebsd-x64": "19.7.0-canary.20240830-83a387a"
},

@@ -102,0 +102,0 @@ "nx-migrations": {

@@ -6,2 +6,3 @@ "use strict";

const versions_1 = require("../../utils/versions");
const shared_options_1 = require("../yargs-utils/shared-options");
exports.yargsConnectCommand = {

@@ -11,5 +12,5 @@ command: 'connect',

describe: `Connect workspace to Nx Cloud`,
builder: (yargs) => (0, documentation_1.linkToNxDevAndExamples)(yargs, 'connect-to-nx-cloud'),
handler: async () => {
await (await Promise.resolve().then(() => require('./connect-to-nx-cloud'))).connectToNxCloudCommand();
builder: (yargs) => (0, documentation_1.linkToNxDevAndExamples)(withConnectOptions(yargs), 'connect-to-nx-cloud'),
handler: async (args) => {
await (await Promise.resolve().then(() => require('./connect-to-nx-cloud'))).connectToNxCloudCommand(args);
await (await Promise.resolve().then(() => require('../../utils/ab-testing'))).recordStat({

@@ -23,2 +24,8 @@ command: 'connect',

};
function withConnectOptions(yargs) {
return (0, shared_options_1.withVerbose)(yargs).option('generateToken', {
type: 'boolean',
description: 'Explicitly asks for a token to be created, do not override existing tokens from Nx Cloud',
});
}
exports.yargsViewLogsCommand = {

@@ -25,0 +32,0 @@ command: 'view-logs',

@@ -8,4 +8,6 @@ import { ConnectToNxCloudOptions } from '../../nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud';

export declare function connectWorkspaceToCloud(options: ConnectToNxCloudOptions, directory?: string): Promise<string>;
export declare function connectToNxCloudCommand(command?: string): Promise<boolean>;
export declare function connectToNxCloudCommand(options: {
generateToken?: boolean;
}, command?: string): Promise<boolean>;
export declare function connectExistingRepoToNxCloudPrompt(command?: string, key?: MessageKey): Promise<boolean>;
export declare function connectToNxCloudWithPrompt(command: string): Promise<void>;

@@ -57,3 +57,3 @@ "use strict";

}
async function connectToNxCloudCommand(command) {
async function connectToNxCloudCommand(options, command) {
const nxJson = (0, configuration_1.readNxJson)();

@@ -70,3 +70,3 @@ const installationSource = process.env.NX_CONSOLE

}
const connectCloudUrl = await (0, url_shorten_1.createNxCloudOnboardingURL)(installationSource, token);
const connectCloudUrl = await (0, url_shorten_1.createNxCloudOnboardingURL)(installationSource, token, options?.generateToken !== true);
output_1.output.log({

@@ -83,5 +83,6 @@ title: '✔ This workspace already has Nx Cloud set up',

const token = await connectWorkspaceToCloud({
generateToken: options?.generateToken,
installationSource: command ?? installationSource,
});
const connectCloudUrl = await (0, url_shorten_1.createNxCloudOnboardingURL)('nx-connect', token);
const connectCloudUrl = await (0, url_shorten_1.createNxCloudOnboardingURL)('nx-connect', token, options?.generateToken !== true);
try {

@@ -121,3 +122,5 @@ const cloudConnectSpinner = ora(`Opening Nx Cloud ${connectCloudUrl} in your browser to connect your workspace.`).start();

const setNxCloud = await nxCloudPrompt('setupNxCloud');
const useCloud = setNxCloud === 'yes' ? await connectToNxCloudCommand(command) : false;
const useCloud = setNxCloud === 'yes'
? await connectToNxCloudCommand({ generateToken: false }, command)
: false;
await (0, ab_testing_1.recordStat)({

@@ -124,0 +127,0 @@ command,

@@ -27,2 +27,6 @@ "use strict";

})
.option('depth', {
type: 'number',
description: 'The depth to clone the source repository (limit this for faster git clone)',
})
.option('interactive', {

@@ -29,0 +33,0 @@ type: 'boolean',

@@ -18,2 +18,6 @@ export interface ImportOptions {

destination: string;
/**
* The depth to clone the source repository (limit this for faster clone times)
*/
depth: number;
verbose: boolean;

@@ -20,0 +24,0 @@ interactive: boolean;

@@ -5,2 +5,6 @@ "use strict";

const path_1 = require("path");
const minimatch_1 = require("minimatch");
const node_fs_1 = require("node:fs");
const chalk = require("chalk");
const js_yaml_1 = require("@zkochan/js-yaml");
const git_utils_1 = require("../../utils/git-utils");

@@ -22,2 +26,3 @@ const promises_1 = require("node:fs/promises");

const needs_install_1 = require("./utils/needs-install");
const file_utils_1 = require("../../project-graph/file-utils");
const importRemoteName = '__tmp_nx_import__';

@@ -59,3 +64,3 @@ async function importHandler(options) {

const sourceRepoPath = (0, path_1.join)(tempImportDirectory, 'repo');
const spinner = createSpinner(`Cloning ${sourceRemoteUrl} into a temporary directory: ${sourceRepoPath}`).start();
const spinner = createSpinner(`Cloning ${sourceRemoteUrl} into a temporary directory: ${sourceRepoPath} (Use --depth to limit commit history and speed up clone times)`).start();
try {

@@ -70,2 +75,3 @@ await (0, promises_1.rm)(tempImportDirectory, { recursive: true });

originName: importRemoteName,
depth: options.depth,
});

@@ -79,2 +85,4 @@ }

spinner.succeed(`Cloned into ${sourceRepoPath}`);
// Detecting the package manager before preparing the source repo for import.
const sourcePackageManager = (0, package_manager_1.detectPackageManager)(sourceGitClient.root);
if (!ref) {

@@ -112,2 +120,3 @@ const branchChoices = await sourceGitClient.listBranches();

required: true,
initial: source ? source : undefined,
},

@@ -118,2 +127,14 @@ ])).destination;

const absDestination = (0, path_1.join)(process.cwd(), destination);
const destinationGitClient = new git_utils_1.GitRepository(process.cwd());
await assertDestinationEmpty(destinationGitClient, absDestination);
const tempImportBranch = getTempImportBranch(ref);
await sourceGitClient.addFetchRemote(importRemoteName, ref);
await sourceGitClient.fetch(importRemoteName, ref);
spinner.succeed(`Fetched ${ref} from ${sourceRemoteUrl}`);
spinner.start(`Checking out a temporary branch, ${tempImportBranch} based on ${ref}`);
await sourceGitClient.checkout(tempImportBranch, {
new: true,
base: `${importRemoteName}/${ref}`,
});
spinner.succeed(`Created a ${tempImportBranch} branch based on ${ref}`);
try {

@@ -125,9 +146,6 @@ await (0, promises_1.stat)(absSource);

}
const destinationGitClient = new git_utils_1.GitRepository(process.cwd());
await assertDestinationEmpty(destinationGitClient, absDestination);
const tempImportBranch = getTempImportBranch(ref);
const packageManager = (0, package_manager_1.detectPackageManager)(workspace_root_1.workspaceRoot);
const originalPackageWorkspaces = await (0, needs_install_1.getPackagesInPackageManagerWorkspace)(packageManager);
const relativeDestination = (0, path_1.relative)(destinationGitClient.root, absDestination);
await (0, prepare_source_repo_1.prepareSourceRepo)(sourceGitClient, ref, source, relativeDestination, tempImportBranch, sourceRemoteUrl, importRemoteName);
await (0, prepare_source_repo_1.prepareSourceRepo)(sourceGitClient, ref, source, relativeDestination, tempImportBranch, sourceRemoteUrl);
await createTemporaryRemote(destinationGitClient, (0, path_1.join)(sourceRepoPath, '.git'), importRemoteName);

@@ -143,15 +161,65 @@ await (0, merge_remote_source_1.mergeRemoteSource)(destinationGitClient, sourceRemoteUrl, tempImportBranch, destination, importRemoteName, ref);

const { plugins, updatePackageScripts } = await (0, init_v2_1.detectPlugins)(nxJson, options.interactive);
if (packageManager !== sourcePackageManager) {
output_1.output.warn({
title: `Mismatched package managers`,
bodyLines: [
`The source repository is using a different package manager (${sourcePackageManager}) than this workspace (${packageManager}).`,
`This could lead to install issues due to discrepancies in "package.json" features.`,
],
});
}
// If install fails, we should continue since the errors could be resolved later.
let installFailed = false;
if (plugins.length > 0) {
output_1.output.log({ title: 'Installing Plugins' });
(0, init_v2_1.installPlugins)(workspace_root_1.workspaceRoot, plugins, pmc, updatePackageScripts);
await destinationGitClient.amendCommit();
try {
output_1.output.log({ title: 'Installing Plugins' });
(0, init_v2_1.installPlugins)(workspace_root_1.workspaceRoot, plugins, pmc, updatePackageScripts);
await destinationGitClient.amendCommit();
}
catch (e) {
installFailed = true;
output_1.output.error({
title: `Install failed: ${e.message || 'Unknown error'}`,
bodyLines: [e.stack],
});
}
}
else if (await (0, needs_install_1.needsInstall)(packageManager, originalPackageWorkspaces)) {
try {
output_1.output.log({
title: 'Installing dependencies for imported code',
});
(0, utils_1.runInstall)(workspace_root_1.workspaceRoot, (0, package_manager_1.getPackageManagerCommand)(packageManager));
await destinationGitClient.amendCommit();
}
catch (e) {
installFailed = true;
output_1.output.error({
title: `Install failed: ${e.message || 'Unknown error'}`,
bodyLines: [e.stack],
});
}
}
console.log(await destinationGitClient.showStat());
if (installFailed) {
const pmc = (0, package_manager_1.getPackageManagerCommand)(packageManager);
output_1.output.warn({
title: `The import was successful, but the install failed`,
bodyLines: [
`You may need to run "${pmc.install}" manually to resolve the issue. The error is logged above.`,
],
});
}
await warnOnMissingWorkspacesEntry(packageManager, pmc, relativeDestination);
// When only a subdirectory is imported, there might be devDependencies in the root package.json file
// that needs to be ported over as well.
if (ref) {
output_1.output.log({
title: 'Installing dependencies for imported code',
title: `Check root dependencies`,
bodyLines: [
`"dependencies" and "devDependencies" are not imported from the source repository (${sourceRemoteUrl}).`,
`You may need to add some of those dependencies to this workspace in order to run tasks successfully.`,
],
});
(0, utils_1.runInstall)(workspace_root_1.workspaceRoot, (0, package_manager_1.getPackageManagerCommand)(packageManager));
await destinationGitClient.amendCommit();
}
console.log(await destinationGitClient.showStat());
output_1.output.log({

@@ -183,1 +251,68 @@ title: `Merging these changes into ${(0, command_line_utils_1.getBaseRef)(nxJson)}`,

}
// If the user imports a project that isn't in NPM/Yarn/PNPM workspaces, then its dependencies
// will not be installed. We should warn users and provide instructions on how to fix this.
async function warnOnMissingWorkspacesEntry(pm, pmc, pkgPath) {
if (!(0, package_manager_1.isWorkspacesEnabled)(pm, workspace_root_1.workspaceRoot)) {
output_1.output.warn({
title: `Missing workspaces in package.json`,
bodyLines: pm === 'npm'
? [
`We recommend enabling NPM workspaces to install dependencies for the imported project.`,
`Add \`"workspaces": ["${pkgPath}"]\` to package.json and run "${pmc.install}".`,
`See: https://docs.npmjs.com/cli/using-npm/workspaces`,
]
: pm === 'yarn'
? [
`We recommend enabling Yarn workspaces to install dependencies for the imported project.`,
`Add \`"workspaces": ["${pkgPath}"]\` to package.json and run "${pmc.install}".`,
`See: https://yarnpkg.com/features/workspaces`,
]
: pm === 'bun'
? [
`We recommend enabling Bun workspaces to install dependencies for the imported project.`,
`Add \`"workspaces": ["${pkgPath}"]\` to package.json and run "${pmc.install}".`,
`See: https://bun.sh/docs/install/workspaces`,
]
: [
`We recommend enabling PNPM workspaces to install dependencies for the imported project.`,
`Add the following entry to to pnpm-workspace.yaml and run "${pmc.install}":`,
chalk.bold(`packages:\n - '${pkgPath}'`),
`See: https://pnpm.io/workspaces`,
],
});
}
else {
// Check if the new package is included in existing workspaces entries. If not, warn the user.
let workspaces = null;
if (pm === 'npm' || pm === 'yarn' || pm === 'bun') {
const packageJson = (0, file_utils_1.readPackageJson)();
workspaces = packageJson.workspaces;
}
else if (pm === 'pnpm') {
const yamlPath = (0, path_1.join)(workspace_root_1.workspaceRoot, 'pnpm-workspace.yaml');
if ((0, node_fs_1.existsSync)(yamlPath)) {
const yamlContent = await node_fs_1.promises.readFile(yamlPath, 'utf-8');
const yaml = (0, js_yaml_1.load)(yamlContent);
workspaces = yaml.packages;
}
}
if (workspaces) {
const isPkgIncluded = workspaces.some((w) => (0, minimatch_1.minimatch)(pkgPath, w));
if (!isPkgIncluded) {
const pkgsDir = (0, path_1.dirname)(pkgPath);
output_1.output.warn({
title: `Project missing in workspaces`,
bodyLines: pm === 'npm' || pm === 'yarn' || pm === 'bun'
? [
`The imported project (${pkgPath}) is missing the "workspaces" field in package.json.`,
`Add "${pkgsDir}/*" to workspaces run "${pmc.install}".`,
]
: [
`The imported project (${pkgPath}) is missing the "packages" field in pnpm-workspaces.yaml.`,
`Add "${pkgsDir}/*" to packages run "${pmc.install}".`,
],
});
}
}
}
}
import { GitRepository } from '../../../utils/git-utils';
export declare function prepareSourceRepo(gitClient: GitRepository, ref: string, source: string, relativeDestination: string, tempImportBranch: string, sourceRemoteUrl: string, originName: string): Promise<void>;
export declare function prepareSourceRepo(gitClient: GitRepository, ref: string, source: string, relativeDestination: string, tempImportBranch: string, sourceRemoteUrl: string): Promise<void>;

@@ -7,95 +7,41 @@ "use strict";

const promises_1 = require("node:fs/promises");
async function prepareSourceRepo(gitClient, ref, source, relativeDestination, tempImportBranch, sourceRemoteUrl, originName) {
async function prepareSourceRepo(gitClient, ref, source, relativeDestination, tempImportBranch, sourceRemoteUrl) {
const spinner = createSpinner().start(`Fetching ${ref} from ${sourceRemoteUrl}`);
await gitClient.addFetchRemote(originName, ref);
await gitClient.fetch(originName, ref);
spinner.succeed(`Fetched ${ref} from ${sourceRemoteUrl}`);
spinner.start(`Checking out a temporary branch, ${tempImportBranch} based on ${ref}`);
await gitClient.checkout(tempImportBranch, {
new: true,
base: `${originName}/${ref}`,
});
spinner.succeed(`Created a ${tempImportBranch} branch based on ${ref}`);
const relativeSourceDir = (0, path_1.relative)(gitClient.root, (0, path_1.join)(gitClient.root, source));
if (relativeSourceDir !== '') {
if (await gitClient.hasFilterRepoInstalled()) {
spinner.start(`Filtering git history to only include files in ${relativeSourceDir}`);
await gitClient.filterRepo(relativeSourceDir);
}
else {
spinner.start(`Filtering git history to only include files in ${relativeSourceDir} (this might take a few minutes -- install git-filter-repo for faster performance)`);
await gitClient.filterBranch(relativeSourceDir, tempImportBranch);
}
spinner.succeed(`Filtered git history to only include files in ${relativeSourceDir}`);
}
const destinationInSource = (0, path_1.join)(gitClient.root, relativeDestination);
spinner.start(`Moving files and git history to ${destinationInSource}`);
if (relativeSourceDir === '') {
const files = await gitClient.getGitFiles('.');
// The result of filter-branch will contain only the files in the subdirectory at its root.
const files = await gitClient.getGitFiles('.');
try {
await (0, promises_1.rm)(destinationInSource, {
recursive: true,
});
}
catch { }
await (0, promises_1.mkdir)(destinationInSource, { recursive: true });
for (const file of files) {
spinner.start(`Moving files and git history to ${destinationInSource}: ${file}`);
const newPath = (0, path_1.join)(destinationInSource, file);
await (0, promises_1.mkdir)((0, path_1.dirname)(newPath), { recursive: true });
try {
await (0, promises_1.rm)(destinationInSource, {
recursive: true,
});
await gitClient.move(file, newPath);
}
catch { }
await (0, promises_1.mkdir)(destinationInSource, { recursive: true });
const gitignores = new Set();
for (const file of files) {
if ((0, path_1.basename)(file) === '.gitignore') {
gitignores.add(file);
continue;
}
spinner.start(`Moving files and git history to ${destinationInSource}: ${file}`);
const newPath = (0, path_1.join)(destinationInSource, file);
await (0, promises_1.mkdir)((0, path_1.dirname)(newPath), { recursive: true });
try {
await gitClient.move(file, newPath);
}
catch {
await wait(100);
await gitClient.move(file, newPath);
}
catch {
await wait(100);
await gitClient.move(file, newPath);
}
await gitClient.commit(`chore(repo): move ${source} to ${relativeDestination} to prepare to be imported`);
for (const gitignore of gitignores) {
await gitClient.move(gitignore, (0, path_1.join)(destinationInSource, gitignore));
}
await gitClient.amendCommit();
for (const gitignore of gitignores) {
await (0, promises_1.copyFile)((0, path_1.join)(destinationInSource, gitignore), (0, path_1.join)(gitClient.root, gitignore));
}
}
else {
let needsSquash = false;
const needsMove = destinationInSource !== (0, path_1.join)(gitClient.root, source);
if (needsMove) {
try {
await (0, promises_1.rm)(destinationInSource, {
recursive: true,
});
await gitClient.commit(`chore(repo): move ${source} to ${relativeDestination} to prepare to be imported`);
needsSquash = true;
}
catch { }
await (0, promises_1.mkdir)(destinationInSource, { recursive: true });
}
const files = await gitClient.getGitFiles('.');
for (const file of files) {
if (file === '.gitignore') {
continue;
}
spinner.start(`Moving files and git history to ${destinationInSource}: ${file}`);
if (!(0, path_1.relative)(source, file).startsWith('..')) {
if (needsMove) {
const newPath = (0, path_1.join)(destinationInSource, file);
await (0, promises_1.mkdir)((0, path_1.dirname)(newPath), { recursive: true });
try {
await gitClient.move(file, newPath);
}
catch {
await wait(100);
await gitClient.move(file, newPath);
}
}
}
else {
await (0, promises_1.rm)((0, path_1.join)(gitClient.root, file), {
recursive: true,
});
}
}
await gitClient.commit(`chore(repo): move ${source} to ${relativeDestination} to prepare to be imported`);
if (needsSquash) {
await gitClient.squashLastTwoCommits();
}
}
await gitClient.commit(`chore(repo): move ${source} to ${relativeDestination} to prepare to be imported`);
await gitClient.amendCommit();
spinner.succeed(`${sourceRemoteUrl} has been prepared to be imported into this workspace on a temporary branch: ${tempImportBranch} in ${gitClient.root}`);

@@ -102,0 +48,0 @@ }

@@ -212,11 +212,3 @@ "use strict";

return yargs.option('output-style', {
describe: `Defines how Nx emits outputs tasks logs
| option | description |
| --- | --- |
| dynamic | use dynamic output life cycle, previous content is overwritten or modified as new outputs are added, display minimal logs by default, always show errors. This output format is recommended on your local development environments. |
| static | uses static output life cycle, no previous content is rewritten or modified as new outputs are added. This output format is recommened for CI environments. |
| stream | nx by default logs output to an internal output stream, enable this option to stream logs to stdout / stderr |
| stream-without-prefixes | nx prefixes the project name the target is running on, use this option remove the project name prefix from output |
`,
describe: `Defines how Nx emits outputs tasks logs. **dynamic**: use dynamic output life cycle, previous content is overwritten or modified as new outputs are added, display minimal logs by default, always show errors. This output format is recommended on your local development environments. **static**: uses static output life cycle, no previous content is rewritten or modified as new outputs are added. This output format is recommened for CI environments. **stream**: nx by default logs output to an internal output stream, enable this option to stream logs to stdout / stderr. **stream-without-prefixes**: nx prefixes the project name the target is running on, use this option remove the project name prefix from output.`,
type: 'string',

@@ -223,0 +215,0 @@ choices,

@@ -7,1 +7,5 @@ import type { ProjectGraph } from '../../config/project-graph';

export declare function getCachedRegisteredSyncGenerators(): Promise<string[]>;
/**
* @internal
*/
export declare function _getConflictingGeneratorGroups(results: SyncGeneratorChangesResult[]): string[][];

@@ -7,2 +7,3 @@ "use strict";

exports.getCachedRegisteredSyncGenerators = getCachedRegisteredSyncGenerators;
exports._getConflictingGeneratorGroups = _getConflictingGeneratorGroups;
const nx_json_1 = require("../../config/nx-json");

@@ -29,3 +30,2 @@ const tree_1 = require("../../generators/tree");

};
// TODO(leo): check conflicts and reuse the Tree where possible
async function getCachedSyncGeneratorChanges(generators) {

@@ -42,47 +42,11 @@ try {

waitPeriod = 100;
let projects;
let errored = false;
const getProjectsConfigurations = async () => {
if (projects || errored) {
return projects;
}
const { projectGraph, error } = await (0, project_graph_incremental_recomputation_1.getCachedSerializedProjectGraphPromise)();
projects = projectGraph
? (0, project_graph_1.readProjectsConfigurationFromProjectGraph)(projectGraph).projects
: null;
errored = error !== undefined;
return projects;
};
return (await Promise.all(generators.map(async (generator) => {
if (scheduledGenerators.has(generator) ||
!syncGeneratorsCacheResultPromises.has(generator)) {
// it's scheduled to run (there are pending changes to process) or
// it's not scheduled and there's no cached result, so run it
const projects = await getProjectsConfigurations();
if (projects) {
log(generator, 'already scheduled or not cached, running it now');
runGenerator(generator, projects);
}
else {
log(generator, 'already scheduled or not cached, project graph errored');
/**
* This should never happen. This is invoked imperatively, and by
* the time it is invoked, the project graph would have already
* been requested. If it errored, it would have been reported and
* this wouldn't have been invoked. We handle it just in case.
*
* Since the project graph would be reported by the relevant
* handlers separately, we just ignore the error, don't cache
* any result and return an empty result, the next time this is
* invoked the process will repeat until it eventually recovers
* when the project graph is fixed.
*/
return Promise.resolve({ changes: [], generatorName: generator });
}
}
else {
log(generator, 'not scheduled and has cached result, returning cached result');
}
return syncGeneratorsCacheResultPromises.get(generator);
}))).flat();
const results = await getFromCacheOrRunGenerators(generators);
const conflicts = _getConflictingGeneratorGroups(results);
if (!conflicts.length) {
// there are no conflicts
return results;
}
// there are conflicts, so we need to re-run the conflicting generators
// using the same tree
return await processConflictingGenerators(conflicts, results);
}

@@ -137,3 +101,3 @@ catch (e) {

for (const generator of scheduledGenerators) {
runGenerator(generator, projects);
syncGeneratorsCacheResultPromises.set(generator, runGenerator(generator, projects));
}

@@ -155,2 +119,159 @@ await Promise.all(syncGeneratorsCacheResultPromises.values());

}
async function getFromCacheOrRunGenerators(generators) {
let projects;
let errored = false;
const getProjectsConfigurations = async () => {
if (projects || errored) {
return projects;
}
const { projectGraph, error } = await (0, project_graph_incremental_recomputation_1.getCachedSerializedProjectGraphPromise)();
projects = projectGraph
? (0, project_graph_1.readProjectsConfigurationFromProjectGraph)(projectGraph).projects
: null;
errored = error !== undefined;
return projects;
};
return (await Promise.all(generators.map(async (generator) => {
if (scheduledGenerators.has(generator) ||
!syncGeneratorsCacheResultPromises.has(generator)) {
// it's scheduled to run (there are pending changes to process) or
// it's not scheduled and there's no cached result, so run it
const projects = await getProjectsConfigurations();
if (projects) {
log(generator, 'already scheduled or not cached, running it now');
syncGeneratorsCacheResultPromises.set(generator, runGenerator(generator, projects));
}
else {
log(generator, 'already scheduled or not cached, project graph errored');
/**
* This should never happen. This is invoked imperatively, and by
* the time it is invoked, the project graph would have already
* been requested. If it errored, it would have been reported and
* this wouldn't have been invoked. We handle it just in case.
*
* Since the project graph would be reported by the relevant
* handlers separately, we just ignore the error, don't cache
* any result and return an empty result, the next time this is
* invoked the process will repeat until it eventually recovers
* when the project graph is fixed.
*/
return Promise.resolve({ changes: [], generatorName: generator });
}
}
else {
log(generator, 'not scheduled and has cached result, returning cached result');
}
return syncGeneratorsCacheResultPromises.get(generator);
}))).flat();
}
async function runConflictingGenerators(tree, generators) {
const { projectGraph } = await (0, project_graph_incremental_recomputation_1.getCachedSerializedProjectGraphPromise)();
const projects = projectGraph
? (0, project_graph_1.readProjectsConfigurationFromProjectGraph)(projectGraph).projects
: null;
if (!projects) {
/**
* This should never happen. This is invoked imperatively, and by
* the time it is invoked, the project graph would have already
* been requested. If it errored, it would have been reported and
* this wouldn't have been invoked. We handle it just in case.
*
* Since the project graph would be reported by the relevant
* handlers separately, we just ignore the error.
*/
return generators.map((generator) => ({
changes: [],
generatorName: generator,
}));
}
// we need to run conflicting generators sequentially because they use the same tree
const results = [];
for (const generator of generators) {
log(generator, 'running it now');
results.push(await runGenerator(generator, projects, tree));
}
return results;
}
async function processConflictingGenerators(conflicts, initialResults) {
const conflictRunResults = (await Promise.all(conflicts.map((generators) => {
const [firstGenerator, ...generatorsToRun] = generators;
// it must exists because the conflicts were identified from the initial results
const firstGeneratorResult = initialResults.find((r) => r.generatorName === firstGenerator);
const tree = new tree_1.FsTree(workspace_root_1.workspaceRoot, false, `running sync generators ${generators.join(',')}`);
// pre-apply the changes from the first generator to avoid running it
for (const change of firstGeneratorResult.changes) {
if (change.type === 'CREATE' || change.type === 'UPDATE') {
tree.write(change.path, change.content, change.options);
}
else if (change.type === 'DELETE') {
tree.delete(change.path);
}
}
/**
* We don't cache the results of conflicting generators because they
* use the same tree, so some files might contain results from multiple
* generators and we don't have guarantees that the same combination of
* generators will run together.
*/
return runConflictingGenerators(tree, generatorsToRun);
}))).flat();
/**
* The order of the results from the re-run generators is important because
* the last result from a group of conflicting generators will contain the
* changes from the previous conflicting generators. So, instead of replacing
* in-place the initial results, we first add the results from the re-run
* generators, and then add the initial results that were not from a
* conflicting generator.
*/
const results = [...conflictRunResults];
for (const result of initialResults) {
if (conflictRunResults.every((r) => r.generatorName !== result.generatorName)) {
// this result is not from a conflicting generator, so we add it to the
// results
results.push(result);
}
}
return results;
}
/**
* @internal
*/
function _getConflictingGeneratorGroups(results) {
const changedFileToGeneratorMap = new Map();
for (const result of results) {
for (const change of result.changes) {
if (!changedFileToGeneratorMap.has(change.path)) {
changedFileToGeneratorMap.set(change.path, new Set());
}
changedFileToGeneratorMap.get(change.path).add(result.generatorName);
}
}
const conflicts = [];
for (const generatorSet of changedFileToGeneratorMap.values()) {
if (generatorSet.size === 1) {
// no conflicts
continue;
}
if (conflicts.length === 0) {
// there are no conflicts yet, so we just add the first group
conflicts.push(new Set(generatorSet));
continue;
}
// identify if any of the current generator sets intersect with any of the
// existing conflict groups
const generatorsArray = Array.from(generatorSet);
const existingConflictGroup = conflicts.find((group) => generatorsArray.some((generator) => group.has(generator)));
if (existingConflictGroup) {
// there's an intersecting group, so we merge the two
for (const generator of generatorsArray) {
existingConflictGroup.add(generator);
}
}
else {
// there's no intersecting group, so we create a new one
conflicts.push(new Set(generatorsArray));
}
}
return conflicts.map((group) => Array.from(group));
}
function collectAllRegisteredSyncGenerators(projectGraph) {

@@ -199,12 +320,11 @@ const nxJson = (0, nx_json_1.readNxJson)();

}
function runGenerator(generator, projects) {
function runGenerator(generator, projects, tree) {
log('running scheduled generator', generator);
// remove it from the scheduled set
scheduledGenerators.delete(generator);
const tree = new tree_1.FsTree(workspace_root_1.workspaceRoot, false, `running sync generator ${generator}`);
// run the generator and cache the result
syncGeneratorsCacheResultPromises.set(generator, (0, sync_generators_1.runSyncGenerator)(tree, generator, projects).then((result) => {
tree ??= new tree_1.FsTree(workspace_root_1.workspaceRoot, false, `running sync generator ${generator}`);
return (0, sync_generators_1.runSyncGenerator)(tree, generator, projects).then((result) => {
log(generator, 'changes:', result.changes.map((c) => c.path).join(', '));
return result;
}));
});
}

@@ -211,0 +331,0 @@ function hashProjectGraph(projectGraph) {

@@ -10,5 +10,6 @@ import { Tree } from '../../../generators/tree';

directory?: string;
generateToken?: boolean;
}
export declare function connectToNxCloud(tree: Tree, schema: ConnectToNxCloudOptions, nxJson?: NxJsonConfiguration<string[] | "*">): Promise<string>;
export declare function connectToNxCloud(tree: Tree, schema: ConnectToNxCloudOptions, nxJson?: NxJsonConfiguration<string[] | "*">): Promise<string | null>;
declare function connectToNxCloudGenerator(tree: Tree, options: ConnectToNxCloudOptions): Promise<void>;
export default connectToNxCloudGenerator;

@@ -91,16 +91,18 @@ "use strict";

}
else {
const usesGithub = schema.github ?? (await (0, url_shorten_1.repoUsesGithub)(schema.github));
let responseFromCreateNxCloudWorkspaceV2;
// do NOT create Nx Cloud token (createNxCloudWorkspace)
// if user is using github and is running nx-connect
if (!(usesGithub && schema.installationSource === 'nx-connect')) {
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(getRootPackageName(tree), schema.installationSource, getNxInitDate());
addNxCloudIdToNxJson(tree, responseFromCreateNxCloudWorkspaceV2?.nxCloudId, schema.directory);
await (0, format_changed_files_with_prettier_if_available_1.formatChangedFilesWithPrettierIfAvailable)(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
}
}
const isGitHubDetected = schema.github ?? (await (0, url_shorten_1.repoUsesGithub)(schema.github));
let responseFromCreateNxCloudWorkspaceV2;
/**
* Do not create an Nx Cloud token if the user is using GitHub and
* is running `nx-connect` AND `token` is undefined (override)
*/
if (!schema.generateToken &&
isGitHubDetected &&
schema.installationSource === 'nx-connect')
return null;
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(getRootPackageName(tree), schema.installationSource, getNxInitDate());
addNxCloudIdToNxJson(tree, responseFromCreateNxCloudWorkspaceV2?.nxCloudId, schema.directory);
await (0, format_changed_files_with_prettier_if_available_1.formatChangedFilesWithPrettierIfAvailable)(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
}

@@ -107,0 +109,0 @@ async function connectToNxCloudGenerator(tree, options) {

@@ -24,2 +24,6 @@ {

},
"generateToken": {
"type": "boolean",
"description": "Explicitly asks for a token to be created, do not override existing tokens from Nx Cloud"
},
"github": {

@@ -26,0 +30,0 @@ "type": "boolean",

@@ -1,4 +0,4 @@

import { ExecSyncOptions } from 'child_process';
export declare function cloneFromUpstream(url: string, destination: string, { originName }?: {
export declare function cloneFromUpstream(url: string, destination: string, { originName, depth }?: {
originName: string;
depth?: number;
}): Promise<GitRepository>;

@@ -11,3 +11,2 @@ export declare class GitRepository {

addFetchRemote(remoteName: string, branch: string): Promise<string>;
private execAsync;
showStat(): Promise<string>;

@@ -17,3 +16,2 @@ listBranches(): Promise<string[]>;

reset(ref: string): Promise<string>;
squashLastTwoCommits(): Promise<string>;
mergeUnrelatedHistories(ref: string, message: string): Promise<string>;

@@ -30,11 +28,10 @@ fetch(remote: string, ref?: string): Promise<string>;

deleteGitRemote(name: string): Promise<string>;
deleteBranch(branch: string): Promise<string>;
addGitRemote(name: string, url: string): Promise<string>;
hasFilterRepoInstalled(): Promise<boolean>;
filterRepo(subdirectory: string): Promise<string>;
filterBranch(subdirectory: string, branchName: string): Promise<string>;
private execAsync;
private quotePath;
}
/**
* This is used by the squash editor script to update the rebase file.
*/
export declare function updateRebaseFile(contents: string): string;
export declare function fetchGitRemote(name: string, branch: string, execOptions: ExecSyncOptions): string | Buffer;
/**
* This is currently duplicated in Nx Console. Please let @MaxKless know if you make changes here.

@@ -41,0 +38,0 @@ */

@@ -5,4 +5,2 @@ "use strict";

exports.cloneFromUpstream = cloneFromUpstream;
exports.updateRebaseFile = updateRebaseFile;
exports.fetchGitRemote = fetchGitRemote;
exports.getGithubSlugOrNull = getGithubSlugOrNull;

@@ -13,5 +11,4 @@ exports.extractUserAndRepoFromGitHubUrl = extractUserAndRepoFromGitHubUrl;

const child_process_1 = require("child_process");
const path_1 = require("path");
const devkit_exports_1 = require("../devkit-exports");
const path_1 = require("path");
const SQUASH_EDITOR = (0, path_1.join)(__dirname, 'squash.js');
function execAsync(command, execOptions) {

@@ -27,5 +24,8 @@ return new Promise((res, rej) => {

}
async function cloneFromUpstream(url, destination, { originName } = { originName: 'origin' }) {
await execAsync(`git clone ${url} ${destination} --depth 1 --origin ${originName}`, {
async function cloneFromUpstream(url, destination, { originName, depth } = {
originName: 'origin',
}) {
await execAsync(`git clone ${url} ${destination} ${depth ? `--depth ${depth}` : ''} --origin ${originName}`, {
cwd: (0, path_1.dirname)(destination),
maxBuffer: 10 * 1024 * 1024,
});

@@ -46,10 +46,5 @@ return new GitRepository(destination);

}
addFetchRemote(remoteName, branch) {
return this.execAsync(`git config --add remote.${remoteName}.fetch "+refs/heads/${branch}:refs/remotes/${remoteName}/${branch}"`);
async addFetchRemote(remoteName, branch) {
return await this.execAsync(`git config --add remote.${remoteName}.fetch "+refs/heads/${branch}:refs/remotes/${remoteName}/${branch}"`);
}
execAsync(command) {
return execAsync(command, {
cwd: this.root,
});
}
async showStat() {

@@ -68,5 +63,7 @@ return await this.execAsync(`git show --stat`);

async getGitFiles(path) {
return (await this.execAsync(`git ls-files ${path}`))
// Use -z to return file names exactly as they are stored in git, separated by NULL (\x00) character.
// This avoids problems with special characters in file names.
return (await this.execAsync(`git ls-files -z ${path}`))
.trim()
.split('\n')
.split('\x00')
.map((s) => s.trim())

@@ -76,52 +73,72 @@ .filter(Boolean);

async reset(ref) {
return this.execAsync(`git reset ${ref} --hard`);
return await this.execAsync(`git reset ${ref} --hard`);
}
async squashLastTwoCommits() {
return this.execAsync(`git -c core.editor="node ${SQUASH_EDITOR}" rebase --interactive --no-autosquash HEAD~2`);
}
async mergeUnrelatedHistories(ref, message) {
return this.execAsync(`git merge ${ref} -X ours --allow-unrelated-histories -m "${message}"`);
return await this.execAsync(`git merge ${ref} -X ours --allow-unrelated-histories -m "${message}"`);
}
async fetch(remote, ref) {
return this.execAsync(`git fetch ${remote}${ref ? ` ${ref}` : ''}`);
return await this.execAsync(`git fetch ${remote}${ref ? ` ${ref}` : ''}`);
}
async checkout(branch, opts) {
return this.execAsync(`git checkout ${opts.new ? '-b ' : ' '}${branch}${opts.base ? ' ' + opts.base : ''}`);
return await this.execAsync(`git checkout ${opts.new ? '-b ' : ' '}${branch}${opts.base ? ' ' + opts.base : ''}`);
}
async move(path, destination) {
return this.execAsync(`git mv "${path}" "${destination}"`);
return await this.execAsync(`git mv ${this.quotePath(path)} ${this.quotePath(destination)}`);
}
async push(ref, remoteName) {
return this.execAsync(`git push -u -f ${remoteName} ${ref}`);
return await this.execAsync(`git push -u -f ${remoteName} ${ref}`);
}
async commit(message) {
return this.execAsync(`git commit -am "${message}"`);
return await this.execAsync(`git commit -am "${message}"`);
}
async amendCommit() {
return this.execAsync(`git commit --amend -a --no-edit`);
return await this.execAsync(`git commit --amend -a --no-edit`);
}
deleteGitRemote(name) {
return this.execAsync(`git remote rm ${name}`);
async deleteGitRemote(name) {
return await this.execAsync(`git remote rm ${name}`);
}
deleteBranch(branch) {
return this.execAsync(`git branch -D ${branch}`);
async addGitRemote(name, url) {
return await this.execAsync(`git remote add ${name} ${url}`);
}
addGitRemote(name, url) {
return this.execAsync(`git remote add ${name} ${url}`);
async hasFilterRepoInstalled() {
try {
await this.execAsync(`git filter-repo --help`);
return true;
}
catch {
return false;
}
}
// git-filter-repo is much faster than filter-branch, but needs to be installed by user
// Use `hasFilterRepoInstalled` to check if it's installed
async filterRepo(subdirectory) {
// filter-repo requires POSIX path to work
const posixPath = subdirectory.split(path_1.sep).join(path_1.posix.sep);
return await this.execAsync(`git filter-repo -f --subdirectory-filter ${this.quotePath(posixPath)}`);
}
async filterBranch(subdirectory, branchName) {
// filter-repo requires POSIX path to work
const posixPath = subdirectory.split(path_1.sep).join(path_1.posix.sep);
// We need non-ASCII file names to not be quoted, or else filter-branch will exclude them.
await this.execAsync(`git config core.quotepath false`);
return await this.execAsync(`git filter-branch --subdirectory-filter ${this.quotePath(posixPath)} -- ${branchName}`);
}
execAsync(command) {
return execAsync(command, {
cwd: this.root,
maxBuffer: 10 * 1024 * 1024,
});
}
quotePath(path) {
return process.platform === 'win32'
? // Windows/CMD only understands double-quotes, single-quotes are treated as part of the file name
// Bash and other shells will substitute `$` in file names with a variable value.
`"${path}"`
: // e.g. `git mv "$$file.txt" "libs/a/$$file.txt"` will not work since `$$` is swapped with the PID of the last process.
// Using single-quotes prevents this substitution.
`'${path}'`;
}
}
exports.GitRepository = GitRepository;
/**
* This is used by the squash editor script to update the rebase file.
*/
function updateRebaseFile(contents) {
const lines = contents.split('\n');
const lastCommitIndex = lines.findIndex((line) => line === '') - 1;
lines[lastCommitIndex] = lines[lastCommitIndex].replace('pick', 'fixup');
return lines.join('\n');
}
function fetchGitRemote(name, branch, execOptions) {
return (0, child_process_1.execSync)(`git fetch ${name} ${branch} --depth 1`, execOptions);
}
/**
* This is currently duplicated in Nx Console. Please let @MaxKless know if you make changes here.

@@ -128,0 +145,0 @@ */

@@ -6,3 +6,3 @@ import type { GeneratorCallback } from '../config/misc-interfaces';

import type { ProjectConfiguration } from '../config/workspace-json-project-json';
import { FsTree, type FileChange, type Tree } from '../generators/tree';
import { type FileChange, type Tree } from '../generators/tree';
export type SyncGeneratorResult = void | {

@@ -22,3 +22,3 @@ callback?: GeneratorCallback;

export declare function collectAllRegisteredSyncGenerators(projectGraph: ProjectGraph, nxJson: NxJsonConfiguration): Promise<string[]>;
export declare function runSyncGenerator(tree: FsTree, generatorSpecifier: string, projects: Record<string, ProjectConfiguration>): Promise<SyncGeneratorChangesResult>;
export declare function runSyncGenerator(tree: Tree, generatorSpecifier: string, projects: Record<string, ProjectConfiguration>): Promise<SyncGeneratorChangesResult>;
export declare function collectEnabledTaskSyncGeneratorsFromProjectGraph(projectGraph: ProjectGraph, nxJson: NxJsonConfiguration): Set<string>;

@@ -25,0 +25,0 @@ export declare function collectEnabledTaskSyncGeneratorsFromTaskGraph(taskGraph: TaskGraph, projectGraph: ProjectGraph, nxJson: NxJsonConfiguration): Set<string>;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc