feat(core): obey git's init.defaultBranch when initializing nx.json (#7192)
This commit is contained in:
parent
2bc103d599
commit
ec633e3dfb
@ -106,6 +106,7 @@
|
|||||||
"@types/react": "17.0.3",
|
"@types/react": "17.0.3",
|
||||||
"@types/react-dom": "17.0.3",
|
"@types/react-dom": "17.0.3",
|
||||||
"@types/react-router-dom": "5.1.7",
|
"@types/react-router-dom": "5.1.7",
|
||||||
|
"@types/semver": "^7.3.8",
|
||||||
"@types/tmp": "^0.2.0",
|
"@types/tmp": "^0.2.0",
|
||||||
"@types/yargs": "^15.0.5",
|
"@types/yargs": "^15.0.5",
|
||||||
"@typescript-eslint/eslint-plugin": "~4.31.1",
|
"@typescript-eslint/eslint-plugin": "~4.31.1",
|
||||||
|
|||||||
@ -24,6 +24,7 @@ import { readFileSync } from 'fs';
|
|||||||
import { basename } from 'path';
|
import { basename } from 'path';
|
||||||
import { serializeJson } from '../../utilities/fileutils';
|
import { serializeJson } from '../../utilities/fileutils';
|
||||||
import { resolveUserExistingPrettierConfig } from '../../utilities/prettier';
|
import { resolveUserExistingPrettierConfig } from '../../utilities/prettier';
|
||||||
|
import { deduceDefaultBase } from '../../utilities/default-base';
|
||||||
import {
|
import {
|
||||||
angularCliVersion,
|
angularCliVersion,
|
||||||
nxVersion,
|
nxVersion,
|
||||||
@ -426,7 +427,7 @@ async function createAdditionalFiles(host: Tree, options: Schema) {
|
|||||||
serializeJson({
|
serializeJson({
|
||||||
npmScope: options.npmScope,
|
npmScope: options.npmScope,
|
||||||
affected: {
|
affected: {
|
||||||
defaultBase: `${options.defaultBase}` || 'master',
|
defaultBase: `${options.defaultBase}` || deduceDefaultBase(),
|
||||||
},
|
},
|
||||||
implicitDependencies: {
|
implicitDependencies: {
|
||||||
'angular.json': '*',
|
'angular.json': '*',
|
||||||
|
|||||||
@ -12,8 +12,7 @@
|
|||||||
},
|
},
|
||||||
"defaultBase": {
|
"defaultBase": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Default base branch for affected.",
|
"description": "Default base branch for affected."
|
||||||
"default": "master"
|
|
||||||
},
|
},
|
||||||
"skipInstall": {
|
"skipInstall": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
|
|||||||
@ -16,9 +16,15 @@ import { join } from 'path';
|
|||||||
import * as yargsParser from 'yargs-parser';
|
import * as yargsParser from 'yargs-parser';
|
||||||
import { spawn, SpawnOptions } from 'child_process';
|
import { spawn, SpawnOptions } from 'child_process';
|
||||||
|
|
||||||
|
import { gte } from 'semver';
|
||||||
|
|
||||||
import { workspaceGenerator } from '../workspace/workspace';
|
import { workspaceGenerator } from '../workspace/workspace';
|
||||||
import { nxVersion } from '../../utils/versions';
|
import { nxVersion } from '../../utils/versions';
|
||||||
import { Preset } from '../utils/presets';
|
import { Preset } from '../utils/presets';
|
||||||
|
import {
|
||||||
|
checkGitVersion,
|
||||||
|
deduceDefaultBase,
|
||||||
|
} from '../../utilities/default-base';
|
||||||
|
|
||||||
export interface Schema {
|
export interface Schema {
|
||||||
cli: 'nx' | 'angular';
|
cli: 'nx' | 'angular';
|
||||||
@ -110,11 +116,8 @@ async function initializeGitRepo(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const hasCommand = await execute(['--version']).then(
|
const gitVersion = checkGitVersion();
|
||||||
() => true,
|
if (!gitVersion) {
|
||||||
() => false
|
|
||||||
);
|
|
||||||
if (!hasCommand) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const insideRepo = await execute(
|
const insideRepo = await execute(
|
||||||
@ -130,7 +133,13 @@ async function initializeGitRepo(
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const defaultBase = options.defaultBase || deduceDefaultBase();
|
||||||
|
if (gte(gitVersion, '2.28.0')) {
|
||||||
|
await execute(['init', '-b', defaultBase]);
|
||||||
|
} else {
|
||||||
await execute(['init']);
|
await execute(['init']);
|
||||||
|
await execute(['checkout', '-b', defaultBase]);
|
||||||
|
}
|
||||||
await execute(['add', '.']);
|
await execute(['add', '.']);
|
||||||
if (options.commit) {
|
if (options.commit) {
|
||||||
const message = options.commit.message || 'initial commit';
|
const message = options.commit.message || 'initial commit';
|
||||||
|
|||||||
@ -31,8 +31,7 @@
|
|||||||
},
|
},
|
||||||
"defaultBase": {
|
"defaultBase": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Default base branch for affected.",
|
"description": "Default base branch for affected."
|
||||||
"default": "master"
|
|
||||||
},
|
},
|
||||||
"skipInstall": {
|
"skipInstall": {
|
||||||
"description": "Skip installing dependency packages.",
|
"description": "Skip installing dependency packages.",
|
||||||
|
|||||||
@ -54,8 +54,7 @@
|
|||||||
},
|
},
|
||||||
"defaultBase": {
|
"defaultBase": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Default base branch for affected.",
|
"description": "Default base branch for affected."
|
||||||
"default": "master"
|
|
||||||
},
|
},
|
||||||
"skipInstall": {
|
"skipInstall": {
|
||||||
"description": "Skip installing dependency packages.",
|
"description": "Skip installing dependency packages.",
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import { readFileSync } from 'fs';
|
|||||||
import { join, join as pathJoin } from 'path';
|
import { join, join as pathJoin } from 'path';
|
||||||
import { reformattedWorkspaceJsonOrNull } from '@nrwl/tao/src/shared/workspace';
|
import { reformattedWorkspaceJsonOrNull } from '@nrwl/tao/src/shared/workspace';
|
||||||
import { Preset } from '../utils/presets';
|
import { Preset } from '../utils/presets';
|
||||||
|
import { deduceDefaultBase } from '../../utilities/default-base';
|
||||||
|
|
||||||
export const DEFAULT_NRWL_PRETTIER_CONFIG = {
|
export const DEFAULT_NRWL_PRETTIER_CONFIG = {
|
||||||
singleQuote: true,
|
singleQuote: true,
|
||||||
@ -130,6 +131,7 @@ export async function workspaceGenerator(host: Tree, options: Schema) {
|
|||||||
if (!options.name) {
|
if (!options.name) {
|
||||||
throw new Error(`Invalid options, "name" is required.`);
|
throw new Error(`Invalid options, "name" is required.`);
|
||||||
}
|
}
|
||||||
|
options = normalizeOptions(options);
|
||||||
createFiles(host, options);
|
createFiles(host, options);
|
||||||
createPrettierrc(host, options);
|
createPrettierrc(host, options);
|
||||||
if (options.cli === 'angular') {
|
if (options.cli === 'angular') {
|
||||||
@ -155,3 +157,11 @@ function addPropertyWithStableKeys(obj: any, key: string, value: string) {
|
|||||||
obj[k] = copy[k];
|
obj[k] = copy[k];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeOptions(options: Schema) {
|
||||||
|
let defaultBase = options.defaultBase || deduceDefaultBase();
|
||||||
|
return {
|
||||||
|
...options,
|
||||||
|
defaultBase,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
57
packages/workspace/src/utilities/default-base.spec.ts
Normal file
57
packages/workspace/src/utilities/default-base.spec.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import * as cp from 'child_process';
|
||||||
|
import { checkGitVersion, deduceDefaultBase } from './default-base';
|
||||||
|
|
||||||
|
describe('deduceDefaultBase', () => {
|
||||||
|
const execSyncSpy = jest.spyOn(cp, 'execSync');
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work when not set', () => {
|
||||||
|
execSyncSpy.mockReturnValue(Buffer.from(''));
|
||||||
|
const result = deduceDefaultBase();
|
||||||
|
expect(result).toEqual('master');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work when set', () => {
|
||||||
|
execSyncSpy.mockReturnValue(Buffer.from('main'));
|
||||||
|
const result = deduceDefaultBase();
|
||||||
|
expect(result).toEqual('main');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work with extra line terminators', () => {
|
||||||
|
execSyncSpy.mockReturnValue(
|
||||||
|
Buffer.from(`main
|
||||||
|
`)
|
||||||
|
);
|
||||||
|
const result = deduceDefaultBase();
|
||||||
|
expect(result).toEqual('main');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('checkGitVersion', () => {
|
||||||
|
const execSyncSpy = jest.spyOn(cp, 'execSync');
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work with text before semver', () => {
|
||||||
|
execSyncSpy.mockReturnValue(Buffer.from(`git version 2.33.0`));
|
||||||
|
const result = checkGitVersion();
|
||||||
|
expect(result).toEqual('2.33.0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work with 4 digit versions', () => {
|
||||||
|
execSyncSpy.mockReturnValue(Buffer.from(`git version 2.33.0.5`));
|
||||||
|
const result = checkGitVersion();
|
||||||
|
expect(result).toEqual('2.33.0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should work with msysgit versions', () => {
|
||||||
|
execSyncSpy.mockReturnValue(Buffer.from(`git version 1.8.3.msysgit.0`));
|
||||||
|
const result = checkGitVersion();
|
||||||
|
expect(result).toEqual('1.8.3');
|
||||||
|
});
|
||||||
|
});
|
||||||
23
packages/workspace/src/utilities/default-base.ts
Normal file
23
packages/workspace/src/utilities/default-base.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { execSync } from 'child_process';
|
||||||
|
|
||||||
|
// TODO (v13): Update to main
|
||||||
|
export function deduceDefaultBase(): string {
|
||||||
|
const nxDefaultBase = 'master';
|
||||||
|
try {
|
||||||
|
return (
|
||||||
|
execSync('git config --get init.defaultBranch').toString().trim() ||
|
||||||
|
nxDefaultBase
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return nxDefaultBase;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkGitVersion(): string | null {
|
||||||
|
try {
|
||||||
|
let gitVersionOutput = execSync('git --version').toString().trim();
|
||||||
|
return gitVersionOutput.match(/([0-9]+\.[0-9]+\.+[0-9]+)/)[0];
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6291,6 +6291,11 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/selenium-webdriver/-/selenium-webdriver-3.0.17.tgz#50bea0c3c2acc31c959c5b1e747798b3b3d06d4b"
|
resolved "https://registry.yarnpkg.com/@types/selenium-webdriver/-/selenium-webdriver-3.0.17.tgz#50bea0c3c2acc31c959c5b1e747798b3b3d06d4b"
|
||||||
integrity sha512-tGomyEuzSC1H28y2zlW6XPCaDaXFaD6soTdb4GNdmte2qfHtrKqhy0ZFs4r/1hpazCfEZqeTSRLvSasmEx89uw==
|
integrity sha512-tGomyEuzSC1H28y2zlW6XPCaDaXFaD6soTdb4GNdmte2qfHtrKqhy0ZFs4r/1hpazCfEZqeTSRLvSasmEx89uw==
|
||||||
|
|
||||||
|
"@types/semver@^7.3.8":
|
||||||
|
version "7.3.8"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.8.tgz#508a27995498d7586dcecd77c25e289bfaf90c59"
|
||||||
|
integrity sha512-D/2EJvAlCEtYFEYmmlGwbGXuK886HzyCc3nZX/tkFTQdEU8jZDAgiv08P162yB17y4ZXZoq7yFAnW4GDBb9Now==
|
||||||
|
|
||||||
"@types/serve-static@*":
|
"@types/serve-static@*":
|
||||||
version "1.13.9"
|
version "1.13.9"
|
||||||
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.9.tgz#aacf28a85a05ee29a11fb7c3ead935ac56f33e4e"
|
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.9.tgz#aacf28a85a05ee29a11fb7c3ead935ac56f33e4e"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user