Skip to content

Commit

Permalink
feat: add alias command as an eventual replacement for the 3 existing…
Browse files Browse the repository at this point in the history
… alias commands

This is a non breaking change but does add deprecation messages for pkg-alias, map-alias and npm-alias, all of which
are replaced by this new alias command.

The alias command detects type from eik.json which was not possible in earlier versions of the cli which was the reason for the existence of 3 separate commands instead of 1.
If the command is run in a directory that does not contain an eik.json file, type can be provided via the command line argument --type. If neither are present, an error is thrown.
  • Loading branch information
digitalsadhu committed Aug 2, 2024
1 parent 5814b82 commit ff4a27c
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 50 deletions.
111 changes: 111 additions & 0 deletions commands/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import ora from 'ora';
import semver from 'semver';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
import { Alias as AliasFormatter } from '../formatters/index.js';

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

export const aliases = ['a'];

export const describe = `Create 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 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. Alias type is detected from an eik.json file in the current working directory.`;

export const builder = (yargs) => {
const cwd = getCWD();
const defaults = getDefaults(cwd);

yargs
.positional('name', {
describe: 'Name matching existing name for a package on Eik server',
type: 'string',
default: defaults.name,
})
.positional('version', {
describe:
'Version matching existing version for a package on Eik server',
type: 'string',
default: defaults.version,
})
.positional('alias', {
describe:
'Alias for a semver version. Must be the semver major component of version. Eg. 1.0.0 should be given as 1',
type: 'string',
default: defaults.version ? semver.major(defaults.version) : null,
});

yargs.options({
server: {
alias: 's',
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.',
default: '',
alias: 't',
},
});

yargs.default('token', defaults.token, defaults.token ? '######' : '');

yargs.example(`eik alias my-app 1.0.0 1`);
yargs.example(`eik alias my-app 1.7.3 1`);
yargs.example(`eik alias my-app 6.3.1 6`);
yargs.example(
`eik alias my-app 6.3.1 6 --server https://assets.myeikserver.com`,
);
yargs.example(`eik alias my-app 4.2.2 4 --debug`);
yargs.example(`eik alias my-app 4.2.2 4 --type package`);
};

export const handler = async (argv) => {
const spinner = ora({ stream: process.stdout }).start('working...');
let success = false;
const { debug, server, type } = argv;
const log = logger(spinner, debug);
let af;

try {
const data = await new Alias({
type,
logger: log,
...argv,
}).run();

// TODO: get rid of this rediculous formatter class idea that past me put here to irk present and future me.
// Smells like DRY silliness
af = new AliasFormatter(data);

const createdOrUpdated = data.update ? 'Updated' : 'Created';
log.info(
`${createdOrUpdated} alias for "${type}" "${data.name}". ("${data.version}" => "v${data.alias}")`,
);
success = true;
} catch (err) {
log.warn(err.message);
}

spinner.text = '';
spinner.stopAndPersist();
if (success) {
af?.format(server);
} else {
process.exit(1);
}
};
2 changes: 2 additions & 0 deletions commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as packageAlias from './package-alias.js';
import * as ping from './ping.js';
import * as publish from './publish.js';
import * as version from './version.js';
import * as alias from './alias.js';

