Skip to content

Commit

Permalink
refactor: only use function declaration
Browse files Browse the repository at this point in the history
Except where it changes functionality.
  • Loading branch information
ComradeVanti committed Jan 13, 2024
1 parent 6bcbac1 commit 95640c1
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 100 deletions.
6 changes: 3 additions & 3 deletions src/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type AddResult = {
/**
* @throws Error An unhandled error occurred
*/
export const add = async function (
export async function add(
pkgs: PackageReference | PackageReference[],
options: AddOptions
): Promise<AddResultCode> {
Expand All @@ -52,7 +52,7 @@ export const add = async function (
const env = await parseEnv(options, true);
if (env === null) return 1;

const addSingle = async function (pkg: PackageReference): Promise<AddResult> {
const addSingle = async (pkg: PackageReference): Promise<AddResult> => {
// dirty flag
let dirty = false;
// is upstream package flag
Expand Down Expand Up @@ -241,4 +241,4 @@ export const add = async function (
if (result.dirty)
log.notice("", "please open Unity project to apply changes");
return result.code;
};
}
4 changes: 2 additions & 2 deletions src/cmd-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type DepsOptions = CmdOptions<{
/**
* @throws Error An unhandled error occurred
*/
export const deps = async function (
export async function deps(
pkg: PackageReference,
options: DepsOptions
): Promise<DepsResultCode> {
Expand Down Expand Up @@ -53,4 +53,4 @@ export const deps = async function (
});

return 0;
};
}
26 changes: 12 additions & 14 deletions src/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ type LoginResultCode = 0 | 1;
/**
* @throws Error An unhandled error occurred
*/
export const login = async function (
options: LoginOptions
): Promise<LoginResultCode> {
export async function login(options: LoginOptions): Promise<LoginResultCode> {
// parse env
const env = await parseEnv(options, false);
if (env === null) return 1;
Expand Down Expand Up @@ -81,12 +79,12 @@ export const login = async function (
);

return 0;
};
}

/**
* Return npm login token
*/
const npmLogin = async function (
async function npmLogin(
username: string,
password: string,
email: string,
Expand Down Expand Up @@ -119,13 +117,13 @@ const npmLogin = async function (
return { code: 1 };
}
}
};
}

/**
* Write npm token to .npmrc
* @throws Error An unhandled error occurred
*/
const writeNpmToken = async function (registry: RegistryUrl, token: string) {
async function writeNpmToken(registry: RegistryUrl, token: string) {
const configPath = getNpmrcPath();
// read config
let content = "";
Expand All @@ -137,27 +135,27 @@ const writeNpmToken = async function (registry: RegistryUrl, token: string) {
const newContent = lines.join("\n") + "\n";
fs.writeFileSync(configPath, newContent);
log.notice("config", `saved to npm config: ${configPath}`);
};
}

/**
* Return .npmrc config file path
* @throws Error Home-path could not be determined
*/
export const getNpmrcPath = function () {
export function getNpmrcPath() {
const dirPath = process.env.USERPROFILE
? process.env.USERPROFILE
: process.env.HOME;
if (dirPath === undefined) throw new Error("Could not determine home path");
return path.join(dirPath, ".npmrc");
};
}

/**
* Generate .npmrc file content lines
* @param {*} content
* @param {*} registry
* @param {*} token
*/
export const generateNpmrcLines = function (
export function generateNpmrcLines(
content: string,
registry: RegistryUrl,
token: string
Expand Down Expand Up @@ -186,13 +184,13 @@ export const generateNpmrcLines = function (
// Remove empty lines
lines = lines.filter((l) => l);
return lines;
};
}

/**
* Write npm token to Unity
* @throws Error The specified authentication information was missing
*/
const writeUnityToken = async function (
async function writeUnityToken(
configDir: string,
_auth: Base64 | null,
alwaysAuth: boolean,
Expand Down Expand Up @@ -222,4 +220,4 @@ const writeUnityToken = async function (
}
// Write config file
await saveUpmConfig(config, configDir);
};
}
8 changes: 3 additions & 5 deletions src/cmd-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type RemoveResult = {
dirty: boolean;
};

export const remove = async function (
export async function remove(
pkgs: PackageReference[] | PackageReference,
options: RemoveOptions
): Promise<RemoveResultCode> {
Expand All @@ -34,9 +34,7 @@ export const remove = async function (
const env = await parseEnv(options, true);
if (env === null) return 1;

const removeSingle = async function (
pkg: PackageReference
): Promise<RemoveResult> {
const removeSingle = async (pkg: PackageReference): Promise<RemoveResult> => {
// dirty flag
let dirty = false;
// parse name
Expand Down Expand Up @@ -93,4 +91,4 @@ export const remove = async function (
if (result.dirty)
log.notice("", "please open Unity project to apply changes");
return result.code;
};
}
20 changes: 10 additions & 10 deletions src/cmd-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ export type OldSearchResult =
* Get npm fetch options
* @param registry The registry for which to get the options
*/
const getNpmFetchOptions = function (registry: Registry): Options {
function getNpmFetchOptions(registry: Registry): Options {
const opts: Options = {
log,
registry: registry.url,
};
const auth = registry.auth;
if (auth !== null) Object.assign(opts, auth);
return opts;
};
}

const searchEndpoint = async function (
async function searchEndpoint(
registry: Registry,
keyword: string
): Promise<TableRow[] | undefined> {
Expand All @@ -59,9 +59,9 @@ const searchEndpoint = async function (
}
log.warn("", "fast search endpoint is not available, using old search.");
}
};
}

const searchOld = async function (
async function searchOld(
registry: Registry,
keyword: string
): Promise<TableRow[] | undefined> {
Expand Down Expand Up @@ -97,16 +97,16 @@ const searchOld = async function (
}
log.warn("", "/-/all endpoint is not available");
}
};
}

const getTable = function () {
function getTable() {
return new Table({
head: ["Name", "Version", "Date"],
colWidths: [42, 20, 12],
});
};
}

const getTableRow = function (packument: SearchedPackument): TableRow {
function getTableRow(packument: SearchedPackument): TableRow {
const name = packument.name;
const version = tryGetLatestVersion(packument);
let date = "";
Expand All @@ -117,7 +117,7 @@ const getTableRow = function (packument: SearchedPackument): TableRow {
}
assert(version !== undefined);
return [name, version, date, ""];
};
}

export async function search(
keyword: string,
Expand Down
8 changes: 4 additions & 4 deletions src/cmd-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type ViewOptions = CmdOptions;

type ViewResultCode = 0 | 1;

export const view = async function (
export async function view(
pkg: PackageReference,
options: ViewOptions
): Promise<ViewResultCode> {
Expand All @@ -43,9 +43,9 @@ export const view = async function (
// print info
printInfo(packument);
return 0;
};
}

const printInfo = function (packument: UnityPackument) {
function printInfo(packument: UnityPackument) {
const versionCount = Object.keys(packument.versions).length;
const ver = tryGetLatestVersion(packument);
assert(ver !== undefined);
Expand Down Expand Up @@ -107,4 +107,4 @@ const printInfo = function (packument: UnityPackument) {
for (const version in packument.versions) {
console.log(" " + chalk.greenBright(version));
}
};
}
21 changes: 12 additions & 9 deletions src/registry-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,24 @@ function normalizeClientFunction<TParam, TData>(
fn: (uri: string, params: TParam, cb: ClientCallback<TData>) => void
): (uri: string, params: TParam) => Promise<TData> {
const bound = fn.bind(client);
const withNormalizedError = (

function withNormalizedError(
uri: string,
params: TParam,
cb: (error: NpmClientError | null, data: TData) => void
) => {
) {
return bound(uri, params, (error, data, raw, res) => {
cb(error !== null ? new NpmClientError(error, res) : null, data);
});
};
}

return promisify(withNormalizedError);
}

/**
* Return npm client
*/
export const getNpmClient = (): NpmClient => {
export function getNpmClient(): NpmClient {
// create client
const client = new RegClient({ log });
return {
Expand All @@ -111,9 +113,10 @@ export const getNpmClient = (): NpmClient => {
get: normalizeClientFunction(client, client.get),
adduser: normalizeClientFunction(client, client.adduser),
};
};
}

// Fetch package info json from registry
export const fetchPackument = async function (
export async function fetchPackument(
registry: Registry,
name: DomainName
): Promise<UnityPackument | undefined> {
Expand All @@ -124,10 +127,10 @@ export const fetchPackument = async function (
} catch (err) {
/* empty */
}
};
}

// Fetch package dependencies
export const fetchPackageDependencies = async function (
export async function fetchPackageDependencies(
registry: Registry,
upstreamRegistry: Registry,
name: DomainName,
Expand Down Expand Up @@ -258,4 +261,4 @@ export const fetchPackageDependencies = async function (
}
}
return [depsValid, depsInvalid];
};
}
4 changes: 2 additions & 2 deletions src/types/domain-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function isDomainName(s: string): s is DomainName {
* Detect if the given package name is an internal package
* @param name The name of the package
*/
export const isInternalPackage = (name: DomainName): boolean => {
export function isInternalPackage(name: DomainName): boolean {
const internals = [
"com.unity.ugui",
"com.unity.2d.sprite",
Expand All @@ -68,7 +68,7 @@ export const isInternalPackage = (name: DomainName): boolean => {
"com.unity.ugui",
];
return /com.unity.modules/i.test(name) || internals.includes(name);
};
}

/**
* Constructs a domain-name from a string.
Expand Down
Loading

0 comments on commit 95640c1

Please sign in to comment.