This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
122 lines (94 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Module dependencies
*/
var util = require('util');
var _ = require('lodash');
var defaultsDeep = require('merge-defaults');
/**
* sails-generate-hook
*
* Usage:
* `sails generate hook`
*
* @description Generates a hook
* @help See http://links.sailsjs.org/docs/generators
*/
module.exports = {
/**
* `before()` is run before executing any of the `targets`
* defined below.
*
* This is where we can validate user input, configure default
* scope variables, get extra dependencies, and so on.
*
* @param {Object} scope
* @param {Function} cb [callback]
*/
before: function (scope, cb) {
// scope.args are the raw command line arguments.
//
// e.g. if someone runs:
// $ sails generate hook user find create update
// then `scope.args` would be `['user', 'find', 'create', 'update']`
if (!scope.args[0]) {
return cb( new Error('Please provide a name for this hook.') );
}
// scope.rootPath is the base path for this generator
//
// e.g. if this generator specified the target:
// './Foobar.md': { copy: 'Foobar.md' }
//
// And someone ran this generator from `/Users/dbowie/sailsStuff`,
// then `/Users/dbowie/sailsStuff/Foobar.md` would be created.
if (!scope.rootPath) {
return cb( INVALID_SCOPE_VARIABLE('rootPath') );
}
// Attach defaults
defaultsDeep(scope, {
createdAt: new Date()
});
// Decide the output id for use in targets below:
scope.id = scope.args[0];
// When finished, we trigger a callback with no error
// to begin generating files/folders as specified by
// the `targets` below.
cb();
},
/**
* The files/folders to generate.
* @type {Object}
*/
targets: {
'./api/hooks/:id/index.js': { template: 'hook.template.js' }
},
/**
* The absolute path to the `templates` for this generator
* (for use with the `template` helper)
*
* @type {String}
*/
templatesDirectory: require('path').resolve(__dirname, './templates')
};
/**
* INVALID_SCOPE_VARIABLE()
*
* Helper method to put together a nice error about a missing or invalid
* scope variable. We should always validate any required scope variables
* to avoid inadvertently smashing someone's filesystem.
*
* @param {String} varname [the name of the missing/invalid scope variable]
* @param {String} details [optional - additional details to display on the console]
* @param {String} message [optional - override for the default message]
* @return {Error}
* @api private
*/
function INVALID_SCOPE_VARIABLE (varname, details, message) {
var DEFAULT_MESSAGE =
'Issue encountered in generator "hook":\n'+
'Missing required scope variable: `%s`"\n' +
'If you are the author of `sails-generate-hook`, please resolve this '+
'issue and publish a new patch release.';
message = (message || DEFAULT_MESSAGE) + (details ? '\n'+details : '');
message = util.inspect(message, varname);
return new Error(message);
}