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

[sitecore-jss-cli] RAV fetchWith uses old GraphQl endpoint path in package.json #792

Merged
merged 5 commits into from
Aug 25, 2021
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
227 changes: 93 additions & 134 deletions packages/sitecore-jss-cli/src/create/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,56 @@ import { expect } from 'chai';
import {
applyNameToPackageJson,
applyHostNameToSitecoreConfig,
applyNameToSitecoreConfig,
applyNameReplacement,
} from './index';

describe('applyNameReplacement', () => {
const mockConfig = (appName: string, customPath?: string) => {
return `
<configuration>
<sitecore>
<sites>
<site patch:before="site[@name='website']"
inherits="website"
name="${appName}"
rootPath="${customPath ? customPath : '/sitecore/content/'}${appName}"
startItem="/home"
database="master" />
</sites>
<javaScriptServices>
<apps>
<app name="${appName}"
sitecorePath="${customPath ? customPath : '/sitecore/content/'}${appName}"
useLanguageSpecificLayout="true"
graphQLEndpoint="${customPath ? customPath : '/api/'}${appName}"
inherits="defaults"
/>
</apps>
</javaScriptServices>
<api>
<GraphQL>
<endpoints>
<${appName}GraphQLEndpoint url="${customPath ? customPath : '/api/'}${appName}">
<schema>
<content>
<templates>
<paths>
<templates>${
customPath ? customPath : '/sitecore/templates/Project/'
}${appName}</templates>
</paths>
</templates>
</content>
</schema>
</${appName}GraphQLEndpoint>
</endpoints>
</GraphQL>
</api>
</sitecore>
</configuration>
`;
};

it('should replace name', () => {
const result = applyNameReplacement('this is a test.', 'test', 'passing test');
expect(result).to.equal('this is a passing test.');
Expand Down Expand Up @@ -41,31 +86,62 @@ describe('applyNameReplacement', () => {
const result = applyNameReplacement('this is a te$+.', 'te$+', 'passing test');
expect(result).to.equal('this is a passing test.');
});
});

describe('applyNameToPackageJson', () => {
it('should apply name using defaults', () => {
const result = applyNameToPackageJson(
{
name: 'FooName',
config: { appName: 'FooAppName', sitecoreDistPath: 'na', graphQLEndpointPath: 'na' },
},
'bar'
it('should apply name using replaceName', () => {
const config = mockConfig('FooApp', '/somewhere/else/');
const result = applyNameReplacement(config, 'FooApp', 'Bar');
expect(result).to.match(/<site ((.|\n|\r)*?)name="Bar"/, 'site name');
expect(result).to.match(
/<site ((.|\n|\r)*?)rootPath="\/somewhere\/else\/Bar"/,
'site root path'
);
expect(result).to.match(/<app ((.|\n|\r)*?)name="Bar"/, 'app name');
expect(result).to.match(
/<app ((.|\n|\r)*?)sitecorePath="\/somewhere\/else\/Bar"/,
'app sitecorePath'
);
expect(result).to.match(
/<app ((.|\n|\r)*?)graphQLEndpoint="\/somewhere\/else\/Bar"/,
'app graphQLEndpoint'
);
expect(result).to.match(
/<BarGraphQLEndpoint ((.|\n|\r)*?)url="\/somewhere\/else\/Bar"/,
'GraphQL endpoint'
);
});

it('should not apply name using replaceName if no match', () => {
const config = mockConfig('FooApp', '/somewhere/else/');
const result = applyNameReplacement(config, 'BarApp', 'Bar');
expect(result).to.not.match(/<site ((.|\n|\r)*?)name="Bar"/, 'site name');
expect(result).to.not.match(
/<site ((.|\n|\r)*?)rootPath="\/somewhere\/else\/Bar"/,
'site root path'
);
expect(result).to.not.match(/<app ((.|\n|\r)*?)name="Bar"/, 'app name');
expect(result).to.not.match(
/<app ((.|\n|\r)*?)sitecorePath="\/somewhere\/else\/Bar"/,
'app sitecorePath'
);
expect(result).to.not.match(
/<app ((.|\n|\r)*?)graphQLEndpoint="\/somewhere\/else\/Bar"/,
'app graphQLEndpoint'
);
expect(result).to.not.match(
/<BarGraphQLEndpoint ((.|\n|\r)*?)url="\/somewhere\/else\/Bar"/,
'GraphQL endpoint'
);
expect(result.name).to.equal('bar');
expect(result.config.appName).to.equal('bar');
expect(result.config.sitecoreDistPath).to.equal('/dist/bar');
expect(result.config.graphQLEndpointPath).to.equal('/api/bar');
});
});

describe('applyNameToPackageJson', () => {
it('should apply name using replaceName', () => {
const result = applyNameToPackageJson(
{
name: 'FooName',
config: {
appName: 'FooAppName',
sitecoreDistPath: '/somewhere/dist/FooAppName',
graphQLEndpointPath: '/somewhere/api/FooAppName',
},
},
'bar',
Expand All @@ -74,7 +150,6 @@ describe('applyNameToPackageJson', () => {
expect(result.name).to.equal('bar');
expect(result.config.appName).to.equal('bar');
expect(result.config.sitecoreDistPath).to.equal('/somewhere/dist/bar');
expect(result.config.graphQLEndpointPath).to.equal('/somewhere/api/bar');
});

it('should not apply name using replaceName if no match', () => {
Expand Down Expand Up @@ -102,7 +177,8 @@ describe('applyNameToPackageJson', () => {
name: 'FooName',
config: { appName: 'FooAppName' },
},
'bar'
'bar',
'JssTestWeb'
);
expect(result.config.sitecoreDistPath).to.be.undefined;
expect(result.config.graphQLEndpointPath).to.be.undefined;
Expand Down Expand Up @@ -131,120 +207,3 @@ describe('applyHostNameToSitecoreConfig', () => {
expect(result).to.match(/<site ((.|\n|\r)*?)hostName="bar.localhost"/, 'site host name');
});
});

describe('applyNameToSitecoreConfig', () => {
const mockConfig = (appName: string, customPath?: string) => {
return `
<configuration>
<sitecore>
<sites>
<site patch:before="site[@name='website']"
inherits="website"
name="${appName}"
rootPath="${customPath ? customPath : '/sitecore/content/'}${appName}"
startItem="/home"
database="master" />
</sites>
<javaScriptServices>
<apps>
<app name="${appName}"
sitecorePath="${customPath ? customPath : '/sitecore/content/'}${appName}"
useLanguageSpecificLayout="true"
graphQLEndpoint="${customPath ? customPath : '/api/'}${appName}"
inherits="defaults"
/>
</apps>
</javaScriptServices>
<api>
<GraphQL>
<endpoints>
<${appName}GraphQLEndpoint url="${customPath ? customPath : '/api/'}${appName}">
<schema>
<content>
<templates>
<paths>
<templates>${
customPath ? customPath : '/sitecore/templates/Project/'
}${appName}</templates>
</paths>
</templates>
</content>
</schema>
</${appName}GraphQLEndpoint>
</endpoints>
</GraphQL>
</api>
</sitecore>
</configuration>
`;
};

it('should apply name using defaults', () => {
const config = mockConfig('FooApp', '/somewhere/else/');
const result = applyNameToSitecoreConfig(config, 'Bar');
expect(result).to.match(/<site ((.|\n|\r)*?)name="Bar"/, 'site name');
expect(result).to.match(
/<site ((.|\n|\r)*?)rootPath="\/sitecore\/content\/Bar"/,
'site root path'
);
expect(result).to.match(/<app ((.|\n|\r)*?)name="Bar"/, 'app name');
expect(result).to.match(
/<app ((.|\n|\r)*?)sitecorePath="\/sitecore\/content\/Bar"/,
'app sitecorePath'
);
expect(result).to.match(
/<app ((.|\n|\r)*?)graphQLEndpoint="\/api\/Bar"/,
'app graphQLEndpoint'
);
expect(result).to.match(
/<BarGraphQLEndpoint ((.|\n|\r)*?)url="\/api\/Bar"/,
'GraphQL endpoint'
);
});

it('should apply name using replaceName', () => {
const config = mockConfig('FooApp', '/somewhere/else/');
const result = applyNameToSitecoreConfig(config, 'Bar', 'FooApp');
expect(result).to.match(/<site ((.|\n|\r)*?)name="Bar"/, 'site name');
expect(result).to.match(
/<site ((.|\n|\r)*?)rootPath="\/somewhere\/else\/Bar"/,
'site root path'
);
expect(result).to.match(/<app ((.|\n|\r)*?)name="Bar"/, 'app name');
expect(result).to.match(
/<app ((.|\n|\r)*?)sitecorePath="\/somewhere\/else\/Bar"/,
'app sitecorePath'
);
expect(result).to.match(
/<app ((.|\n|\r)*?)graphQLEndpoint="\/somewhere\/else\/Bar"/,
'app graphQLEndpoint'
);
expect(result).to.match(
/<BarGraphQLEndpoint ((.|\n|\r)*?)url="\/somewhere\/else\/Bar"/,
'GraphQL endpoint'
);
});

it('should not apply name using replaceName if no match', () => {
const config = mockConfig('FooApp', '/somewhere/else/');
const result = applyNameToSitecoreConfig(config, 'Bar', 'BarApp');
expect(result).to.not.match(/<site ((.|\n|\r)*?)name="Bar"/, 'site name');
expect(result).to.not.match(
/<site ((.|\n|\r)*?)rootPath="\/somewhere\/else\/Bar"/,
'site root path'
);
expect(result).to.not.match(/<app ((.|\n|\r)*?)name="Bar"/, 'app name');
expect(result).to.not.match(
/<app ((.|\n|\r)*?)sitecorePath="\/somewhere\/else\/Bar"/,
'app sitecorePath'
);
expect(result).to.not.match(
/<app ((.|\n|\r)*?)graphQLEndpoint="\/somewhere\/else\/Bar"/,
'app graphQLEndpoint'
);
expect(result).to.not.match(
/<BarGraphQLEndpoint ((.|\n|\r)*?)url="\/somewhere\/else\/Bar"/,
'GraphQL endpoint'
);
});
});
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
77 changes: 11 additions & 66 deletions packages/sitecore-jss-cli/src/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,18 @@ export function applyNameReplacement(value: string, replaceName: string, withNam
/**
* @param {PackageJson} pkg package.json object
* @param {string} name App name
* @param {string} [replaceName] Optional token which will enable "name replacement mode". If omitted, default/conventional values are used.
* @param {string} replaceName Name value to replace
*/
export function applyNameToPackageJson(pkg: PackageJson, name: string, replaceName?: string) {
export function applyNameToPackageJson(pkg: PackageJson, name: string, replaceName: string) {
pkg.name = name; // root name will never match "replaceName", so always set directly
pkg.config.appName = replaceName
? applyNameReplacement(pkg.config.appName, replaceName, name)
: name;
pkg.config.appName = applyNameReplacement(pkg.config.appName, replaceName, name);

if (pkg.config.sitecoreDistPath) {
pkg.config.sitecoreDistPath = replaceName
? applyNameReplacement(pkg.config.sitecoreDistPath, replaceName, name)
: `/dist/${name}`;
}
if (pkg.config.graphQLEndpointPath) {
pkg.config.graphQLEndpointPath = replaceName
? applyNameReplacement(pkg.config.graphQLEndpointPath, replaceName, name)
: `/api/${name}`;
pkg.config.sitecoreDistPath = applyNameReplacement(
pkg.config.sitecoreDistPath,
replaceName,
name
);
}
return pkg;
}
Expand All @@ -65,67 +60,17 @@ export function applyHostNameToSitecoreConfig(configXml: string, hostName: strin
return configXml;
}

/**
* @param {string} configXml Sitecore configuration xml
* @param {string} name App name
* @param {string} [replaceName] Optional token which will enable "name replacement mode". If omitted, default/conventional values are used.
*/
export function applyNameToSitecoreConfig(configXml: string, name: string, replaceName?: string) {
if (replaceName) {
return applyNameReplacement(configXml, replaceName, name);
}

// replace site name
configXml = configXml.replace(/<site ((.|\n|\r)*?)name="[^"]+"/g, `<site $1name="${name}"`);

// replace root path
configXml = configXml.replace(
/<site ((.|\n|\r)*?)rootPath="[^"]+"/g,
`<site $1rootPath="/sitecore/content/${name}"`
);

// replace jss app name
configXml = configXml.replace(/<app ((.|\n|\r)*?)name="[^"]+"/g, `<app $1name="${name}"`);

// replace jss app sitecorePath
configXml = configXml.replace(
/<app ((.|\n|\r)*?)sitecorePath="[^"]+"/g,
`<app $1sitecorePath="/sitecore/content/${name}"`
);

// replace jss app graphQLEndpoint
configXml = configXml.replace(
/<app ((.|\n|\r)*?)graphQLEndpoint="[^"]+"/g,
`<app $1graphQLEndpoint="/api/${name}"`
);

// replace GraphQL url
configXml = configXml.replace(
/<([^ ]+)GraphQLEndpoint ((.|\n|\r)*?)url="[^"]+"/g,
`<${name}GraphQLEndpoint $2url="/api/${name}"`
);

configXml = configXml.replace(/<\/(.+)GraphQLEndpoint>/g, `</${name}GraphQLEndpoint>`);

// replace GraphQL templates path
configXml = configXml.replace(
/(<templates>\/sitecore\/templates\/Project\/)[^<]+(<\/templates>)/g,
`$1${name}$2`
);
return configXml;
}

/**
* @param {string} projectFolder Project folder
* @param {string} name App name
* @param {string} hostName App hostname
* @param {string} [replaceName] Optional token which will enable "name replacement mode" on project files. If omitted, default/conventional values are used.
* @param {string} replaceName Name value to replace
*/
export function applyNameToProject(
projectFolder: string,
name: string,
hostName: string,
replaceName?: string
replaceName: string
) {
// Apply name to package.json file
console.log(chalk.cyan(`Applying name ${name} to package.json...`));
Expand All @@ -145,7 +90,7 @@ export function applyNameToProject(
console.log(
chalk.cyan(`Applying name ${name} and hostName ${hostName} to ${sitecoreConfigPath}...`)
);
configXml = applyNameToSitecoreConfig(configXml, name, replaceName);
configXml = applyNameReplacement(configXml, replaceName, name);
configXml = applyHostNameToSitecoreConfig(configXml, hostName);

fs.unlinkSync(sitecoreConfigPath);
Expand Down
2 changes: 1 addition & 1 deletion samples/angular/jss-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { applyNameToProject } = require('@sitecore-jss/sitecore-jss-cli/dist/cjs/
module.exports = function createJssProject(argv, nextSteps) {
console.log(`Executing create script: ${__filename}...`);

applyNameToProject(__dirname, argv.name, argv.hostName);
applyNameToProject(__dirname, argv.name, argv.hostName, 'JssAngularWeb');

// Replace app name in Angular-specific locations
function replaceAngularAppNameInFile(filePath) {
Expand Down
Loading