Skip to content

Commit 90ac914

Browse files
committed
feat: config manipulation
1 parent 0997b31 commit 90ac914

File tree

10 files changed

+632
-107
lines changed

10 files changed

+632
-107
lines changed

lib/commands/clean.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export class CleanCommand implements ICommand {
88
public allowedParameters: ICommandParameter[] = [];
99

1010
constructor(
11-
private $fs: IFileSystem,
12-
private $logger: ILogger,
13-
private $projectHelper: IProjectHelper
14-
) {}
11+
private $fs: IFileSystem,
12+
private $logger: ILogger,
13+
private $projectHelper: IProjectHelper
14+
) {}
1515

1616
public async execute(args: string[]): Promise<void> {
1717
this.$fs.deleteDirectory(
@@ -21,10 +21,16 @@ export class CleanCommand implements ICommand {
2121
path.join(this.$projectHelper.projectDir, constants.PLATFORMS_DIR_NAME)
2222
);
2323
this.$fs.deleteDirectory(
24-
path.join(this.$projectHelper.projectDir, constants.NODE_MODULES_FOLDER_NAME)
24+
path.join(
25+
this.$projectHelper.projectDir,
26+
constants.NODE_MODULES_FOLDER_NAME
27+
)
2528
);
2629
this.$fs.deleteFile(
27-
path.join(this.$projectHelper.projectDir, constants.PACKAGE_LOCK_JSON_FILE_NAME)
30+
path.join(
31+
this.$projectHelper.projectDir,
32+
constants.PACKAGE_LOCK_JSON_FILE_NAME
33+
)
2834
);
2935

3036
this.$logger.info("Project successfully cleaned.");

lib/commands/update.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class UpdateCommand implements ICommand {
2727

2828
public async execute(args: string[]): Promise<void> {
2929
if (this.$options.markingMode) {
30+
// ns update --markingMode
3031
await this.$markingModeService.handleMarkingModeFullDeprecation({
3132
projectDir: this.$projectData.projectDir,
3233
forceSwitch: true,

lib/definitions/project.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
IDependencyInformation,
1717
ISpawnResult,
1818
} from "../common/declarations";
19+
import { SupportedConfigValues } from "../tools/config-manipulation/config-transformer";
1920

2021
interface IProjectName {
2122
/**
@@ -302,6 +303,12 @@ interface IProjectConfigService {
302303
* @param key the property key path
303304
*/
304305
getValue(key: string): any;
306+
/**
307+
* Set value for a given config key path
308+
* @param key the property key path
309+
* @param value
310+
*/
311+
setValue(key: string, value: SupportedConfigValues): any;
305312

306313
detectInfo(
307314
projectDir?: string

lib/project-data.ts

+53-43
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import { parseJson } from "./common/helpers";
55
import { EOL } from "os";
66
import { cache } from "./common/decorators";
77
import {
8-
IProjectData,
98
INsConfig,
109
IProjectConfigService,
10+
IProjectData,
1111
} from "./definitions/project";
1212
import {
1313
IAndroidResourcesMigrationService,
14-
IStaticConfig,
1514
IOptions,
15+
IStaticConfig,
1616
} from "./declarations";
1717
import {
18-
IStringDictionary,
19-
IFileSystem,
2018
IErrors,
19+
IFileSystem,
2120
IProjectHelper,
21+
IStringDictionary,
2222
} from "./common/declarations";
2323
import { injector } from "./common/yok";
2424
import { IInjector } from "./common/definitions/yok";
@@ -68,16 +68,19 @@ export class ProjectData implements IProjectData {
6868
public platformsDir: string;
6969
public projectFilePath: string;
7070
public projectIdentifiers: Mobile.IProjectIdentifier;
71+
7172
get projectId(): string {
7273
this.warnProjectId();
7374
return this.projectIdentifiers.ios;
7475
}
76+
7577
//just in case hook/extension modifies it.
7678
set projectId(identifier: string) {
7779
this.warnProjectId();
7880
this.projectIdentifiers.ios = identifier;
7981
this.projectIdentifiers.android = identifier;
8082
}
83+
8184
public projectName: string;
8285
public packageJsonData: any;
8386
public nsConfig: INsConfig;
@@ -137,16 +140,23 @@ export class ProjectData implements IProjectData {
137140
): void {
138141
projectDir = projectDir || this.$projectHelper.projectDir || "";
139142
const projectFilePath = this.getProjectFilePath(projectDir);
140-
// If no project found, projectDir should be null
141-
// handle migration cases
142-
let isMigrate = false;
143-
if (this.$options.argv && this.$options.argv._ && this.$options.argv._.length) {
144-
this.$logger.info('this.$options.argv._[0]:', this.$options.argv._[0]);
145-
isMigrate = this.$options.argv._[0] === 'migrate';
146-
}
147-
this.$logger.info('about to call readConfig...')
148-
this.$logger.info('isMigrate:', isMigrate);
149-
const nsConfig: INsConfig = isMigrate ? null : this.projectConfig.readConfig(projectDir);
143+
// If no project found, projectDir should be null
144+
// handle migration cases
145+
let isMigrate = false;
146+
if (
147+
this.$options.argv &&
148+
this.$options.argv._ &&
149+
this.$options.argv._.length
150+
) {
151+
this.$logger.debug(
152+
"the value of this.$options.argv._[0] is: " + this.$options.argv._[0]
153+
);
154+
isMigrate = this.$options.argv._[0] === "migrate";
155+
}
156+
this.$logger.debug(`'initializingProjectData, isMigrate is ${isMigrate}.`);
157+
const nsConfig: INsConfig = isMigrate
158+
? null
159+
: this.projectConfig.readConfig(projectDir);
150160
let packageJsonData = null;
151161

152162
try {
@@ -272,15 +282,14 @@ export class ProjectData implements IProjectData {
272282
public getAppDirectoryRelativePath(): string {
273283
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY]) {
274284
return this.nsConfig[constants.CONFIG_NS_APP_ENTRY];
275-
}
276-
277-
if (this.$fs.exists(path.resolve(this.projectDir, constants.SRC_DIR))) {
278-
return constants.SRC_DIR;
279-
} else {
280-
// legacy project setup often uses app folder
281-
return constants.APP_FOLDER_NAME;
282-
}
285+
}
283286

287+
if (this.$fs.exists(path.resolve(this.projectDir, constants.SRC_DIR))) {
288+
return constants.SRC_DIR;
289+
} else {
290+
// legacy project setup often uses app folder
291+
return constants.APP_FOLDER_NAME;
292+
}
284293
}
285294

286295
public getNsConfigRelativePath(): string {
@@ -305,27 +314,27 @@ export class ProjectData implements IProjectData {
305314
private initializeProjectIdentifiers(
306315
config: INsConfig
307316
): Mobile.IProjectIdentifier {
308-
if (config) {
309-
const identifier: Mobile.IProjectIdentifier = {
310-
ios: config.id,
311-
android: config.id,
312-
};
313-
314-
if (config.ios && config.ios.id) {
315-
identifier.ios = config.ios.id;
316-
}
317-
if (config.android && config.android.id) {
318-
identifier.android = config.android.id;
319-
}
320-
321-
return identifier;
322-
} else {
323-
// when migrating projects this can be ignored
324-
return {
325-
ios: '',
326-
android: ''
327-
};
328-
}
317+
if (config) {
318+
const identifier: Mobile.IProjectIdentifier = {
319+
ios: config.id,
320+
android: config.id,
321+
};
322+
323+
if (config.ios && config.ios.id) {
324+
identifier.ios = config.ios.id;
325+
}
326+
if (config.android && config.android.id) {
327+
identifier.android = config.android.id;
328+
}
329+
330+
return identifier;
331+
} else {
332+
// when migrating projects this can be ignored
333+
return {
334+
ios: "",
335+
android: "",
336+
};
337+
}
329338
}
330339

331340
private getProjectType(): string {
@@ -360,4 +369,5 @@ export class ProjectData implements IProjectData {
360369
);
361370
}
362371
}
372+
363373
injector.register("projectData", ProjectData, true);

lib/services/marking-mode-service.ts

+10-47
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import * as helpers from "../common/helpers";
2-
import * as path from "path";
32
import { EOL } from "os";
4-
import { PACKAGE_JSON_FILE_NAME, LoggerConfigData } from "../constants";
5-
import { IProjectDataService } from "../definitions/project";
6-
import { IFileSystem } from "../common/declarations";
3+
import { LoggerConfigData } from "../constants";
4+
import { IProjectConfigService } from "../definitions/project";
75
import { injector } from "../common/yok";
86

97
const enum MarkingMode {
@@ -17,47 +15,23 @@ const MARKING_MODE_NONE_CONFIRM_MSG = `Do you want to switch your app to the rec
1715
More info about the reasons for this change can be found in the link below:
1816
https://www.nativescript.org/blog/markingmode-none-is-official-boost-android-performance-while-avoiding-memory-issues`;
1917

20-
// TODO: update to work with new configs!
2118
export class MarkingModeService implements IMarkingModeService {
2219
constructor(
23-
private $fs: IFileSystem,
2420
private $logger: ILogger,
25-
private $projectDataService: IProjectDataService,
21+
private $projectConfigService: IProjectConfigService,
2622
private $prompter: IPrompter
2723
) {}
2824

2925
public async handleMarkingModeFullDeprecation(
3026
options: IMarkingModeFullDeprecationOptions
3127
): Promise<void> {
32-
this.$logger.error(
33-
"handleMarkingModeFullDeprecation has not yet been updated to the new config handling!!!!"
28+
let markingModeValue = this.$projectConfigService.getValue(
29+
"android.markingMode"
3430
);
35-
return;
36-
37-
const { projectDir, skipWarnings, forceSwitch } = options;
38-
const projectData = this.$projectDataService.getProjectData(projectDir);
39-
const innerPackageJsonPath = path.join(
40-
projectData.getAppDirectoryPath(projectDir),
41-
PACKAGE_JSON_FILE_NAME
42-
);
43-
if (!this.$fs.exists(innerPackageJsonPath)) {
44-
return;
45-
}
46-
47-
const innerPackageJson = this.$fs.readJson(innerPackageJsonPath);
48-
let markingModeValue =
49-
(innerPackageJson &&
50-
innerPackageJson.android &&
51-
typeof innerPackageJson.android[MARKING_MODE_PROP] === "string" &&
52-
innerPackageJson.android[MARKING_MODE_PROP]) ||
53-
"";
31+
const { skipWarnings, forceSwitch } = options;
5432

5533
if (forceSwitch) {
56-
this.setMarkingMode(
57-
innerPackageJsonPath,
58-
innerPackageJson,
59-
MarkingMode.None
60-
);
34+
this.setMarkingMode(MarkingMode.None);
6135
return;
6236
}
6337

@@ -73,27 +47,16 @@ __Improve your app by switching to "${MARKING_MODE_PROP}:${MarkingMode.None}".__
7347
);
7448

7549
markingModeValue = hasSwitched ? MarkingMode.None : MarkingMode.Full;
76-
this.setMarkingMode(
77-
innerPackageJsonPath,
78-
innerPackageJson,
79-
markingModeValue
80-
);
50+
this.setMarkingMode(markingModeValue);
8151
}
8252

8353
if (!skipWarnings && markingModeValue.toLowerCase() !== MarkingMode.None) {
8454
this.showMarkingModeFullWarning();
8555
}
8656
}
8757

88-
private setMarkingMode(
89-
packagePath: string,
90-
packageValue: any,
91-
newMode: string
92-
) {
93-
packageValue = packageValue || {};
94-
packageValue.android = packageValue.android || {};
95-
packageValue.android[MARKING_MODE_PROP] = newMode;
96-
this.$fs.writeJson(packagePath, packageValue);
58+
private setMarkingMode(newMode: string) {
59+
this.$projectConfigService.setValue("android.markingMode", newMode);
9760
}
9861

9962
private showMarkingModeFullWarning() {

lib/services/project-config-service.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ import * as path from "path";
77
import * as _ from "lodash";
88
import * as ts from "typescript";
99
import { IFileSystem, IProjectHelper } from "../common/declarations";
10-
import { injector } from "../common/yok";
1110
import { INsConfig, IProjectConfigService } from "../definitions/project";
1211
import { IInjector } from "../common/definitions/yok";
12+
import {
13+
ConfigTransformer,
14+
IConfigTransformer,
15+
SupportedConfigValues,
16+
} from "../tools/config-manipulation/config-transformer";
1317
import { IBasePluginData } from "../definitions/plugins";
1418
import semver = require("semver/preload");
19+
import { injector } from "../common/yok";
20+
import { EOL } from "os";
1521

1622
export class ProjectConfigService implements IProjectConfigService {
1723
constructor(
@@ -119,6 +125,39 @@ export default {
119125
return _.get(this.readConfig(), key);
120126
}
121127

128+
public setValue(key: string, value: SupportedConfigValues) {
129+
const { hasTS, configJSFilePath, configTSFilePath } = this.detectInfo();
130+
const configFilePath = configTSFilePath || configJSFilePath;
131+
const configContent = this.$fs.readText(configFilePath);
132+
133+
try {
134+
const transformer: IConfigTransformer = new ConfigTransformer(
135+
configContent
136+
);
137+
const newContent = transformer.setValue(key, value);
138+
this.$fs.writeFile(configFilePath, newContent);
139+
} catch (error) {
140+
this.$logger.error(`Failed to update config.` + error);
141+
} finally {
142+
// verify config is updated correctly
143+
if (this.getValue(key) !== value) {
144+
this.$logger.error(
145+
`${EOL}Failed to update ${
146+
hasTS ? CONFIG_FILE_NAME_TS : CONFIG_FILE_NAME_JS
147+
}.${EOL}`
148+
);
149+
this.$logger.printMarkdown(
150+
`Please manually update \`${
151+
hasTS ? CONFIG_FILE_NAME_TS : CONFIG_FILE_NAME_JS
152+
}\` and set \`${key}\` to \`${value}\`.${EOL}`
153+
);
154+
155+
// restore original content
156+
this.$fs.writeFile(configFilePath, configContent);
157+
}
158+
}
159+
}
160+
122161
public writeDefaultConfig(projectDir: string, appId?: string) {
123162
const configTSFilePath = path.join(
124163
projectDir || this.projectHelper.projectDir,

0 commit comments

Comments
 (0)