Skip to content

Commit 98fdfcb

Browse files
committed
Merge pull request #30 from bguiz/task-yargs-rev-merge
Task yargs rev merge, ongoing testing testing will be done on master before the next release, thanks @bguiz
2 parents aa4cf99 + 6de7a22 commit 98fdfcb

File tree

19 files changed

+1652
-1318
lines changed

19 files changed

+1652
-1318
lines changed

bin/cli.js

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ var path = require('path'),
1111
wordwrap = require('wordwrap'),
1212
runSequence = require('run-sequence'),
1313
chalk = require('chalk'),
14-
prettyTime = require('pretty-hrtime');
14+
prettyTime = require('pretty-hrtime'),
15+
yargs = require('yargs');
1516

16-
var yargs = require('./../lib/util/yargs');
17+
var taskYargs = require('../lib/util/task-yargs'),
18+
taskYargsRun = require('../lib/util/task-yargs-run');
1719

1820
// TODO @bholloway menus
1921
// var mainMenu = require('../lib/cli/mainMenu');
20-
require('../index');
2122

2223
// we need to duplicate some event handlers from the gulp cli since we have bypassed it
2324
gulp.on('task_start', function (e) {
@@ -32,43 +33,77 @@ gulp.on('task_stop', function (e) {
3233
);
3334
});
3435

35-
// describe the top level arguments
36-
yargs.getInstance()
36+
var packageJson = require(path.join(__dirname, '..', 'package.json'));
37+
var helpOption = {
38+
key: 'help',
39+
value: {
40+
describe: 'This help message, or help on a specific task',
41+
alias: ['h', '?'],
42+
boolean: true
43+
}
44+
};
45+
var defaultYargsInstance = yargs
3746
.usage(wordwrap(2, 80)([
38-
'Angularity is an opinionated build tool for AngularJS projects.',
47+
packageJson.description,
3948
'',
40-
'Tasks include:',
41-
yargs.listTasks().join(', ')
49+
'Tasks include:'
50+
//TODO add task list here
4251
].join('\n')))
43-
.example('angularity', 'Interactive menu')
52+
// .example('angularity', 'Interactive menu') //TODO reinstate when interactive menu is reinstated
4453
.example('angularity -v', 'Display the version of angularity')
45-
.example('angularity -h \<task name\>', 'Get help on a particular task')
54+
.example('angularity \<task name\> -h', 'Get help on a particular task')
4655
.example('angularity \<task name\>', 'Run the given task')
47-
.describe('h', 'This help message, or help on a specific task').alias('h', '?').alias('h', 'help')
48-
.describe('version', 'Display the version of angularity').alias('version', 'v').boolean('version')
49-
.wrap(80);
56+
.option('version', {
57+
describe: 'Display the curent version',
58+
alias: ['v'],
59+
boolean: true
60+
})
61+
.option(helpOption.key, helpOption.value);
5062

51-
// find the yargs instance that is most appropriate for the given command line parameters
52-
var cliArgs = yargs.resolveArgv();
63+
taskYargsRun.taskYargs.register('help', {
64+
description: (wordwrap(2, 80)('Displays context-specific help messages')),
65+
prerequisiteTasks: [],
66+
options: [
67+
{
68+
key: 'help',
69+
value: {
70+
describe: 'This help message, or help on a specific task',
71+
alias: ['h', '?'],
72+
boolean: true
73+
}
74+
}
75+
],
76+
checks: []
77+
});
5378

54-
// show help
55-
if (cliArgs.help) {
56-
yargs
57-
.getInstance(cliArgs.taskName || cliArgs.help)
58-
.showHelp();
59-
}
60-
// run a task
61-
else if (cliArgs.taskName) {
62-
runSequence(cliArgs.taskName);
79+
require('../index');
80+
81+
//TODO move these to ../index.js
82+
require('../tasks/html')(taskYargsRun);
83+
require('../tasks/css')(taskYargsRun);
84+
require('../tasks/javascript')(taskYargsRun);
85+
require('../tasks/test')(taskYargsRun);
86+
require('../tasks/build')(taskYargsRun);
87+
require('../tasks/release')(taskYargsRun);
88+
require('../tasks/server')(taskYargsRun);
89+
require('../tasks/watch')(taskYargsRun);
90+
require('../tasks/init')(taskYargsRun);
91+
require('../tasks/webstorm')(taskYargsRun);
92+
93+
var cliArgs;
94+
var taskName = taskYargsRun.taskYargs.getCurrentName();
95+
if (taskName) {
96+
taskYargsRun.current();
6397
}
64-
// show the version string
65-
else if (cliArgs.version) {
66-
var packagePath = path.join(__dirname, '..', 'package.json');
67-
var version = require(packagePath).version;
68-
console.log('angularity:', version);
98+
else {
99+
cliArgs = defaultYargsInstance.argv;
100+
if (cliArgs.version) {
101+
console.log('angularity:', packageJson.version);
102+
}
103+
else {
104+
if (!cliArgs.help) {
105+
console.log('Task specified is not recognised');
106+
}
107+
defaultYargsInstance.showHelp();
108+
}
69109
}
70-
// interactive menu
71-
// TODO @bholloway reimplement after yargs refactor regression
72-
// else {
73-
// mainMenu.prompt();
74-
// }

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('require-dir')('./tasks');
1+
'use strict';

lib/test/karma.js

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var yargs = require('../util/yargs'),
1212
streams = require('../config/streams'),
1313
platform = require('../config/platform');
1414

15+
//TODO @bguiz get this from config
1516
var defaultReporterName = 'karma-angularity-reporter';
1617

1718
var basePathRegex = /['"]?\s*\/\*+\s*ANGULARITY_BASE_PATH\s*\*+\/\s*['"]?/ ;
@@ -37,14 +38,21 @@ function getKarmaReporterPluginPath(reporterName) {
3738
throw 'Reporter name is unspecified';
3839
}
3940

40-
//Default reporter is a dependency of angularity
41-
var reporterPath = (reporterName === defaultReporterName) ?
42-
path.resolve(__dirname, '../../node_modules', defaultReporterName) :
43-
reporterName;
41+
var reporterPath;
42+
43+
// Default reporter is a dependency of angularity
44+
if (reporterName === defaultReporterName) {
45+
reporterPath = path.resolve(__dirname, '../../node_modules', defaultReporterName);
46+
}
47+
else {
48+
reporterPath = reporterName;
49+
}
50+
4451
//Specific reporters are expected to be dependencies of the local project
45-
reporterPath = (path.dirname(reporterPath) === '.') ?
46-
path.resolve('node_modules', 'karma-' + reporterName + '-reporter') :
47-
reporterPath;
52+
if (path.dirname(reporterPath) === '.') { //not an absolute path
53+
reporterPath = path.resolve('node_modules', 'karma-' +
54+
getKarmaReporterName(reporterName) + '-reporter');
55+
}
4856

4957
//Either way, the absolute path of the reporter is `require`d
5058
//Only to determine if it is indeed `require`able,
@@ -55,8 +63,8 @@ function getKarmaReporterPluginPath(reporterName) {
5563
catch (ex) {
5664
throw 'Attempt to require reporter from path ' + reporterPath + ' with no success.';
5765
}
58-
reporterPath = path.normalize(reporterPath);
5966

67+
reporterPath = path.normalize(reporterPath);
6068
if (platform.isWindows()) {
6169
//Sanitise path for Windows
6270
//Replace any single backslash characters in file paths with
@@ -74,7 +82,7 @@ function getKarmaReporterPluginPath(reporterName) {
7482
*/
7583
function getKarmaReporterName(reporterName) {
7684
if (typeof reporterName !== 'string') {
77-
throw 'Get Karma Reporter Name: Reporter name is unspecified';
85+
throw 'Reporter name is unspecified';
7886
}
7987

8088
var name = (path.dirname(reporterName) === '.') ?
@@ -172,54 +180,27 @@ function karmaRun() {
172180
var appPath = path.join(__dirname, 'karma-background.js');
173181
var data = querystring.escape(JSON.stringify(options));
174182

175-
childProcess.spawn('node', [appPath, data], {
176-
cwd : process.cwd(),
177-
stdio: 'inherit',
178-
env : lodashMerge(process.env, {
179-
//NOTE this workaround is necessary, see issue:
180-
//https://github.com/sindresorhus/supports-color/issues/13
181-
//TODO @bguiz remove workaround when issue has been resolved
182-
SUPPORTS_COLOR: true
183+
childProcess
184+
.spawn('node', [appPath, data], {
185+
cwd : process.cwd(),
186+
stdio: 'inherit',
187+
env : lodashMerge(process.env, {
188+
//NOTE this workaround is necessary, see issue:
189+
//https://github.com/sindresorhus/supports-color/issues/13
190+
//TODO @bguiz remove workaround when issue has been resolved
191+
SUPPORTS_COLOR: true
192+
})
183193
})
184-
})
185194
.on('close', function() {
186195
done();
187196
});
188197
}
189198
return through.obj(transformFn, flushFn);
190199
}
191200

192-
var yargsOptionDefinition = {
193-
key : 'karma-reporter',
194-
value: {
195-
describe: 'Specify a custom Karma reporter to use. Either a locally npm installed module, or an asolute path to ' +
196-
'one.',
197-
alias : ['k'],
198-
default : defaultReporterName,
199-
string : true
200-
}
201-
};
202-
var checkKarmaReporter = yargs.createCheck()
203-
.withGate(function (argv) {
204-
return !argv.help;
205-
})
206-
.withTest({
207-
'karma-reporter': function(value) {
208-
if (typeof value !== 'undefined') {
209-
try {
210-
getKarmaReporterPluginPath(value);
211-
}
212-
catch (ex) {
213-
return 'Illegal value for "karma-reporter": ' + ex;
214-
}
215-
}
216-
}
217-
})
218-
.commit();
219-
220201
module.exports = {
221-
createConfig: karmaCreateConfig,
222-
run : karmaRun,
223-
yargsCheck : checkKarmaReporter,
224-
yargsOption : yargsOptionDefinition
202+
createConfig : karmaCreateConfig,
203+
run : karmaRun,
204+
defaultReporterName: defaultReporterName,
205+
getPluginPath : getKarmaReporterPluginPath
225206
};

lib/util/jshint-reporter.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

3-
var path = require('path'),
4-
gulpJshint = require('gulp-jshint');
3+
var path = require('path');
54

6-
var yargs = require('./yargs');
5+
var gulpJshint = require('gulp-jshint');
76

7+
var yargs = require('./yargs');
8+
9+
//TODO @bguiz get this from config
810
var defaultReporterName = 'angularity-jshint-reporter';
911

1012
/**
@@ -76,38 +78,7 @@ function getJsHintReporter(reporterName) {
7678
}
7779
}
7880

79-
var yargsOptionDefiniton = {
80-
key: 'jshint-reporter',
81-
value: {
82-
describe: 'Specify a custom JsHint reporter to use. Either a locally npm installed module, or the absolute path ' +
83-
'to one.',
84-
alias : ['j'],
85-
default : defaultReporterName,
86-
string : true
87-
}
88-
};
89-
var checkJsHintReporter = yargs.createCheck()
90-
.withGate(function (argv) {
91-
return !argv.help;
92-
})
93-
.withTest({
94-
'jshint-reporter': function(value) {
95-
if (typeof value === 'undefined') {
96-
return;
97-
}
98-
try {
99-
getJsHintReporter(value);
100-
}
101-
catch (ex) {
102-
return 'Illegal value for "jshint-reporter": ' + ex;
103-
}
104-
}
105-
})
106-
.commit();
107-
10881
module.exports = {
10982
get : getJsHintReporter,
110-
yargsCheck : checkJsHintReporter,
111-
yargsOption : yargsOptionDefiniton,
11283
defaultReporterName: defaultReporterName
11384
};

lib/util/task-yargs-run.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
var taskYargsRun = require('task-yargs/run');
4+
var tyRunInstance = taskYargsRun();
5+
6+
//NOTE This file is to maintain a single task yargs run instance,
7+
// as we wish to use the same one across multiple gulp tasks.
8+
// In addition, we may augment or proxy some methods within it.
9+
10+
function checkFlagMissing(opt, key, value) {
11+
if ((!!opt.value.isOptional) &&
12+
(typeof value === 'undefined')) {
13+
// all other options must be specified, or have default defined
14+
return true;
15+
}
16+
if ((typeof value === 'undefined') ||
17+
((typeof value === 'string') && (value.length < 1) && (key !== 'description'))) {
18+
throw new Error('Required option "' + key + '" is not specified');
19+
}
20+
}
21+
22+
function checkFlagType(opt, ket, value) {
23+
var valueType = (typeof value);
24+
var typeIsOk = false;
25+
var validTypes = ['string', 'boolean', 'number']
26+
.filter(function(selType) {
27+
return !!(opt.value[selType]);
28+
});
29+
if (!opt.value.isMultiple) {
30+
validTypes.forEach(function(validType) {
31+
if (valueType === validType) {
32+
typeIsOk = true;
33+
}
34+
});
35+
}
36+
else {
37+
value = [].concat(value);
38+
if (value.length === 0) {
39+
typeIsOk = true;
40+
}
41+
else {
42+
value.forEach(function (subValue) {
43+
validTypes.forEach(function(validType) {
44+
if (typeof subValue === validType) {
45+
typeIsOk = true;
46+
}
47+
});
48+
});
49+
}
50+
}
51+
if (!typeIsOk) {
52+
var expectedType = JSON.stringify(validTypes);
53+
if (opt.value.isMultiple) {
54+
expectedType = 'Array<' + expectedType + '>'
55+
}
56+
throw new Error('' + key + ' is expected to be of types ' +
57+
expectedType + ' but a ' + (typeof value) + ' was provided instead.');
58+
}
59+
}
60+
61+
tyRunInstance.checkFlagType = checkFlagType;
62+
tyRunInstance.checkFlagMissing = checkFlagMissing;
63+
64+
module.exports = tyRunInstance;

0 commit comments

Comments
 (0)