Skip to content

Commit ef0f8c2

Browse files
committed
feat(project-data): add initialize from content
1 parent 159c1e7 commit ef0f8c2

File tree

6 files changed

+102
-44
lines changed

6 files changed

+102
-44
lines changed

lib/bootstrap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ $injector.require("options", "./options");
66
$injector.require("nativescript-cli", "./nativescript-cli");
77

88
$injector.require("projectData", "./project-data");
9-
$injector.require("projectDataService", "./services/project-data-service");
9+
$injector.requirePublic("projectDataService", "./services/project-data-service");
1010
$injector.requirePublic("projectService", "./services/project-service");
1111
$injector.require("androidProjectService", "./services/android-project-service");
1212
$injector.require("iOSEntitlementsService", "./services/ios-entitlements-service");

lib/definitions/project.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ interface IProjectData extends IProjectDir {
6363
appDirectoryPath: string;
6464
appResourcesDirectoryPath: string;
6565
projectType: string;
66+
nsConfig: any;
6667
/**
6768
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
6869
* @param {string} projectDir Project root directory.
6970
* @returns {void}
7071
*/
7172
initializeProjectData(projectDir?: string): void;
73+
initializeProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): void;
7274
getAppDirectoryPath(projectDir?: string): string;
75+
getAppDirectoryRelativePath(): string;
7376
getAppResourcesDirectoryPath(projectDir?: string): string;
77+
getAppResourcesRelativeDirectoryPath(): string;
7478
}
7579

