gaffer-generator 1.2.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +4 -2
  2. package/cli.js +43 -37
  3. package/lib/argv.js +9 -0
  4. package/lib/create.js +40 -0
  5. package/{src → lib}/generate/directory.js +13 -14
  6. package/{src → lib}/generate/file.js +13 -13
  7. package/{src → lib}/generate/node.js +6 -9
  8. package/lib/generate/root.js +95 -0
  9. package/{src → lib}/templateArgs.js +1 -1
  10. package/lib/utils/contentsDiffer.js +20 -0
  11. package/lib/utils/log.js +26 -0
  12. package/lib/utils/logChange.js +16 -0
  13. package/lib/utils/logError.js +10 -0
  14. package/lib/utils/logRemoval.js +10 -0
  15. package/lib/utils/lowerCaseFirst.js +11 -0
  16. package/lib/utils/normalizeLineEndings.js +8 -0
  17. package/lib/utils/normalizePath.js +10 -0
  18. package/lib/utils/parameterizeString.js +19 -0
  19. package/lib/utils/safeRead.js +17 -0
  20. package/lib/utils/safeWrite.js +23 -0
  21. package/lib/utils.js +16 -0
  22. package/package.json +39 -11
  23. package/.github/dependabot.yml +0 -8
  24. package/.github/workflows/codeql-analysis.yml +0 -67
  25. package/SECURITY.md +0 -15
  26. package/example/sample.json +0 -431
  27. package/example/sample.templateroot/date-parser.ts +0 -10
  28. package/example/sample.templateroot/is-set.ts +0 -3
  29. package/example/sample.templateroot/models/_eachEnum.fileName_.ts +0 -13
  30. package/example/sample.templateroot/models/_eachModel.fileName_.ts +0 -73
  31. package/example/sample.templateroot/models/index.ts +0 -14
  32. package/example/sample.templateroot/services/_eachController.fileName_.ts +0 -82
  33. package/example/sample.templateroot/services/index.ts +0 -11
  34. package/example/sample.templateroot/template.js +0 -413
  35. package/jest.config.js +0 -185
  36. package/renovate.json +0 -6
  37. package/src/__mocks__/fs.js +0 -59
  38. package/src/create.js +0 -38
  39. package/src/create.test.js +0 -5
  40. package/src/generate/directory.test.js +0 -5
  41. package/src/generate/file.test.js +0 -5
  42. package/src/generate/node.test.js +0 -5
  43. package/src/generate/root.js +0 -86
  44. package/src/generate/root.test.js +0 -5
  45. package/src/lodash.js +0 -17205
  46. package/src/utils.js +0 -179
  47. package/src/utils.test.js +0 -146
