Skip to content

Commit

Permalink
WIP #240 adding pacote for resolving package names
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb committed Apr 25, 2018
1 parent 1de7a6f commit fea13e6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 42 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ webgme.all.import = function(projectName, opts, callback) {
});
return deferred.promise;
})
.then(() => { // read in the webgme-cli config for the given project
projectName = opts.packageName || utils.getPackageName(projectName);
.then(() => utils.getPackageName(projectName))
.then(projectName => { // read in the webgme-cli config for the given project
projectName = opts.packageName || projectName;
projectName = projectName.toLowerCase();
let configPath = utils.getConfigPath(projectName);
let config = require(configPath);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"mkdirp": "^0.5.1",
"mongo-uri": "^0.1.2",
"nop": "^1.0.0",
"npm": "^3.8.9",
"pacote": "^7.6.1",
"plural": "^0.1.6",
"q": "^1.4.1",
"ramda": "^0.15.1",
Expand Down
2 changes: 1 addition & 1 deletion src/BaseManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var _ = require('lodash'),
path = require('path'),
fs = require('fs'),
npm = require('npm'),
pacote = require('pacote'),
rm_rf = require('rimraf'),
exists = require('exists-file'),
R = require('ramda'),
Expand Down
24 changes: 14 additions & 10 deletions src/ComponentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,22 @@ ComponentManager.prototype.import = function(args, callback) {
project = args.project;

// Add the project to the package.json
var pkgProject = args.packageName || utils.getPackageName(project);
this._logger.info(`Adding ${componentName} from ${pkgProject}`);
const getPkgName = args.packageName ? Q(args.packageName) :
utils.getPackageName(project);

// Add the component to the webgme config component paths
// FIXME: Call this without --save then later save it
if (args.skipInstall) {
this._addComponentFromProject(componentName, pkgProject, callback);
} else {
utils.installProject(project, args.dev, (err, result) => {
return getPkgName.then(pkgProject => {
this._logger.info(`Adding ${componentName} from ${pkgProject}`);

// Add the component to the webgme config component paths
// FIXME: Call this without --save then later save it
if (args.skipInstall) {
this._addComponentFromProject(componentName, pkgProject, callback);
});
}
} else {
utils.installProject(project, args.dev, (err, result) => {
this._addComponentFromProject(componentName, pkgProject, callback);
});
}
})
};

ComponentManager.prototype._addComponentFromProject = function(name, project, callback) {
Expand Down
14 changes: 5 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const spawn = childProcess.exec;
const Logger = require('./Logger');
const logger = new Logger();
const PROJECT_CONFIG = 'webgme-setup.json';
const pacote = require('pacote');

var _ = require('lodash'),
fs = require('fs'),
Expand Down Expand Up @@ -364,15 +365,10 @@ var getPathContaining = function(paths, item) {
* @return {String} name
*/
var getPackageName = function(npmPackage) {
// FIXME: It currently assumes everything is a github url. Should support
// hashes, packages, etc
// Ideally, we could use an npm feature to do this
if (npmPackage[0] === '.') { // File path
return npmPackage.split(path.sep).pop();
}

// Github url: project/repo
return npmPackage.split('/').pop().replace(/#.*$/, '');
const options = {
cache: path.join(__dirname, '..', 'cache')
};
return pacote.manifest(npmPackage, options).then(pkg => pkg.name);
};

var loadPaths = function(requirejs) {
Expand Down
26 changes: 7 additions & 19 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,19 @@ describe('utils', function() {
});
});

describe('getPackageName', function() {
it('should resolve basic github repo', function() {
var name = utils.getPackageName('webgme/formula');
assert.equal(name, 'formula');
});
describe.only('getPackageName', function() {
before(() => this.timeout(5000));

it('should resolve github repo w/ branch', function() {
var name = utils.getPackageName('webgme/formula#router_update');
assert.equal(name, 'formula');
it('should resolve basic github repo', function() {
return utils.getPackageName('webgme/formula')
.then(name => assert.equal(name, 'formula'));
});

it('should resolve relative file path', function() {
var name = utils.getPackageName('../webgme/formula');
assert.equal(name, 'formula');
return utils.getPackageName('./test/res/MismatchPkgName/')
.then(name => assert.equal(name, 'somename'));
});

it('should resolve absolute file path', function() {
var name = utils.getPackageName('/home/brian/projects/webgme/formula');
assert.equal(name, 'formula');
});

it('should file path from home dir', function() {
var name = utils.getPackageName('~/projects/webgme/formula');
assert.equal(name, 'formula');
});
});

after(function(done) {
Expand Down

0 comments on commit fea13e6

Please sign in to comment.