Skip to content

Commit

Permalink
Merge pull request #77 from cksource/t/1
Browse files Browse the repository at this point in the history
Feature: Introduced a smarter `cwd` resolver which scans directory tree up in order to find the `mgit.json` file. If the file won't be found, an exception will be thrown. Closes #1.
  • Loading branch information
Reinmar committed May 4, 2018
2 parents 1e7a353 + 53c72fc commit 751c10f
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/commands/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );
const chalk = require( 'chalk' );
const exec = require( '../utils/exec' );

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );
const exec = require( '../utils/exec' );

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/savehashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

const path = require( 'path' );
const path = require( 'upath' );
const updateJsonFile = require( '../utils/updatejsonfile' );

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );
const chalk = require( 'chalk' );

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

const path = require( 'path' );
const path = require( 'upath' );
const createForkPool = require( './utils/createforkpool' );
const logDisplay = require( './utils/displaylog' );
const getOptions = require( './utils/getoptions' );
Expand Down
20 changes: 18 additions & 2 deletions lib/utils/getcwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@

'use strict';

const fs = require( 'fs' );
const path = require( 'upath' );

/**
* Returns an absolute path to the directory with configuration file.
*
* It scans directory tree up for `mgit.json` file. If the file won't be found,
* an exception should be thrown.
*
* @returns {String}
*/
module.exports = function cwdResolver() {
// TODO: Try to find path to the configuration file based on cwd.
let cwd = process.cwd();

while ( !fs.existsSync( path.join( cwd, 'mgit.json' ) ) ) {
const parentCwd = path.resolve( cwd, '..' );

if ( cwd === parentCwd ) {
throw new Error( 'Cannot find the "mgit.json" file.' );
}

cwd = parentCwd;
}

return process.cwd();
return cwd;
};
2 changes: 1 addition & 1 deletion lib/utils/getoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );

/**
* @param {Object} Call options.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"generic-pool": "^3.4.1",
"meow": "^4.0.0",
"minimatch": "^3.0.4",
"shelljs": "^0.8.1"
"shelljs": "^0.8.1",
"upath": "^1.0.4"
},
"devDependencies": {
"@ckeditor/ckeditor5-dev-env": "^8.0.5",
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );
const sinon = require( 'sinon' );
const mockery = require( 'mockery' );
const expect = require( 'chai' ).expect;
Expand Down
4 changes: 1 addition & 3 deletions tests/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );
const sinon = require( 'sinon' );
const mockery = require( 'mockery' );
const expect = require( 'chai' ).expect;
Expand Down Expand Up @@ -86,8 +86,6 @@ describe( 'commands/exec', () => {
throw new Error( 'Supposed to be rejected.' );
},
response => {
expect( stubs.path.join.calledOnce ).to.equal( true );

const err = 'Package "test-package" is not available. Run "mgit bootstrap" in order to download the package.';
expect( response.logs.error[ 0 ] ).to.equal( err );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/savehashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const path = require( 'path' );
const path = require( 'upath' );
const sinon = require( 'sinon' );
const mockery = require( 'mockery' );
const expect = require( 'chai' ).expect;
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

const fs = require( 'fs' );
const path = require( 'path' );
const path = require( 'upath' );
const sinon = require( 'sinon' );
const mockery = require( 'mockery' );
const expect = require( 'chai' ).expect;
Expand Down
2 changes: 1 addition & 1 deletion tests/default-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const resolver = require( '../lib/default-resolver' );
const getOptions = require( '../lib/utils/getoptions' );
const expect = require( 'chai' ).expect;
const cwd = require( 'path' ).resolve( __dirname, 'fixtures', 'project-a' );
const cwd = require( 'upath' ).resolve( __dirname, 'fixtures', 'project-a' );

describe( 'default resolver()', () => {
describe( 'with default options', () => {
Expand Down
69 changes: 69 additions & 0 deletions tests/utils/getcwd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* jshint mocha:true */

'use strict';

const fs = require( 'fs' );
const getCwd = require( '../../lib/utils/getcwd' );
const expect = require( 'chai' ).expect;
const sinon = require( 'sinon' );

describe( 'utils', () => {
let sandbox;

beforeEach( () => {
sandbox = sinon.sandbox.create();
} );

afterEach( () => {
sandbox.restore();
} );
describe( 'getCwd()', () => {
it( 'returns "process.cwd()" value if the "mgit.json" has been found', () => {
sandbox.stub( process, 'cwd' ).returns( '/workspace/ckeditor/ckeditor5' );
sandbox.stub( fs, 'existsSync' ).returns( true );

expect( getCwd() ).to.equal( '/workspace/ckeditor/ckeditor5' );
} );

it( 'scans dir tree up in order to find "mgit.json" file', () => {
sandbox.stub( process, 'cwd' ).returns( '/workspace/ckeditor/ckeditor5/packages/ckeditor5-engine/node_modules/@ckeditor' );

const existsSync = sandbox.stub( fs, 'existsSync' );

// /workspace/ckeditor/ckeditor5/packages/ckeditor5-engine/node_modules/@ckeditor
existsSync.onCall( 0 ).returns( false );
// /workspace/ckeditor/ckeditor5/packages/ckeditor5-engine/node_modules
existsSync.onCall( 1 ).returns( false );
// /workspace/ckeditor/ckeditor5/packages/ckeditor5-engine
existsSync.onCall( 2 ).returns( false );
// /workspace/ckeditor/ckeditor5/packages
existsSync.onCall( 3 ).returns( false );
// /workspace/ckeditor/ckeditor5
existsSync.onCall( 4 ).returns( true );

expect( getCwd() ).to.equal( '/workspace/ckeditor/ckeditor5' );

expect( existsSync.getCall( 0 ).args[ 0 ] ).to.equal(
'/workspace/ckeditor/ckeditor5/packages/ckeditor5-engine/node_modules/@ckeditor/mgit.json'
);
expect( existsSync.getCall( 1 ).args[ 0 ] ).to.equal(
'/workspace/ckeditor/ckeditor5/packages/ckeditor5-engine/node_modules/mgit.json'
);
expect( existsSync.getCall( 2 ).args[ 0 ] ).to.equal( '/workspace/ckeditor/ckeditor5/packages/ckeditor5-engine/mgit.json' );
expect( existsSync.getCall( 3 ).args[ 0 ] ).to.equal( '/workspace/ckeditor/ckeditor5/packages/mgit.json' );
expect( existsSync.getCall( 4 ).args[ 0 ] ).to.equal( '/workspace/ckeditor/ckeditor5/mgit.json' );
} );

it( 'throws an error if the "mgit.json" cannot be found', () => {
sandbox.stub( process, 'cwd' ).returns( '/workspace/ckeditor' );
sandbox.stub( fs, 'existsSync' ).returns( false );

expect( () => getCwd() ).to.throw( Error, 'Cannot find the "mgit.json" file.' );
} );
} );
} );
2 changes: 1 addition & 1 deletion tests/utils/getoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

const getOptions = require( '../../lib/utils/getoptions' );
const path = require( 'path' );
const path = require( 'upath' );
const expect = require( 'chai' ).expect;
const cwd = path.resolve( __dirname, '..', 'fixtures', 'project-a' );

Expand Down

0 comments on commit 751c10f

Please sign in to comment.