Removes defaultBranch answer from inquirer and uses cwd to run commands in directory

Signed-off-by: Leonardo Maier <leonarmaier@gmail.com>
This commit is contained in:
Leonardo Maier
2022-09-28 08:54:25 -03:00
parent b57a21a7fc
commit 02561f0a5b
3 changed files with 43 additions and 35 deletions
+25 -28
View File
@@ -38,34 +38,31 @@ export default async (opts: OptionValues): Promise<void> => {
/* eslint-disable-next-line no-restricted-syntax */
const paths = findPaths(__dirname);
const answers: Answers = await inquirer.prompt(
[
{
type: 'input',
name: 'name',
message: chalk.blue('Enter a name for the app [required]'),
validate: (value: any) => {
if (!value) {
return chalk.red('Please enter a name for the app');
} else if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value)) {
return chalk.red(
'App name must be lowercase and contain only letters, digits, and dashes.',
);
}
return true;
},
when: (a: Answers) => {
const envName = process.env.BACKSTAGE_APP_NAME;
if (envName) {
a.name = envName;
return false;
}
return true;
},
const answers: Answers = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: chalk.blue('Enter a name for the app [required]'),
validate: (value: any) => {
if (!value) {
return chalk.red('Please enter a name for the app');
} else if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(value)) {
return chalk.red(
'App name must be lowercase and contain only letters, digits, and dashes.',
);
}
return true;
},
],
{ defaultBranch: 'master' },
);
when: (a: Answers) => {
const envName = process.env.BACKSTAGE_APP_NAME;
if (envName) {
a.name = envName;
return false;
}
return true;
},
},
]);
const templateDir = paths.resolveOwn('templates/default-app');
const tempDir = resolvePath(os.tmpdir(), answers.name);
@@ -112,7 +109,7 @@ export default async (opts: OptionValues): Promise<void> => {
await moveAppTask(tempDir, appDir, answers.name);
}
if (gitConfig) {
if (gitConfig?.name && gitConfig?.email) {
Task.section('Initializing git repository');
await initGitRepository(appDir);
}
+17 -4
View File
@@ -17,7 +17,8 @@
import fs from 'fs-extra';
import mockFs from 'mock-fs';
import child_process from 'child_process';
import path from 'path';
import path, { resolve as resolvePath } from 'path';
import os from 'os';
import {
Task,
buildAppTask,
@@ -100,6 +101,7 @@ describe('tasks', () => {
const mockExec = child_process.exec as unknown as jest.MockedFunction<
(
command: string,
options: any,
callback: (error: null, stdout: any, stderr: any) => void,
) => void
>;
@@ -287,8 +289,10 @@ describe('tasks', () => {
});
describe('readGitConfig', () => {
const tmpDir = resolvePath(os.tmpdir(), 'git-temp-dir');
it('should return git config if git package is installed and git credentials are set', async () => {
mockExec.mockImplementation((_command, callback) => {
mockExec.mockImplementation((_command, _options, callback) => {
callback(null, { stdout: 'main' }, 'standard error');
});
@@ -304,15 +308,22 @@ describe('tasks', () => {
});
expect(mockExec).toHaveBeenCalledWith(
'git config user.name',
{ cwd: tmpDir },
expect.any(Function),
);
expect(mockExec).toHaveBeenCalledWith(
'git config user.email',
{ cwd: tmpDir },
expect.any(Function),
);
expect(mockExec).toHaveBeenCalledWith(
'git init',
{ cwd: tmpDir },
expect.any(Function),
);
expect(mockExec).toHaveBeenCalledWith('git init', expect.any(Function));
expect(mockExec).toHaveBeenCalledWith(
'git commit --allow-empty -m "Initial commit"',
{ cwd: tmpDir },
expect.any(Function),
);
});
@@ -326,7 +337,7 @@ describe('tasks', () => {
});
it('should return false if git package is installed but git credentials are not set', async () => {
mockExec.mockImplementation((_command, callback) => {
mockExec.mockImplementation((_command, _options, callback) => {
callback(null, { stdout: null }, 'standard error');
});
@@ -337,10 +348,12 @@ describe('tasks', () => {
expect(gitConfig).toBeUndefined();
expect(mockExec).toHaveBeenCalledWith(
'git config user.name',
{ cwd: tmpDir },
expect.any(Function),
);
expect(mockExec).toHaveBeenCalledWith(
'git config user.email',
{ cwd: tmpDir },
expect.any(Function),
);
});
+1 -3
View File
@@ -254,7 +254,7 @@ export async function readGitConfig(): Promise<GitConfig | undefined> {
const tempDir = resolvePath(os.tmpdir(), 'git-temp-dir');
const runCmd = (cmd: string) =>
exec(cmd).catch(error => {
exec(cmd, { cwd: tempDir }).catch(error => {
process.stdout.write(error.stderr);
process.stdout.write(error.stdout);
throw new Error(`Could not execute command ${chalk.cyan(cmd)}`);
@@ -267,8 +267,6 @@ export async function readGitConfig(): Promise<GitConfig | undefined> {
try {
await fs.mkdir(tempDir);
process.chdir(tempDir);
const [gitUsername, gitEmail] = await Promise.all([
runCmd('git config user.name'),
runCmd('git config user.email'),