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(core): add migration to disable adding plugins automatically in existing workspaces #21508

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
"version": "17.3.0-beta.6",
"description": "Updates the nx wrapper.",
"implementation": "./src/migrations/update-17-3-0/update-nxw"
},
"18.0.0-disable-adding-plugins-for-existing-workspaces": {
"cli": "nx",
"version": "18.0.0-beta.2",
"description": "Updates .env to disabled adding plugins when generating projects in an existing Nx workspace",
"implementation": "./src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces",
"x-repair-skip": true
}
}
}
25 changes: 15 additions & 10 deletions packages/nx/src/command-line/repair/repair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ export async function repair(
}
const verbose = process.env.NX_VERBOSE_LOGGING === 'true';
return handleErrors(verbose, async () => {
const nxMigrations = Object.entries(migrationsJson.generators).map(
([name, migration]) => {
return {
package: 'nx',
cli: 'nx',
name,
description: migration.description,
version: migration.version,
} as const;
}
const nxMigrations = Object.entries(migrationsJson.generators).reduce(
(agg, [name, migration]) => {
const skip = migration['x-repair-skip'];
if (!skip) {
agg.push({
package: 'nx',
cli: 'nx',
name,
description: migration.description,
version: migration.version,
} as const);
}
return agg;
},
[]
);

const migrations = [...nxMigrations, ...extraMigrations];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createTree } from '../../generators/testing-utils/create-tree';
import migrate from './disable-crystal-for-existing-workspaces';

describe('disable crystal for existing workspaces', () => {
it("should create a .env if it doesn't exist", () => {
const tree = createTree();
migrate(tree);
expect(tree.read('.env', 'utf-8')).toMatchInlineSnapshot(`
"# Nx 18 enables using plugins to infer targets by default
# This is disabled for existing workspaces to maintain compatibility
# For more info, see: https://nx.dev/concepts/inferred-tasks
NX_ADD_PLUGINS=false"
`);
});

it('should update an existing .env', () => {
const tree = createTree();
tree.write('.env', 'FOO=bar');
migrate(tree);
expect(tree.read('.env', 'utf-8')).toMatchInlineSnapshot(`
"FOO=bar

# Nx 18 enables using plugins to infer targets by default
# This is disabled for existing workspaces to maintain compatibility
# For more info, see: https://nx.dev/concepts/inferred-tasks
NX_ADD_PLUGINS=false"
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Tree } from '../../generators/tree';
import { logger } from '../../utils/logger';
import ignore from 'ignore';

export default function migrate(tree: Tree) {
const ig = ignore();
try {
ig.add(tree.read('.gitignore', 'utf-8'));
if (ig.ignores('.env')) {
logger.warn(
'NX The NX_ADD_PLUGINS=false environment variable was added to your .env file for backwards compatibility. However, your .env is ignored by git. Other contributors should add this key to their .env file or ensure that the environment variable is true when generating code with Nx.'
FrozenPandaz marked this conversation as resolved.
Show resolved Hide resolved
);
}
} catch {}

if (!tree.exists('.env')) {
tree.write('.env', '');
}

const dotenv = tree.read('.env', 'utf-8');
const newDotenvContents = [
'# Nx 18 enables using plugins to infer targets by default',
'# This is disabled for existing workspaces to maintain compatibility',
'# For more info, see: https://nx.dev/concepts/inferred-tasks',
'NX_ADD_PLUGINS=false',
];

if (dotenv.length) {
newDotenvContents.unshift(dotenv, '');
}

tree.write('.env', newDotenvContents.join('\n'));
}