Skip to content
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

feat: default template version based on cli major #5517

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy