Skip to content

Commit

Permalink
Merge branch 'master' into color-and-no-color-flags
Browse files Browse the repository at this point in the history
  • Loading branch information
wswoodruff authored Jan 29, 2021
2 parents 18f6a09 + dae9481 commit 7d8080b
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 66 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ language: node_js
node_js:
- "8"
- "10"
- "12"
- "14"
- "node"

os:
- "linux"
- "windows"

before_script: |
if [[ "${TRAVIS_OS_NAME}" == "windows" ]]; then
git config core.symlinks true
git reset --hard
fi
after_script: "npm run coveralls"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017-2019, Devin Ivy and collaborators
Copyright (c) 2017-2020, Devin Ivy and collaborators

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
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ Any hapi plugin can create commands that are runnable with `hpal run`! Commands
- `server` - the initialized hapi server.
- `args` - an array of all the command's CLI arguments. For example, running `hpal run my-plugin --custom-flag value` will result in `args` being `['--custom-flag', 'value']`.
- `root` - an absolute path to the project's root directory.
- `ctx` - a context object containing some hpal internals that may be useful during testing. Also contains an error class `DisplayError` than can be used to indicate a "safe" failure to hpal. Throwing a `DisplayError` will output the error's `message` and exit the process with code `1`, but not display a stack trace as would happen with an unexpected error.
- `description` - a string description of the command displayed by `hpal run --list`.
- `ctx` - a context object containing some hpal internals that may be useful during testing, plus some public helpers. The following are public:
- `colors` - an object with functions for basic formatting of CLI output with colors and styles: `colors.green(str)`, `colors.yellow(str)`, `colors.red(str)`, `colors.grey(str)`, and `colors.bold(str)`. When the CLI does not support color, these functions take no effect.
- `DisplayError` - a class that can be used to indicate a "safe" failure to hpal. Throwing a `DisplayError` will output the error's `message` and exit the process with code `1`, but not display a stack trace as would happen with an unexpected error.
- `description` - a string description of the command displayed by `hpal run --list`. May alternatively be a function with signature `function (ctx)` that receives `ctx` as described above and returns a string description.

For example, here is a plugin that creates a command to display the server's route table,
```js
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ internals.pkgOwners = {
hodgepodge: 'hapipal',
underdog: 'hapipal',
hecks: 'hapipal',
lalalambda: 'hapipal'
lalalambda: 'hapipal',
avocat: 'hapipal'
};

internals.anchorize = (str) => {
Expand Down
34 changes: 27 additions & 7 deletions lib/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const Os = require('os');
const Path = require('path');
const ChildProcess = require('child_process');
const StableStringify = require('json-stable-stringify');
const Helpers = require('../helpers');
const DisplayError = require('../display-error');

Expand Down Expand Up @@ -87,21 +86,37 @@ module.exports = async (cwd, dir, ctx) => {

const cmp = (x, y) => {

const xScore = order.indexOf(x.key) === -1 ? order.length : order.indexOf(x.key);
const yScore = order.indexOf(y.key) === -1 ? order.length : order.indexOf(y.key);

const xScore = order.indexOf(x) === -1 ? order.length : order.indexOf(x);
const yScore = order.indexOf(y) === -1 ? order.length : order.indexOf(y);
return xScore - yScore;
};

const finalPkgStringified = StableStringify(finalPkg, { cmp, space: 2 });
finalPkg = internals.sortObject(finalPkg, cmp);
finalPkg.dependencies = internals.sortObject(finalPkg.dependencies);
finalPkg.devDependencies = internals.sortObject(finalPkg.devDependencies);

const finalPkgStringified = JSON.stringify(finalPkg, null, 2);
const projectName = finalPkg.name || Path.basename(dir);

await Promise.all([
Helpers.writeFile(Path.join(dir, 'package.json'), `${finalPkgStringified}${Os.EOL}`),
Helpers.writeFile(Path.join(dir, 'README.md'), `# ${pkg.name}${Os.EOL}`)
Helpers.writeFile(Path.join(dir, 'README.md'), `# ${projectName}${Os.EOL}`)
]);

