Skip to content

Commit

Permalink
feat: convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Oct 16, 2023
1 parent 4992adb commit 99d3c01
Show file tree
Hide file tree
Showing 28 changed files with 1,366 additions and 558 deletions.
2 changes: 1 addition & 1 deletion bin/openupm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require("../lib/cli");
require("../build/lib/cli");
2 changes: 1 addition & 1 deletion bin/openupm-cn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

if (!process.argv.includes("--cn")) process.argv.push("--cn");
require("../lib/cli");
require("../build/lib/cli");
51 changes: 28 additions & 23 deletions lib/cli.js → lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const program = require("commander");
const add = require("./cmd-add");
const remove = require("./cmd-remove");
const search = require("./cmd-search");
const view = require("./cmd-view");
const deps = require("./cmd-deps");
const { login } = require("./cmd-login");
const { log } = require("./logger");
require("pkginfo")(module);
import { program } from "commander";
import pkginfo from "pkginfo";
import updateNotifier from "update-notifier";
import { add } from "./cmd-add";
import { remove } from "./cmd-remove";
import { search } from "./cmd-search";
import { view } from "./cmd-view";
import { deps } from "./cmd-deps";
import { login } from "./cmd-login";

import log from "./logger";

// update-notifier
const updateNotifier = require("update-notifier");
const pkg = require("../package.json");

import pkg from "../package.json";

pkginfo(module);
const notifier = updateNotifier({ pkg });
notifier.notify();

