Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appbuilder/proton-static-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ export class ProtonStaticConfig extends StaticConfigBase {
public CLIENT_NAME = "Desktop Client - Universal";

public ANALYTICS_EXCEPTIONS_API_KEY: string = null;

public PROFILE_DIR_NAME: string = ".appbuilder-desktop-universal";
}
$injector.register("staticConfig", ProtonStaticConfig);
4 changes: 2 additions & 2 deletions commands/post-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class PostInstallCommand implements ICommand {
private $staticConfig: Config.IStaticConfig,
private $commandsService: ICommandsService,
private $helpService: IHelpService,
private $options: ICommonOptions,
private $settingsService: ISettingsService,
private $doctorService: IDoctorService,
private $analyticsService: IAnalyticsService,
private $logger: ILogger) {
Expand All @@ -18,7 +18,7 @@ export class PostInstallCommand implements ICommand {
// it is no longer accessible for the user initiating the installation
// patch the owner here
if (process.env.SUDO_USER) {
await this.$fs.setCurrentUserAsOwner(this.$options.profileDir, process.env.SUDO_USER);
await this.$fs.setCurrentUserAsOwner(this.$settingsService.getProfileDir(), process.env.SUDO_USER);
}
}

Expand Down
4 changes: 2 additions & 2 deletions commands/preuninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export class PreUninstallCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];

constructor(private $fs: IFileSystem,
private $options: ICommonOptions) { }
private $settingsService: ISettingsService) { }

public async execute(args: string[]): Promise<void> {
this.$fs.deleteFile(path.join(this.$options.profileDir, "KillSwitches", "cli"));
this.$fs.deleteFile(path.join(this.$settingsService.getProfileDir(), "KillSwitches", "cli"));
}
}

Expand Down
14 changes: 13 additions & 1 deletion declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ interface IConfigurationSettings {
* This string will be used when constructing the UserAgent http header.
* @type {string}
*/
userAgentName: string;
userAgentName?: string;

/**
* Describes the profile directory that will be used for various CLI settings, like user-settings.json file location, extensions, etc.
* @type {string}
*/
profileDir?: string;
}

/**
Expand All @@ -92,6 +98,12 @@ interface ISettingsService {
* @returns {void}
*/
setSettings(settings: IConfigurationSettings): void;

/**
* Returns currently used profile directory.
* @returns {string}
*/
getProfileDir(): string;
}

