Skip to content

Commit b882ac8

Browse files
committed
feat: auto format config after change with prettier + detect user prettier config
1 parent 95812b8 commit b882ac8

File tree

6 files changed

+199
-18
lines changed

6 files changed

+199
-18
lines changed

lib/controllers/migrate-controller.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,10 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
855855
}
856856

857857
// migrate data into nativescript.config.ts
858-
const success = this.$projectConfigService.setValue("", dataToMigrate);
858+
const success = await this.$projectConfigService.setValue(
859+
"",
860+
dataToMigrate
861+
);
859862

860863
if (!success) {
861864
this.$errors.fail(
@@ -869,7 +872,7 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
869872
rootPackageJsonData.nativescript &&
870873
rootPackageJsonData.nativescript.id
871874
) {
872-
this.$projectConfigService.setValue(
875+
await this.$projectConfigService.setValue(
873876
"id",
874877
rootPackageJsonData.nativescript.id
875878
);

lib/services/marking-mode-service.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class MarkingModeService implements IMarkingModeService {
2727
const { skipWarnings, forceSwitch } = options;
2828

2929
if (forceSwitch) {
30-
this.setMarkingMode(MarkingMode.None);
30+
await this.setMarkingMode(MarkingMode.None);
3131
return;
3232
}
3333

@@ -36,8 +36,8 @@ export class MarkingModeService implements IMarkingModeService {
3636
}
3737
}
3838

39-
private setMarkingMode(newMode: string) {
40-
this.$projectConfigService.setValue("android.markingMode", newMode);
39+
private async setMarkingMode(newMode: string) {
40+
await this.$projectConfigService.setValue("android.markingMode", newMode);
4141
}
4242

4343
private showMarkingModeFullWarning() {

lib/services/project-config-service.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import { IBasePluginData } from "../definitions/plugins";
1919
import { injector } from "../common/yok";
2020
import { EOL } from "os";
2121
import { IOptions } from "../declarations";
22+
import {
23+
format as prettierFormat,
24+
resolveConfig as resolvePrettierConfig,
25+
} from "prettier";
2226
import semver = require("semver/preload");
2327

2428
export class ProjectConfigService implements IProjectConfigService {
@@ -161,7 +165,10 @@ export default {
161165
return _.get(this.readConfig(), key);
162166
}
163167

164-
public setValue(key: string, value: SupportedConfigValues): boolean {
168+
public async setValue(
169+
key: string,
170+
value: SupportedConfigValues
171+
): Promise<boolean> {
165172
const { hasTS, configJSFilePath, configTSFilePath } = this.detectInfo();
166173
const configFilePath = configTSFilePath || configJSFilePath;
167174

@@ -188,7 +195,24 @@ export default {
188195
configContent
189196
);
190197
const newContent = transformer.setValue(key, value);
191-
this.$fs.writeFile(configFilePath, newContent);
198+
const prettierOptions = (await resolvePrettierConfig(
199+
this.projectHelper.projectDir,
200+
{ editorconfig: true }
201+
)) || {
202+
semi: false,
203+
singleQuote: true,
204+
};
205+
this.$logger.trace(
206+
"updating config, prettier options: ",
207+
prettierOptions
208+
);
209+
this.$fs.writeFile(
210+
configFilePath,
211+
prettierFormat(newContent, {
212+
...prettierOptions,
213+
parser: "typescript",
214+
})
215+
);
192216
} catch (error) {
193217
this.$logger.error(`Failed to update config.` + error);
194218
} finally {

package-lock.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"pbxproj-dom": "1.2.0",
7777
"plist": "3.0.1",
7878
"plist-merge-patch": "0.1.1",
79+
"prettier": "2.1.1",
7980
"proper-lockfile": "4.1.1",
8081
"proxy-lib": "0.4.0",
8182
"qr-image": "3.2.0",
@@ -116,6 +117,7 @@
116117
"@types/node": "14.0.27",
117118
"@types/ora": "3.2.0",
118119
"@types/pacote": "^11.1.0",
120+
"@types/prettier": "2.1.0",
119121
"@types/proper-lockfile": "4.1.1",
120122
"@types/qr-image": "3.2.3",
121123
"@types/request": "2.48.5",
@@ -144,7 +146,6 @@
144146
"latest-version": "5.1.0",
145147
"lint-staged": "^10.2.13",
146148
"mocha": "8.1.1",
147-
"prettier": "2.1.1",
148149
"should": "13.2.3",
149150
"sinon": "9.0.2",
150151
"source-map-support": "0.5.19",

0 commit comments

Comments
 (0)