Skip to content

Commit

Permalink
Migrate to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Dec 12, 2021
1 parent 2c9e06d commit 56597a9
Show file tree
Hide file tree
Showing 24 changed files with 339 additions and 334 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"no-duplicate-case": 2,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Allowed args after and between options
- Replaced `chalk` with `ansi-colors`
- Migrated to ESM

## 3.0.0-beta.1

Expand Down
10 changes: 5 additions & 5 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const getCommandHelp = require('./help');
const parseArgv = require('./parse-argv');
const Params = require('./params');
const Option = require('./option');
import getCommandHelp from './help.js';
import parseArgv from './parse-argv.js';
import Params from './params.js';
import Option from './option.js';

const noop = () => {}; // nothing todo
const self = value => value;
Expand All @@ -20,7 +20,7 @@ const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res,
return res;
}, { initial: {}, setters: {} });

module.exports = class Command {
export default class Command {
constructor(usage = '') {
const [name, params] = usage.trim().split(/(\s+.*)$/);

Expand Down
4 changes: 2 additions & 2 deletions lib/help.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const colors = require('ansi-colors');
import colors from 'ansi-colors';

const MAX_LINE_WIDTH = process.stdout.columns || 200;
const MIN_OFFSET = 20;
Expand Down Expand Up @@ -115,7 +115,7 @@ function optionsHelp(command) {
* @return {String}
* @api private
*/
module.exports = function getCommandHelp(command, commandPath) {
export default function getCommandHelp(command, commandPath) {
commandPath = Array.isArray(commandPath) && commandPath.length
? commandPath.concat(command.name).join(' ')
: command.name;
Expand Down
28 changes: 15 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
const path = require('path');
const Params = require('./params');
const Option = require('./option');
const Command = require('./command');
const Error = require('./parse-argv-error');
const getCommandHelp = require('./help');
import { basename, extname } from 'path';
import Params from './params.js';
import Option from './option.js';
import Command from './command.js';
import Error from './parse-argv-error.js';
import getCommandHelp from './help.js';

function nameFromProcessArgv() {
return path.basename(process.argv[1], path.extname(process.argv[1]));
return basename(process.argv[1], extname(process.argv[1]));
}

module.exports = {
function command(name, params, config) {
name = name || nameFromProcessArgv() || 'command';

return new Command(name, params, config);
}

export {
Error,
Params,
Command,
Option,

getCommandHelp,
command: function(name, params, config) {
name = name || nameFromProcessArgv() || 'command';

return new Command(name, params, config);
}
command
};
5 changes: 3 additions & 2 deletions lib/option.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Params = require('./params');
import Params from './params.js';

const camelcase = name => name.replace(/-(.)/g, (m, ch) => ch.toUpperCase());
const ensureFunction = (fn, fallback) => typeof fn === 'function' ? fn : fallback;
const self = value => value;

module.exports = class Option {
export default class Option {
static normalizeOptions(opt1, opt2) {
const raw = typeof opt1 === 'function'
? { normalize: opt1, default: opt2 }
Expand Down
2 changes: 1 addition & 1 deletion lib/params.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = class Params {
export default class Params {
constructor(params = '', context) {
// params = ..<required> ..[optional]
// <foo> - required
Expand Down
2 changes: 1 addition & 1 deletion lib/parse-argv-error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = class CliError extends Error {
export default class CliError extends Error {
constructor(...args) {
super(...args);
this.name = 'CliError';
Expand Down
4 changes: 2 additions & 2 deletions lib/parse-argv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CliError = require('./parse-argv-error');
import CliError from './parse-argv-error.js';

function findVariants(command, entry) {
return [
Expand Down Expand Up @@ -47,7 +47,7 @@ function consumeOptionParams(option, rawOptions, argv, index, suggestPoint) {
return index + tokens.length - 1;
}

module.exports = function parseArgv(command, argv, context, suggestMode) {
export default function parseArgv(command, argv, context, suggestMode) {
const suggestPoint = suggestMode ? argv.length - 1 : -1;
const rawOptions = [];
const result = {
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
"argument",
"completion"
],
"type": "module",
"main": "lib/index.js",
"exports": {
".": {
"import": "./lib/index.js",
"require": "./cjs/index.cjs"
},
"./package.json": "./package.json"
},
"files": [
"lib"
],
Expand Down
19 changes: 9 additions & 10 deletions test/command-action.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const assert = require('assert');
const cli = require('../lib');
import { equal, notDeepEqual, deepEqual } from 'assert';
import * as clap from 'clap';

describe('action()', function() {
it('should have an expected input', function() {
describe('action()', () => {
it('should have an expected input', () => {
const calls = [];
const command = cli
.command('test [foo]')
const command = clap.command('test [foo]')
.option('--bar', 'bar option')
.action(function(...args) {
.action((...args) => {
calls.push({
this: this,
arguments: args
Expand All @@ -16,9 +15,9 @@ describe('action()', function() {

command.run(['abc', '--', 'rest', 'args']);

assert.equal(calls.length, 1);
assert.notDeepEqual(calls[0].this, command);
assert.deepEqual(calls[0].arguments, [{
equal(calls.length, 1);
notDeepEqual(calls[0].this, command);
deepEqual(calls[0].arguments, [{
commandPath: ['test'],
options: { bar: false },
args: ['abc'],
Expand Down
70 changes: 35 additions & 35 deletions test/command-args-and-options.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const assert = require('assert');
const cli = require('../lib');
import { deepStrictEqual, throws, equal } from 'assert';
import * as clap from 'clap';

describe('command run', function() {
describe('command run', () => {
describe('args and options', () => {
let command;

beforeEach(function() {
command = cli.command('test [foo]')
beforeEach(() => {
command = clap.command('test [foo]')
.option('--foo', 'Foo')
.option('--bar <number>', 'Bar', Number);
});

it('no arguments', function() {
it('no arguments', () => {
const actual = command.run([]);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -25,10 +25,10 @@ describe('command run', function() {
});
});

it('args', function() {
it('args', () => {
const actual = command.run(['qux']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -39,10 +39,10 @@ describe('command run', function() {
});
});

it('options', function() {
it('options', () => {
const actual = command.run(['--foo', '--bar', '123']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -54,10 +54,10 @@ describe('command run', function() {
});
});

it('literal args', function() {
it('literal args', () => {
const actual = command.run(['--', '--one', '--two', '123']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -68,10 +68,10 @@ describe('command run', function() {
});
});

it('args & options', function() {
it('args & options', () => {
const actual = command.run(['qux', '--foo', '--bar', '123']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -83,10 +83,10 @@ describe('command run', function() {
});
});

it('args & options before', function() {
it('args & options before', () => {
const actual = command.run(['--foo', '--bar', '123', 'qux']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -98,10 +98,10 @@ describe('command run', function() {
});
});

it('args & literal args', function() {
it('args & literal args', () => {
const actual = command.run(['qux', '--', '--one', '--two', '123']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -112,10 +112,10 @@ describe('command run', function() {
});
});

it('options & literal args', function() {
it('options & literal args', () => {
const actual = command.run(['--foo', '--bar', '123', '--', '--one', '--two', '123']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -127,10 +127,10 @@ describe('command run', function() {
});
});

it('args & options & literal args', function() {
it('args & options & literal args', () => {
const actual = command.run(['qux', '--foo', '--bar', '123', '--', '--one', '--two', '123']);

assert.deepStrictEqual(actual, {
deepStrictEqual(actual, {
commandPath: ['test'],
options: {
__proto__: null,
Expand All @@ -145,12 +145,12 @@ describe('command run', function() {

describe('multi arg option', () => {
it('x', () => {
const command = cli.command()
const command = clap.command()
.option('--option <arg1> [arg2]', 'description', function(value, oldValue) {
return (oldValue || []).concat(value);
});

assert.deepStrictEqual(
deepStrictEqual(
command.run(['--option','foo', 'bar', '--option', 'baz']).options,
{
__proto__: null,
Expand All @@ -162,36 +162,36 @@ describe('command run', function() {

describe('required argument', () => {
let action;
const command = cli
const command = clap
.command('test <arg1>')
.action(() => action = '1')
.command('nested <arg2>')
.action(() => action = '2')
.end();

beforeEach(function() {
beforeEach(() => {
action = '';
});

it('should throw exception if no first argument', function() {
assert.throws(
it('should throw exception if no first argument', () => {
throws(
() => command.run([]),
/Missed required argument\(s\) for command "test"/
);
});
it('should throw exception if no second argument', function() {
assert.throws(
it('should throw exception if no second argument', () => {
throws(
() => command.run(['one', 'nested']),
/Missed required argument\(s\) for command "nested"/
);
});
it('should treat first argument as value', function() {
it('should treat first argument as value', () => {
command.run(['nested']);
assert.equal(action, '1');
equal(action, '1');
});
it('should run nested action', function() {
it('should run nested action', () => {
command.run(['one', 'nested', 'two']);
assert.equal(action, '2');
equal(action, '2');
});
});
});
Loading

0 comments on commit 56597a9

Please sign in to comment.