Skip to content

Commit 78bb197

Browse files
committed
fix(extensions): fix error when global modules folder doesn't exist
closes #723 - default to process.cwd() when folder shown by global-modules doesn't exist
1 parent 1752cc8 commit 78bb197

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/utils/find-extensions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const fs = require('fs');
23
const path = require('path');
34
const findPlugins = require('find-plugins');
45
const createDebug = require('debug');
@@ -25,7 +26,7 @@ module.exports = function findExtensions() {
2526
keyword: 'ghost-cli-extension',
2627
configName: 'ghost-cli',
2728
include: localExtensions,
28-
dir: npmRoot,
29+
dir: fs.existsSync(npmRoot) ? npmRoot : process.cwd(),
2930
scanAllDirs: true,
3031
sort: true
3132
}).map((ext) => {

test/unit/utils/find-extensions-spec.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const localExtensions = [
1313
];
1414

1515
describe('Unit: Utils > find-extensions', function () {
16-
let findExtensions, findStub;
16+
let findExtensions, findStub, existsStub;
1717

1818
beforeEach(() => {
1919
findStub = sinon.stub().returns([
@@ -28,13 +28,17 @@ describe('Unit: Utils > find-extensions', function () {
2828
}
2929
]);
3030

31+
existsStub = sinon.stub();
32+
3133
findExtensions = proxyquire(modulePath, {
3234
'find-plugins': findStub,
33-
'global-modules': '.'
35+
'global-modules': '.',
36+
fs: {existsSync: existsStub}
3437
});
3538
});
3639

3740
it('calls find-plugins with proper args', function () {
41+
existsStub.returns(true);
3842
findExtensions();
3943
expect(findStub.calledOnce).to.be.true;
4044
const args = findStub.args[0][0];
@@ -53,7 +57,28 @@ describe('Unit: Utils > find-extensions', function () {
5357
expect(args).to.deep.equal(expected);
5458
});
5559

60+
it('uses process.cwd() when global modules dir doesn\'t exist', function () {
61+
existsStub.returns(false);
62+
findExtensions();
63+
expect(findStub.calledOnce).to.be.true;
64+
const args = findStub.args[0][0];
65+
66+
const expected = {
67+
keyword: 'ghost-cli-extension',
68+
configName: 'ghost-cli',
69+
scanAllDirs: true,
70+
dir: process.cwd(),
71+
sort: true
72+
};
73+
74+
const extensions = args.include.map((ext) => ext.split('extensions/')[1]);
75+
delete args.include;
76+
expect(extensions).to.deep.equal(localExtensions);
77+
expect(args).to.deep.equal(expected);
78+
});
79+
5680
it('generates proper extension names', function () {
81+
existsStub.returns(true);
5782
const names = findExtensions().map((ext) => ext.name);
5883
const expectedNames = ['test', 'rest', undefined];
5984
expect(names).to.deep.equal(expectedNames);

0 commit comments

Comments
 (0)