export const commands = [
init,
Expand All @@ -22,4 +23,5 @@ export const commands = [
ping,
publish,
version,
alias,
];
12 changes: 3 additions & 9 deletions commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const command = 'init';

const aliases = ['i'];

const describe = `Creates a new default "eik.json" file and saves it to the current working directory
Override default "eik.json" fields using command line flags --server, --name, --major, --js and --css`;
const describe = `Creates a new default "eik.json" file and saves it to the current working directory. Override default "eik.json" fields using command line flags --server, --name, --major, --js and --css`;

const builder = (yargs) => {
yargs.example('eik init');
Expand All @@ -21,17 +20,12 @@ const builder = (yargs) => {
yargs.options({
server: {
alias: 's',
describe: `Specify asset server field in "eik.json".
This the URL to an Eik asset server
Eg. --server https://assets.myeikserver.com`,
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`,
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: {
Expand Down
15 changes: 3 additions & 12 deletions commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export const command = 'login';

export const aliases = [];

export const describe = `Authenticate against an Eik server and save the returned token to an .eikrc file in the users home directory.
You can specify key and server values to authenticate against using the --key and --server flags which will then bypass login prompts
It is possible to be authenticated against multiple asset servers simultaneously. Simply call "eik login" multiple times.`;
export const describe = `Authenticate against an Eik server and save the returned token to an .eikrc file in the users home directory. You can specify key and server values to authenticate against using the --key and --server flags which will then bypass login prompts. It is possible to be authenticated against multiple asset servers simultaneously. Simply call "eik login" multiple times.`;

export const builder = (yargs) => {
yargs.example('eik login --server https://assets.myserver.com');
Expand All @@ -28,20 +26,13 @@ export const builder = (yargs) => {
yargs.options({
server: {
alias: 's',
describe: `Eik server address
Specify location of the Eik asset server to authenticate against.
If an eik.json file is present in the current working directory, the files server value will be used as default.
If no eik.json file is present in the current working directory and this flag is not specified, a prompt will be presented to ask for the server address to be input
Eg. --server https://assets.myeikserver.com`,
describe: `Eik server address. Specify location of the Eik asset server to authenticate against. If an eik.json file is present in the current working directory, the files server value will be used as default. If no eik.json file is present in the current working directory and this flag is not specified, a prompt will be presented to ask for the server address to be input. Eg. --server https://assets.myeikserver.com`,
type: 'string',
default: defaults.server,
},
key: {
alias: 'k',
describe: `Login access key.
This is a passkey for a given user account and needs to be configured on the server.
If this flag is not specifed, a prompt will be used to ask for the key to be input.
Eg. --key ########`,
describe: `Login access key. This is a passkey for a given user account and needs to be configured on the server. If this flag is not specifed, a prompt will be used to ask for the key to be input. Eg. --key ########`,
type: 'string',
default: '',
},
Expand Down
10 changes: 6 additions & 4 deletions commands/map-alias.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @deprecated in favor of `alias` command

import ora from 'ora';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
Expand All @@ -7,10 +9,7 @@ export const command = 'map-alias <name> <version> <alias>';

export const aliases = ['ma'];

export const describe = `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 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();
Expand Down Expand Up @@ -100,3 +99,6 @@ export const handler = async (argv) => {
process.exit(1);
}
};

export const deprecated =
'"map-alias" will be removed in a future version. Please use "alias" instead';
4 changes: 1 addition & 3 deletions commands/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export const command = 'map <name> <version> <file>';

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 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();
Expand Down
3 changes: 1 addition & 2 deletions commands/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export const command = 'meta <name>';

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 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();
Expand Down
10 changes: 6 additions & 4 deletions commands/npm-alias.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @deprecated in favor of `alias` command

import ora from 'ora';
import Alias from '../classes/alias.js';
import { logger, getDefaults, getCWD } from '../utils/index.js';
Expand All @@ -7,10 +9,7 @@ export const command = 'npm-alias <name> <version> <alias>';

export const aliases = ['na', 'dep-alias', 'dependency-alias'];

export const describe = `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 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();
Expand Down Expand Up @@ -95,3 +94,6 @@ export const handler = async (argv) => {
process.exit(1);
}
};

export const deprecated =
'"npm-alias" will be removed in a future version. Please use "alias" instead';
12 changes: 7 additions & 5 deletions commands/package-alias.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @deprecated in favor of `alias` command

import ora from 'ora';
import semver from 'semver';
import Alias from '../classes/alias.js';
Expand All @@ -8,10 +10,7 @@ export const command = 'package-alias [name] [version] [alias]';

export const aliases = ['pkg-alias', 'pa'];

export const describe = `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 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();
Expand Down Expand Up @@ -99,8 +98,11 @@ export const handler = async (argv) => {
spinner.text = '';
spinner.stopAndPersist();
if (success) {
af.format(server);
af?.format(server);
} else {
process.exit(1);
}
};

export const deprecated =
'"package-alias" will be removed in a future version. Please use "alias" instead';
3 changes: 1 addition & 2 deletions commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export const builder = (yargs) => {
type: 'boolean',
},
token: {
describe: `Provide a jwt token to be used to authenticate with the Eik server.
Automatically determined if authenticated (via eik login)`,
describe: `Provide a jwt token to be used to authenticate with the Eik server. Automatically determined if authenticated (via eik login)`,
type: 'string',
alias: 't',
},
Expand Down
Loading

0 comments on commit ff4a27c

Please sign in to comment.