Skip to content

Commit

Permalink
feat(project-data): add initialize from content
Browse files Browse the repository at this point in the history
  • Loading branch information
KristianDD committed Feb 22, 2018
1 parent 159c1e7 commit 09973b6
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $injector.require("options", "./options");
$injector.require("nativescript-cli", "./nativescript-cli");

$injector.require("projectData", "./project-data");
$injector.require("projectDataService", "./services/project-data-service");
$injector.requirePublic("projectDataService", "./services/project-data-service");
$injector.requirePublic("projectService", "./services/project-service");
$injector.require("androidProjectService", "./services/android-project-service");
$injector.require("iOSEntitlementsService", "./services/ios-entitlements-service");
Expand Down
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 1 files
+1 −1 file-system.ts
4 changes: 4 additions & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ interface IProjectData extends IProjectDir {
appDirectoryPath: string;
appResourcesDirectoryPath: string;
projectType: string;
nsConfig: any;
/**
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
* @param {string} projectDir Project root directory.
* @returns {void}
*/
initializeProjectData(projectDir?: string): void;
initializeProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): void;
getAppDirectoryPath(projectDir?: string): string;
getAppDirectoryRelativePath(): string;
getAppResourcesDirectoryPath(projectDir?: string): string;
getAppResourcesRelativeDirectoryPath(): string;
}