package/src/utils.js DELETED
@@ -1,179 +0,0 @@
1
- require('colors');
2
- const _ = require('./lodash');
3
- const get = _.get;
4
- const fs = require('fs');
5
- const path = require('path');
6
- const argv = require('yargs').argv;
7
- const fetch = require('node-fetch');
8
-
9
- /*
10
- Public API.
11
- */
12
- exports.contentsDiffer = contentsDiffer;
13
- exports.safeRead = safeRead;
14
- exports.safeWrite = safeWrite;
15
- exports.log = log;
16
- exports.logChange = logChange;
17
- exports.logRemoval = logRemoval;
18
- exports.logError = logError;
19
- exports.parameterizeString = parameterizeString;
20
- exports.lowerCaseFirst = lowerCaseFirst;
21
- exports.fetch = fetch;
22
- exports.normalizePath = normalizePath;
23
-
24
- /*
25
- Implementation.
26
- */
27
-
28
- /**
29
- * Looks at two strings to see if they contain the same content, ignoring line endings.
30
- * @param a
31
- * @param b
32
- * @returns {boolean}
33
- */
34
- function contentsDiffer(a, b) {
35
- // Are a and b both empty?
36
- if (!a && !b) {
37
- return false;
38
- }
39
- // Is only one of them empty?
40
- if (!a || !b) {
41
- return true;
42
- }
43
- // Otherwise, compare them while ignoring line endings to see if they're equivalent.
44
- return normalizeLineEndings(a) !== normalizeLineEndings(b);
45
- }
46
-
47
- /**
48
- * Attempts to read in the provided file, returning the string contents if it exists, or null.
49
- * @param file
50
- * @returns {string}
51
- */
52
- function safeRead(file) {
53
- let url = normalizePath(typeof file === 'string'
54
- ? file
55
- : path.join(file.dirname, file.basename));
56
- return fs.existsSync(url)
57
- ? fs.readFileSync(url, 'UTF-8')
58
- : null;
59
- }
60
-
61
- /**
62
- * Writes the contents to the file, making sure the parent directories of the file exist.
63
- * @param file
64
- * @param contents
65
- */
66
- function safeWrite(file, contents) {
67
- const url = normalizePath(typeof file === 'string'
68
- ? file
69
- : path.join(file.dirname, file.basename));
70
- const dirs = url.split(path.sep).slice(0, -1);
71
- // Note: we start at 1 to avoid trying to create the root directory.
72
- for (let i = 1; i < dirs.length; i++) {
73
- let collectivePath = dirs.slice(0, i + 1).join(path.sep);
74
- if (!fs.existsSync(collectivePath)) {
75
- fs.mkdirSync(collectivePath);
76
- }
77
- }
78
- fs.writeFileSync(url, contents, 'UTF-8');
79
- }
80
-
81
- /**
82
- * If the file doesn't exist, logs a message saying it is being created. Otherwise, it's being updated.
83
- * @param file
84
- */
85
- function logChange(file) {
86
- let url = normalizePath(typeof file === 'string'
87
- ? file
88
- : path.join(file.dirname, file.basename));
89
- log((`${fs.existsSync(url) ? 'updating' : 'creating'}: `).cyan + url.magenta);
90
- }
91
-
92
- /**
93
- * Logs a message saying the file is being removed.
94
- * @param url
95
- */
96
- function logRemoval(url) {
97
- log((`removing: `).red + url.magenta);
98
- }
99
-
100
- /**
101
- * Logs an error.
102
- * @param text
103
- * @param [err]
104
- */
105
- function logError(text, err) {
106
- log(text, err, true);
107
- }
108
-
109
- /**
110
- * Logs a message to the console with a nice prefix.
111
- * @param text The text to write.
112
- * @param [error] The optional error to log
113
- * @param writeAsError If we should use console.error or console.log.
114
- */
115
- function log(text, error = null, writeAsError = false) {
116
- if (argv.silent) {
117
- return;
118
- }
119
- let prefix = `[${new Date().toTimeString().split(' ')[0]}] `.gray;
120
- if (writeAsError) {
121
- if (error) {
122
- console.error(prefix + text, error);
123
- }
124
- else {
125
- console.error(prefix + text);
126
- }
127
- }
128
- else {
129
- console.log(prefix + text);
130
- }
131
- }
132
-
133
- /**
134
- * Given a string with _parameters_ within it, substitute them with the real values from the given context.
135
- * @param str
136
- * @param context
137
- * @returns {string}
138
- */
139
- function parameterizeString(str, context) {
140
- return str.replace(
141
- /_[a-z]*\.?[a-z]+_/ig,
142
- match => {
143
- const variableName = match.slice(1, -1);
144
- return get(context, variableName)
145
- || get(context, variableName.toLowerCase())
146
- || get(context, variableName.toUpperCase())
147
- || match;
148
- });
149
- }
150
-
151
- /**
152
- * Lowers the first letter of the provided string, leaving the rest of it alone.
153
- * @param val
154
- * @returns {string}
155
- */
156
- function lowerCaseFirst(val) {
157
- if (!val || !val.toLowerCase) {
158
- return val;
159
- }
160
- return val[0].toLowerCase() + val.substr(1);
161
- }
162
-
163
- /**
164
- * Normalizes a path across platforms. (Root / paths are assumed to be on C:\ on windows).
165
- * @param ref
166
- * @returns {string}
167
- */
168
- function normalizePath(ref) {
169
- return ref && ref.startsWith('/') ? path.resolve(ref) : path.normalize(ref);
170
- }
171
-
172
- /**
173
- * Normalizes line endings for comparison purposes.
174
- * @param contents
175
- * @returns {string}
176
- */
177
- function normalizeLineEndings(contents) {
178
- return contents.replace(/\r\n?/g, '\n');
179
- }
package/src/utils.test.js DELETED
@@ -1,146 +0,0 @@
1
- const spyLog = jest.spyOn(console, 'log');
2
- const spyError = jest.spyOn(console, 'error');
3
-
4
- jest.mock('fs');
5
- const utils = require('./utils');
6
- const fs = require('fs');
7
-
8
- const _n = utils.normalizePath;
9
-
10
- describe('contentsDiffer', () => {
11
- test('handles one or more empty contents', () => {
12
- expect(utils.contentsDiffer('', '')).toBe(false);
13
- expect(utils.contentsDiffer('a', '')).toBe(true);
14
- expect(utils.contentsDiffer('', 'b')).toBe(true);
15
- });
16
- test('handles normal cases', () => {
17
- expect(utils.contentsDiffer('a', 'a')).toBe(false);
18
- expect(utils.contentsDiffer('a', 'b')).toBe(true);
19
- expect(utils.contentsDiffer('a', 'A')).toBe(true);
20
- });
21
- test('handles line endings', () => {
22
- expect(utils.contentsDiffer('a\na', 'a\na')).toBe(false);
23
- expect(utils.contentsDiffer('a\r\na', 'a\na')).toBe(false);
24
- expect(utils.contentsDiffer('a\r\na', 'a\r\na')).toBe(false);
25
- expect(utils.contentsDiffer('a\na', 'a\r\na')).toBe(false);
26
- });
27
- });
28
-
29
- describe('safeRead', () => {
30
- test('returns null when file does not exist', () => {
31
- fs.__setResponse('existsSync', false);
32
- expect(utils.safeRead(_n('/foo/bar.js'))).toBe(null);
33
- });
34
- test('returns contents when file does exist', () => {
35
- fs.__setResponse('existsSync', true);
36
- fs.__setResponse('readFileSync', 'baz');
37
- expect(utils.safeRead(_n('/foo/bar.js'))).toBe('baz');
38
- });
39
- test('allows string argument', () => {
40
- fs.__setResponse('existsSync', false);
41
- expect(utils.safeRead(_n('/foo/bar.js'))).toBe(null);
42
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo/bar.js'));
43
- });
44
- test('allows file argument', () => {
45
- fs.__setResponse('existsSync', false);
46
- expect(utils.safeRead({ dirname: _n('/foo'), basename: 'bar.js' })).toBe(null);
47
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo/bar.js'));
48
- });
49
- });
50
-
51
- describe('safeWrite', () => {
52
- test('creates directories recursively', () => {
53
- fs.__setResponse('existsSync', false);
54
- utils.safeWrite(_n('/foo/bar/baz.js'), 'qux');
55
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo'));
56
- expect(fs.__spy.mkdirSync).toHaveBeenCalledWith(_n('/foo'));
57
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo/bar'));
58
- expect(fs.__spy.mkdirSync).toHaveBeenCalledWith(_n('/foo/bar'));
59
- expect(fs.__spy.writeFileSync).toHaveBeenCalledWith(_n('/foo/bar/baz.js'), 'qux', 'UTF-8');
60
- });
61
- test('only creates directories when they do not exist', () => {
62
- fs.__setResponse('existsSync', true);
63
- utils.safeWrite(_n('/foo/bar/baz.js'), 'qux');
64
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo'));
65
- expect(fs.__spy.mkdirSync).not.toHaveBeenCalledWith(_n('/foo'));
66
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo/bar'));
67
- expect(fs.__spy.mkdirSync).not.toHaveBeenCalledWith(_n('/foo/bar'));
68
- expect(fs.__spy.writeFileSync).toHaveBeenCalledWith(_n('/foo/bar/baz.js'), 'qux', 'UTF-8');
69
- });
70
- test('allows string argument', () => {
71
- fs.__setResponse('existsSync', false);
72
- utils.safeWrite(_n('/foo/bar.js'), 'baz');
73
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo'));
74
- expect(fs.__spy.mkdirSync).toHaveBeenCalledWith(_n('/foo'));
75
- expect(fs.__spy.writeFileSync).toHaveBeenCalledWith(_n('/foo/bar.js'), 'baz', 'UTF-8');
76
- });
77
- test('allows file argument', () => {
78
- fs.__setResponse('existsSync', false);
79
- utils.safeWrite({ dirname: _n('/foo'), basename: 'bar.js' }, 'baz');
80
- expect(fs.__spy.existsSync).toHaveBeenCalledWith(_n('/foo'));
81
- expect(fs.__spy.mkdirSync).toHaveBeenCalledWith(_n('/foo'));
82
- expect(fs.__spy.writeFileSync).toHaveBeenCalledWith(_n('/foo/bar.js'), 'baz', 'UTF-8');
83
- });
84
- });
85
-
86
- describe('logChange', () => {
87
- test('calls console.log', () => {
88
- expect(spyError).not.toHaveBeenCalled();
89
- expect(spyLog).not.toHaveBeenCalled();
90
- utils.logChange('foo');
91
- expect(spyError).not.toHaveBeenCalled();
92
- expect(spyLog).toHaveBeenCalled();
93
- });
94
- });
95
-
96
- describe('logRemoval', () => {
97
- test('calls console.log', () => {
98
- expect(spyError).not.toHaveBeenCalled();
99
- expect(spyLog).not.toHaveBeenCalled();
100
- utils.logRemoval('foo');
101
- expect(spyError).not.toHaveBeenCalled();
102
- expect(spyLog).toHaveBeenCalled();
103
- });
104
- });
105
-
106
- describe('logError', () => {
107
- test('calls console.error', () => {
108
- expect(spyError).not.toHaveBeenCalled();
109
- expect(spyLog).not.toHaveBeenCalled();
110
- utils.logError('foo');
111
- expect(spyError).toHaveBeenCalled();
112
- expect(spyLog).not.toHaveBeenCalled();
113
- });
114
- });
115
-
116
- describe('parameterizeString', () => {
117
- test('translates', () => {
118
- expect(utils.parameterizeString('foo', {})).toBe('foo');
119
- expect(utils.parameterizeString('_foo_', {})).toBe('_foo_');
120
- expect(utils.parameterizeString('_foo_', { foo: 'bar' })).toBe('bar');
121
- expect(utils.parameterizeString('_foo.bar_', { foo: { bar: 'baz' } })).toBe('baz');
122
- expect(utils.parameterizeString('_foo_', { foo: 2 })).toBe('2');
123
- });
124
- });
125
-
126
- describe('lowerCaseFirst', () => {
127
- test('lowers first letter', () => {
128
- expect(utils.lowerCaseFirst('Foo')).toBe('foo');
129
- expect(utils.lowerCaseFirst('FOO')).toBe('fOO');
130
- });
131
- test('leaves strings with lower-first alone', () => {
132
- expect(utils.lowerCaseFirst('')).toBe('');
133
- expect(utils.lowerCaseFirst('foo')).toBe('foo');
134
- expect(utils.lowerCaseFirst('fOO')).toBe('fOO');
135
- expect(utils.lowerCaseFirst('foO')).toBe('foO');
136
- });
137
- test('ignores numbers at the start', () => {
138
- expect(utils.lowerCaseFirst('0oO')).toBe('0oO');
139
- });
140
- test('passes through non-strings', () => {
141
- expect(utils.lowerCaseFirst(false)).toBe(false);
142
- expect(utils.lowerCaseFirst(undefined)).toBe(undefined);
143
- expect(utils.lowerCaseFirst(null)).toBe(null);
144
- expect(utils.lowerCaseFirst(1)).toBe(1);
145
- });
146
- });