Skip to content

Commit 6987ec2

Browse files
NathanWalkerrigor789
authored andcommitted
chore: config file consolidation
1 parent c71156f commit 6987ec2

13 files changed

+268
-21
lines changed

lib/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ injector.require("options", "./options");
99
injector.requirePublicClass("constants", "./constants-provider");
1010
injector.require("projectData", "./project-data");
1111
injector.requirePublic("projectDataService", "./services/project-data-service");
12+
injector.requirePublic("projectConfigService", "./services/project-config-service");
1213
injector.require("performanceService", "./services/performance-service");
1314
injector.requirePublic("projectService", "./services/project-service");
1415
injector.require("androidProjectService", "./services/android-project-service");

lib/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export const RESOURCES_DIR = "res";
5757
export const CONFIG_NS_FILE_NAME = "nsconfig.json";
5858
export const CONFIG_NS_APP_RESOURCES_ENTRY = "appResourcesPath";
5959
export const CONFIG_NS_APP_ENTRY = "appPath";
60+
export const CONFIG_FILE_NAME_DISPLAY = "nativescript.config.(js|ts)";
61+
export const CONFIG_FILE_NAME_JS = "nativescript.config.js";
62+
export const CONFIG_FILE_NAME_TS = "nativescript.config.ts";
6063
export const DEPENDENCIES_JSON_NAME = "dependencies.json";
6164
export const APK_EXTENSION_NAME = ".apk";
6265
export const AAB_EXTENSION_NAME = ".aab";

