Skip to content

Commit e68dcc8

Browse files
committed
fix: order package.json keys the right way on project creation
1 parent f32642e commit e68dcc8

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

lib/constants.ts

+6
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,16 @@ export const TemplatesV2PackageJsonKeysToRemove: Array<String> = [
106106
"version",
107107
"displayName",
108108
"templateType",
109+
"description",
109110
"author",
111+
"license",
112+
"repository",
113+
"publishConfig",
114+
"files",
110115
"keywords",
111116
"homepage",
112117
"bugs",
118+
"nativescript",
113119
];
114120

115121
export class SaveOptions {

lib/services/project-service.ts

+30-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
IErrors,
2626
IFileSystem,
2727
IProjectHelper,
28-
IStringDictionary,
2928
IChildProcess,
3029
} from "../common/declarations";
3130
import * as _ from "lodash";
@@ -188,7 +187,7 @@ export class ProjectService implements IProjectService {
188187

189188
await this.extractTemplate(projectDir, templateData);
190189

191-
this.alterPackageJsonData(projectDir, appId);
190+
this.alterPackageJsonData(projectCreationSettings);
192191
this.$projectConfigService.writeDefaultConfig(projectDir, appId);
193192

194193
await this.ensureAppResourcesExist(projectDir);
@@ -256,30 +255,45 @@ export class ProjectService implements IProjectService {
256255
}
257256

258257
@performanceLog()
259-
private alterPackageJsonData(projectDir: string, appId: string): void {
258+
private alterPackageJsonData(
259+
projectCreationSettings: IProjectCreationSettings
260+
): void {
261+
const { projectDir, projectName } = projectCreationSettings;
260262
const projectFilePath = path.join(
261263
projectDir,
262264
this.$staticConfig.PROJECT_FILE_NAME
263265
);
264266

265267
let packageJsonData = this.$fs.readJson(projectFilePath);
266268

267-
packageJsonData = {
268-
...packageJsonData,
269-
...this.packageJsonDefaultData,
269+
// clean up keys from the template package.json that we don't care about.
270+
Object.keys(packageJsonData).forEach((key) => {
271+
if (
272+
key.startsWith("_") ||
273+
constants.TemplatesV2PackageJsonKeysToRemove.includes(key)
274+
) {
275+
delete packageJsonData[key];
276+
}
277+
});
278+
279+
// this is used to ensure the order of keys is consistent, the blanks are filled in from the template
280+
const packageJsonSchema = {
281+
name: projectName,
282+
main: "",
283+
version: "1.0.0",
284+
private: true,
285+
dependencies: {},
286+
devDependencies: {},
287+
// anythign else would go below
270288
};
271289

272-
this.$fs.writeJson(projectFilePath, packageJsonData);
273-
}
290+
packageJsonData = Object.assign(packageJsonSchema, packageJsonData);
274291

275-
private get packageJsonDefaultData(): IStringDictionary {
276-
return {
277-
private: "true",
278-
description: "NativeScript Application",
279-
license: "SEE LICENSE IN <your-license-filename>",
280-
readme: "NativeScript Application",
281-
repository: "<fill-your-repository-here>",
282-
};
292+
console.log({
293+
packageJsonData,
294+
});
295+
296+
this.$fs.writeJson(projectFilePath, packageJsonData);
283297
}
284298
}
285299

0 commit comments

Comments
 (0)