Skip to content

Commit 7d6e2f4

Browse files
authored
Merge pull request #64 from chris-steele/develop
Added unregister and rename commands
2 parents 8b5107a + c34734a commit 7d6e2f4

File tree

12 files changed

+372
-14
lines changed

12 files changed

+372
-14
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,35 @@ From within a plugin directory
8080

8181
The package will be registered with the registry on a first come first serve basis.
8282

83+
##### Unregister a plugin
84+
85+
From within a plugin directory
86+
87+
adapt unregister
88+
89+
`name` will be read from `bower.json` in the current directory
90+
91+
Alternatively, from any directory
92+
93+
adapt unregister <plugin name>
94+
95+
You will be prompted to authenticate with GitHub.
96+
97+
The package will be unregistered with the registry if the authenticated user is a collaborator on the given endpoint or a collaborator on the Adapt framework.
98+
99+
##### Rename a plugin
100+
101+
From any directory
102+
103+
adapt rename <plugin name> <new plugin name>
104+
105+
You will be prompted to authenticate with GitHub.
106+
107+
The package will be renamed if the authenticated user is a collaborator on the endpoint registered with the plugin or a collaborator on the Adapt framework.
108+
83109
Release History
84110
===============
111+
- 2.0.5 - Added rename and unregister commands and corrected some typos
85112
- 2.0.4 - Corrected version numbers
86113
- 2.0.3 - Fixed issue#53 - npm dependency is git url
87114
- 2.0.2 - Fixed issue#32 - Error: Cannot find module 'Q' on case sensitive file systems

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.4
1+
2.0.5

json/help-register.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"command": "register",
3-
"description": "Register a plugin to adapt's remote registory by reading bower.json file. Check folowing URL for more detail - https://github.com/adaptlearning/adapt_framework/wiki/Registering-a-plugin",
3+
"description": "Register a plugin to adapt's remote registry by reading bower.json file. Check folowing URL for more detail - https://github.com/adaptlearning/adapt_framework/wiki/Registering-a-plugin",
44
"usage": [
55
"register"
66
]

json/help-rename.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"command": "rename",
3+
"description": "Unregister a plugin at Adapt's remote registry. Specify as two arguments the current name of the plugin to be renamed and a new name. Please note that you must authenticate with GitHub to unregister and must be a collaborator on the endpoint registered with the plugin or a collaborator on the Adapt framework. Access to GitHub is for authentication only.",
4+
"usage": [
5+
"rename <plugin name> <new plugin name>"
6+
]
7+
}

json/help-search.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"command": "search",
3-
"description": "List out all the plugin(s) in adapt's remote registory or could also search a plugin with supplied partial name.",
3+
"description": "List out all the plugin(s) in adapt's remote registry or could also search a plugin with supplied partial name.",
44
"usage": [
55
"search",
66
"search <plugin-name>"

json/help-unregister.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"command": "unregister",
3+
"description": "Unregister a plugin at Adapt's remote registry. This command can be run without arguments in a directory which contains the bower.json file for the plugin to be unregistered. Alternatively, specify the name of the plugin as a single argument. Please note that you must authenticate with GitHub to unregister and must be a collaborator on the endpoint registered with the plugin or a collaborator on the Adapt framework. Access to GitHub is for authentication only.",
4+
"usage": [
5+
"unregister",
6+
"unregister <plugin name>"
7+
]
8+
}

json/help.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
"<command>"
44
],
55
"commands": {
6-
"create": "Create a new adapt course or plugin",
7-
"devinstall": "Get the framework and plugins as git repository",
6+
"create": "Create a new Adapt course or plugin",
7+
"devinstall": "Get the framework and plugins as Git repository",
88
"help": "List out the commands available with adapt-cli",
9-
"install": "Install plugin(s) within the adapt course directory",
10-
"ls": "List all the plugins name mentioned in adapt.json",
11-
"register": "Register a plugins to adapt remote registory",
12-
"search": "List/Search for plugin(s) from remote registory",
9+
"install": "Install plugin(s) within the Adapt course directory",
10+
"ls": "List all the plugin names mentioned in adapt.json",
11+
"rename": "Rename a plugin at the Adapt remote registry",
12+
"register": "Register a plugin with the Adapt remote registry",
13+
"search": "List/Search for plugin(s) at the Adapt remote registry",
1314
"uninstall": "Remove a local plugin",
15+
"unregister": "Unregister a plugin from the Adapt remote registry",
1416
"version": "Display version of adapt-cli"
1517
}
1618
}

lib/commands/register.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ module.exports = {
2727
return properties;
2828
})
2929
.then(function (properties) {
30+
// given a package name, create two Plugin representations
31+
// if supplied name is adapt-contrib-myPackageName do a check against this name only
32+
// if suppled name is adapt-myPackageName check against this name and adapt-contrib-myPackageName
33+
// becase we don't want to allow adapt-myPackageName if adapt-contrib-myPackageName exists
3034
var plugin = new Plugin(properties.name),
3135
contribPlugin = new Plugin(properties.name, true),
3236
searches = [exists(contribPlugin)];
@@ -49,11 +53,13 @@ module.exports = {
4953
.then(function (registered) {
5054
if(!registered.result) throw new Error('The plugin was unable to register.');
5155
renderer.log(chalk.green(registered.plugin.packageName), 'has been registered successfully.');
56+
done();
5257
})
5358
.fail(function (err) {
5459
renderer.log(chalk.red(err));
5560
done(err);
56-
});
61+
})
62+
.done();
5763
}
5864
};
5965