interface IProjectDataService {
Expand Down
115 changes: 75 additions & 40 deletions lib/project-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as constants from "./constants";
import * as path from "path";
import { parseJson } from "./common/helpers";
import { EOL } from "os";

interface IProjectType {
Expand Down Expand Up @@ -32,6 +33,7 @@ export class ProjectData implements IProjectData {
public projectFilePath: string;
public projectId: string;
public projectName: string;
public nsConfig: any;
get appDirectoryPath(): string {
return this.getAppDirectoryPath();
}
Expand All @@ -51,86 +53,119 @@ export class ProjectData implements IProjectData {

public initializeProjectData(projectDir?: string): void {
projectDir = projectDir || this.$projectHelper.projectDir;

// If no project found, projectDir should be null
if (projectDir) {
const projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
let data: any = null;
const projectFilePath = this.getProjectFilePath(projectDir);

if (this.$fs.exists(projectFilePath)) {
let fileContent: any = null;
try {
fileContent = this.$fs.readJson(projectFilePath);
data = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
} catch (err) {
this.$errors.failWithoutHelp(`The project file ${this.projectFilePath} is corrupted. ${EOL}` +
`Consider restoring an earlier version from your source control or backup.${EOL}` +
`Additional technical info: ${err.toString()}`);
}

if (data) {
this.projectDir = projectDir;
this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
this.projectFilePath = projectFilePath;
this.projectId = data.id;
this.dependencies = fileContent.dependencies;
this.devDependencies = fileContent.devDependencies;
this.projectType = this.getProjectType();

return;
}
let packageJsonContent: any = null;
packageJsonContent = this.$fs.readText(projectFilePath);
const nsConfigContent: any = this.getNsConfigContent(projectDir);

this.initializeProjectDataFromContent(packageJsonContent, nsConfigContent, projectDir);
}

return;
}

this.errorInvalidProject(projectDir);
}

public initializeProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): void {
projectDir = projectDir || this.$projectHelper.projectDir || "";
const projectFilePath = this.getProjectFilePath(projectDir);
// If no project found, projectDir should be null
let nsData: any = null;
let nsConfig: any = null;
let packageJsonData: any = null;

try {
packageJsonData = parseJson(packageJsonContent);
nsData = packageJsonData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
} catch (err) {
this.$errors.failWithoutHelp(`The project file ${this.projectFilePath} is corrupted. ${EOL}` +
`Consider restoring an earlier version from your source control or backup.${EOL}` +
`Additional technical info: ${err.toString()}`);
}

try {
nsConfig = nsconfigContent ? parseJson(nsconfigContent) : null;
} catch (err) {
this.$errors.failWithoutHelp(`The NativeScript configuration file ${constants.CONFIG_NS_FILE_NAME} is corrupted. ${EOL}` +
`Consider restoring an earlier version from your source control or backup.${EOL}` +
`Additional technical info: ${err.toString()}`);
}

if (nsData) {
this.projectDir = projectDir;
this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
this.projectFilePath = projectFilePath;
this.projectId = nsData.id;
this.dependencies = packageJsonData.dependencies;
this.devDependencies = packageJsonData.devDependencies;
this.projectType = this.getProjectType();
this.nsConfig = nsConfig;

return;
}

this.errorInvalidProject(projectDir);
}

private errorInvalidProject(projectDir: string): void {
const currentDir = path.resolve(".");
this.$logger.trace(`Unable to find project. projectDir: ${projectDir}, options.path: ${this.$options.path}, ${currentDir}`);

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

private getProjectFilePath(projectDir: string): string {
return path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
}

public getAppResourcesDirectoryPath(projectDir?: string): string {
if (!projectDir) {
projectDir = this.projectDir;
}

const configNS = this.getNsConfig(projectDir);
let absoluteAppResourcesDirPath: string;

if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
return path.resolve(projectDir, this.getAppResourcesRelativeDirectoryPath());
}

absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
public getAppResourcesRelativeDirectoryPath(): string {
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
return this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
}

return absoluteAppResourcesDirPath || path.join(this.getAppDirectoryPath(projectDir), constants.APP_RESOURCES_FOLDER_NAME);
return path.join(this.getAppDirectoryRelativePath(), constants.APP_RESOURCES_FOLDER_NAME);
}

public getAppDirectoryPath(projectDir?: string): string {
if (!projectDir) {
projectDir = this.projectDir;
}

const configNS = this.getNsConfig(projectDir);
let absoluteAppDirPath: string;

if (configNS && configNS[constants.CONFIG_NS_APP_ENTRY]) {
const appDirPath = configNS[constants.CONFIG_NS_APP_ENTRY];
return path.resolve(projectDir, this.getAppDirectoryRelativePath());
}

absoluteAppDirPath = path.resolve(projectDir, appDirPath);
public getAppDirectoryRelativePath(): string {
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY]) {
return this.nsConfig[constants.CONFIG_NS_APP_ENTRY];
}

return absoluteAppDirPath || path.join(projectDir, constants.APP_FOLDER_NAME);
return constants.APP_FOLDER_NAME;
}

private getNsConfig(projectDir: string): Object {
private getNsConfigContent(projectDir: string): string {
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);

if (!this.$fs.exists(configNSFilePath)) {
return null;
}

return this.$fs.readJson(configNSFilePath);
return this.$fs.readText(configNSFilePath);
}

private getProjectType(): string {
Expand Down
8 changes: 8 additions & 0 deletions lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "path";
import { ProjectData } from "../project-data";
import { exported } from "../common/decorators";

interface IProjectFileData {
projectData: any;
Expand Down Expand Up @@ -41,6 +42,13 @@ export class ProjectDataService implements IProjectDataService {
return projectDataInstance;
}

@exported("projectDataService")
public getProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): IProjectData {
const projectDataInstance = this.$injector.resolve<IProjectData>(ProjectData);
projectDataInstance.initializeProjectDataFromContent(packageJsonContent, nsconfigContent, projectDir);
return projectDataInstance;
}

private getValue(projectDir: string, propertyName: string): any {
const projectData = this.getProjectFileData(projectDir).projectData;

Expand Down
7 changes: 4 additions & 3 deletions test/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe("projectData", () => {

testInjector.register("fs", {
exists: () => true,
readJson: (): any => null
readJson: (): any => null,
readText: (): any => null
});

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

fs.readJson = () => ({
fs.readText = () => (JSON.stringify({
nativescript: {},
dependencies: dependencies,
devDependencies: devDependencies
});
}));

const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
projectHelper.projectDir = "projectDir";
Expand Down
10 changes: 10 additions & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export class ProjectDataStub implements IProjectData {
projectFilePath: string;
projectId: string;
dependencies: any;
nsConfig: any;
get appDirectoryPath(): string {
return this.getAppDirectoryPath();
}
Expand All @@ -264,6 +265,9 @@ export class ProjectDataStub implements IProjectData {
public initializeProjectData(projectDir?: string): void {
this.projectDir = this.projectDir || projectDir;
}
public initializeProjectDataFromContent(): void {
return;
}
public getAppResourcesDirectoryPath(projectDir?: string): string {
if (!projectDir) {
projectDir = this.projectDir;
Expand All @@ -284,13 +288,19 @@ export class ProjectDataStub implements IProjectData {

return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
}
public getAppResourcesRelativeDirectoryPath(): string {
return "";
}
public getAppDirectoryPath(projectDir?: string): string {
if (!projectDir) {
projectDir = this.projectDir;
}

return path.join(projectDir, "app") || "";
}
public getAppDirectoryRelativePath(): string {
return "";
}
}

export class PlatformProjectServiceStub extends EventEmitter implements IPlatformProjectService {
Expand Down

0 comments on commit 09973b6

Please sign in to comment.