Expand Down Expand Up @@ -39,7 +43,7 @@ openupm add <pkg> [otherPkgs...]
openupm add <pkg>@<version> [otherPkgs...]`
)
.action(async function (pkg, otherPkgs, options) {
options._global = program.opts()
options._global = program.opts();
const pkgs = [pkg].concat(otherPkgs);
const retCode = await add(pkgs, options);
if (retCode) process.exit(retCode);
Expand All @@ -49,8 +53,8 @@ program
.command("remove <pkg> [otherPkgs...]")
.aliases(["rm", "uninstall"])
.description("remove package from manifest json")
.action(async function(pkg, otherPkgs, options) {
options._global = program.opts()
.action(async function (pkg, otherPkgs, options) {
options._global = program.opts();
const pkgs = [pkg].concat(otherPkgs);
const retCode = await remove(pkgs, options);
if (retCode) process.exit(retCode);
Expand All @@ -60,8 +64,8 @@ program
.command("search <keyword>")
.aliases(["s", "se", "find"])
.description("Search package by keyword")
.action(async function(keyword, options) {
options._global = program.opts()
.action(async function (keyword, options) {
options._global = program.opts();
const retCode = await search(keyword, options);
if (retCode) process.exit(retCode);
});
Expand All @@ -70,8 +74,8 @@ program
.command("view <pkg>")
.aliases(["v", "info", "show"])
.description("view package information")
.action(async function(pkg, options) {
options._global = program.opts()
.action(async function (pkg, options) {
options._global = program.opts();
const retCode = await view(pkg, options);
if (retCode) process.exit(retCode);
});
Expand All @@ -85,8 +89,8 @@ program
openupm deps <pkg>
openupm deps <pkg>@<version>`
)
.action(async function(pkg, options) {
options._global = program.opts()
.action(async function (pkg, options) {
options._global = program.opts();
const retCode = await deps(pkg, options);
if (retCode) process.exit(retCode);
});
Expand All @@ -104,19 +108,20 @@ program
"always auth for tarball hosted on a different domain"
)
.description("authenticate with a scoped registry")
.action(async function(options) {
options._global = program.opts()
.action(async function (options) {
options._global = program.opts();
try {
const retCode = await login(options);
if (retCode) process.exit(retCode);
} catch (err) {
// @ts-ignore
log.error("", err.message);
process.exit(1);
}
});

// prompt for invalid command
program.on("command:*", function() {
program.on("command:*", function () {
log.warn("", `unknown command: ${program.args.join(" ")}`);
log.warn("", "see --help for a list of available commands");
process.exit(1);
Expand Down
14 changes: 6 additions & 8 deletions lib/client.js → lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const { promisify } = require("util");
const RegClient = require("another-npm-registry-client");
import {promisify} from "util";

// @ts-ignore
import RegClient from "another-npm-registry-client";
import log from "./logger";

const { log } = require("./logger");

/**
* Return npm client
*/
const getNpmClient = function() {
export const getNpmClient = function() {
// create client
const client = new RegClient({ log });
return {
Expand All @@ -17,7 +19,3 @@ const getNpmClient = function() {
adduser: promisify(client.adduser.bind(client))
};
};

module.exports = {
getNpmClient
};
67 changes: 45 additions & 22 deletions lib/cmd-add.js → lib/cmd-add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { log } = require("./logger");
const url = require("url");
const {
import log from "./logger";
import url from "url";
import {
compareEditorVersion,
env,
fetchPackageDependencies,
Expand All @@ -10,10 +10,20 @@ const {
parseEditorVersion,
parseEnv,
parseName,
saveManifest
} = require("./core");
saveManifest,
} from "./core";
import { GlobalOptions, Pkg, PkgName, ScopedRegistry } from "./types";

const add = async function(pkgs, options) {
export type AddOptions = {
test: boolean;
force: boolean;
_global: GlobalOptions;
};

export const add = async function (
pkgs: Pkg | Pkg[],
options: AddOptions
): Promise<number> {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
const envOk = await parseEnv(options, { checkPath: true });
Expand All @@ -25,16 +35,24 @@ const add = async function(pkgs, options) {
await _add({ pkg, testables: options.test, force: options.force })
);
const result = {
code: results.filter(x => x.code != 0).length > 0 ? 1 : 0,
dirty: results.filter(x => x.dirty).length > 0
code: results.filter((x) => x.code != 0).length > 0 ? 1 : 0,
dirty: results.filter((x) => x.dirty).length > 0,
};
// print manifest notice
if (result.dirty)
log.notice("", "please open Unity project to apply changes");
return result.code;
};

const _add = async function({ pkg, testables, force }) {
const _add = async function ({
pkg,
testables,
force,
}: {
pkg: Pkg;
testables: boolean;
force: boolean;
}) {
// dirty flag
let dirty = false;
// is upstream package flag
Expand All @@ -49,7 +67,7 @@ const _add = async function({ pkg, testables, force }) {
manifest.dependencies = {};
}
// packages that added to scope registry
const pkgsInScope = [];
const pkgsInScope: PkgName[] = [];
const isGitOrLocal =
version &&
(version.startsWith("git") ||
Expand All @@ -70,7 +88,7 @@ const _add = async function({ pkg, testables, force }) {
const versions = Object.keys(pkgInfo.versions);
// eslint-disable-next-line require-atomic-updates
if (!version || version == "latest") version = getLatestVersion(pkgInfo);
if (versions.filter(x => x == version).length <= 0) {
if (versions.filter((x) => x == version).length <= 0) {
log.warn(
"404",
`version ${version} is not a valid choice of: ${versions
Expand All @@ -79,6 +97,7 @@ const _add = async function({ pkg, testables, force }) {
);
return { code: 1, dirty };
}

const versionInfo = pkgInfo.versions[version];
// verify editor version
if (versionInfo.unity) {
Expand Down Expand Up @@ -130,16 +149,18 @@ const _add = async function({ pkg, testables, force }) {
const [depsValid, depsInvalid] = await fetchPackageDependencies({
name,
version,
deep: true
deep: true,
});
// add depsValid to pkgsInScope.
depsValid
.filter(x => !x.upstream && !x.internal)
.map(x => x.name)
.forEach(name => pkgsInScope.push(name));
.filter((x) => !x.upstream && !x.internal)
.map((x) => x.name)
.forEach((name) => pkgsInScope.push(name));
// print suggestion for depsInvalid
depsInvalid.forEach(depObj => {
depsInvalid.forEach((depObj) => {
if (depObj.reason == "package404" || depObj.reason == "version404") {
// TODO: Do null check on manifest
// @ts-ignore
const resolvedVersion = manifest.dependencies[depObj.name];
depObj.resolved = Boolean(resolvedVersion);
if (!depObj.resolved)
Expand All @@ -149,7 +170,7 @@ const _add = async function({ pkg, testables, force }) {
);
}
});
if (depsInvalid.filter(x => !x.resolved).length > 0) {
if (depsInvalid.filter((x) => !x.resolved).length > 0) {
if (!force) {
log.error(
"missing dependencies",
Expand All @@ -162,6 +183,8 @@ const _add = async function({ pkg, testables, force }) {
}
// add to dependencies
const oldVersion = manifest.dependencies[name];
// TODO: Do undefined check on version
// @ts-ignore
manifest.dependencies[name] = version;
if (!oldVersion) {
// Log the added package
Expand All @@ -181,24 +204,26 @@ const _add = async function({ pkg, testables, force }) {
manifest.scopedRegistries = [];
dirty = true;
}
const filterEntry = x => {
const filterEntry = (x: ScopedRegistry): boolean => {
let addr = x.url || "";
if (addr.endsWith("/")) addr = addr.slice(0, -1);
return addr == env.registry;
};
if (manifest.scopedRegistries.filter(filterEntry).length <= 0) {
manifest.scopedRegistries.push({
// TODO: Handle null name
// @ts-ignore
name: url.parse(env.registry).hostname,
url: env.registry,
scopes: []
scopes: [],
});
dirty = true;
}
let entry = manifest.scopedRegistries.filter(filterEntry)[0];
// apply pkgsInScope
let scopesSet = new Set(entry.scopes || []);
pkgsInScope.push(env.namespace);
pkgsInScope.forEach(name => {
pkgsInScope.forEach((name) => {
if (!scopesSet.has(name)) {
scopesSet.add(name);
dirty = true;
Expand All @@ -220,5 +245,3 @@ const _add = async function({ pkg, testables, force }) {
}
return { code: 0, dirty };
};

module.exports = add;
34 changes: 23 additions & 11 deletions lib/cmd-deps.js → lib/cmd-deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const { log } = require("./logger");
const { fetchPackageDependencies, parseEnv, parseName } = require("./core");
import log from "./logger";
import { fetchPackageDependencies, parseEnv, parseName } from "./core";
import { GlobalOptions, Pkg, PkgName, PkgVersionName } from "./types";

const deps = async function(pkg, options) {
export type DepsOptions = {
deep: boolean;
_global: GlobalOptions;
};

export const deps = async function (pkg: Pkg, options: DepsOptions) {
// parse env
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
Expand All @@ -12,24 +18,30 @@ const deps = async function(pkg, options) {
return 0;
};

const _deps = async function({ name, version, deep }) {
const _deps = async function ({
name,
version,
deep,
}: {
name: PkgName;
version: PkgVersionName;
deep: boolean;
}) {
// eslint-disable-next-line no-unused-vars
const [depsValid, depsInvalid] = await fetchPackageDependencies({
name,
version,
deep
deep,
});
depsValid
.filter(x => !x.self)
.forEach(x => log.notice("dependency", `${x.name}@${x.version}`));
.filter((x) => !x.self)
.forEach((x) => log.notice("dependency", `${x.name}@${x.version}`));
depsInvalid
.filter(x => !x.self)
.forEach(x => {
.filter((x) => !x.self)
.forEach((x) => {
let reason = "unknown";
if (x.reason == "package404") reason = "missing dependency";
else if (x.reason == "version404") reason = "missing dependency version";
log.warn(reason, `${x.name}@${x.version}`);
});
};

module.exports = deps;
Loading

0 comments on commit 99d3c01

Please sign in to comment.