Skip to content

Commit

Permalink
fix(cli): adopt cli for commander 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed May 9, 2022
1 parent ffdd2fa commit 3696d3c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 50 deletions.
8 changes: 7 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ program
openupm add <pkg> [otherPkgs...]
openupm add <pkg>@<version> [otherPkgs...]`
)
.action(async function(pkg, otherPkgs, options) {
.action(async function (pkg, otherPkgs, options) {
options._global = program.opts()
const pkgs = [pkg].concat(otherPkgs);
const retCode = await add(pkgs, options);
if (retCode) process.exit(retCode);
Expand All @@ -49,6 +50,7 @@ program
.aliases(["rm", "uninstall"])
.description("remove package from manifest json")
.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 @@ -59,6 +61,7 @@ program
.aliases(["s", "se", "find"])
.description("Search package by keyword")
.action(async function(keyword, options) {
options._global = program.opts()
const retCode = await search(keyword, options);
if (retCode) process.exit(retCode);
});
Expand All @@ -68,6 +71,7 @@ program
.aliases(["v", "info", "show"])
.description("view package information")
.action(async function(pkg, options) {
options._global = program.opts()
const retCode = await view(pkg, options);
if (retCode) process.exit(retCode);
});
Expand All @@ -82,6 +86,7 @@ openupm deps <pkg>
openupm deps <pkg>@<version>`
)
.action(async function(pkg, options) {
options._global = program.opts()
const retCode = await deps(pkg, options);
if (retCode) process.exit(retCode);
});
Expand All @@ -100,6 +105,7 @@ program
)
.description("authenticate with a scoped registry")
.action(async function(options) {
options._global = program.opts()
try {
const retCode = await login(options);
if (retCode) process.exit(retCode);
Expand Down
10 changes: 5 additions & 5 deletions lib/cmd-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const login = async function(options) {
if (!options.password)
options.password = await promptly.password("Password: ");
if (!options.email) options.email = await promptly.prompt("Email: ");
if (!options.parent.registry)
options.parent.registry = await promptly.prompt("Registry: ", {
if (!options._global.registry)
options._global.registry = await promptly.prompt("Registry: ", {
validator: [validateRegistry]
});
let token = null;
Expand All @@ -38,7 +38,7 @@ const login = async function(options) {
username: options.username,
password: options.password,
email: options.email,
registry: options.parent.registry
registry: options._global.registry
});
if (result.code == 1) return result.code;
if (!result.token) {
Expand All @@ -48,7 +48,7 @@ const login = async function(options) {
token = result.token;
// write npm token
await writeNpmToken({
registry: options.parent.registry,
registry: options._global.registry,
token: result.token
});
}
Expand All @@ -58,7 +58,7 @@ const login = async function(options) {
alwaysAuth: options.alwaysAuth || false,
basicAuth: options.basicAuth || false,
email: options.email,
registry: options.parent.registry,
registry: options._global.registry,
token
});
};
Expand Down
20 changes: 10 additions & 10 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ const parseEnv = async function(options, { checkPath }) {
// the dict of auth param for npm registry API
env.auth = {};
// log level
log.level = options.parent.verbose ? "verbose" : "notice";
log.level = options._global.verbose ? "verbose" : "notice";
// color
if (options.parent.color === false) env.color = false;
if (options._global.color === false) env.color = false;
if (process.env.NODE_ENV == "test") env.color = false;
if (!env.color) {
chalk.level = 0;
log.disableColor();
}
// upstream
if (options.parent.upstream === false) env.upstream = false;
if (options._global.upstream === false) env.upstream = false;
// region cn
if (options.parent.cn === true) {
if (options._global.cn === true) {
env.registry = "https://package.openupm.cn";
env.upstreamRegistry = "https://packages.unity.cn";
env.region = "cn";
log.notice("region", "cn");
}
// registry
if (options.parent.registry) {
let registry = options.parent.registry;
if (options._global.registry) {
let registry = options._global.registry;
if (!registry.toLowerCase().startsWith("http"))
registry = "http://" + registry;
if (registry.endsWith("/")) registry = registry.slice(0, -1);
Expand All @@ -70,8 +70,8 @@ const parseEnv = async function(options, { checkPath }) {
.join(".");
}
// auth
if (options.parent.systemUser) env.systemUser = true;
if (options.parent.wsl) env.wsl = true;
if (options._global.systemUser) env.systemUser = true;
if (options._global.wsl) env.wsl = true;
const upmConfig = await loadUpmConfig();
if (upmConfig) {
env.npmAuth = upmConfig.npmAuth;
Expand Down Expand Up @@ -108,8 +108,8 @@ const parseEnv = async function(options, { checkPath }) {
// return if no need to check path
if (!checkPath) return true;
// cwd
if (options.parent.chdir) {
const cwd = path.resolve(options.parent.chdir);
if (options._global.chdir) {
const cwd = path.resolve(options._global.chdir);
if (!fs.existsSync(cwd)) {
log.error("env", `can not resolve path ${cwd}`);
return false;
Expand Down
8 changes: 4 additions & 4 deletions test/test-cmd-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ const {

describe("cmd-add.js", function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
upstream: false,
chdir: getWorkDir("test-openupm-cli")
}
};
const upstreamOptions = {
parent: {
_global: {
registry: "http://example.com",
upstream: true,
chdir: getWorkDir("test-openupm-cli")
}
};
const testableOptions = {
parent: {
_global: {
registry: "http://example.com",
upstream: false,
chdir: getWorkDir("test-openupm-cli")
},
test: true
};
const forceOptions = {
parent: {
_global: {
registry: "http://example.com",
upstream: false,
chdir: getWorkDir("test-openupm-cli")
Expand Down
2 changes: 1 addition & 1 deletion test/test-cmd-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {

describe("cmd-deps.js", function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand Down
8 changes: 4 additions & 4 deletions test/test-cmd-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("cmd-remove.js", function() {
});
it("remove pkg", async function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand All @@ -69,7 +69,7 @@ describe("cmd-remove.js", function() {
});
it("remove pkg@1.0.0", async function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand All @@ -83,7 +83,7 @@ describe("cmd-remove.js", function() {
});
it("remove pkg-not-exist", async function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand All @@ -97,7 +97,7 @@ describe("cmd-remove.js", function() {
});
it("remove more than one pkgs", async function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand Down
4 changes: 2 additions & 2 deletions test/test-cmd-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ describe("cmd-search.js", function() {
let stdoutInspect = null;
let stderrInspect = null;
const options = {
parent: {
_global: {
registry: "http://example.com",
upstream: false,
chdir: getWorkDir("test-openupm-cli")
}
};
const upstreamOptions = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand Down
4 changes: 2 additions & 2 deletions test/test-cmd-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const {

describe("cmd-view.js", function() {
const options = {
parent: {
_global: {
registry: "http://example.com",
upstream: false,
chdir: getWorkDir("test-openupm-cli")
}
};
const upstreamOptions = {
parent: {
_global: {
registry: "http://example.com",
chdir: getWorkDir("test-openupm-cli")
}
Expand Down
Loading

0 comments on commit 3696d3c

Please sign in to comment.