Skip to content

Commit

Permalink
refactor: extract and rename function
Browse files Browse the repository at this point in the history
Rename the parseName function to splitPackageName to better reflect it's purpose. Also moved to separate module
  • Loading branch information
ComradeVanti committed Oct 25, 2023
1 parent 996aa41 commit 138f5d1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
8 changes: 4 additions & 4 deletions lib/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
loadManifest,
parseEditorVersion,
parseEnv,
parseName,
saveManifest,
} from "./core";
import { isUrlVersion } from "./utils/pkg-version";
import { splitPackageName } from "./utils/pkg-name";

export type AddOptions = {
test: boolean;
Expand Down Expand Up @@ -58,9 +58,9 @@ const _add = async function ({
// is upstream package flag
let isUpstreamPackage = false;
// parse name
const parseResult = parseName(pkg);
const name = parseResult.name;
let version = parseResult.version;
const split = splitPackageName(pkg);
const name = split.name;
let version = split.version;

// load manifest
const manifest = loadManifest();
Expand Down
5 changes: 3 additions & 2 deletions lib/cmd-deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import log from "./logger";
import { fetchPackageDependencies, parseEnv, parseName } from "./core";
import { fetchPackageDependencies, parseEnv } from "./core";
import { splitPackageName } from "./utils/pkg-name";

export type DepsOptions = {
deep: boolean;
Expand All @@ -11,7 +12,7 @@ export const deps = async function (pkg: PkgName, options: DepsOptions) {
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
// parse name
const { name, version } = parseName(pkg);
const { name, version } = splitPackageName(pkg);
// deps
await _deps({ name, version, deep: options.deep });
return 0;
Expand Down
9 changes: 5 additions & 4 deletions lib/cmd-remove.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import log from "./logger";
import { env, loadManifest, parseEnv, parseName, saveManifest } from "./core";
import { env, loadManifest, parseEnv, saveManifest } from "./core";
import { splitPackageName } from "./utils/pkg-name";

export type RemoveOptions = {
_global: GlobalOptions;
Expand Down Expand Up @@ -30,9 +31,9 @@ const _remove = async function (pkg: PkgName) {
// dirty flag
let dirty = false;
// parse name
const parseResult = parseName(pkg);
const name = parseResult.name;
let version = parseResult.version;
const split = splitPackageName(pkg);
const name = split.name;
let version = split.version;
if (version) {
log.warn("", `please replace '${name}@${version}' with '${name}'`);
return { code: 1, dirty };
Expand Down
11 changes: 3 additions & 8 deletions lib/cmd-view.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import chalk from "chalk";
import log from "./logger";
import {
env,
fetchPackageInfo,
getLatestVersion,
parseEnv,
parseName,
} from "./core";
import { env, fetchPackageInfo, getLatestVersion, parseEnv } from "./core";
import assert from "assert";
import { splitPackageName } from "./utils/pkg-name";

export type ViewOptions = {
_global: GlobalOptions;
Expand All @@ -18,7 +13,7 @@ export const view = async function (pkg: PkgName, options: ViewOptions) {
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
// parse name
const { name, version } = parseName(pkg);
const { name, version } = splitPackageName(pkg);
if (version) {
log.warn("", `please replace '${name}@${version}' with '${name}'`);
return 1;
Expand Down
12 changes: 0 additions & 12 deletions lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,6 @@ export const parseEnv = async function (
return true;
};

// Parse name to {name, version}
export const parseName = function (pkg: PkgName): {
name: ReverseDomainName;
version: PkgVersion | undefined;
} {
const segs = pkg.split("@");
const name = segs[0];
const version =
segs.length > 1 ? segs.slice(1, segs.length).join("@") : undefined;
return { name, version };
};

// Get npm fetch options
export const getNpmFetchOptions = function (): search.Options {
const opts: search.Options = {
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/pkg-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Split package-name, which may include a version into the actual name of the
* package and the version if it exists
*/
export const splitPackageName = function (pkgName: PkgName): {
name: ReverseDomainName;
version: PkgVersion | undefined;
} {
const segments = pkgName.split("@");
const name = segments[0];
const version =
segments.length > 1
? segments.slice(1, segments.length).join("@")
: undefined;
return { name, version };
};

0 comments on commit 138f5d1

Please sign in to comment.