diff --git a/src/commands/template/generate/ui-bundle/index.ts b/src/commands/template/generate/ui-bundle/index.ts index 17e1ccca..8f471b28 100644 --- a/src/commands/template/generate/ui-bundle/index.ts +++ b/src/commands/template/generate/ui-bundle/index.ts @@ -27,6 +27,11 @@ const messages = Messages.loadMessages('@salesforce/plugin-templates', 'ui-bundl export const UI_BUNDLES_DIR = 'uiBundles'; const GRAPHQLRC_FILENAME = '.graphqlrc.yml'; +// Templates that scaffold GraphQL tooling (a bundle-level .graphqlrc.yml plus a codegen config) and +// therefore need a project-root .graphqlrc.yml so the GraphQL LSP and lint tooling can auto-discover +// the schema across every ui-bundle in the project. +const TEMPLATES_WITH_GRAPHQL = new Set(['reactbasic', 'angularbasic']); + export default class UiBundleGenerate extends SfCommand { public static readonly summary = messages.getMessage('summary'); public static readonly description = messages.getMessage('description'); @@ -132,7 +137,7 @@ export default class UiBundleGenerate extends SfCommand { templates: getCustomTemplates(this.configAggregator), }); - if (flags.template === 'reactbasic') { + if (TEMPLATES_WITH_GRAPHQL.has(flags.template)) { const bundleSourcePath = path.join(result.outputDir, flags.name, GRAPHQLRC_FILENAME); const newPath = await UiBundleGenerate.createGraphqlrcAtProjectRoot(bundleSourcePath); if (newPath) { diff --git a/test/commands/template/generate/ui-bundle/index.nut.ts b/test/commands/template/generate/ui-bundle/index.nut.ts index 8885e7ec..59a39a80 100644 --- a/test/commands/template/generate/ui-bundle/index.nut.ts +++ b/test/commands/template/generate/ui-bundle/index.nut.ts @@ -153,17 +153,53 @@ describe('template generate ui-bundle:', () => { }); describe('Check UI bundle creation with angularbasic template', () => { - it('should create Angular UI bundle with all required files', () => { + afterEach(() => { + const rootGraphqlrc = path.join(projectDir, '.graphqlrc.yml'); + if (fs.existsSync(rootGraphqlrc)) { + fs.unlinkSync(rootGraphqlrc); + } + }); + + it('should create Angular UI bundle with all required files and create .graphqlrc.yml at the project root', () => { const outputDir = path.join(projectDir, 'force-app', 'main', 'default', UI_BUNDLES_DIR); - execCmd(`template generate ui-bundle --name MyAngularApp --template angularbasic --output-dir "${outputDir}"`, { - ensureExitCode: 0, - }); + const firstResult = execCmd( + `template generate ui-bundle --name MyAngularApp --template angularbasic --output-dir "${outputDir}"`, + { + ensureExitCode: 0, + } + ); assert.file([ path.join(outputDir, 'MyAngularApp', 'MyAngularApp.uibundle-meta.xml'), path.join(outputDir, 'MyAngularApp', 'src', 'index.html'), path.join(outputDir, 'MyAngularApp', 'ui-bundle.json'), path.join(outputDir, 'MyAngularApp', 'package.json'), ]); + + const rootGraphqlrc = path.join(projectDir, '.graphqlrc.yml'); + assert.file(rootGraphqlrc); + assert.fileContent(rootGraphqlrc, "schema: 'schema.graphql'"); + assert.fileContent(rootGraphqlrc, "documents: './**/src/**/*.{graphql,js,ts,jsx,tsx}'"); + expect(firstResult.shellOutput.stdout).to.contain('create .graphqlrc.yml'); + + const contentBefore = fs.readFileSync(rootGraphqlrc, 'utf8'); + const mtimeBefore = fs.statSync(rootGraphqlrc).mtimeMs; + + const secondResult = execCmd( + `template generate ui-bundle --name MyAngularApp2 --template angularbasic --output-dir "${outputDir}"`, + { + ensureExitCode: 0, + } + ); + assert.file([ + path.join(outputDir, 'MyAngularApp2', 'MyAngularApp2.uibundle-meta.xml'), + path.join(outputDir, 'MyAngularApp2', 'package.json'), + ]); + expect(secondResult.shellOutput.stdout).to.not.contain('create .graphqlrc.yml'); + + const contentAfter = fs.readFileSync(rootGraphqlrc, 'utf8'); + const mtimeAfter = fs.statSync(rootGraphqlrc).mtimeMs; + expect(contentAfter).to.equal(contentBefore); + expect(mtimeAfter).to.equal(mtimeBefore); }); });