Skip to content

Commit

Permalink
feat: support specifying a config file using --config or -c cli flags
Browse files Browse the repository at this point in the history
depends on eik-lib/common#314
fixes: #553
  • Loading branch information
digitalsadhu committed Aug 5, 2024
1 parent 5ddd162 commit a78b9de
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 189 deletions.
15 changes: 2 additions & 13 deletions commands/alias.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ora from 'ora';
import semver from 'semver';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import { Alias as AliasFormatter } from '../formatters/index.js';

export const command = 'alias [name] [version] [alias]';
Expand All @@ -11,8 +11,7 @@ export const aliases = ['a'];
export const describe = `Create or update a semver major alias for a package, NPM package or import map as identified by its name and version. A package with the given name and version must already exist on the Eik server. The alias should be the semver major part of the package version. Eg. for a package of version 5.4.3, you should use 5 as the alias. The alias type (npm, map, package) is detected from eik.json in the current working directory.`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs
.positional('name', {
Expand All @@ -38,21 +37,11 @@ export const builder = (yargs) => {
describe: 'Specify location of Eik asset server.',
default: defaults.server,
},
cwd: {
alias: 'c',
describe: 'Alter the current working directory.',
default: defaults.cwd,
},
type: {
describe:
'Alter the alias type. Default is detected from eik.json. Valid values are `package`, `npm`, or `map` Eg. --type npm',
default: defaults.type,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
token: {
describe:
'Provide a jwt token to be used to authenticate with the Eik server.',
Expand Down
10 changes: 0 additions & 10 deletions commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ const builder = (yargs) => {
describe: `Specify asset server field in "eik.json". This the URL to an Eik asset server Eg. --server https://assets.myeikserver.com`,
default: '',
},
cwd: {
alias: 'c',
describe: `Alter the current working directory. Defaults to the directory where the command is being run. This affects where the generated "eik.json" file will be saved. Eg. --cwd /path/to/save/to`,
default: process.cwd(),
},
version: {
alias: 'v',
describe: `Specify the semver version field in "eik.json". Eg. --version 1.0.0`,
Expand All @@ -39,11 +34,6 @@ const builder = (yargs) => {
Eg. --name my-great-app`,
default: '',
},
debug: {
describe: 'Logs additional messages during command run',
default: false,
type: 'boolean',
},
});
};

Expand Down
23 changes: 4 additions & 19 deletions commands/integrity.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { join } from 'path';
import ora from 'ora';
import { helpers } from '@eik/common';
import Integrity from '../classes/integrity.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import json from '../utils/json/index.js';

const { configStore } = helpers;

export const command = 'integrity [name] [version]';

export const aliases = ['int'];

export const describe = `Retrieve file integrity information for package name and version defined in eik.json, then populate integrity.json file with this information`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs.options({
server: {
alias: 's',
describe: 'Specify location of asset server.',
default: defaults.server,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
cwd: {
alias: 'c',
describe: 'Alter current working directory.',
default: defaults.cwd,
},
});

yargs.example(`eik integrity`);
Expand All @@ -42,10 +28,9 @@ export const builder = (yargs) => {
export const handler = async (argv) => {
const spinner = ora({ stream: process.stdout }).start('working...');
let integrity = false;
const { debug, cwd } = argv;
const { debug, cwd, config } = argv;
const l = logger(spinner, debug);
const config = configStore.findInDirectory(cwd);
const { name, server, version, type, out } = config;
const { name, version, server, out, type } = getDefaults(config || cwd);

try {
integrity = await new Integrity({
Expand Down
10 changes: 2 additions & 8 deletions commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import os from 'os';
import readline from 'readline';
import ora from 'ora';
import Login from '../classes/login.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import json from '../utils/json/index.js';

const homedir = os.homedir();
Expand All @@ -20,8 +20,7 @@ export const builder = (yargs) => {
);
yargs.example('eik login --server https://assets.myserver.com --debug');

const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs.options({
server: {
Expand All @@ -36,11 +35,6 @@ export const builder = (yargs) => {
type: 'string',
default: '',
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
});
};

Expand Down
15 changes: 2 additions & 13 deletions commands/map-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ora from 'ora';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import { Alias as AliasFormatter } from '../formatters/index.js';

export const command = 'map-alias <name> <version> <alias>';
Expand All @@ -12,8 +12,7 @@ export const aliases = ['ma'];
export const describe = `DEPRECATED: This command has been replaced by the alias command and will be removed in a future version. Create a semver major alias for an import map as identified by its name and version. An import map with the given name and version must already exist on asset server. Alias should be the semver major part of the import map version. Eg. For an import map of version 5.4.3, you should use 5 as the alias`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs
.positional('name', {
Expand All @@ -35,16 +34,6 @@ export const builder = (yargs) => {
describe: 'Specify location of asset server.',
default: defaults.server,
},
cwd: {
alias: 'c',
describe: 'Alter current working directory.',
default: defaults.cwd,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
token: {
describe:
'Provide a jwt token to be used to authenticate with the Eik server.',
Expand Down
15 changes: 2 additions & 13 deletions commands/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path';
import ora from 'ora';
import PublishMap from '../classes/publish/map.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import { Artifact } from '../formatters/index.js';

export const command = 'map <name> <version> <file>';
Expand All @@ -11,8 +11,7 @@ export const aliases = ['m'];
export const describe = `Upload an import map file to the server under a given name and version. A name/version combination must be unique and a version must be semver compliant. Subsquent published versions must increase. Eg. 1.0.0 1.0.1, 1.1.0, 2.0.0 etc.`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs
.positional('name', {
Expand All @@ -36,16 +35,6 @@ export const builder = (yargs) => {
describe: 'Specify location of asset server.',
default: defaults.server,
},
cwd: {
alias: 'c',
describe: 'Alter current working directory.',
default: defaults.cwd,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
token: {
describe:
'Provide a jwt token to be used to authenticate with the Eik server.',
Expand Down
16 changes: 3 additions & 13 deletions commands/meta.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ora from 'ora';
import Meta from '../classes/meta.js';
import { Artifact } from '../formatters/index.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';

export const command = 'meta <name>';

Expand All @@ -10,8 +10,7 @@ export const aliases = ['show'];
export const describe = `Retrieve meta information by package, map or npm name.If a given name exists in several types (package and map for example), results will be returned and displayed from all matching types`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs.positional('name', {
describe:
Expand All @@ -25,16 +24,6 @@ export const builder = (yargs) => {
describe: 'Specify location of asset server.',
default: defaults.server,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
cwd: {
alias: 'c',
describe: 'Alter current working directory.',
default: defaults.cwd,
},
});

yargs.example(`eik meta lit-html`);
Expand All @@ -49,6 +38,7 @@ export const handler = async (argv) => {
const l = logger(spinner, debug);

try {
// @ts-expect-error
meta = await new Meta({ logger: l, ...argv }).run();
spinner.text = '';
spinner.stopAndPersist();
Expand Down
15 changes: 2 additions & 13 deletions commands/npm-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ora from 'ora';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import { Alias as AliasFormatter } from '../formatters/index.js';

export const command = 'npm-alias <name> <version> <alias>';
Expand All @@ -12,8 +12,7 @@ export const aliases = ['na', 'dep-alias', 'dependency-alias'];
export const describe = `DEPRECATED: This command has been replaced by the alias command and will be removed in a future version. Create a semver major alias for an NPM package as identified by its name and version. An NPM package with the given name and version must already exist on the asset server. Alias should be the semver major part of the NPM package version. Eg. For an NPM package of version 5.4.3, you should use 5 as the alias`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs
.positional('name', {
Expand All @@ -36,16 +35,6 @@ export const builder = (yargs) => {
describe: 'Specify location of asset server.',
default: defaults.server,
},
cwd: {
alias: 'c',
describe: 'Alter current working directory.',
default: defaults.cwd,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
token: {
describe:
'Provide a jwt token to be used to authenticate with the Eik server.',
Expand Down
15 changes: 2 additions & 13 deletions commands/package-alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ora from 'ora';
import semver from 'semver';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';
import { Alias as AliasFormatter } from '../formatters/index.js';

export const command = 'package-alias [name] [version] [alias]';
Expand All @@ -13,8 +13,7 @@ export const aliases = ['pkg-alias', 'pa'];
export const describe = `DEPRECATED: This command has been replaced by the alias command and will be removed in a future version. Create a semver major alias for a package as identified by its name and version. A package with the given name and version must already exist on asset server. Alias should be the semver major part of the package version. Eg. For a package of version 5.4.3, you should use 5 as the alias`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs
.positional('name', {
Expand All @@ -41,16 +40,6 @@ export const builder = (yargs) => {
describe: 'Specify location of Eik asset server.',
default: defaults.server,
},
cwd: {
alias: 'c',
describe: 'Alter the current working directory.',
default: defaults.cwd,
},
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
token: {
describe:
'Provide a jwt token to be used to authenticate with the Eik server.',
Expand Down
14 changes: 3 additions & 11 deletions commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ora from 'ora';
import Ping from '../classes/ping.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { logger, getDefaults } from '../utils/index.js';

export const command = 'ping [server]';

Expand All @@ -9,22 +9,13 @@ export const aliases = [];
export const describe = `Ping an Eik server to check that it is responding.`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);
const defaults = getDefaults(yargs.argv.config || yargs.argv.cwd);

yargs.positional('server', {
describe: 'Specify location of Eik server to ping.',
default: defaults.server,
});

yargs.options({
debug: {
describe: 'Logs additional messages',
default: false,
type: 'boolean',
},
});

yargs.example(`eik ping`);
yargs.example(`eik ping http://assets.myeikserver.com`);
yargs.example(`eik ping http://assets.myeikserver.com --debug`);
Expand All @@ -37,6 +28,7 @@ export const handler = async (argv) => {
try {
await new Ping({ logger: logger(spinner, debug), server }).run();
} catch (err) {
// @ts-expect-error
logger.warn(err.message);
}

Expand Down
Loading

0 comments on commit a78b9de

Please sign in to comment.