-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Conversation
5107f84
to
82641ad
Compare
lib/commands/update.ts
Outdated
private backup(tmpDir: string): void { | ||
shelljs.rm("-fr", tmpDir); | ||
shelljs.mkdir(tmpDir); | ||
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir); |
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.
you can use PACKAGE_JSON_NAME
constant here
lib/commands/update.ts
Outdated
} catch (error) { | ||
this.$logger.error("Could not backup project folders!"); | ||
shelljs.rm("-fr", tmpDir); |
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.
It's better to use $fs.deleteDirectory
here in order to make use of the dependency injection and it'll be easier to mock
lib/commands/update.ts
Outdated
} | ||
|
||
private restoreBackup(tmpDir: string): void { | ||
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir); |
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.
you can use $fs.copyFile
here
lib/commands/update.ts
Outdated
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)); |
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.
Instead of using shelljs
directly it'd be better to introduce a $fs.deleteDirectory
method and use it everywhere instead of shelljs.rm
lib/commands/update.ts
Outdated
const folderToCopy = path.join(tmpDir, folder); | ||
|
||
if (this.$fs.exists(folderToCopy)) { | ||
shelljs.cp("-fr", folderToCopy, this.$projectData.projectDir); |
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.
Same as all other shelljs.cp
occurrences - use $fs.copyFile
instead
lib/commands/update.ts
Outdated
private backup(tmpDir: string): void { | ||
shelljs.rm("-fr", tmpDir); | ||
shelljs.mkdir(tmpDir); | ||
shelljs.cp(path.join(this.$projectData.projectDir, "package.json"), tmpDir); |
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.
Also you can use $fs.copyFile
lib/commands/update.ts
Outdated
for (const folder of this.folders) { | ||
const folderToCopy = path.join(this.$projectData.projectDir, folder); | ||
if (this.$fs.exists(folderToCopy)) { | ||
shelljs.cp("-rf", folderToCopy, tmpDir); |
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.
$fs.copyFile
lib/commands/update.ts
Outdated
for (const folder of folders) { | ||
shelljs.rm("-fr", folder); | ||
for (const folder of this.folders) { | ||
shelljs.rm("-rf", path.join(this.$projectData.projectDir, folder)); |
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.
Same as the other shelljs.rm
occurrences
lib/commands/update.ts
Outdated
} | ||
} | ||
|
||
this.restoreBackup(tmpDir); | ||
this.$logger.error("Could not update the project!"); | ||
} finally { | ||
shelljs.rm("-fr", tmpDir); |
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.
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)) { |
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.
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
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.
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.
f8aa52e
to
adca13b
Compare
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.
Splendid work
run ci |
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.
Please add test for invalid version passed to tns update
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.
Look at comments
lib/commands/update.ts
Outdated
@@ -15,49 +15,34 @@ export class UpdateCommand implements ICommand { | |||
this.$projectData.initializeProjectData(); | |||
} | |||
|
|||
static readonly folders: string[] = ["lib", "hooks", "platforms", "node_modules"]; |
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.
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); |
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.
better API to understand is this.backup(source, destination), or if you use only source, why do you need a temp folder?
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.
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 () => { |
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.
typo: falders
aee6ef7
to
6bf0f6d
Compare
Fixes #3202