Skip to content

Fix update command and add tests. #3227

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 4 commits into from
Nov 30, 2017
Merged

Conversation

KristianDD
Copy link
Contributor

Fixes #3202

@KristianDD KristianDD added the bug label Nov 22, 2017
@KristianDD KristianDD self-assigned this Nov 22, 2017
@KristianDD KristianDD force-pushed the kddimitrov/fix-update-command branch from 5107f84 to 82641ad Compare November 22, 2017 17:12
private backup(tmpDir: string): void {
shelljs.rm("-fr", tmpDir);
shelljs.mkdir(tmpDir);
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use PACKAGE_JSON_NAME constant here

} catch (error) {
this.$logger.error("Could not backup project folders!");
shelljs.rm("-fr", tmpDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use $fs.deleteDirectory here in order to make use of the dependency injection and it'll be easier to mock

}

private restoreBackup(tmpDir: string): void {
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use $fs.copyFile here

private restoreBackup(tmpDir: string): void {
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir);
for (const folder of this.folders) {
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using shelljs directly it'd be better to introduce a $fs.deleteDirectory method and use it everywhere instead of shelljs.rm

const folderToCopy = path.join(tmpDir, folder);

if (this.$fs.exists(folderToCopy)) {
shelljs.cp("-fr", folderToCopy, this.$projectData.projectDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as all other shelljs.cp occurrences - use $fs.copyFile instead

private backup(tmpDir: string): void {
shelljs.rm("-fr", tmpDir);
shelljs.mkdir(tmpDir);
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you can use $fs.copyFile

for (const folder of this.folders) {
const folderToCopy = path.join(this.$projectData.projectDir, folder);
if (this.$fs.exists(folderToCopy)) {
shelljs.cp("-rf", folderToCopy, tmpDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$fs.copyFile

for (const folder of folders) {
shelljs.rm("-fr", folder);
for (const folder of this.folders) {
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the other shelljs.rm occurrences

}
}

this.restoreBackup(tmpDir);
this.$logger.error("Could not update the project!");
} finally {
shelljs.rm("-fr", tmpDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note you can replace this shelljs.rm occurrence too


for (const platform of availablePlatforms) {
for (const platform of _.xor(platforms.installed, platforms.packagePlatforms)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic has changed a bit here - instead of iterating over all availablePlatforms we are basically iterating over packagePlatforms because platforms.packagePlatforms is platforms.installed concatenated with packagePlatforms and the xor will basically only return packagePlatforms.
Is this expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is intentional, because we are missing the following part:

if (platformVersion) {
 	packagePlatforms.push(platform);
 	this.$projectDataService.removeNSProperty(this.$projectData.projectDir, platformData.frameworkPackageName);
}

The if statement is moved to getPlatforms and we want to call removeNSProperty only for the ones that passed the condition.

The behavior should be the same.

@KristianDD KristianDD force-pushed the kddimitrov/fix-update-command branch from f8aa52e to adca13b Compare November 28, 2017 14:36
Copy link
Contributor

@Mitko-Kerezov Mitko-Kerezov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splendid work

@KristianDD
Copy link
Contributor Author

run ci

@Plamen5kov Plamen5kov self-requested a review November 29, 2017 14:24
Copy link
Contributor

@Plamen5kov Plamen5kov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test for invalid version passed to tns update

Copy link
Contributor

@Plamen5kov Plamen5kov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at comments

@@ -15,49 +15,34 @@ export class UpdateCommand implements ICommand {
this.$projectData.initializeProjectData();
}

static readonly folders: string[] = ["lib", "hooks", "platforms", "node_modules"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have constants required anyway, please add lib and hooks there and replace all strings with constants

shelljs.cp("-rf", folderToCopy, tmpDir);
}
}
this.backup(tmpDir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better API to understand is this.backup(source, destination), or if you use only source, why do you need a temp folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I send the parameter just to avoid repetition of:
const tmpDir = path.join(this.$projectData.projectDir, UpdateCommand.tempFolder);

test/update.ts Outdated
});
});

it("calls remove for all falders", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: falders

@KristianDD KristianDD force-pushed the kddimitrov/fix-update-command branch from aee6ef7 to 6bf0f6d Compare November 30, 2017 10:57
@KristianDD KristianDD merged commit b343e26 into master Nov 30, 2017
@KristianDD KristianDD deleted the kddimitrov/fix-update-command branch November 30, 2017 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants