Skip to content
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 21 additions & 2 deletions src/commands/webapp/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link

Choose a reason for hiding this comment

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

We should create a prerelease / canary build to verify this actually works as expected when installed.


export default class WebAppGenerate extends SfCommand<CreateOutput> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
Expand Down Expand Up @@ -74,11 +91,13 @@ export default class WebAppGenerate extends SfCommand<CreateOutput> {
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,
});
}
}
20 changes: 20 additions & 0 deletions test/commands/webapp/generate.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"outDir": "lib",
"rootDir": "src",
"baseUrl": ".",
"skipLibCheck": true
"skipLibCheck": true,
"module": "node16"
},
"include": ["./src/**/*.ts"]
}