Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 13, 2019
1 parent 0061615 commit 0945b56
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 219 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
13 changes: 3 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
language: node_js
node_js:
- '5'
- '4'
- '0.12'
- '0.10'
before_install:
- sudo add-apt-repository "deb mirror://mirrors.ubuntu.com/mirrors.txt trusty main restricted universe multiverse" -y
- sudo apt-get update -qq
- sudo apt-get install -t trusty php5-cli
script:
- npm test
- '12'
- '10'
- '8'
3 changes: 2 additions & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable quote-props */
'use strict';
module.exports = function (grunt) {

module.exports = grunt => {
grunt.initConfig({
simplemocha: {
test: {
Expand Down
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89 changes: 44 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
{
"name": "grunt-php",
"version": "1.5.1",
"description": "Start a PHP-server",
"keywords": [
"gruntplugin",
"php",
"server",
"webserver",
"web-server",
"http"
],
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"repository": "sindresorhus/grunt-php",
"scripts": {
"test": "xo && grunt"
},
"engines": {
"node": ">=0.10.0"
},
"license": "MIT",
"files": [
"tasks"
],
"dependencies": {
"bin-version-check": "^2.0.0",
"get-port": "^2.0.0",
"object-assign": "^4.0.1",
"opn": "^4.0.1"
},
"devDependencies": {
"got": "^5.6.0",
"grunt": "^1.0.1",
"grunt-browser-sync": "^2.0.0",
"grunt-cli": "^1.2.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-simple-mocha": "^0.4.0",
"xo": "*"
},
"peerDependencies": {
"grunt": ">=0.4.0"
}
"name": "grunt-php",
"version": "1.5.1",
"description": "Start a PHP-server",
"license": "MIT",
"repository": "sindresorhus/grunt-php",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && grunt"
},
"files": [
"tasks"
],
"keywords": [
"gruntplugin",
"php",
"server",
"webserver",
"web-server",
"http"
],
"dependencies": {
"bin-version-check": "^4.0.0",
"get-port": "^5.0.0",
"open": "^6.3.0"
},
"devDependencies": {
"got": "^9.6.0",
"grunt": "^1.0.4",
"grunt-browser-sync": "^2.0.0",
"grunt-cli": "^1.2.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-simple-mocha": "^0.4.0",
"xo": "^0.24.0"
},
"peerDependencies": {
"grunt": ">=1"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install --save-dev grunt-php
## Usage

```js
require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks
require('load-grunt-tasks')(grunt);

grunt.initConfig({
php: {
Expand Down Expand Up @@ -145,7 +145,7 @@ The port on which you want to access the webserver. Task will fail if the port i
### hostname

Type: `string`<br>
Default: `'127.0.0.1'` *(usually same as `localhost`)*
Default: `'127.0.0.1'` *(Usually the same as `localhost`)*

The hostname the webserver will use.

Expand All @@ -169,7 +169,7 @@ This option can also be enabled ad-hoc by running the task like `grunt php:targe

### open

Type: `boolean` or `string`
Type: `boolean | string`
Default: `false`

Open a browser when task is triggered.
Expand Down
154 changes: 78 additions & 76 deletions tasks/php.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
/* eslint-disable guard-for-in */
'use strict';
var path = require('path');
var spawn = require('child_process').spawn;
var http = require('http');
var open = require('opn');
var binVersionCheck = require('bin-version-check');
var getPort = require('get-port');
var objectAssign = require('object-assign');

module.exports = function (grunt) {
var checkServerTries = 0;

function checkServer(hostname, port, path, cb) {
setTimeout(function () {
const {promisify} = require('util');
const path = require('path');
const {spawn} = require('child_process');
const http = require('http');
const open = require('open');
const binVersionCheck = require('bin-version-check');
const getPort = require('get-port');

module.exports = grunt => {
let checkServerTries = 0;

// TODO: Refactor this into using a `Promise` constructor
const checkServer = (hostname, port, path, callback) => {
setTimeout(() => {
http.request({
method: 'HEAD',
hostname: hostname,
port: port,
path: path
}, function (res) {
var statusCodeType = Number(res.statusCode.toString()[0]);
if ([2, 3, 4].indexOf(statusCodeType) !== -1) {
cb();
hostname,
port,
path
}, response => {
const statusCodeType = Number(response.statusCode.toString()[0]);
if ([2, 3, 4].includes(statusCodeType)) {
callback();
return;
} else if (statusCodeType === 5) {
grunt.fail.warn(
'Server docroot returned 500-level response. Please check ' +
'your configuration for possible errors.'
);
}

cb();
if (statusCodeType === 5) {
grunt.fail.warn('Server docroot returned 500-level response. Please check your configuration for possible errors.');
callback();
return;
}

checkServer(hostname, port, path, cb);
}).on('error', function () {
// back off after 1s
checkServer(hostname, port, path, callback);
}).on('error', () => {
// Back off after 1 second
if (++checkServerTries > 20) {
cb();
callback();
return;
}

grunt.verbose.writeln('PHP server not started. Retrying...');
checkServer(hostname, port, path, cb);
checkServer(hostname, port, path, callback);
}).end();
}, 50);
}
};

const checkServerPromise = promisify(checkServer);

grunt.registerMultiTask('php', 'Start a PHP-server', function () {
var cb = this.async();
var options = this.options({
grunt.registerMultiTask('php', 'Start a PHP-server', async function () {
const done = this.async();

const options = this.options({
port: 8000,
hostname: '127.0.0.1',
base: '.',
Expand All @@ -61,69 +63,69 @@ module.exports = function (grunt) {
directives: {}
});

getPort().then(function (port) {
try {
const port = await getPort();

if (options.port === '?') {
options.port = port;
}

var host = options.hostname + ':' + options.port;
var args = ['-S', host];
const host = `${options.hostname}:${options.port}`;

const spawnArguments = ['-S', host];

if (options.base) {
args.push('-t', path.resolve(options.base));
spawnArguments.push('-t', path.resolve(options.base));
}

if (options.ini) {
args.push('-c', options.ini);
spawnArguments.push('-c', options.ini);
}

if (options.directives) {
for (var key in options.directives) {
args.push('-d', key + '=' + options.directives[key]);
for (const key in options.directives) {
spawnArguments.push('-d', `${key}=${options.directives[key]}`);
}
}

if (options.router) {
args.push(options.router);
spawnArguments.push(options.router);
}

binVersionCheck(options.bin, '>=5.4', function (err) {
if (err) {
grunt.warn(err);
cb();
return;
await binVersionCheck(options.bin, '>=5.4');

const cp = spawn(options.bin, spawnArguments, {
stdio: options.silent ? 'ignore' : 'inherit',
env: {
...process.env,
...options.env
}
});

var cp = spawn(options.bin, args, {
stdio: options.silent ? 'ignore' : 'inherit',
env: objectAssign({}, process.env, options.env)
});
// Quit PHP when Grunt is done
process.on('exit', () => {
cp.kill();
});

// quit PHP when grunt is done
process.on('exit', function () {
cp.kill();
});
let pathname = '/';
if (typeof options.open === 'string') {
pathname = (options.open.indexOf('/') === 0 ? '' : '/') + options.open;
}

var path = '/';
if (typeof options.open === 'string') {
path = (options.open.indexOf('/') === 0 ? '' : '/') + options.open;
}
// Check when the server is ready. Tried doing it by listening
// to the child process `data` event, but it's not triggered...
await checkServerPromise(options.hostname, options.port, pathname);

// check when the server is ready. tried doing it by listening
// to the child process `data` event, but it's not triggered...
checkServer(options.hostname, options.port, path, function () {
if (!this.flags.keepalive && !options.keepalive) {
cb();
}

if (options.open) {
open('http://' + host + path);
}
}.bind(this));
}.bind(this));
}.bind(this)).catch(function (err) {
grunt.warn(err);
cb();
});
if (!this.flags.keepalive && !options.keepalive) {
done();
}

if (options.open) {
await open(`http://${host}${pathname}`);
}
} catch (error) {
grunt.warn(error);
done();
}
});
};
Loading

0 comments on commit 0945b56

Please sign in to comment.