Skip to content

Commit 6e027e8

Browse files
Add isvalid {N} project method (#2602)
We need a method in the public API to check if a directory contains valid NativeScript project.
1 parent fdbd80a commit 6e027e8

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

lib/definitions/project.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ interface IProjectService {
4444
* @returns {Promise<void>}
4545
*/
4646
createProject(projectSettings: IProjectSettings): Promise<void>;
47+
48+
/**
49+
* Checks if the specified project is valid NativeScript project.
50+
* @param {string} pathToProject Directory to check.
51+
* @returns {boolean} returns true if the project is valid NativeScript project.
52+
*/
53+
isValidNativeScriptProject(pathToProject?: string): boolean;
4754
}
4855

4956
interface IProjectData {

lib/services/project-service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import * as constants from "../constants";
22
import * as path from "path";
33
import * as shelljs from "shelljs";
4-
import { exportedPromise } from "../common/decorators";
4+
import { exportedPromise, exported } from "../common/decorators";
55

66
export class ProjectService implements IProjectService {
77

88
constructor(private $npm: INodePackageManager,
99
private $errors: IErrors,
1010
private $fs: IFileSystem,
1111
private $logger: ILogger,
12+
private $projectData: IProjectData,
1213
private $projectDataService: IProjectDataService,
1314
private $projectHelper: IProjectHelper,
1415
private $projectNameService: IProjectNameService,
@@ -66,6 +67,17 @@ export class ProjectService implements IProjectService {
6667
this.$logger.printMarkdown("Project `%s` was successfully created.", projectName);
6768
}
6869

70+
@exported("projectService")
71+
public isValidNativeScriptProject(pathToProject?: string): boolean {
72+
try {
73+
this.$projectData.initializeProjectData(pathToProject);
74+
} catch (e) {
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
6981
private getDataFromJson(templatePath: string): any {
7082
let templatePackageJsonPath = path.join(templatePath, constants.PACKAGE_JSON_FILE_NAME);
7183
if (this.$fs.exists(templatePackageJsonPath)) {

test/project-commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class ProjectServiceMock implements IProjectService {
1414
selectedTemplateName = projectOptions.template;
1515
isProjectCreated = true;
1616
}
17+
18+
isValidNativeScriptProject(pathToProject?: string): boolean {
19+
return true;
20+
}
1721
}
1822

1923
class ProjectNameValidatorMock implements IProjectNameValidator {

test/project-service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class ProjectIntegrationTest {
110110
this.testInjector.register("projectHelper", ProjectHelperLib.ProjectHelper);
111111
this.testInjector.register("projectTemplatesService", ProjectTemplatesService);
112112
this.testInjector.register("projectNameValidator", mockProjectNameValidator);
113+
this.testInjector.register("projectData", {});
113114

114115
this.testInjector.register("fs", FileSystem);
115116
this.testInjector.register("projectDataService", ProjectDataServiceLib.ProjectDataService);

0 commit comments

Comments
 (0)