Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing to setup the nextjs project with command #1084

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions src/Command/App/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Acquia\DrupalEnvironmentDetector\AcquiaDrupalEnvironmentDetector;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;

Expand All @@ -22,8 +21,7 @@ class NewCommand extends CommandBase {
* {inheritdoc}.
*/
protected function configure(): void {
$this->setDescription('Create a new Drupal project')
->addOption('distribution', NULL, InputOption::VALUE_REQUIRED, 'The Composer package name of the Drupal distribution to download')
danepowell marked this conversation as resolved.
Show resolved Hide resolved
$this->setDescription('Create a new Drupal or Next.js project')
->addArgument('directory', InputArgument::OPTIONAL, 'The destination directory')
->setAliases(['new']);
}
Expand All @@ -36,13 +34,14 @@ protected function configure(): void {
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->output->writeln('Acquia recommends most customers use <options=bold>acquia/drupal-recommended-project</>, which includes useful utilities such as Acquia Connector.');
$this->output->writeln('<options=bold>acquia/drupal-minimal-project</> is the most minimal application that will run on the Cloud Platform.');
$this->output->writeln('Acquia recommends most customers use <options=bold>acquia/drupal-recommended-project</> to setup a Drupal project, which includes useful utilities such as Acquia Connector.');
danepowell marked this conversation as resolved.
Show resolved Hide resolved
$this->output->writeln('<options=bold>acquia/next-acms</> is a starter template for building a headless site powered by Acquia CMS and Next.js.');
$distros = [
'acquia/drupal-recommended-project',
'acquia/drupal-minimal-project',
'acquia_drupal_recommended' => 'acquia/drupal-recommended-project',
'acquia_next_acms' => 'acquia/next-acms',
];
$project = $this->io->choice('Choose a starting project', $distros, $distros[0]);
$project = $this->io->choice('Choose a starting project', array_values($distros), $distros['acquia_drupal_recommended']);
$project = array_search($project, $distros);

if ($input->hasArgument('directory') && $input->getArgument('directory')) {
$dir = Path::canonicalize($input->getArgument('directory'));
Expand All @@ -52,17 +51,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$dir = '/home/ide/project';
}
else {
$dir = Path::makeAbsolute('drupal', getcwd());
$dir = Path::makeAbsolute($project, getcwd());
danepowell marked this conversation as resolved.
Show resolved Hide resolved
}

$output->writeln('<info>Creating project. This may take a few minutes.</info>');
$this->localMachineHelper->checkRequiredBinariesExist(['composer']);
$this->createProject($project, $dir);

if ($project === 'acquia_next_acms') {
$success_message = "<info>New Next JS project created in $dir. 🎉</info>";
$this->localMachineHelper->checkRequiredBinariesExist(['node']);
$this->createNextJsProject($dir);
}
else {
$success_message = "<info>New 💧 Drupal project created in $dir. 🎉</info>";
$this->localMachineHelper->checkRequiredBinariesExist(['composer']);
$this->createDrupalProject($distros[$project], $dir);
}

$this->initializeGitRepository($dir);

$output->writeln('');
$output->writeln("<info>New 💧 Drupal project created in $dir. 🎉");
$output->writeln($success_message);

return 0;
}
Expand All @@ -76,13 +84,31 @@ protected function commandRequiresAuthentication(InputInterface $input): bool {
return FALSE;
}

/**
* @param string $dir
*
* @throws \Exception
*/
private function createNextJsProject(string $dir): void {
$process = $this->localMachineHelper->execute([
'npx',
'create-next-app',
'-e',
'https://github.com/acquia/next-acms/tree/main/starters/basic-starter',
$dir,
]);
if (!$process->isSuccessful()) {
throw new AcquiaCliException("Unable to create new next-acms project.");
}
}

/**
* @param $project
* @param string $dir
*
* @throws \Exception
*/
private function createProject($project, string $dir): void {
private function createDrupalProject($project, string $dir): void {
$process = $this->localMachineHelper->execute([
'composer',
'create-project',
Expand Down
92 changes: 83 additions & 9 deletions tests/phpunit/src/Commands/NewCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,42 @@ protected function createCommand(): Command {
return $this->injectCommand(NewCommand::class);
}

public function provideTestNewCommand(): array {
public function provideTestNewDrupalCommand(): array {
return [
['acquia/drupal-recommended-project'],
['acquia/drupal-minimal-project'],
['acquia/drupal-minimal-project', 'test-dir'],
[['acquia_drupal_recommended' => 'acquia/drupal-recommended-project']],
[['acquia_drupal_recommended' => 'acquia/drupal-recommended-project', 'test-dir']],
];
}

public function provideTestNewNextJsAppCommand(): array {
return [
[['acquia_next_acms' => 'acquia/next-acms']],
[['acquia_next_acms' => 'acquia/next-acms'], 'test-dir'],
];
}

/**
* Tests the 'new' command.
* Tests the 'new' command for Drupal project.
*
* @dataProvider provideTestNewCommand
* @dataProvider provideTestNewDrupalCommand
*
* @param string $project
* @param array $package
* @param string $directory
*
* @throws \Exception
*/
public function testNewCommand(string $project, string $directory = 'drupal'): void {
public function testNewDrupalCommand(array $package, string $directory = 'drupal'): void {
$this->newProjectDir = Path::makeAbsolute($directory, $this->projectFixtureDir);
$project_key = array_keys($package)[0];
$project = $package[$project_key];

$process = $this->prophet->prophesize(Process::class);
$process->isSuccessful()->willReturn(TRUE);
$process->getExitCode()->willReturn(0);

$local_machine_helper = $this->mockLocalMachineHelper();

$this->mockGetFilesystem($local_machine_helper);
$mock_file_system = $this->mockGetFilesystem($local_machine_helper);
$local_machine_helper->checkRequiredBinariesExist(["composer"])->shouldBeCalled();
$this->mockExecuteComposerCreate($this->newProjectDir, $local_machine_helper, $process, $project);
$local_machine_helper->checkRequiredBinariesExist(["git"])->shouldBeCalled();
Expand All @@ -71,10 +79,58 @@ public function testNewCommand(string $project, string $directory = 'drupal'): v
], $inputs);
$this->prophet->checkPredictions();
$output = $this->getDisplay();
$this->assertStringContainsString('Acquia recommends most customers use acquia/drupal-recommended-project to setup a Drupal project', $output);
$this->assertStringContainsString('Choose a starting project', $output);
$this->assertStringContainsString($project, $output);
$this->assertTrue($mock_file_system->isAbsolutePath($this->newProjectDir), 'Directory path is not absolute');
$this->assertStringContainsString('New 💧 Drupal project created in ' . $this->newProjectDir, $output);
}

/**
* Tests the 'new' command for Next.js App.
*
* @dataProvider provideTestNewNextJsAppCommand
*
* @param array $package
* @param string $directory
*
* @throws \Exception
*/
public function testNewNextJSAppCommand(array $package, string $directory = 'nextjs'): void {
$this->newProjectDir = Path::makeAbsolute($directory, $this->projectFixtureDir);
$project_key = array_keys($package)[0];
$project = $package[$project_key];

$process = $this->prophet->prophesize(Process::class);
$process->isSuccessful()->willReturn(TRUE);
$process->getExitCode()->willReturn(0);

$local_machine_helper = $this->mockLocalMachineHelper();

$mock_file_system = $this->mockGetFilesystem($local_machine_helper);

$local_machine_helper->checkRequiredBinariesExist(["node"])->shouldBeCalled();
$this->mockExecuteNpxCreate($this->newProjectDir, $local_machine_helper, $process);
$local_machine_helper->checkRequiredBinariesExist(["git"])->shouldBeCalled();
$this->mockExecuteGitInit($local_machine_helper, $this->newProjectDir, $process);
$this->mockExecuteGitAdd($local_machine_helper, $this->newProjectDir, $process);
$this->mockExecuteGitCommit($local_machine_helper, $this->newProjectDir, $process);

$this->command->localMachineHelper = $local_machine_helper->reveal();
$inputs = [
// Choose a starting project
$project,
];
$this->executeCommand([
'directory' => $directory,
], $inputs);
$this->prophet->checkPredictions();
$output = $this->getDisplay();
$this->assertStringContainsString('acquia/next-acms is a starter template for building a headless site', $output);
$this->assertStringContainsString('Choose a starting project', $output);
$this->assertStringContainsString($project, $output);
$this->assertTrue($mock_file_system->isAbsolutePath($this->newProjectDir), 'Directory path is not absolute');
$this->assertStringContainsString('New Next JS project created in ' . $this->newProjectDir, $output);
}

/**
Expand Down Expand Up @@ -104,6 +160,24 @@ protected function mockExecuteComposerCreate(
->shouldBeCalled();
}

protected function mockExecuteNpxCreate(
string $project_dir,
ObjectProphecy $local_machine_helper,
ObjectProphecy $process,
): void {
$command = [
'npx',
'create-next-app',
'-e',
'https://github.com/acquia/next-acms/tree/main/starters/basic-starter',
$project_dir,
];
$local_machine_helper
->execute($command)
->willReturn($process->reveal())
->shouldBeCalled();
}

/**
* @param \Prophecy\Prophecy\ObjectProphecy $local_machine_helper
* @param string $project_dir
Expand Down