diff --git a/packages/create-app/src/createApp.ts b/packages/create-app/src/createApp.ts index fd70a4c238..182e043565 100644 --- a/packages/create-app/src/createApp.ts +++ b/packages/create-app/src/createApp.ts @@ -38,34 +38,31 @@ export default async (opts: OptionValues): Promise => { /* 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 => { await moveAppTask(tempDir, appDir, answers.name); } - if (gitConfig) { + if (gitConfig?.name && gitConfig?.email) { Task.section('Initializing git repository'); await initGitRepository(appDir); } diff --git a/packages/create-app/src/lib/tasks.test.ts b/packages/create-app/src/lib/tasks.test.ts index b8d3202976..0144f9f575 100644 --- a/packages/create-app/src/lib/tasks.test.ts +++ b/packages/create-app/src/lib/tasks.test.ts @@ -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), ); }); diff --git a/packages/create-app/src/lib/tasks.ts b/packages/create-app/src/lib/tasks.ts index 6b40ecac63..5514b930fc 100644 --- a/packages/create-app/src/lib/tasks.ts +++ b/packages/create-app/src/lib/tasks.ts @@ -254,7 +254,7 @@ export async function readGitConfig(): Promise { 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 { try { await fs.mkdir(tempDir); - process.chdir(tempDir); - const [gitUsername, gitEmail] = await Promise.all([ runCmd('git config user.name'), runCmd('git config user.email'),