Skip to content

Commit

Permalink
fix: move --wsl and --system-user as parent options
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Aug 8, 2020
1 parent 417b59e commit 0184fa4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 69 deletions.
4 changes: 2 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ program
.option("-c, --chdir <path>", "change the working directory")
.option("-r, --registry <url>", "specify registry url")
.option("-v, --verbose", "output extra debugging")
.option("--system-user", "auth for Windows system user")
.option("--wsl", "auth for Windows when using WSL")
.option("--no-upstream", "don't use upstream unity registry")
.option("--no-color", "disable color");

Expand Down Expand Up @@ -87,8 +89,6 @@ program
.option("-e, --email <email>", "email address")
.option("-r, --registry <url>", "registry url")
.option("--always-auth", "use basic authentication instead of token")
.option("--system-user", "auth for Windows system user")
.option("--wsl", "auth for Windows when using WSL")
.description("authenticate with a scoped registry")
.action(async function(options) {
try {
Expand Down
72 changes: 12 additions & 60 deletions lib/cmd-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ const fs = require("fs");
const path = require("path");

const _ = require("lodash");
const mkdirp = require("mkdirp");
const promptly = require("promptly");
const isWsl = require("is-wsl");
const TOML = require("@iarna/toml");

const { getNpmClient } = require("./client");
const { log } = require("./logger");
const { parseEnv } = require("./core");
const { execute } = require("./utils/process");
const {
getUpmConfigDir,
loadUpmConfig,
saveUpmConfig,
parseEnv
} = require("./core");

const login = async function(options) {
// parse env
Expand Down Expand Up @@ -56,9 +58,7 @@ const login = async function(options) {
alwaysAuth: options.alwaysAuth || false,
email: options.email,
registry: options.parent.registry,
systemUser: options.systemUser,
token,
wsl: options.wsl
token
});
};

Expand Down Expand Up @@ -172,23 +172,12 @@ const writeUnityToken = async function({
alwaysAuth,
email,
registry,
systemUser,
token,
wsl
token
}) {
// Create config dir if necessary
const configDir = await getUpmConfigDir({
systemUser,
wsl
});
mkdirp.sync(configDir);
// Read config file if exists
const configPath = path.join(configDir, ".upmconfig.toml");
let config = {};
if (fs.existsSync(configPath)) {
const content = fs.readFileSync(configPath, "utf8");
config = TOML.parse(content);
}
const configDir = await getUpmConfigDir();
// Read config file
const config = (await loadUpmConfig(configDir)) || {};
if (!config.npmAuth) config.npmAuth = {};
// Remove ending slash of registry
if (registry.endsWith("/")) registry = registry.replace(/\/$/, "");
Expand All @@ -200,44 +189,7 @@ const writeUnityToken = async function({
if (alwaysAuth) config["npmAuth"][registry]._auth = _auth;
else config["npmAuth"][registry].token = token;
// Write config file
const newContent = TOML.stringify(config);
fs.writeFileSync(configPath, newContent, "utf8");
log.notice("config", "saved to unity config: " + configPath);
};

/**
* Return .upmconfig.toml config file directory
*/
const getUpmConfigDir = async function({ systemUser, wsl }) {
let dirPath = "";
const systemUserSubPath = "Unity/config/ServiceAccounts";
if (wsl) {
if (!isWsl) {
throw new Error("no WSL detected");
}
if (systemUser) {
const allUserProfilePath = await execute(
'wslpath "$(wslvar ALLUSERSPROFILE)"',
{ trim: true }
);
dirPath = path.join(allUserProfilePath, systemUserSubPath);
} else {
dirPath = await execute('wslpath "$(wslvar USERPROFILE)"', {
trim: true
});
}
} else {
dirPath = process.env.USERPROFILE
? process.env.USERPROFILE
: process.env.HOME;
if (systemUser) {
if (!process.env.ALLUSERSPROFILE) {
throw new Error("env ALLUSERSPROFILE is empty");
}
dirPath = path.join(process.env.ALLUSERSPROFILE, systemUserSubPath);
}
}
return dirPath;
await saveUpmConfig(config, configDir);
};

module.exports = {
Expand Down
83 changes: 76 additions & 7 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const fs = require("fs");
const os = require("os");
const path = require("path");
const url = require("url");

const _ = require("lodash");
const chalk = require("chalk");
const fs = require("fs");
const keyFileStorage = require("key-file-storage").default;
const mkdirp = require("mkdirp");
const isIp = require("is-ip");
const { log } = require("./logger");
const path = require("path");
const os = require("os");
const isWsl = require("is-wsl");
const keyFileStorage = require("key-file-storage").default;
const superagent = require("superagent");
// extend with Request#proxy()
require("superagent-proxy")(superagent);
const url = require("url");
const TOML = require("@iarna/toml");

const { execute } = require("./utils/process");
const { getNpmClient } = require("./client");
const { log } = require("./logger");

const env = {};

Expand All @@ -24,6 +30,8 @@ const parseEnv = function(options, { checkPath }) {
env.upstream = true;
env.color = true;
env.upstreamRegistry = "https://packages.unity.com";
env.systemUser = false;
env.wsl = false;
// log level
log.level = options.parent.verbose ? "verbose" : "notice";
// color
Expand Down Expand Up @@ -51,6 +59,10 @@ const parseEnv = function(options, { checkPath }) {
.slice(0, 2)
.join(".");
}
// auth
if (options.parent.systemUser) env.systemUser = true;
if (options.parent.wsl) env.wsl = true;
// return if no need to check path
if (!checkPath) return true;
// cwd
if (options.parent.chdir) {
Expand Down Expand Up @@ -268,6 +280,60 @@ const cleanCache = function() {
delete cache["*"];
};

// Get .upmconfig.toml directory
const getUpmConfigDir = async function() {
let dirPath = "";
const systemUserSubPath = "Unity/config/ServiceAccounts";
if (env.wsl) {
if (!isWsl) {
throw new Error("no WSL detected");
}
if (env.systemUser) {
const allUserProfilePath = await execute(
'wslpath "$(wslvar ALLUSERSPROFILE)"',
{ trim: true }
);
dirPath = path.join(allUserProfilePath, systemUserSubPath);
} else {
dirPath = await execute('wslpath "$(wslvar USERPROFILE)"', {
trim: true
});
}
} else {
dirPath = process.env.USERPROFILE
? process.env.USERPROFILE
: process.env.HOME;
if (env.systemUser) {
if (!process.env.ALLUSERSPROFILE) {
throw new Error("env ALLUSERSPROFILE is empty");
}
dirPath = path.join(process.env.ALLUSERSPROFILE, systemUserSubPath);
}
}
return dirPath;
};

// Load .upmconfig.toml
const loadUpmConfig = async function(configDir) {
if (configDir === undefined) configDir = await getUpmConfigDir();
const configPath = path.join(configDir, ".upmconfig.toml");
if (fs.existsSync(configPath)) {
const content = fs.readFileSync(configPath, "utf8");
const config = TOML.parse(content);
return config;
}
};

// Save .upmconfig.toml
const saveUpmConfig = async function(config, configDir) {
if (configDir === undefined) configDir = await getUpmConfigDir();
mkdirp.sync(configDir);
const configPath = path.join(configDir, ".upmconfig.toml");
const content = TOML.stringify(config);
fs.writeFileSync(configPath, content, "utf8");
log.notice("config", "saved unity config at " + configPath);
};

module.exports = {
env,
getCache,
Expand All @@ -279,5 +345,8 @@ module.exports = {
loadManifest,
parseEnv,
parseName,
saveManifest
saveManifest,
getUpmConfigDir,
loadUpmConfig,
saveUpmConfig
};

0 comments on commit 0184fa4

Please sign in to comment.