/**
Expand Down
1 change: 1 addition & 0 deletions definitions/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare module Config {
PATH_TO_BOOTSTRAP: string;
QR_SIZE: number;
INSTALLATION_SUCCESS_MESSAGE?: string;
PROFILE_DIR_NAME: string
}

interface IConfig {
Expand Down
17 changes: 12 additions & 5 deletions options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export class OptionsBase {
verbose: { type: OptionType.Boolean, alias: "v" },
version: { type: OptionType.Boolean },
help: { type: OptionType.Boolean, alias: "h" },
profileDir: { type: OptionType.String, default: this.defaultProfileDir },
profileDir: { type: OptionType.String },
analyticsClient: { type: OptionType.String },
path: { type: OptionType.String, alias: "p" },
// This will parse all non-hyphenated values as strings.
_: { type: OptionType.String }
};

constructor(public options: IDictionary<IDashedOption>,
public defaultProfileDir: string,
private $errors: IErrors,
private $staticConfig: Config.IStaticConfig) {
private $staticConfig: Config.IStaticConfig,
private $settingsService: ISettingsService) {

this.options = _.extend({}, this.commonOptions, this.options, this.globalOptions);
this.setArgv();
Expand Down Expand Up @@ -180,17 +180,24 @@ export class OptionsBase {
});

this.argv = yargs(process.argv.slice(2)).options(opts).argv;

// For backwards compatibility
// Previously profileDir had a default option and calling `this.$options.profileDir` always returned valid result.
// Now the profileDir should be used from $settingsService, but ensure the `this.$options.profileDir` returns the same value.
this.$settingsService.setSettings({ profileDir: this.argv.profileDir});
this.argv.profileDir = this.argv["profile-dir"] = this.$settingsService.getProfileDir();

this.adjustDashedOptions();
}

private adjustDashedOptions(): void {
_.each(this.optionNames, optionName => {
Object.defineProperty(OptionsBase.prototype, optionName, {
configurable: true,
get: function () {
get: () => {
return this.getOptionValue(optionName);
},
set: function (value: any) {
set: (value: any) => {
this.argv[optionName] = value;
}
});
Expand Down
4 changes: 2 additions & 2 deletions services/lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class LockFile implements ILockFile {

@cache()
private get defaultLockFilePath(): string {
return path.join(this.$options.profileDir, "lockfile.lock");
return path.join(this.$settingsService.getProfileDir(), "lockfile.lock");
}

private get defaultLockParams(): lockfile.Options {
Expand All @@ -22,7 +22,7 @@ export class LockFile implements ILockFile {
}

constructor(private $fs: IFileSystem,
private $options: ICommonOptions) {
private $settingsService: ISettingsService) {
}

public lock(lockFilePath?: string, lockFileOpts?: lockfile.Options): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions services/proxy-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class ProxyService implements IProxyService {

constructor(private $credentialsService: ICredentialsService,
private $fs: IFileSystem,
private $options: ICommonOptions,
private $settingsService: ISettingsService,
private $staticConfig: Config.IStaticConfig) {
this.proxyCacheFilePath = path.join(this.$options.profileDir, Proxy.CACHE_FILE_NAME);
this.proxyCacheFilePath = path.join(this.$settingsService.getProfileDir(), Proxy.CACHE_FILE_NAME);
this.credentialsKey = `${this.$staticConfig.CLIENT_NAME}_PROXY`;
}

Expand Down
26 changes: 23 additions & 3 deletions services/settings-service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import { exported } from "../decorators";
import * as path from "path";
import * as osenv from "osenv";

export class SettingsService implements ISettingsService {
constructor(private $staticConfig: Config.IStaticConfig) { }
private _profileDir: string;

constructor(private $staticConfig: Config.IStaticConfig,
private $hostInfo: IHostInfo) {
this._profileDir = this.getDefaultProfileDir();
}

@exported("settingsService")
setSettings(settings: IConfigurationSettings): void {
if (settings.userAgentName) {
public setSettings(settings: IConfigurationSettings): void {
if (settings && settings.userAgentName) {
this.$staticConfig.USER_AGENT_NAME = settings.userAgentName;
}

if (settings && settings.profileDir) {
this._profileDir = path.resolve(settings.profileDir);
}
}

public getProfileDir(): string {
return this._profileDir;
}

private getDefaultProfileDir(): string {
const defaultProfileDirLocation = this.$hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local", "share");
return path.join(defaultProfileDirLocation, this.$staticConfig.PROFILE_DIR_NAME);
}
}

Expand Down
1 change: 1 addition & 0 deletions static-config-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export abstract class StaticConfigBase implements Config.IStaticConfig {
public version: string = null;
public pathToPackageJson: string;
private _userAgent: string = null;
public abstract PROFILE_DIR_NAME: string;

public get USER_AGENT_NAME(): string {
if (!this._userAgent) {
Expand Down
36 changes: 27 additions & 9 deletions test/unit-tests/common-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function createTestInjector(): IInjector {
CLIENT_NAME: ""
});
testInjector.register("hostInfo", {});
testInjector.register("settingsService", {
setSettings: (settings: IConfigurationSettings): any => undefined,
getProfileDir: () => "profileDir"
});

return testInjector;
}
Expand Down Expand Up @@ -199,15 +203,15 @@ describe("common options", () => {
describe("when commandSpecificOptions are passed", () => {
it("breaks execution when commandSpecificOptions are passed and user tries to use invalid option", () => {
process.argv.push("--invalidOption");
const options = testInjector.resolve(OptionsBase, { options: knownOpts, defaultProfileDir: "1" });
const options = testInjector.resolve(OptionsBase, { options: knownOpts });
options.validateOptions({ test1: { type: OptionType.String } });
process.argv.pop();
assert.isTrue(isExecutionStopped);
});

it("does not break execution when commandSpecificOptions are passed and user tries to use option valid for CLI, but not for this command", () => {
process.argv.push("--json");
const options = testInjector.resolve(OptionsBase, { options: knownOpts, defaultProfileDir: "1" });
const options = testInjector.resolve(OptionsBase, { options: knownOpts });
options.validateOptions({ test1: { type: OptionType.String } });
process.argv.pop();
assert.isFalse(isExecutionStopped);
Expand All @@ -217,7 +221,14 @@ describe("common options", () => {
const expectedProfileDir = "TestDir";
process.argv.push("--profile-dir");
process.argv.push(expectedProfileDir);
const options = testInjector.resolve(OptionsBase, { options: knownOpts, defaultProfileDir: "1" });
const settingsService = testInjector.resolve<ISettingsService>("settingsService");
let valuePassedToSetSettings: string;
settingsService.setSettings = (settings: IConfigurationSettings): any => {
valuePassedToSetSettings = settings.profileDir;
};

settingsService.getProfileDir = () => valuePassedToSetSettings;
const options = testInjector.resolve(OptionsBase, { options: knownOpts });
options.validateOptions({ test1: { type: OptionType.String } });
assert.equal(options.profileDir, expectedProfileDir);
process.argv.pop();
Expand Down Expand Up @@ -276,12 +287,19 @@ describe("common options", () => {
});
});

function createOptionsWithProfileDir(profileDir: string): ICommonOptions {
function createOptionsWithProfileDir(defaultProfileDir?: string): ICommonOptions {
const testInjector = new Yok();
testInjector.register("errors", {});
testInjector.register("staticConfig", {});
let valuePassedToSetSettings: string;
testInjector.register("settingsService", {
setSettings: (settings: IConfigurationSettings): any => {
valuePassedToSetSettings = settings.profileDir;
},
getProfileDir: () => valuePassedToSetSettings || defaultProfileDir
});

const options = testInjector.resolve(OptionsBase, { options: {}, defaultProfileDir: profileDir });
const options = testInjector.resolve(OptionsBase, { options: {} });
return options;
}

Expand All @@ -292,7 +310,7 @@ describe("common options profile-dir tests", () => {
const expectedProfileDir = "TestDir";
process.argv.push("--profile-dir");
process.argv.push(expectedProfileDir);
const options = createOptionsWithProfileDir("");
const options = createOptionsWithProfileDir();
options.validateOptions();
process.argv.pop();
process.argv.pop();
Expand All @@ -301,7 +319,7 @@ describe("common options profile-dir tests", () => {

it("sets default profile-dir when it is not passed on command line", () => {
const profileDir = "TestDir";
const options = createOptionsWithProfileDir("TestDir");
const options = createOptionsWithProfileDir(profileDir);
options.validateOptions();
assert.equal(options.profileDir, profileDir);
});
Expand All @@ -310,7 +328,7 @@ describe("common options profile-dir tests", () => {
const expectedProfileDir = "TestDir";
process.argv.push("--profile-dir");
process.argv.push(expectedProfileDir);
const options = createOptionsWithProfileDir("TestDir123");
const options = createOptionsWithProfileDir();
options.validateOptions();
process.argv.pop();
process.argv.pop();
Expand All @@ -321,7 +339,7 @@ describe("common options profile-dir tests", () => {
const expectedProfileDir = "TestDir";
process.argv.push("--profileDir");
process.argv.push(expectedProfileDir);
const options = createOptionsWithProfileDir("");
const options = createOptionsWithProfileDir();
options.validateOptions();
process.argv.pop();
process.argv.pop();
Expand Down
Loading