-
-
Notifications
You must be signed in to change notification settings - Fork 197
Plamen5kov/remove obsolete code #2461
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import * as constants from "../constants"; | ||
import * as osenv from "osenv"; | ||
import * as path from "path"; | ||
import * as shelljs from "shelljs"; | ||
|
||
|
@@ -19,72 +18,36 @@ export class ProjectService implements IProjectService { | |
if (!projectName) { | ||
this.$errors.fail("You must specify <App name> when creating a new project."); | ||
} | ||
|
||
projectName = await this.$projectNameService.ensureValidName(projectName, { force: this.$options.force }); | ||
|
||
let projectId = this.$options.appid || this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX); | ||
|
||
let projectDir = path.join(path.resolve(this.$options.path || "."), projectName); | ||
this.$fs.createDirectory(projectDir); | ||
if (this.$fs.exists(projectDir) && !this.$fs.isEmptyDir(projectDir)) { | ||
this.$errors.fail("Path already exists and is not empty %s", projectDir); | ||
} | ||
|
||
let projectId = this.$options.appid || this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX); | ||
this.createPackageJson(projectDir, projectId); | ||
|
||
let customAppPath = this.getCustomAppPath(); | ||
if (customAppPath) { | ||
customAppPath = path.resolve(customAppPath); | ||
if (!this.$fs.exists(customAppPath)) { | ||
this.$errors.failWithoutHelp(`The specified path "${customAppPath}" doesn't exist. Check that you specified the path correctly and try again.`); | ||
} | ||
|
||
let customAppContents = this.$fs.enumerateFilesInDirectorySync(customAppPath); | ||
if (customAppContents.length === 0) { | ||
this.$errors.failWithoutHelp(`The specified path "${customAppPath}" is empty directory.`); | ||
} | ||
this.$logger.trace(`Creating a new NativeScript project with name ${projectName} and id ${projectId} at location ${projectDir}`); | ||
if (!selectedTemplate) { | ||
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["default"]; | ||
} | ||
let templatePath = await this.$projectTemplatesService.prepareTemplate(selectedTemplate, projectDir); | ||
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 believe all of these operations should be in the try-catch block below. |
||
await this.extractTemplate(projectDir, templatePath); | ||
|
||
this.$logger.trace("Creating a new NativeScript project with name %s and id %s at location %s", projectName, projectId, projectDir); | ||
|
||
let projectAppDirectory = path.join(projectDir, constants.APP_FOLDER_NAME); | ||
let appPath: string = null; | ||
if (customAppPath) { | ||
this.$logger.trace("Using custom app from %s", customAppPath); | ||
|
||
// Make sure that the source app/ is not a direct ancestor of a target app/ | ||
let relativePathFromSourceToTarget = path.relative(customAppPath, projectAppDirectory); | ||
// path.relative returns second argument if the paths are located on different disks | ||
// so in this case we don't need to make the check for direct ancestor | ||
if (relativePathFromSourceToTarget !== projectAppDirectory) { | ||
let doesRelativePathGoUpAtLeastOneDir = relativePathFromSourceToTarget.split(path.sep)[0] === ".."; | ||
if (!doesRelativePathGoUpAtLeastOneDir) { | ||
this.$errors.fail("Project dir %s must not be created at/inside the template used to create the project %s.", projectDir, customAppPath); | ||
} | ||
} | ||
this.$logger.trace("Copying custom app into %s", projectAppDirectory); | ||
appPath = customAppPath; | ||
} else { | ||
let templatePath = await this.$projectTemplatesService.prepareTemplate(selectedTemplate, projectDir); | ||
this.$logger.trace(`Copying application from '${templatePath}' into '${projectAppDirectory}'.`); | ||
let templatePackageJson = this.$fs.readJson(path.join(templatePath, "package.json")); | ||
selectedTemplate = templatePackageJson.name; | ||
appPath = templatePath; | ||
} | ||
let packageName = constants.TNS_CORE_MODULES_NAME; | ||
await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true }); | ||
|
||
try { | ||
//TODO: plamen5kov: move copy of template and npm uninstall in prepareTemplate logic | ||
await this.createProjectCore(projectDir, appPath, projectId); | ||
let templatePackageJsonData = this.getDataFromJson(appPath); | ||
let templatePackageJsonData = this.getDataFromJson(templatePath); | ||
this.mergeProjectAndTemplateProperties(projectDir, templatePackageJsonData); //merging dependencies from template (dev && prod) | ||
this.removeMergedDependencies(projectDir, templatePackageJsonData); | ||
|
||
await this.$npm.install(projectDir, projectDir, { "ignore-scripts": this.$options.ignoreScripts }); | ||
selectedTemplate = selectedTemplate || ""; | ||
let templateName = (constants.RESERVED_TEMPLATE_NAMES[selectedTemplate.toLowerCase()] || selectedTemplate/*user template*/) || constants.RESERVED_TEMPLATE_NAMES["default"]; | ||
await this.$npm.uninstall(selectedTemplate, { save: true }, projectDir); | ||
|
||
// TODO: plamen5kov: remove later (put only so tests pass (need to fix tests)) | ||
this.$logger.trace(`Using NativeScript verified template: ${templateName} with version undefined.`); | ||
let templatePackageJson = this.$fs.readJson(path.join(templatePath, "package.json")); | ||
await this.$npm.uninstall(templatePackageJson.name, { save: true }, projectDir); | ||
} catch (err) { | ||
this.$fs.deleteDirectory(projectDir); | ||
throw err; | ||
|
@@ -103,6 +66,18 @@ export class ProjectService implements IProjectService { | |
return null; | ||
} | ||
|
||
private async extractTemplate(projectDir: string, realTemplatePath: string): Promise<void> { | ||
this.$fs.ensureDirectoryExists(projectDir); | ||
|
||
let appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME); | ||
this.$fs.createDirectory(appDestinationPath); | ||
|
||
this.$logger.trace(`Copying application from '${realTemplatePath}' into '${appDestinationPath}'.`); | ||
shelljs.cp('-R', path.join(realTemplatePath, "*"), appDestinationPath); | ||
|
||
this.$fs.createDirectory(path.join(projectDir, "platforms")); | ||
} | ||
|
||
private removeMergedDependencies(projectDir: string, templatePackageJsonData: any): void { | ||
let extractedTemplatePackageJsonPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.PACKAGE_JSON_FILE_NAME); | ||
for (let key in templatePackageJsonData) { | ||
|
@@ -129,6 +104,8 @@ export class ProjectService implements IProjectService { | |
} | ||
this.$logger.trace("New project package.json data: ", projectPackageJsonData); | ||
this.$fs.writeJson(projectPackageJsonPath, projectPackageJsonData); | ||
} else { | ||
this.$errors.failWithoutHelp(`Couldn't find package.json data in installed template`); | ||
} | ||
} | ||
|
||
|
@@ -147,42 +124,9 @@ export class ProjectService implements IProjectService { | |
return sortedDeps; | ||
} | ||
|
||
private async createProjectCore(projectDir: string, appSourcePath: string, projectId: string): Promise<void> { | ||
this.$fs.ensureDirectoryExists(projectDir); | ||
|
||
let appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME); | ||
this.$fs.createDirectory(appDestinationPath); | ||
|
||
shelljs.cp('-R', path.join(appSourcePath, "*"), appDestinationPath); | ||
|
||
this.$fs.createDirectory(path.join(projectDir, "platforms")); | ||
|
||
let tnsModulesVersion = this.$options.tnsModulesVersion; | ||
let packageName = constants.TNS_CORE_MODULES_NAME; | ||
if (tnsModulesVersion) { | ||
packageName = `${packageName}@${tnsModulesVersion}`; | ||
} | ||
await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true }); | ||
} | ||
|
||
private createPackageJson(projectDir: string, projectId: string): void { | ||
this.$projectDataService.initialize(projectDir); | ||
this.$projectDataService.setValue("id", projectId); | ||
} | ||
|
||
private getCustomAppPath(): string { | ||
let customAppPath = this.$options.copyFrom || this.$options.linkTo; | ||
if (customAppPath) { | ||
if (customAppPath.indexOf("http://") === 0) { | ||
this.$errors.fail("Only local paths for custom app are supported."); | ||
} | ||
|
||
if (customAppPath.substr(0, 1) === '~') { | ||
customAppPath = path.join(osenv.home(), customAppPath.substr(1)); | ||
} | ||
} | ||
|
||
return customAppPath; | ||
} | ||
} | ||
$injector.register("projectService", ProjectService); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this changed? When you mark method with
async
keyword, the returned value is always wrapped in Promise.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.
didn't know that, will revert it to previous state