Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 25c9f0f

Browse files
Use profileDir from settingsService
Currently CLI's configuration directory is used from `$options.profileDir`. When user passes `--profileDir <path>`, the `$options.profileDir` value is populated. In case user does not pass anything, a default value is set. All services that require configuration directory use the `$options.profileDir`. However, this causes several issues: - `$options` is intended for use only when CLI is used as a standalone command line. In case you are using it as a library, the `$options` object will not be populated. - Unable to test local installations of extensions when CLI is used as library - there's no way to set the custom profileDir when using CLI as a library. So the extensions are always loaded from the default location. In order to resolve these issues, move the logic for profileDir in `settingsService` and introduce a new method to get the profileDir. In order to ensure code is backwards compatible (i.e. extensions that use `$options.profileDir` should still work), modify `$options` to set the value of `profileDir` in `settingsService`. Whenever you want to test local extensions you can use: ```JavaScript const tns = require("nativescript"); tns.settingsService.setSettings({ profileDir: "my custom dir" }); Promise.all(tns.extensibilityService.loadExtensions()) .then((result) => { console.log("Loaded extensions:", result); // write your code here }); ``` Replace all places where `$options.profileDir` is used with `$settingsService.getProfileDir()`.
1 parent 06d4f68 commit 25c9f0f

File tree

13 files changed

+213
-26
lines changed

13 files changed

+213
-26
lines changed

appbuilder/proton-static-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ export class ProtonStaticConfig extends StaticConfigBase {
2020
public CLIENT_NAME = "Desktop Client - Universal";
2121

2222
public ANALYTICS_EXCEPTIONS_API_KEY: string = null;
23+
24+
public PROFILE_DIR_NAME: string = ".appbuilder-desktop-universal";
2325
}
2426
$injector.register("staticConfig", ProtonStaticConfig);

commands/post-install.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class PostInstallCommand implements ICommand {
33
private $staticConfig: Config.IStaticConfig,
44
private $commandsService: ICommandsService,
55
private $helpService: IHelpService,
6-
private $options: ICommonOptions,
6+
private $settingsService: ISettingsService,
77
private $doctorService: IDoctorService,
88
private $analyticsService: IAnalyticsService,
99
private $logger: ILogger) {
@@ -18,7 +18,7 @@ export class PostInstallCommand implements ICommand {
1818
// it is no longer accessible for the user initiating the installation
1919
// patch the owner here
2020
if (process.env.SUDO_USER) {
21-
await this.$fs.setCurrentUserAsOwner(this.$options.profileDir, process.env.SUDO_USER);
21+
await this.$fs.setCurrentUserAsOwner(this.$settingsService.getProfileDir(), process.env.SUDO_USER);
2222
}
2323
}
2424

commands/preuninstall.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ export class PreUninstallCommand implements ICommand {
66
public allowedParameters: ICommandParameter[] = [];
77

88
constructor(private $fs: IFileSystem,
9-
private $options: ICommonOptions) { }
9+
private $settingsService: ISettingsService) { }
1010

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

declarations.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ interface IConfigurationSettings {
7979
* This string will be used when constructing the UserAgent http header.
8080
* @type {string}
8181
*/
82-
userAgentName: string;
82+
userAgentName?: string;
83+
84+
/**
85+
* Describes the profile directory that will be used for various CLI settings, like user-settings.json file location, extensions, etc.
86+
* @type {string}
87+
*/
88+
profileDir?: string;
8389
}
8490

8591
/**
@@ -92,6 +98,12 @@ interface ISettingsService {
9298
* @returns {void}
9399
*/
94100
setSettings(settings: IConfigurationSettings): void;
101+
102+
/**
103+
* Returns currently used profile directory.
104+
* @returns {string}
105+
*/
106+
getProfileDir(): string;
95107
}
96108

