Skip to content

Commit

Permalink
feat: default template version based on cli major (#5517)
Browse files Browse the repository at this point in the history
  • Loading branch information
janoshrubos authored Jun 1, 2021
1 parent b5e3c5f commit 2f1d876
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
35 changes: 29 additions & 6 deletions lib/services/project-templates-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "path";
import * as semver from "semver";
import * as constants from "../constants";
import { performanceLog } from "../common/decorators";
import {
Expand All @@ -9,6 +10,7 @@ import {
import {
IPackageInstallationManager,
INodePackageManager,
IStaticConfig,
} from "../declarations";
import {
IFileSystem,
Expand All @@ -26,7 +28,8 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
private $logger: ILogger,
private $packageInstallationManager: IPackageInstallationManager,
private $pacoteService: IPacoteService,
private $packageManager: INodePackageManager
private $packageManager: INodePackageManager,
private $staticConfig: IStaticConfig
) {}

@performanceLog()
Expand All @@ -45,11 +48,11 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
constants.RESERVED_TEMPLATE_NAMES[templateNameParts.name] ||
templateNameParts.name;

const version =
templateNameParts.version ||
(await this.$packageInstallationManager.getLatestCompatibleVersionSafe(
templateValue
));
const version = await this.getDesiredVersion(
templateValue,
templateNameParts.version
);

const fullTemplateName = await this.$packageManager.getPackageFullName({
name: templateValue,
version: version,
Expand Down Expand Up @@ -116,5 +119,25 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
);
}
}

private async getDesiredVersion(
templateName: string,
defaultVersion?: string
) {
if (defaultVersion) {
return defaultVersion;
}

try {
const cliMajorVersion = semver.parse(
semver.coerce(this.$staticConfig.version)
).major;
return `^${cliMajorVersion}.0.0`;
} catch (err) {
return this.$packageInstallationManager.getLatestCompatibleVersionSafe(
templateName
);
}
}
}
injector.register("projectTemplatesService", ProjectTemplatesService);
7 changes: 2 additions & 5 deletions test/project-templates-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { IAnalyticsService, IFileSystem } from "../lib/common/declarations";
import { IEventActionData } from "../lib/common/definitions/google-analytics";

const nativeScriptValidatedTemplatePath = "nsValidatedTemplatePath";
const compatibleTemplateVersion = "1.2.3";

function createTestInjector(
configuration: {
Expand All @@ -33,6 +32,7 @@ function createTestInjector(
exists: (pathToCheck: string) => false,
readJson: (pathToFile: string) => configuration.packageJsonContent || {},
});
injector.register("staticConfig", { version: "8.0.0" });

class NpmStub extends stubs.NodePackageManagerStub {
public async install(
Expand Down Expand Up @@ -70,9 +70,6 @@ function createTestInjector(

return Promise.resolve(nativeScriptValidatedTemplatePath);
}
async getLatestCompatibleVersionSafe(packageName: string): Promise<string> {
return compatibleTemplateVersion;
}
}

injector.register("packageInstallationManager", NpmInstallationManagerStub);
Expand Down Expand Up @@ -275,7 +272,7 @@ describe("project-templates-service", () => {
{
name: "is correct when scoped package name without version is passed",
templateName: "@nativescript/vue-template",
expectedVersion: compatibleTemplateVersion,
expectedVersion: "^8.0.0",
expectedTemplateName: "@nativescript/vue-template",
},
{
Expand Down

0 comments on commit 2f1d876

Please sign in to comment.