-
Notifications
You must be signed in to change notification settings - Fork 438
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
[FEATURE] add npm_script Task #211
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5571092
[FEATURE] add npm_script Task
Kanti 1532347
[FEATURE] add phpspec and doc for npm_script task
Kanti 3c88790
[TASK] add npm_script to tasks.md
Kanti 88e8941
[TASK] add working_directory and change default value for triggered_by
Kanti 9914a5e
[TASK] use absoulute path as WorkingDirectory
Kanti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# NPM script | ||
|
||
The NPM script task will run your configured npm script. | ||
It lives under the `npm_script` namespace and has following configurable parameters: | ||
|
||
```yaml | ||
# grumphp.yml | ||
parameters: | ||
tasks: | ||
npm_script: | ||
script: ~ | ||
triggered_by: [js, jsx, coffee, ts, less, sass, scss] | ||
working_directory: "./" | ||
``` | ||
|
||
**script** | ||
|
||
*Default: null* | ||
|
||
This option specifies which NPM script you want to run. | ||
This option is set to null by default. | ||
This means that grumphp will stop immediately. | ||
Note that this script should be used to verify things. | ||
It is also possible to alter code during commit, | ||
but this is surely NOT recommended! | ||
|
||
|
||
**triggered_by** | ||
|
||
*Default: [js, jsx, coffee, ts, less, sass, scss]* | ||
|
||
This option will specify which file extensions will trigger the NPM script. | ||
By default NPM script will be triggered by altering any file. | ||
You can overwrite this option to whatever file you want to use! | ||
|
||
|
||
**working_directory** | ||
|
||
*Default: "./"* | ||
|
||
This option specifies in which directory the NPM script should be run. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace spec\GrumPHP\Task; | ||
|
||
use GrumPHP\Collection\FilesCollection; | ||
use GrumPHP\Collection\ProcessArgumentsCollection; | ||
use GrumPHP\Configuration\GrumPHP; | ||
use GrumPHP\Formatter\ProcessFormatterInterface; | ||
use GrumPHP\Process\ProcessBuilder; | ||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use GrumPHP\Task\NpmScript; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* @mixin NpmScript | ||
*/ | ||
class NpmScriptSpec extends ObjectBehavior | ||
{ | ||
function let(GrumPHP $grumPHP, ProcessBuilder $processBuilder, ProcessFormatterInterface $formatter) | ||
{ | ||
$grumPHP->getTaskConfiguration('npm_script')->willReturn(array('script' => 'test', 'working_directory' => './')); | ||
$this->beConstructedWith($grumPHP, $processBuilder, $formatter); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('GrumPHP\Task\NpmScript'); | ||
} | ||
|
||
function it_should_have_a_name() | ||
{ | ||
$this->getName()->shouldBe('npm_script'); | ||
} | ||
|
||
function it_should_have_configurable_options() | ||
{ | ||
$options = $this->getConfigurableOptions(); | ||
$options->shouldBeAnInstanceOf('Symfony\Component\OptionsResolver\OptionsResolver'); | ||
$options->getDefinedOptions()->shouldContain('script'); | ||
$options->getDefinedOptions()->shouldContain('triggered_by'); | ||
$options->getDefinedOptions()->shouldContain('working_directory'); | ||
} | ||
|
||
function it_should_run_in_git_pre_commit_context(GitPreCommitContext $context) | ||
{ | ||
$this->canRunInContext($context)->shouldReturn(true); | ||
} | ||
|
||
function it_should_run_in_run_context(RunContext $context) | ||
{ | ||
$this->canRunInContext($context)->shouldReturn(true); | ||
} | ||
|
||
function it_does_not_do_anything_if_there_are_no_files(ProcessBuilder $processBuilder, ContextInterface $context) | ||
{ | ||
$processBuilder->createArgumentsForCommand('npm')->shouldNotBeCalled(); | ||
$processBuilder->buildProcess()->shouldNotBeCalled(); | ||
$context->getFiles()->willReturn(new FilesCollection()); | ||
|
||
$result = $this->run($context); | ||
$result->shouldBeAnInstanceOf('GrumPHP\Runner\TaskResultInterface'); | ||
$result->getResultCode()->shouldBe(TaskResult::SKIPPED); | ||
} | ||
|
||
function it_runs_the_suite(ProcessBuilder $processBuilder, Process $process, ContextInterface $context) | ||
{ | ||
$arguments = new ProcessArgumentsCollection(); | ||
$processBuilder->createArgumentsForCommand('npm')->willReturn($arguments); | ||
$processBuilder->buildProcess($arguments)->willReturn($process); | ||
|
||
$process->run()->shouldBeCalled(); | ||
$process->setWorkingDirectory('./')->shouldBeCalled(); | ||
$process->isSuccessful()->willReturn(true); | ||
|
||
$context->getFiles()->willReturn(new FilesCollection(array( | ||
new SplFileInfo('test.js', '.', 'test.js') | ||
))); | ||
|
||
$result = $this->run($context); | ||
$result->shouldBeAnInstanceOf('GrumPHP\Runner\TaskResultInterface'); | ||
$result->isPassed()->shouldBe(true); | ||
} | ||
|
||
function it_throws_exception_if_the_process_fails( | ||
ProcessBuilder $processBuilder, | ||
Process $process, | ||
ContextInterface $context | ||
) { | ||
$arguments = new ProcessArgumentsCollection(); | ||
$processBuilder->createArgumentsForCommand('npm')->willReturn($arguments); | ||
$processBuilder->buildProcess($arguments)->willReturn($process); | ||
|
||
$process->run()->shouldBeCalled(); | ||
$process->setWorkingDirectory('./')->shouldBeCalled(); | ||
$process->isSuccessful()->willReturn(false); | ||
|
||
$context->getFiles()->willReturn(new FilesCollection(array( | ||
new SplFileInfo('test.js', '.', 'test.js') | ||
))); | ||
|
||
$result = $this->run($context); | ||
$result->shouldBeAnInstanceOf('GrumPHP\Runner\TaskResultInterface'); | ||
$result->isPassed()->shouldBe(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace GrumPHP\Task; | ||
|
||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* NpmScript task | ||
*/ | ||
class NpmScript extends AbstractExternalTask | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'npm_script'; | ||
} | ||
|
||
/** | ||
* @return OptionsResolver | ||
*/ | ||
public function getConfigurableOptions() | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults(array( | ||
'script' => null, | ||
'triggered_by' => array('js', 'jsx', 'coffee', 'ts', 'less', 'sass', 'scss'), | ||
'working_directory' => './', | ||
)); | ||
|
||
$resolver->addAllowedTypes('script', array('string')); | ||
$resolver->addAllowedTypes('triggered_by', array('array')); | ||
$resolver->addAllowedTypes('working_directory', array('string')); | ||
|
||
return $resolver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function canRunInContext(ContextInterface $context) | ||
{ | ||
return ($context instanceof GitPreCommitContext || $context instanceof RunContext); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(ContextInterface $context) | ||
{ | ||
$config = $this->getConfiguration(); | ||
$files = $context->getFiles()->extensions($config['triggered_by']); | ||
if (0 === count($files)) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand('npm'); | ||
$arguments->addRequiredArgument('%s', $config['script']); | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->setWorkingDirectory($config['working_directory']); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return TaskResult::createFailed($this, $context, $this->formatter->format($process)); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this use the correct directory if you add relative directories? Maybe we should make it relative to the getcwd() ?