97109
/**

definitions/config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare module Config {
2828
PATH_TO_BOOTSTRAP: string;
2929
QR_SIZE: number;
3030
INSTALLATION_SUCCESS_MESSAGE?: string;
31+
PROFILE_DIR_NAME: string
3132
}
3233

3334
interface IConfig {

options.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ export class OptionsBase {
1212
verbose: { type: OptionType.Boolean, alias: "v" },
1313
version: { type: OptionType.Boolean },
1414
help: { type: OptionType.Boolean, alias: "h" },
15-
profileDir: { type: OptionType.String, default: this.defaultProfileDir },
15+
profileDir: { type: OptionType.String },
1616
analyticsClient: { type: OptionType.String },
1717
path: { type: OptionType.String, alias: "p" },
1818
// This will parse all non-hyphenated values as strings.
1919
_: { type: OptionType.String }
2020
};
2121

2222
constructor(public options: IDictionary<IDashedOption>,
23-
public defaultProfileDir: string,
2423
private $errors: IErrors,
25-
private $staticConfig: Config.IStaticConfig) {
24+
private $staticConfig: Config.IStaticConfig,
25+
private $settingsService: ISettingsService) {
2626

2727
this.options = _.extend({}, this.commonOptions, this.options, this.globalOptions);
2828
this.setArgv();
@@ -180,17 +180,24 @@ export class OptionsBase {
180180
});
181181

182182
this.argv = yargs(process.argv.slice(2)).options(opts).argv;
183+
184+
// For backwards compatibility
185+
// Previously profileDir had a default option and calling `this.$options.profileDir` always returned valid result.
186+
// Now the profileDir should be used from $settingsService, but ensure the `this.$options.profileDir` returns the same value.
187+
this.$settingsService.setSettings({ profileDir: this.argv.profileDir});
188+
this.argv.profileDir = this.argv["profile-dir"] = this.$settingsService.getProfileDir();
189+
183190
this.adjustDashedOptions();
184191
}
185192

186193
private adjustDashedOptions(): void {
187194
_.each(this.optionNames, optionName => {
188195
Object.defineProperty(OptionsBase.prototype, optionName, {
189196
configurable: true,
190-
get: function () {
197+
get: () => {
191198
return this.getOptionValue(optionName);
192199
},
193-
set: function (value: any) {
200+
set: (value: any) => {
194201
this.argv[optionName] = value;
195202
}
196203
});

services/lockfile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class LockFile implements ILockFile {
66

77
@cache()
88
private get defaultLockFilePath(): string {
9-
return path.join(this.$options.profileDir, "lockfile.lock");
9+
return path.join(this.$settingsService.getProfileDir(), "lockfile.lock");
1010
}
1111

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

2424
constructor(private $fs: IFileSystem,
25-
private $options: ICommonOptions) {
25+
private $settingsService: ISettingsService) {
2626
}
2727

2828
public lock(lockFilePath?: string, lockFileOpts?: lockfile.Options): Promise<void> {

services/proxy-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export class ProxyService implements IProxyService {
88

99
constructor(private $credentialsService: ICredentialsService,
1010
private $fs: IFileSystem,
11-
private $options: ICommonOptions,
11+
private $settingsService: ISettingsService,
1212
private $staticConfig: Config.IStaticConfig) {
13-
this.proxyCacheFilePath = path.join(this.$options.profileDir, Proxy.CACHE_FILE_NAME);
13+
this.proxyCacheFilePath = path.join(this.$settingsService.getProfileDir(), Proxy.CACHE_FILE_NAME);
1414
this.credentialsKey = `${this.$staticConfig.CLIENT_NAME}_PROXY`;
1515
}
1616

services/settings-service.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
import { exported } from "../decorators";
2+
import * as path from "path";
3+
import * as osenv from "osenv";
24

35
export class SettingsService implements ISettingsService {
4-
constructor(private $staticConfig: Config.IStaticConfig) { }
6+
private _profileDir: string;
7+
8+
constructor(private $staticConfig: Config.IStaticConfig,
9+
private $hostInfo: IHostInfo) {
10+
this._profileDir = this.getDefaultProfileDir();
11+
}
512

613
@exported("settingsService")
7-
setSettings(settings: IConfigurationSettings): void {
8-
if (settings.userAgentName) {
14+
public setSettings(settings: IConfigurationSettings): void {
15+
if (settings && settings.userAgentName) {
916
this.$staticConfig.USER_AGENT_NAME = settings.userAgentName;
1017
}
18+
19+
if (settings && settings.profileDir) {
20+
this._profileDir = path.resolve(settings.profileDir);
21+
}
22+
}
23+
24+
public getProfileDir(): string {
25+
return this._profileDir;
26+
}
27+
28+
private getDefaultProfileDir(): string {
29+
const defaultProfileDirLocation = this.$hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local", "share");
30+
return path.join(defaultProfileDirLocation, this.$staticConfig.PROFILE_DIR_NAME);
1131
}
1232
}
1333

static-config-base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export abstract class StaticConfigBase implements Config.IStaticConfig {
1919
public version: string = null;
2020
public pathToPackageJson: string;
2121
private _userAgent: string = null;
22+
public abstract PROFILE_DIR_NAME: string;
2223

2324
public get USER_AGENT_NAME(): string {
2425
if (!this._userAgent) {

0 commit comments

Comments
 (0)