7680
interface IProjectDataService {

lib/project-data.ts

+75-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as constants from "./constants";
22
import * as path from "path";
3+
import { parseJson } from "./common/helpers";
34
import { EOL } from "os";
45

56
interface IProjectType {
@@ -32,6 +33,7 @@ export class ProjectData implements IProjectData {
3233
public projectFilePath: string;
3334
public projectId: string;
3435
public projectName: string;
36+
public nsConfig: any;
3537
get appDirectoryPath(): string {
3638
return this.getAppDirectoryPath();
3739
}
@@ -51,86 +53,119 @@ export class ProjectData implements IProjectData {
5153

5254
public initializeProjectData(projectDir?: string): void {
5355
projectDir = projectDir || this.$projectHelper.projectDir;
56+
5457
// If no project found, projectDir should be null
5558
if (projectDir) {
56-
const projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
57-
let data: any = null;
59+
const projectFilePath = this.getProjectFilePath(projectDir);
5860

5961
if (this.$fs.exists(projectFilePath)) {
60-
let fileContent: any = null;
61-
try {
62-
fileContent = this.$fs.readJson(projectFilePath);
63-
data = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
64-
} catch (err) {
65-
this.$errors.failWithoutHelp(`The project file ${this.projectFilePath} is corrupted. ${EOL}` +
66-
`Consider restoring an earlier version from your source control or backup.${EOL}` +
67-
`Additional technical info: ${err.toString()}`);
68-
}
69-
70-
if (data) {
71-
this.projectDir = projectDir;
72-
this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
73-
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
74-
this.projectFilePath = projectFilePath;
75-
this.projectId = data.id;
76-
this.dependencies = fileContent.dependencies;
77-
this.devDependencies = fileContent.devDependencies;
78-
this.projectType = this.getProjectType();
79-
80-
return;
81-
}
62+
let packageJsonContent: any = null;
63+
packageJsonContent = this.$fs.readText(projectFilePath);
64+
const nsConfigContent: any = this.getNsConfigContent(projectDir);
65+
66+
this.initializeProjectDataFromContent(packageJsonContent, nsConfigContent, projectDir);
8267
}
68+
69+
return;
70+
}
71+
72+
this.errorInvalidProject(projectDir);
73+
}
74+
75+
public initializeProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): void {
76+
projectDir = projectDir || this.$projectHelper.projectDir || "";
77+
const projectFilePath = this.getProjectFilePath(projectDir);
78+
// If no project found, projectDir should be null
79+
let nsData: any = null;
80+
let nsConfig: any = null;
81+
let packageJsonData: any = null;
82+
83+
try {
84+
packageJsonData = parseJson(packageJsonContent);
85+
nsData = packageJsonData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
86+
} catch (err) {
87+
this.$errors.failWithoutHelp(`The project file ${this.projectFilePath} is corrupted. ${EOL}` +
88+
`Consider restoring an earlier version from your source control or backup.${EOL}` +
89+
`Additional technical info: ${err.toString()}`);
90+
}
91+
92+
try {
93+
nsConfig = nsconfigContent ? parseJson(nsconfigContent) : null;
94+
} catch (err) {
95+
this.$errors.failWithoutHelp(`The NativeScript configuration file ${constants.CONFIG_NS_FILE_NAME} is corrupted. ${EOL}` +
96+
`Consider restoring an earlier version from your source control or backup.${EOL}` +
97+
`Additional technical info: ${err.toString()}`);
8398
}
8499

100+
if (nsData) {
101+
this.projectDir = projectDir;
102+
this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
103+
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
104+
this.projectFilePath = projectFilePath;
105+
this.projectId = nsData.id;
106+
this.dependencies = packageJsonData.dependencies;
107+
this.devDependencies = packageJsonData.devDependencies;
108+
this.projectType = this.getProjectType();
109+
this.nsConfig = nsConfig;
110+
111+
return;
112+
}
113+
114+
this.errorInvalidProject(projectDir);
115+
}
116+
117+
private errorInvalidProject(projectDir: string) {
85118
const currentDir = path.resolve(".");
86119
this.$logger.trace(`Unable to find project. projectDir: ${projectDir}, options.path: ${this.$options.path}, ${currentDir}`);
87120

88121
// This is the case when no project file found
89122
this.$errors.fail("No project found at or above '%s' and neither was a --path specified.", projectDir || this.$options.path || currentDir);
90123
}
91124

125+
private getProjectFilePath(projectDir: string): string {
126+
return path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
127+
}
128+
92129
public getAppResourcesDirectoryPath(projectDir?: string): string {
93130
if (!projectDir) {
94131
projectDir = this.projectDir;
95132
}
96133

97-
const configNS = this.getNsConfig(projectDir);
98-
let absoluteAppResourcesDirPath: string;
99-
100-
if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
101-
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
134+
return path.resolve(projectDir, this.getAppResourcesRelativeDirectoryPath());
135+
}
102136

103-
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
137+
public getAppResourcesRelativeDirectoryPath(): string {
138+
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
139+
return this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
104140
}
105141

106-
return absoluteAppResourcesDirPath || path.join(this.getAppDirectoryPath(projectDir), constants.APP_RESOURCES_FOLDER_NAME);
142+
return path.join(this.getAppDirectoryRelativePath(), constants.APP_RESOURCES_FOLDER_NAME);
107143
}
108144

109145
public getAppDirectoryPath(projectDir?: string): string {
110146
if (!projectDir) {
111147
projectDir = this.projectDir;
112148
}
113149

114-
const configNS = this.getNsConfig(projectDir);
115-
let absoluteAppDirPath: string;
116-
117-
if (configNS && configNS[constants.CONFIG_NS_APP_ENTRY]) {
118-
const appDirPath = configNS[constants.CONFIG_NS_APP_ENTRY];
150+
return path.resolve(projectDir, this.getAppDirectoryRelativePath());
151+
}
119152

120-
absoluteAppDirPath = path.resolve(projectDir, appDirPath);
153+
public getAppDirectoryRelativePath(): string {
154+
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY]) {
155+
return this.nsConfig[constants.CONFIG_NS_APP_ENTRY];
121156
}
122157

123-
return absoluteAppDirPath || path.join(projectDir, constants.APP_FOLDER_NAME);
158+
return constants.APP_FOLDER_NAME;
124159
}
125160

126-
private getNsConfig(projectDir: string): Object {
161+
private getNsConfigContent(projectDir: string): string {
127162
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
128163

129164
if (!this.$fs.exists(configNSFilePath)) {
130165
return null;
131166
}
132167

133-
return this.$fs.readJson(configNSFilePath);
168+
return this.$fs.readText(configNSFilePath);
134169
}
135170

136171
private getProjectType(): string {

lib/services/project-data-service.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
22
import { ProjectData } from "../project-data";
3+
import { exported } from "../common/decorators";
34

45
interface IProjectFileData {
56
projectData: any;
@@ -41,6 +42,13 @@ export class ProjectDataService implements IProjectDataService {
4142
return projectDataInstance;
4243
}
4344

45+
@exported("projectDataService")
46+
public getProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): IProjectData {
47+
const projectDataInstance = this.$injector.resolve<IProjectData>(ProjectData);
48+
projectDataInstance.initializeProjectDataFromContent(packageJsonContent, nsconfigContent, projectDir);
49+
return projectDataInstance;
50+
}
51+
4452
private getValue(projectDir: string, propertyName: string): any {
4553
const projectData = this.getProjectFileData(projectDir).projectData;
4654

test/project-data.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ describe("projectData", () => {
1515

1616
testInjector.register("fs", {
1717
exists: () => true,
18-
readJson: (): any => null
18+
readJson: (): any => null,
19+
readText: (): any => null
1920
});
2021

2122
testInjector.register("staticConfig", {
@@ -41,11 +42,11 @@ describe("projectData", () => {
4142
const fs = testInjector.resolve("fs");
4243
fs.exists = (filePath: string) => filePath && path.basename(filePath) === "package.json";
4344

44-
fs.readJson = () => ({
45+
fs.readText = () => (JSON.stringify({
4546
nativescript: {},
4647
dependencies: dependencies,
4748
devDependencies: devDependencies
48-
});
49+
}));
4950

5051
const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
5152
projectHelper.projectDir = "projectDir";

test/stubs.ts

+10
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export class ProjectDataStub implements IProjectData {
253253
projectFilePath: string;
254254
projectId: string;
255255
dependencies: any;
256+
nsConfig: any;
256257
get appDirectoryPath(): string {
257258
return this.getAppDirectoryPath();
258259
}
@@ -264,6 +265,9 @@ export class ProjectDataStub implements IProjectData {
264265
public initializeProjectData(projectDir?: string): void {
265266
this.projectDir = this.projectDir || projectDir;
266267
}
268+
public initializeProjectDataFromContent(): void {
269+
return;
270+
}
267271
public getAppResourcesDirectoryPath(projectDir?: string): string {
268272
if (!projectDir) {
269273
projectDir = this.projectDir;
@@ -284,13 +288,19 @@ export class ProjectDataStub implements IProjectData {
284288

285289
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
286290
}
291+
public getAppResourcesRelativeDirectoryPath(): string {
292+
return "";
293+
}
287294
public getAppDirectoryPath(projectDir?: string): string {
288295
if (!projectDir) {
289296
projectDir = this.projectDir;
290297
}
291298

292299
return path.join(projectDir, "app") || "";
293300
}
301+
public getAppDirectoryRelativePath(): string {
302+
return "";
303+
}
294304
}
295305

296306
export class PlatformProjectServiceStub extends EventEmitter implements IPlatformProjectService {

0 commit comments

Comments
 (0)