Skip to content

Commit 3ab8ffe

Browse files
Fixes detecting onprem SPFx projects' versions. Closes #1647
1 parent 117df8e commit 3ab8ffe

2 files changed

Lines changed: 89 additions & 3 deletions

File tree

src/o365/spfx/commands/project/base-project-command.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,19 @@ export abstract class BaseProjectCommand extends Command {
176176
if (fs.existsSync(yoRcPath)) {
177177
try {
178178
const yoRc: any = JSON.parse(fs.readFileSync(yoRcPath, 'utf-8'));
179-
if (yoRc && yoRc['@microsoft/generator-sharepoint'] &&
180-
yoRc['@microsoft/generator-sharepoint'].version) {
181-
return yoRc['@microsoft/generator-sharepoint'].version;
179+
if (yoRc && yoRc['@microsoft/generator-sharepoint']) {
180+
const version: string | undefined = yoRc['@microsoft/generator-sharepoint'].version;
181+
182+
if (version) {
183+
switch (yoRc['@microsoft/generator-sharepoint'].environment) {
184+
case 'onprem19':
185+
return '1.4.1';
186+
case 'onprem':
187+
return '1.1.0';
188+
default:
189+
return version;
190+
}
191+
}
182192
}
183193
}
184194
catch { }

src/o365/spfx/commands/project/project-upgrade.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,82 @@ describe(commands.PROJECT_UPGRADE, () => {
187187
});
188188
});
189189

190+
it('determines correct version from .yo-rc.json for an SP2019 project built using generator v1.10', () => {
191+
const originalExistsSync = fs.existsSync;
192+
sinon.stub(fs, 'existsSync').callsFake((path) => {
193+
if (path.toString().endsWith('.yo-rc.json')) {
194+
return true;
195+
}
196+
else {
197+
return originalExistsSync(path);
198+
}
199+
});
200+
const originalReadFileSync = fs.readFileSync;
201+
const yoRcJson = `{
202+
"@microsoft/generator-sharepoint": {
203+
"isCreatingSolution": true,
204+
"environment": "onprem19",
205+
"version": "1.10.0",
206+
"libraryName": "spfx-1100-sp-2019",
207+
"libraryId": "04b9054d-025f-4e1a-9a85-732c57213b2f",
208+
"packageManager": "npm",
209+
"componentType": "webpart"
210+
}
211+
}`;
212+
sinon.stub(fs, 'readFileSync').callsFake((path, options) => {
213+
if (path.toString().endsWith('.yo-rc.json')) {
214+
return yoRcJson;
215+
}
216+
else {
217+
return originalReadFileSync(path, options);
218+
}
219+
});
220+
const getProjectVersionSpy = sinon.spy(command as any, 'getProjectVersion');
221+
222+
cmdInstance.action = command.action();
223+
cmdInstance.action({ options: { toVersion: '1.4.1' } }, (err?: any) => {
224+
assert.strictEqual(getProjectVersionSpy.lastCall.returnValue, '1.4.1');
225+
});
226+
});
227+
228+
it('determines correct version from .yo-rc.json for an SP2016 project built using generator v1.10', () => {
229+
const originalExistsSync = fs.existsSync;
230+
sinon.stub(fs, 'existsSync').callsFake((path) => {
231+
if (path.toString().endsWith('.yo-rc.json')) {
232+
return true;
233+
}
234+
else {
235+
return originalExistsSync(path);
236+
}
237+
});
238+
const originalReadFileSync = fs.readFileSync;
239+
const yoRcJson = `{
240+
"@microsoft/generator-sharepoint": {
241+
"isCreatingSolution": true,
242+
"environment": "onprem",
243+
"version": "1.10.0",
244+
"libraryName": "spfx-1100-sp-2016",
245+
"libraryId": "300833cb-9264-4b53-8179-2eaf105c1d41",
246+
"packageManager": "npm",
247+
"componentType": "webpart"
248+
}
249+
}`;
250+
sinon.stub(fs, 'readFileSync').callsFake((path, options) => {
251+
if (path.toString().endsWith('.yo-rc.json')) {
252+
return yoRcJson;
253+
}
254+
else {
255+
return originalReadFileSync(path, options);
256+
}
257+
});
258+
const getProjectVersionSpy = sinon.spy(command as any, 'getProjectVersion');
259+
260+
cmdInstance.action = command.action();
261+
cmdInstance.action({ options: { toVersion: '1.1.0' } }, (err?: any) => {
262+
assert.strictEqual(getProjectVersionSpy.lastCall.returnValue, '1.1.0');
263+
});
264+
});
265+
190266
it('tries to determine the current version from package.json if .yo-rc.json doesn\'t exist', () => {
191267
const originalExistsSync = fs.existsSync;
192268
sinon.stub(fs, 'existsSync').callsFake((path) => {

0 commit comments

Comments
 (0)