base-project
Version:
Base plugin that adds a `project` getter to the instance for getting the name of a project. Gets the project name for new (empty) projects, projects with only a .git repository, and/or projects with a package.json.
44 lines (38 loc) • 865 B
JavaScript
/*!
* base-project <https://github.com/jonschlinkert/base-project>
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
;
var isValidInstance = require('is-valid-instance');
var isRegistered = require('is-registered');
var project = require('project-name');
module.exports = function(fn) {
return function(app) {
if (!isValid(app, fn)) return;
var name;
this.define('project', {
configurable: true,
enumerable: true,
set: function(val) {
name = val;
},
get: function() {
return name || (name = project(this.cwd));
}
});
};
};
function isValid(app, fn) {
if (typeof fn === 'function') {
return fn(app);
}
if (!isValidInstance(app)) {
return false;
}
if (isRegistered(app, 'base-project')) {
return false;
}
return true;
}