Emily Xiong aa7c5dc3ea
chore(core): add unit tests for not connected to nx cloud (#26885)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-07-12 13:25:55 -04:00

98 lines
2.4 KiB
TypeScript

import {
Tree,
names,
generateFiles,
getPackageManagerCommand,
formatFiles,
detectPackageManager,
readNxJson,
} from '@nx/devkit';
import { join } from 'path';
import { getNxCloudUrl, isNxCloudUsed } from 'nx/src/utils/nx-cloud-utils';
import { deduceDefaultBase } from 'nx/src/utils/default-base';
function getCiCommands(ci: Schema['ci']): Command[] {
switch (ci) {
case 'circleci': {
return [
{
comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`,
},
{
command: `./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`,
},
];
}
default: {
return [
{
comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`,
},
{ command: `./nx affected -t test build` },
];
}
}
}
export type Command = { command: string } | { comment: string } | string;
export interface Schema {
name: string;
ci: 'github' | 'circleci';
packageManager?: null;
commands?: Command[];
}
export async function ciWorkflowGenerator(tree: Tree, schema: Schema) {
const ci = schema.ci;
const options = getTemplateData(tree, schema);
generateFiles(tree, join(__dirname, 'files', ci), '', options);
await formatFiles(tree);
}
interface Substitutes {
mainBranch: string;
workflowName: string;
workflowFileName: string;
packageManager: string;
packageManagerPrefix: string;
commands: Command[];
nxCloudHost: string;
connectedToCloud: boolean;
}
function getTemplateData(tree: Tree, options: Schema): Substitutes {
const { name: workflowName, fileName: workflowFileName } = names(
options.name
);
const packageManager = detectPackageManager();
const { exec: packageManagerPrefix } =
getPackageManagerCommand(packageManager);
let nxCloudHost: string = 'nx.app';
try {
const nxCloudUrl = getNxCloudUrl(readNxJson(tree));
nxCloudHost = new URL(nxCloudUrl).host;
} catch {}
const mainBranch = deduceDefaultBase();
const commands = options.commands ?? getCiCommands(options.ci);
const connectedToCloud = isNxCloudUsed(readNxJson(tree));
return {
workflowName,
workflowFileName,
packageManager,
packageManagerPrefix,
commands,
mainBranch,
nxCloudHost,
connectedToCloud,
};
}
export default ciWorkflowGenerator;