await Helpers.exec('git add package.json README.md', { cwd: dir });
};

// Bic'd and adapted from domenic/sorted-object (WTFPL)
internals.sortObject = (input, fn) => {

return Object.keys(input).sort(fn)
.reduce((output, key) => {

output[key] = input[key];
return output;
}, {});
};

internals.ensureGitAndNpm = async ({ colors }) => {

try {
Expand All @@ -119,7 +134,12 @@ internals.npmInit = (cwd, ctx) => {

return new Promise((resolve, reject) => {

const subproc = ChildProcess.spawn('npm', ['init'], { cwd });
// There is no way to cover this on a single platform
/* $lab:coverage:off$ */
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
/* $lab:coverage:on$ */

const subproc = ChildProcess.spawn(npmCmd, ['init'], { cwd });

subproc.stdout.pipe(ctx.options.out, { end: false });
ctx.options.in.pipe(subproc.stdin);
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = async (cwd, list, cmd, args, ctx) => {
const commands = Object.keys(plugin.commands || {}).map((cmdName) => {

const name = pluginName.replace(/^hpal-/, '') + ((cmdName === 'default') ? '' : internals.kebabize(`:${cmdName}`));
const description = internals.normalizeCommand(plugin.commands[cmdName]).description;
const command = internals.normalizeCommand(plugin.commands[cmdName]);
const description = typeof command.description === 'function' ? command.description(ctx) : command.description;

return { name, description };
});
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hpal",
"version": "2.2.1",
"version": "2.6.0",
"description": "hapi pal CLI",
"main": "lib/index.js",
"directories": {
Expand All @@ -10,8 +10,8 @@
"hpal": "./bin/hpal"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -I 'FinalizationRegistry,WeakRef' -L test/*.js",
"coveralls": "lab -r lcov test/*.js | coveralls"
"test": "lab -a @hapi/code -t 100 -L -I \"FinalizationRegistry,WeakRef\" test/index.js test/print.js",
"coveralls": "lab -r lcov -I \"FinalizationRegistry,WeakRef\" test/index.js test/print.js | coveralls"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,7 +40,6 @@
"@hapi/wreck": "15.x.x",
"bin-v8-flags-filter": ">=1.2.0 <2",
"glob": "7.x.x",
"json-stable-stringify": "1.x.x",
"marked": "0.7.x",
"marked-terminal": "3.x.x",
"mkdirp": "0.5.x",
Expand All @@ -52,6 +51,7 @@
"@hapi/boom": "7.x.x",
"@hapi/code": "5.x.x",
"@hapi/hapi": "18.x.x",
"@hapi/hapi-20": "npm:@hapi/hapi@20",
"@hapi/lab": "19.x.x",
"coveralls": "3.x.x",
"haute-couture": "3.x.x",
Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-bad-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-bad-require/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

// Here's the bad require
require('does-not-exist');
Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-command-bad-error/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-command-display-error/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-command-no-func/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-default-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-echo-exec-argv/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-kebab-cased-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-kebab-cased-prefixed-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
10 changes: 9 additions & 1 deletion test/closet/run-list-commands/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand All @@ -14,6 +14,10 @@ exports.deployment = async () => {
described: {
description: 'This is what I do',
command: () => null
},
describedFn: {
description: (ctx) => JSON.stringify({ ctx: Object.keys(ctx).sort() }),
command: () => null
}
});
};
Expand All @@ -33,6 +37,10 @@ exports.deployment = async () => {
described: {
description: 'This is what I do',
command: () => null
},
describedFn: {
description: (ctx) => JSON.stringify({ ctx: Object.keys(ctx).sort() }),
command: () => null
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-list-no-commands/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = () => Hapi.server();
2 changes: 1 addition & 1 deletion test/closet/run-no-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-prefixed-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
2 changes: 1 addition & 1 deletion test/closet/run-silent-command/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('@hapi/hapi');
const { Hapi } = require('../../run-util');

exports.deployment = async () => {

Expand Down
Loading

0 comments on commit 7d8080b

Please sign in to comment.