<!-- 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 #
100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
import { execSync, spawn, SpawnOptions } from 'child_process';
|
|
import { deduceDefaultBase } from './default-base';
|
|
import { output } from '../output';
|
|
|
|
export function checkGitVersion(): string | null | undefined {
|
|
try {
|
|
let gitVersionOutput = execSync('git --version', { windowsHide: true })
|
|
.toString()
|
|
.trim();
|
|
return gitVersionOutput.match(/[0-9]+\.[0-9]+\.+[0-9]+/)?.[0];
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function initializeGitRepo(
|
|
directory: string,
|
|
options: {
|
|
defaultBase: string;
|
|
commit?: { message: string; name: string; email: string };
|
|
connectUrl?: string | null;
|
|
}
|
|
) {
|
|
const execute = (args: ReadonlyArray<string>, ignoreErrorStream = false) => {
|
|
const outputStream = 'ignore';
|
|
const errorStream = ignoreErrorStream ? 'ignore' : process.stderr;
|
|
const spawnOptions: SpawnOptions = {
|
|
stdio: [process.stdin, outputStream, errorStream],
|
|
shell: true,
|
|
cwd: directory,
|
|
env: {
|
|
...process.env,
|
|
...(options.commit?.name
|
|
? {
|
|
GIT_AUTHOR_NAME: options.commit.name,
|
|
GIT_COMMITTER_NAME: options.commit.name,
|
|
}
|
|
: {}),
|
|
...(options.commit?.email
|
|
? {
|
|
GIT_AUTHOR_EMAIL: options.commit.email,
|
|
GIT_COMMITTER_EMAIL: options.commit.email,
|
|
}
|
|
: {}),
|
|
},
|
|
windowsHide: true,
|
|
};
|
|
return new Promise<void>((resolve, reject) => {
|
|
spawn('git', args, spawnOptions).on('close', (code) => {
|
|
if (code === 0) {
|
|
resolve();
|
|
} else {
|
|
reject(code);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
const gitVersion = checkGitVersion();
|
|
if (!gitVersion) {
|
|
return;
|
|
}
|
|
const insideRepo = await execute(
|
|
['rev-parse', '--is-inside-work-tree'],
|
|
true
|
|
).then(
|
|
() => true,
|
|
() => false
|
|
);
|
|
if (insideRepo) {
|
|
output.log({
|
|
title:
|
|
'Directory is already under version control. Skipping initialization of git.',
|
|
});
|
|
return;
|
|
}
|
|
const defaultBase = options.defaultBase || deduceDefaultBase();
|
|
const [gitMajor, gitMinor] = gitVersion.split('.');
|
|
|
|
if (+gitMajor > 2 || (+gitMajor === 2 && +gitMinor >= 28)) {
|
|
await execute(['init', '-b', defaultBase]);
|
|
} else {
|
|
await execute(['init']);
|
|
await execute(['checkout', '-b', defaultBase]); // Git < 2.28 doesn't support -b on git init.
|
|
}
|
|
await execute(['add', '.']);
|
|
if (options.commit) {
|
|
let message = `${options.commit.message}` || 'initial commit';
|
|
if (options.connectUrl) {
|
|
message = `${message}
|
|
|
|
To connect your workspace to Nx Cloud, push your repository
|
|
to your git hosting provider and go to the following URL:
|
|
|
|
${options.connectUrl}
|
|
`;
|
|
}
|
|
await execute(['commit', `-m "${message}"`]);
|
|
}
|
|
}
|