diff --git a/package.json b/package.json index 6b55ffd4..6a37c709 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "dependencies": { "@salesforce/core": "^8.25.0", "@salesforce/sf-plugins-core": "^12", - "@salesforce/templates": "^65.4.3" + "@salesforce/templates": "^65.5.2" }, "devDependencies": { "@oclif/plugin-command-snapshot": "^5.3.8", diff --git a/src/commands/webapp/generate.ts b/src/commands/webapp/generate.ts index fa358e91..5d28bffb 100644 --- a/src/commands/webapp/generate.ts +++ b/src/commands/webapp/generate.ts @@ -6,14 +6,31 @@ */ import path from 'node:path'; +import { createRequire } from 'node:module'; import { Flags, SfCommand, Ux } from '@salesforce/sf-plugins-core'; import { CreateOutput, WebApplicationOptions, TemplateType } from '@salesforce/templates'; import { Messages, SfProject } from '@salesforce/core'; -import { getCustomTemplates, runGenerator } from '../../utils/templateCommand.js'; +import { runGenerator } from '../../utils/templateCommand.js'; Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); const messages = Messages.loadMessages('@salesforce/plugin-templates', 'webApplication'); +/** + * Resolves the path to the templates directory in the @salesforce/templates NPM package. + * The package name '@salesforce/templates' version 65.4.4 is used to fetch templates. + * + * @returns The absolute path to the templates directory + */ +function getNpmTemplatesPath(): string { + const require = createRequire(import.meta.url); + // Resolve the package.json to get the package root directory + const packagePath = require.resolve('@salesforce/templates/package.json'); + const packageDir = path.dirname(packagePath); + // Templates are stored in the 'lib/templates' directory within the package + const templatesPath = path.join(packageDir, 'lib', 'templates'); + return templatesPath; +} + export default class WebAppGenerate extends SfCommand { public static readonly summary = messages.getMessage('summary'); public static readonly description = messages.getMessage('description'); @@ -74,11 +91,13 @@ export default class WebAppGenerate extends SfCommand { apiversion: flags['api-version'], }; + const templatesPath = getNpmTemplatesPath(); + return runGenerator({ templateType: TemplateType.WebApplication, opts: flagsAsOptions, ux: new Ux({ jsonEnabled: this.jsonEnabled() }), - templates: getCustomTemplates(this.configAggregator), + templates: templatesPath, }); } } diff --git a/test/commands/webapp/generate.nut.ts b/test/commands/webapp/generate.nut.ts index cdb4eb3f..51c72b28 100644 --- a/test/commands/webapp/generate.nut.ts +++ b/test/commands/webapp/generate.nut.ts @@ -5,6 +5,8 @@ * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import path from 'node:path'; +import fs from 'node:fs'; +import { createRequire } from 'node:module'; import { expect } from 'chai'; import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit'; import { nls } from '@salesforce/templates/lib/i18n/index.js'; @@ -22,6 +24,24 @@ describe('Web application creation tests:', () => { await session?.clean(); }); + describe('Verify templates are loaded from NPM package', () => { + it('should use templates from @salesforce/templates NPM package', () => { + // Verify that templates are loaded from NPM package, not file-based + const require = createRequire(import.meta.url); + const packagePath = require.resolve('@salesforce/templates/package.json'); + const packageDir = path.dirname(packagePath); + const templatesPath = path.join(packageDir, 'lib', 'templates', 'webapplication'); + + // Verify the NPM package templates directory exists + expect(fs.existsSync(templatesPath), 'NPM package templates directory should exist').to.be.true; + + // Verify webapplication templates exist in NPM package + const templateFiles = fs.readdirSync(templatesPath); + expect(templateFiles.length, 'WebApplication templates should exist in NPM package').to.be.greaterThan(0); + expect(templateFiles, 'Should contain webappbasic template').to.include('webappbasic'); + }); + }); + describe('Check webapp creation with default template', () => { it('should create webapp using default template in webApplications directory', () => { const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); diff --git a/tsconfig.json b/tsconfig.json index f46de6d8..f4c97971 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,8 @@ "outDir": "lib", "rootDir": "src", "baseUrl": ".", - "skipLibCheck": true + "skipLibCheck": true, + "module": "node16" }, "include": ["./src/**/*.ts"] }