-
-
Notifications
You must be signed in to change notification settings - Fork 197
Kddimitrov/nsconfig app folder #3356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1142680
21abc36
3eebb1e
28f2f95
1aba3c1
1122be6
487c80c
9c44985
2b5ef94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
@@ -32,6 +33,7 @@ export class ProjectData implements IProjectData { | |
public projectFilePath: string; | ||
public projectId: string; | ||
public projectName: string; | ||
public nsConfig: any; | ||
public appDirectoryPath: string; | ||
public appResourcesDirectoryPath: string; | ||
public dependencies: any; | ||
|
@@ -47,46 +49,130 @@ 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.appDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME); | ||
this.appResourcesDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME); | ||
this.projectId = data.id; | ||
this.dependencies = fileContent.dependencies; | ||
this.devDependencies = fileContent.devDependencies; | ||
this.projectType = this.getProjectType(); | ||
|
||
return; | ||
} | ||
const packageJsonContent = this.$fs.readText(projectFilePath); | ||
const nsConfigContent = this.getNsConfigContent(projectDir); | ||
|
||
this.initializeProjectDataFromContent(packageJsonContent, nsConfigContent, projectDir); | ||
} | ||
|
||
return; | ||
} | ||
|
||
this.errorInvalidProject(projectDir); | ||
} | ||
|
||
public initializeProjectDataFromContent(packageJsonContent: string, nsconfigContent: string, projectDir?: string): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sis0k0 is it ok to pass the content as string or you'll need a method that accepts the parsed json object (i.e. the parsed nsconfig.json) itself? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to pass the content as string and let the project data service do the parsing/validation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I have missed to make it optional as we don't use it in the codebase. |
||
projectDir = projectDir || this.$projectHelper.projectDir || ""; | ||
const projectFilePath = this.getProjectFilePath(projectDir); | ||
// If no project found, projectDir should be null | ||
let nsData = null; | ||
let nsConfig: INsConfig = null; | ||
let packageJsonData = 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 ? <INsConfig>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; | ||
this.appDirectoryPath = this.getAppDirectoryPath(); | ||
this.appResourcesDirectoryPath = this.getAppResourcesDirectoryPath(); | ||
|
||
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 { | ||
const appResourcesRelativePath = this.getAppResourcesRelativeDirectoryPath(); | ||
|
||
return this.resolveToProjectDir(appResourcesRelativePath, projectDir); | ||
} | ||
|
||
public getAppResourcesRelativeDirectoryPath(): string { | ||
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) { | ||
return this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY]; | ||
} | ||
|
||
return path.join(this.getAppDirectoryRelativePath(), constants.APP_RESOURCES_FOLDER_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can shorten this: return this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_RESOURCES_ENTRY]
|| path.join(this.getAppDirectoryRelativePath(), constants.APP_RESOURCES_FOLDER_NAME); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I consider mine more readable and structured. Will change it if you insist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, not a problem ;) |
||
} | ||
|
||
public getAppDirectoryPath(projectDir?: string): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic in this method is very similiar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
const appRelativePath = this.getAppDirectoryRelativePath(); | ||
|
||
return this.resolveToProjectDir(appRelativePath, projectDir); | ||
} | ||
|
||
public getAppDirectoryRelativePath(): string { | ||
if (this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY]) { | ||
return this.nsConfig[constants.CONFIG_NS_APP_ENTRY]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen in case the path in the nsconfig.json is not relative? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add validation for this as supporting paths not relative to the project will be very hard in cloud builds. Maybe we should also change the key inside |
||
} | ||
|
||
return constants.APP_FOLDER_NAME; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above - you can shorten the code: return this.nsConfig && this.nsConfig[constants.CONFIG_NS_APP_ENTRY] || constants.APP_FOLDER_NAME; |
||
} | ||
|
||
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.readText(configNSFilePath); | ||
} | ||
|
||
private resolveToProjectDir(pathToResolve: string, projectDir?: string): string { | ||
if (!projectDir) { | ||
projectDir = this.projectDir; | ||
} | ||
|
||
if (!projectDir) { | ||
return null; | ||
} | ||
|
||
return path.resolve(projectDir, pathToResolve); | ||
} | ||
|
||
private getProjectType(): string { | ||
let detectedProjectType = _.find(ProjectData.PROJECT_TYPES, (projectType) => projectType.isDefaultProjectType).type; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you add a new method to PublicApi, you have to:
NOTE: More information is available here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done