lib/controllers/prepare-controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { hook } from "../common/helpers";
33
import { performanceLog, cache } from "../common/decorators";
44
import { EventEmitter } from "events";
55
import * as path from "path";
6-
import { PREPARE_READY_EVENT_NAME, WEBPACK_COMPILATION_COMPLETE, PACKAGE_JSON_FILE_NAME, PLATFORMS_DIR_NAME, TrackActionNames, AnalyticsEventLabelDelimiter, CONFIG_NS_FILE_NAME } from "../constants";
6+
import { PREPARE_READY_EVENT_NAME, WEBPACK_COMPILATION_COMPLETE, PACKAGE_JSON_FILE_NAME, PLATFORMS_DIR_NAME, TrackActionNames, AnalyticsEventLabelDelimiter, CONFIG_FILE_NAME_JS, CONFIG_FILE_NAME_TS } from "../constants";
77
import { IProjectDataService, IProjectData } from "../definitions/project";
88
import { IPlatformController, INodeModulesDependenciesBuilder, IPlatformsDataService, IPlatformData } from "../definitions/platform";
99
import { IPluginsService } from "../definitions/plugins";
@@ -195,7 +195,8 @@ export class PrepareController extends EventEmitter {
195195

196196
const patterns = [
197197
path.join(projectData.projectDir, PACKAGE_JSON_FILE_NAME),
198-
path.join(projectData.projectDir, CONFIG_NS_FILE_NAME),
198+
path.join(projectData.projectDir, CONFIG_FILE_NAME_JS),
199+
path.join(projectData.projectDir, CONFIG_FILE_NAME_TS),
199200
path.join(projectData.getAppDirectoryPath(), PACKAGE_JSON_FILE_NAME),
200201
path.join(projectData.getAppResourcesRelativeDirectoryPath(), platformData.normalizedPlatformName),
201202
]

lib/definitions/project.d.ts

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,58 @@ interface IProjectService {
7676
isValidNativeScriptProject(pathToProject?: string): boolean;
7777
}
7878

79+
interface INsConfigPlaform {
80+
id?: string;
81+
}
82+
83+
interface INsConfigIOS extends INsConfigPlaform {
84+
discardUncaughtJsExceptions?: boolean;
85+
}
86+
87+
interface INsConfigAndroid extends INsConfigPlaform {
88+
v8Flags?: string;
89+
90+
codeCache?: boolean;
91+
92+
heapSnapshotScript?: string;
93+
94+
"snapshot.blob"?: string;
95+
96+
profilerOutputDir?: string;
97+
98+
gcThrottleTime?: number;
99+
100+
profiling?: string;
101+
102+
markingMode?: string;
103+
104+
handleTimeZoneChanges?: boolean;
105+
106+
maxLogcatObjectSize?: number;
107+
108+
forceLog?: boolean;
109+
110+
memoryCheckInterval?: number;
111+
112+
freeMemoryRatio?: number;
113+
114+
suppressCallJSMethodExceptions?: boolean;
115+
116+
discardUncaughtJsExceptions?: boolean;
117+
118+
enableLineBreakpoints?: boolean;
119+
}
120+
79121
interface INsConfig {
122+
id?: string;
80123
appPath?: string;
81124
appResourcesPath?: string;
82125
shared?: boolean;
83126
previewAppSchema?: string;
84127
overridePods?: string;
85-
webpackConfigPath?: string;
128+
webpackConfigPath?: string;
129+
ios?: INsConfigIOS;
130+
android?: INsConfigAndroid;
86131
}
87132

88133
interface IProjectData extends ICreateProjectData {
@@ -104,7 +149,7 @@ interface IProjectData extends ICreateProjectData {
104149
podfilePath: string;
105150
/**
106151
* Defines if the project is a code sharing one.
107-
* Value is true when project has nsconfig.json and it has `shared: true` in it.
152+
* Value is true when project has nativescript.config and it has `shared: true` in it.
108153
*/
109154
isShared: boolean;
110155

@@ -116,7 +161,7 @@ interface IProjectData extends ICreateProjectData {
116161
/**
117162
* Defines the path to the configuration file passed to webpack process.
118163
* By default this is the webpack.config.js at the root of the application.
119-
* The value can be changed by setting `webpackConfigPath` in nsconfig.json.
164+
* The value can be changed by setting `webpackConfigPath` in nativescript.config.
120165
*/
121166
webpackConfigPath: string;
122167

@@ -160,8 +205,8 @@ interface IProjectDataService {
160205
removeNSProperty(projectDir: string, propertyName: string): void;
161206

162207
/**
163-
* Removes a property from `nsconfig.json`.
164-
* @param {string} projectDir The project directory - the place where the `nsconfig.json` is located.
208+
* Removes a property from `nativescript.config`.
209+
* @param {string} projectDir The project directory - the place where the `nativescript.config` is located.
165210
* @param {string} propertyName The name of the property to be removed.
166211
* @returns {void}
167212
*/
@@ -225,6 +270,19 @@ interface IProjectDataService {
225270
getNSValueFromContent(jsonData: Object, propertyName: string): any;
226271
}
227272

273+
interface IProjectConfigService {
274+
/**
275+
* read the nativescript.config.(js|ts) file
276+
* @returns {INsConfig} the parsed config data
277+
*/
278+
readConfig(): INsConfig;
279+
/**
280+
* Get value for a given config key path
281+
* @param key the property key path
282+
*/
283+
getValue(key: string): any;
284+
}
285+
228286
interface IAssetItem {
229287
path: string;
230288
size: string;

lib/services/project-changes-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path";
2-
import { NativePlatformStatus, PACKAGE_JSON_FILE_NAME, APP_GRADLE_FILE_NAME, BUILD_XCCONFIG_FILE_NAME, PLATFORMS_DIR_NAME, CONFIG_NS_FILE_NAME } from "../constants";
2+
import { NativePlatformStatus, PACKAGE_JSON_FILE_NAME, APP_GRADLE_FILE_NAME, BUILD_XCCONFIG_FILE_NAME, PLATFORMS_DIR_NAME, CONFIG_FILE_NAME_JS, CONFIG_FILE_NAME_TS } from "../constants";
33
import { getHash, hook } from "../common/helpers";
44
import { INodeModulesDependenciesBuilder, IPlatformData } from "../definitions/platform";
55
import { IProjectData } from "../definitions/project";
@@ -78,7 +78,7 @@ export class ProjectChangesService implements IProjectChangesService {
7878
}
7979

8080
// If this causes too much rebuilds of the plugins or uncecessary builds for Android, move overrideCocoapods to prepareInfo.
81-
this._changesInfo.nsConfigChanged = this.filesChanged([path.join(projectData.projectDir, CONFIG_NS_FILE_NAME)]);
81+
this._changesInfo.nsConfigChanged = this.filesChanged([path.join(projectData.projectDir, CONFIG_FILE_NAME_JS), path.join(projectData.projectDir, CONFIG_FILE_NAME_TS)]);
8282
this._changesInfo.nativeChanged = this._changesInfo.nativeChanged || this._changesInfo.nsConfigChanged;
8383

8484
this.$logger.trace(`Set nativeChanged to ${this._changesInfo.nativeChanged}.`);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { CONFIG_FILE_NAME_JS, CONFIG_FILE_NAME_TS, CONFIG_FILE_NAME_DISPLAY } from "../constants";
2+
import * as path from "path";
3+
import * as _ from 'lodash';
4+
import * as ts from 'typescript';
5+
import { IFileSystem } from "../common/declarations";
6+
import { injector } from "../common/yok";
7+
import { IProjectData, INsConfig, IProjectConfigService } from "../definitions/project";
8+
9+
export class ProjectConfigService implements IProjectConfigService {
10+
private _config: INsConfig;
11+
12+
constructor(
13+
private $fs: IFileSystem,
14+
private $logger: ILogger,
15+
private $projectData: IProjectData,
16+
) {
17+
18+
}
19+
20+
public readConfig(): INsConfig {
21+
const configJSFilePath = path.join(this.$projectData.projectDir, CONFIG_FILE_NAME_JS);
22+
const configTSFilePath = path.join(this.$projectData.projectDir, CONFIG_FILE_NAME_TS);
23+
24+
const hasTS = this.$fs.exists(configTSFilePath);
25+
const hasJS = this.$fs.exists(configJSFilePath);
26+
27+
if (!hasTS && !hasJS) {
28+
throw new Error(`You do not appear to have a ${CONFIG_FILE_NAME_DISPLAY} file. Please install NativeScript 7+ "npm i -g nativescript". You can also try running "ns migrate" after you have the latest installed. Exiting for now.`);
29+
}
30+
31+
if (hasTS && hasJS) {
32+
this.$logger.warn(`You have both a ${CONFIG_FILE_NAME_JS} and ${CONFIG_FILE_NAME_TS} file. Defaulting to ${CONFIG_FILE_NAME_TS}.`);
33+
}
34+
35+
let config: INsConfig;
36+
37+
if (hasTS) {
38+
const rawSource = this.$fs.readText(configTSFilePath);
39+
const transpiledSource = ts.transpileModule(rawSource, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
40+
// console.log('transpiledSource.outputText:', transpiledSource.outputText)
41+
config = eval(transpiledSource.outputText);
42+
} else {
43+
const rawSource = this.$fs.readText(configJSFilePath);
44+
// console.log('rawSource:', rawSource)
45+
config = eval(rawSource);
46+
}
47+
48+
return config;
49+
}
50+
51+
public getValue(key: string): any {
52+
if (!this._config) {
53+
this._config = this.readConfig();
54+
}
55+
return _.get(this._config, key);
56+
}
57+
}
58+
59+
injector.register('projectConfigService', ProjectConfigService);

lib/services/project-data-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class ProjectDataService implements IProjectDataService {
186186
if (fstat.isDirectory()) {
187187
if (filePath === pathToProjectNodeModules) {
188188
// we do not want to get the files from node_modules directory of the project.
189-
// We'll get here only when you have nsconfig.json with appDirectoryPath set to "."
189+
// We'll get here only when you have nativescript.config with appDirectoryPath set to "."
190190
return false;
191191
}
192192

@@ -206,7 +206,7 @@ export class ProjectDataService implements IProjectDataService {
206206
}
207207

208208
private updateNsConfigValue(projectDir: string, updateObject?: INsConfig, propertiesToRemove?: string[]): void {
209-
const nsConfigPath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
209+
const nsConfigPath = path.join(projectDir, constants.CONFIG_FILE_NAME_JS);
210210
const currentNsConfig = this.getNsConfig(nsConfigPath);
211211
let newNsConfig = currentNsConfig;
212212
if (updateObject) {

lib/services/webpack/webpack-compiler-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as semver from "semver";
44
import * as _ from 'lodash';
55
import { EventEmitter } from "events";
66
import { performanceLog } from "../../common/decorators";
7-
import { WEBPACK_COMPILATION_COMPLETE, WEBPACK_PLUGIN_NAME, PackageManagers } from "../../constants";
7+
import { WEBPACK_COMPILATION_COMPLETE, WEBPACK_PLUGIN_NAME, PackageManagers, CONFIG_FILE_NAME_DISPLAY } from "../../constants";
88
import { IPackageManager, IPackageInstallationManager } from "../../declarations";
99
import { IPlatformData } from "../../definitions/platform";
1010
import { IProjectData } from "../../definitions/project";
@@ -175,7 +175,7 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
175175
@performanceLog()
176176
private async startWebpackProcess(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<child_process.ChildProcess> {
177177
if (!this.$fs.exists(projectData.webpackConfigPath)) {
178-
this.$errors.fail(`The webpack configuration file ${projectData.webpackConfigPath} does not exist. Ensure you have such file or set correct path in nsconfig.json`);
178+
this.$errors.fail(`The webpack configuration file ${projectData.webpackConfigPath} does not exist. Ensure you have such file or set correct path in ${CONFIG_FILE_NAME_DISPLAY}`);
179179
}
180180

181181
const envData = this.buildEnvData(platformData.platformNameLowerCase, projectData, prepareData);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"tabtab": "https://github.com/Icenium/node-tabtab/tarball/master",
8989
"tar": "6.0.2",
9090
"temp": "0.9.1",
91+
"typescript": "~3.9.0",
9192
"universal-analytics": "0.4.23",
9293
"uuid": "8.3.0",
9394
"winreg": "1.2.4",
@@ -142,8 +143,7 @@
142143
"sinon": "9.0.2",
143144
"source-map-support": "0.5.19",
144145
"tslint": "6.1.3",
145-
"tslint-microsoft-contrib": "6.2.0",
146-
"typescript": "~3.9.0"
146+
"tslint-microsoft-contrib": "6.2.0"
147147
},
148148
"license": "Apache-2.0",
149149
"engines": {

test/nativescript-cli-lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("nativescript-cli-lib", () => {
2525
"getAndroidAssetsStructure"
2626
],
2727
buildController: ["build"],
28-
constants: ["CONFIG_NS_APP_RESOURCES_ENTRY", "CONFIG_NS_APP_ENTRY", "CONFIG_NS_FILE_NAME", "LoggerLevel", "LoggerAppenders"],
28+
constants: ["CONFIG_NS_APP_RESOURCES_ENTRY", "CONFIG_NS_APP_ENTRY", "CONFIG_FILE_NAME_TS", "CONFIG_FILE_NAME_JS", "LoggerLevel", "LoggerAppenders"],
2929
deviceLogProvider: null,
3030
packageManager: ["install", "uninstall", "view", "search"],
3131
extensibilityService: ["loadExtensions", "loadExtension", "getInstalledExtensions", "installExtension", "uninstallExtension"],

0 commit comments

Comments
 (0)