@@ -93,29 +99,29 @@ function confirm(properties) {
9399
schema = {
94100
properties: {
95101
name: {
96-
description: 'name',
102+
description: chalk.cyan('name'),
97103
pattern: /^adapt-[\w|-]+?$/,
98104
message: "Name must prefixed with 'adapt' and each word separated with a hyphen(-)",
99105
type: 'string',
100106
default: plugin.toString() || 'not specified',
101107
required: true
102108
},
103109
repository: {
104-
description: 'repository',
110+
description: chalk.cyan('repository'),
105111
pattern: /([A-Za-z0-9]+@|http(|s)|git\:\/\/)([A-Za-z0-9.]+)(:|\/)([A-Za-z0-9\-\.\/]+)(\.git)?/,
106112
type: 'string',
107113
default: properties.repository || 'not specified',
108114
required: true
109115
},
110116
framework: {
111-
description: 'framework',
117+
description: chalk.cyan('framework'),
112118
pattern: /\bv?(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?\b/ig,
113119
type: 'string',
114120
default: properties.framework || '~2.0.0',
115121
required: false
116122
},
117123
ready: {
118-
description: 'Register now?',
124+
description: chalk.cyan('Register now?'),
119125
message: 'Please specify (y)es or (n)o',
120126
pattern: /^y$|^n$/i,
121127
type: 'string',

lib/commands/rename.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
var bower = require('bower'),
2+
chalk = require('chalk'),
3+
_ = require('lodash'),
4+
prompt = require('prompt'),
5+
Q = require('q'),
6+
request = require('request'),
7+
Constants = require('../Constants'),
8+
Plugin = require('../Plugin'),
9+
authenticate = require('../promise/authenticate'),
10+
log;
11+
12+
module.exports = {
13+
rename: function (renderer) {
14+
log = renderer.log;
15+
16+
var done = arguments[arguments.length-1] || function () {};
17+
18+
if (arguments.length >= 4) {
19+
var params = {
20+
oldName:arguments[1],
21+
newName:arguments[2]
22+
};
23+
24+
// use Plugin to standardise name
25+
params.newName = new Plugin(params.newName).packageName;
26+
27+
log(chalk.yellow('Using registry at', Constants.Registry));
28+
log(chalk.yellow('Plugin will be renamed to', params.newName));
29+
30+
Q(params)
31+
.then(checkPluginNameExists)
32+
.then(checkNewPluginNameDoesNotExist)
33+
.then(authenticate)
34+
.then(confirm)
35+
.then(rename)
36+
.then(function() {
37+
log(chalk.green('The plugin was successfully renamed.'));
38+
done();
39+
})
40+
.catch(function (err) {
41+
log(chalk.red(err));
42+
log('The plugin was not renamed.');
43+
done(err);
44+
})
45+
.done();
46+
47+
} else {
48+
log(chalk.red('You must call rename with the following arguments: <plugin name> <new plugin name>'));
49+
done();
50+
}
51+
}
52+
};
53+
54+
function checkPluginNameExists(params) {
55+
return exists(params.oldName).then(function(exists) {
56+
return exists ? Q.resolve(params) : Q.reject('Plugin "'+params.oldName+'" does not exist');
57+
});
58+
}
59+
60+
function checkNewPluginNameDoesNotExist(params) {
61+
return exists(params.newName).then(function(exists) {
62+
return exists ? Q.reject('Name "'+params.newName+'" already exists') : Q.resolve(params);
63+
});
64+
}
65+
66+
function confirm(params) {
67+
var deferred = Q.defer();
68+
var schema = {
69+
properties: {
70+
ready: {
71+
description: chalk.cyan('Confirm rename now?'),
72+
message: 'Please specify (y)es or (n)o',
73+
pattern: /^y$|^n$/i,
74+
type: 'string',
75+
default: 'y',
76+
required: true,
77+
before: function(value) { return /^y$/i.test(value); }
78+
}
79+
}
80+
};
81+
prompt.message = '';
82+
prompt.delimiter = ' ';
83+
prompt.start();
84+
prompt.get(schema, function (err, confirmation) {
85+
if(err) return deferred.reject(err);
86+
if(!confirmation.ready) deferred.reject(new Error('Aborted. Nothing has been renamed.'));
87+
deferred.resolve(params);
88+
});
89+
return deferred.promise;
90+
}
91+
92+
function rename(params) {
93+
var deferred = Q.defer();
94+
var path = 'packages/rename/'+params.username+'/'+params.oldName+'/'+params.newName;
95+
var query = '?access_token='+params.token;
96+
97+
request({
98+
url: Constants.Registry+'/'+path+query,
99+
method:'GET',
100+
headers: {'User-Agent':'adapt-cli'},
101+
followRedirect:false
102+
}, function(err, res, body) {
103+
if (err) {
104+
deferred.reject(err);
105+
} else {
106+
res.statusCode==201 ? deferred.resolve(params) : deferred.reject("The server responded with "+res.statusCode);
107+
}
108+
});
109+
110+
return deferred.promise;
111+
}
112+
113+
function exists(plugin) {
114+
var deferred = Q.defer();
115+
116+
bower.commands.search(plugin.toString(), { registry: Constants.Registry })
117+
.on('end', function(result) {
118+
var matches = result.filter(exactMatch(plugin.toString()));
119+
deferred.resolve(!!matches.length);
120+
})
121+
.on('error', function (err) {
122+
deferred.reject(err);
123+
});
124+
return deferred.promise;
125+
}
126+
127+
function exactMatch(pattern) {
128+
return function (item) {
129+
if (typeof pattern === 'string') {
130+
if (pattern.toLowerCase() === item.name.toLowerCase()) {
131+
return true;
132+
}
133+
return false
134+
}
135+
var regexp = new RegExp(pattern, 'i');
136+
return regexp.test(item.name);
137+
};
138+
}

0 commit comments

Comments
 (0)