@jjrawlins/cdk-git-tagger 0.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.
@@ -0,0 +1,63 @@
1
+ const realBinding = process.binding('fs');
2
+
3
+ let storedBinding;
4
+
5
+ /**
6
+ * Perform action, bypassing mock FS
7
+ * @param {Function} fn The function.
8
+ * @example
9
+ * // This file exists on the real FS, not on the mocked FS
10
+ * const filePath = '/path/file.json';
11
+ * const data = mock.bypass(() => fs.readFileSync(filePath, 'utf-8'));
12
+ * @return {*} The return.
13
+ */
14
+ module.exports = function bypass(fn) {
15
+ if (typeof fn !== 'function') {
16
+ throw new Error(`Must provide a function to perform for mock.bypass()`);
17
+ }
18
+
19
+ disable();
20
+
21
+ let result;
22
+ try {
23
+ result = fn();
24
+ if (result && typeof result.then === 'function') {
25
+ return result.then(
26
+ (r) => {
27
+ enable();
28
+ return r;
29
+ },
30
+ (err) => {
31
+ enable();
32
+ throw err;
33
+ }
34
+ );
35
+ } else {
36
+ enable();
37
+ return result;
38
+ }
39
+ } catch (err) {
40
+ enable();
41
+ throw err;
42
+ }
43
+ };
44
+
45
+ /**
46
+ * Temporarily disable Mocked FS
47
+ */
48
+ function disable() {
49
+ if (realBinding._mockedBinding) {
50
+ storedBinding = realBinding._mockedBinding;
51
+ delete realBinding._mockedBinding;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Enables Mocked FS after being disabled by disable()
57
+ */
58
+ function enable() {
59
+ if (storedBinding) {
60
+ realBinding._mockedBinding = storedBinding;
61
+ storedBinding = undefined;
62
+ }
63
+ }
@@ -0,0 +1,128 @@
1
+ 'use strict';
2
+
3
+ const constants = require('constants');
4
+
5
+ /**
6
+ * Create a new file descriptor.
7
+ * @param {number} flags Flags.
8
+ * @param {boolean} isPromise descriptor was opened via fs.promises
9
+ * @class
10
+ */
11
+ function FileDescriptor(flags, isPromise = false) {
12
+ /**
13
+ * Flags.
14
+ * @type {number}
15
+ */
16
+ this._flags = flags;
17
+
18
+ /**
19
+ * File system item.
20
+ * @type {Item}
21
+ */
22
+ this._item = null;
23
+
24
+ /**
25
+ * Current file position.
26
+ * @type {number}
27
+ */
28
+ this._position = 0;
29
+
30
+ this._isPromise = isPromise;
31
+ }
32
+
33
+ /**
34
+ * Set the item.
35
+ * @param {Item} item File system item.
36
+ */
37
+ FileDescriptor.prototype.setItem = function (item) {
38
+ this._item = item;
39
+ };
40
+
41
+ /**
42
+ * Get the item.
43
+ * @return {Item} File system item.
44
+ */
45
+ FileDescriptor.prototype.getItem = function () {
46
+ return this._item;
47
+ };
48
+
49
+ /**
50
+ * Get the current file position.
51
+ * @return {number} File position.
52
+ */
53
+ FileDescriptor.prototype.getPosition = function () {
54
+ return this._position;
55
+ };
56
+
57
+ /**
58
+ * Set the current file position.
59
+ * @param {number} position File position.
60
+ */
61
+ FileDescriptor.prototype.setPosition = function (position) {
62
+ this._position = position;
63
+ };
64
+
65
+ /**
66
+ * Check if file opened for appending.
67
+ * @return {boolean} Opened for appending.
68
+ */
69
+ FileDescriptor.prototype.isAppend = function () {
70
+ return (this._flags & constants.O_APPEND) === constants.O_APPEND;
71
+ };
72
+
73
+ /**
74
+ * Check if file opened for creation.
75
+ * @return {boolean} Opened for creation.
76
+ */
77
+ FileDescriptor.prototype.isCreate = function () {
78
+ return (this._flags & constants.O_CREAT) === constants.O_CREAT;
79
+ };
80
+
81
+ /**
82
+ * Check if file opened for reading.
83
+ * @return {boolean} Opened for reading.
84
+ */
85
+ FileDescriptor.prototype.isRead = function () {
86
+ return (this._flags & constants.O_WRONLY) !== constants.O_WRONLY;
87
+ };
88
+
89
+ /**
90
+ * Check if file opened for writing.
91
+ * @return {boolean} Opened for writing.
92
+ */
93
+ FileDescriptor.prototype.isWrite = function () {
94
+ return (
95
+ (this._flags & constants.O_WRONLY) === constants.O_WRONLY ||
96
+ (this._flags & constants.O_RDWR) === constants.O_RDWR
97
+ );
98
+ };
99
+
100
+ /**
101
+ * Check if file opened for truncating.
102
+ * @return {boolean} Opened for truncating.
103
+ */
104
+ FileDescriptor.prototype.isTruncate = function () {
105
+ return (this._flags & constants.O_TRUNC) === constants.O_TRUNC;
106
+ };
107
+
108
+ /**
109
+ * Check if file opened with exclusive flag.
110
+ * @return {boolean} Opened with exclusive.
111
+ */
112
+ FileDescriptor.prototype.isExclusive = function () {
113
+ return (this._flags & constants.O_EXCL) === constants.O_EXCL;
114
+ };
115
+
116
+ /**
117
+ * Check if the file descriptor was opened as a promise
118
+ * @return {boolean} Opened from fs.promise
119
+ */
120
+ FileDescriptor.prototype.isPromise = function () {
121
+ return this._isPromise;
122
+ };
123
+
124
+ /**
125
+ * Export the constructor.
126
+ * @type {function()}
127
+ */
128
+ module.exports = FileDescriptor;
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ const util = require('util');
4
+ const Item = require('./item.js');
5
+ const constants = require('constants');
6
+
7
+ /**
8
+ * A directory.
9
+ * @class
10
+ */
11
+ function Directory() {
12
+ Item.call(this);
13
+
14
+ /**
15
+ * Items in this directory.
16
+ * @type {Object<string, Item>}
17
+ */
18
+ this._items = {};
19
+
20
+ /**
21
+ * Permissions.
22
+ */
23
+ this._mode = 511; // 0777
24
+ }
25
+ util.inherits(Directory, Item);
26
+
27
+ /**
28
+ * Add an item to the directory.
29
+ * @param {string} name The name to give the item.
30
+ * @param {Item} item The item to add.
31
+ * @return {Item} The added item.
32
+ */
33
+ Directory.prototype.addItem = function (name, item) {
34
+ if (this._items.hasOwnProperty(name)) {
35
+ throw new Error('Item with the same name already exists: ' + name);
36
+ }
37
+ this._items[name] = item;
38
+ ++item.links;
39
+ if (item instanceof Directory) {
40
+ // for '.' entry
41
+ ++item.links;
42
+ // for subdirectory
43
+ ++this.links;
44
+ }
45
+ this.setMTime(new Date());
46
+ return item;
47
+ };
48
+
49
+ /**
50
+ * Get a named item.
51
+ * @param {string} name Item name.
52
+ * @return {Item} The named item (or null if none).
53
+ */
54
+ Directory.prototype.getItem = function (name) {
55
+ let item = null;
56
+ if (this._items.hasOwnProperty(name)) {
57
+ item = this._items[name];
58
+ }
59
+ return item;
60
+ };
61
+
62
+ /**
63
+ * Remove an item.
64
+ * @param {string} name Name of item to remove.
65
+ * @return {Item} The orphan item.
66
+ */
67
+ Directory.prototype.removeItem = function (name) {
68
+ if (!this._items.hasOwnProperty(name)) {
69
+ throw new Error('Item does not exist in directory: ' + name);
70
+ }
71
+ const item = this._items[name];
72
+ delete this._items[name];
73
+ --item.links;
74
+ if (item instanceof Directory) {
75
+ // for '.' entry
76
+ --item.links;
77
+ // for subdirectory
78
+ --this.links;
79
+ }
80
+ this.setMTime(new Date());
81
+ return item;
82
+ };
83
+
84
+ /**
85
+ * Get list of item names in this directory.
86
+ * @return {Array<string>} Item names.
87
+ */
88
+ Directory.prototype.list = function () {
89
+ return Object.keys(this._items).sort();
90
+ };
91
+
92
+ /**
93
+ * Get directory stats.
94
+ * @param {bolean} bigint Use BigInt.
95
+ * @return {object} Stats properties.
96
+ */
97
+ Directory.prototype.getStats = function (bigint) {
98
+ const stats = Item.prototype.getStats.call(this, bigint);
99
+ const convert = bigint ? (v) => BigInt(v) : (v) => v;
100
+
101
+ stats[1] = convert(this.getMode() | constants.S_IFDIR); // mode
102
+ stats[8] = convert(1); // size
103
+ stats[9] = convert(1); // blocks
104
+
105
+ return stats;
106
+ };
107
+
108
+ /**
109
+ * Export the constructor.
110
+ * @type {function()}
111
+ */
112
+ module.exports = Directory;
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ const uvBinding = process.binding('uv');
4
+
5
+ /**
6
+ * Error codes from libuv.
7
+ * @enum {number}
8
+ */
9
+ const codes = {};
10
+
11
+ uvBinding.getErrorMap().forEach(function (value, errno) {
12
+ const code = value[0];
13
+ const message = value[1];
14
+ codes[code] = {errno: errno, message: message};
15
+ });
16
+
17
+ /**
18
+ * Create an error.
19
+ * @param {string} code Error code.
20
+ * @param {string} path Path (optional).
21
+ * @class
22
+ */
23
+ function FSError(code, path) {
24
+ if (!codes.hasOwnProperty(code)) {
25
+ throw new Error('Programmer error, invalid error code: ' + code);
26
+ }
27
+ Error.call(this);
28
+ const details = codes[code];
29
+ let message = code + ', ' + details.message;
30
+ if (path) {
31
+ message += " '" + path + "'";
32
+ }
33
+ this.message = message;
34
+ this.code = code;
35
+ this.errno = details.errno;
36
+ if (path !== undefined) {
37
+ this.path = path;
38
+ }
39
+ Error.captureStackTrace(this, FSError);
40
+ }
41
+ FSError.prototype = new Error();
42
+ FSError.codes = codes;
43
+
44
+ /**
45
+ * Create an abort error for when an asynchronous task was aborted.
46
+ * @class
47
+ */
48
+ function AbortError() {
49
+ Error.call(this);
50
+ this.code = 'ABORT_ERR';
51
+ this.name = 'AbortError';
52
+ Error.captureStackTrace(this, AbortError);
53
+ }
54
+ AbortError.prototype = new Error();
55
+
56
+ /**
57
+ * FSError constructor.
58
+ */
59
+ exports.FSError = FSError;
60
+
61
+ /**
62
+ * AbortError constructor.
63
+ */
64
+ exports.AbortError = AbortError;
@@ -0,0 +1,127 @@
1
+ 'use strict';
2
+
3
+ const util = require('util');
4
+ const Item = require('./item.js');
5
+ const constants = require('constants');
6
+
7
+ const EMPTY = Buffer.alloc(0);
8
+
9
+ /**
10
+ * A file.
11
+ * @class
12
+ */
13
+ function File() {
14
+ Item.call(this);
15
+
16
+ /**
17
+ * File content.
18
+ * @type {Buffer}
19
+ */
20
+ this._content = EMPTY;
21
+ }
22
+ util.inherits(File, Item);
23
+
24
+ /**
25
+ * Get the file contents.
26
+ * @return {Buffer} File contents.
27
+ */
28
+ File.prototype.getContent = function () {
29
+ this.setATime(new Date());
30
+ return this._content;
31
+ };
32
+
33
+ /**
34
+ * Set the file contents.
35
+ * @param {string|Buffer} content File contents.
36
+ */
37
+ File.prototype.setContent = function (content) {
38
+ if (typeof content === 'string') {
39
+ content = Buffer.from(content);
40
+ } else if (!Buffer.isBuffer(content)) {
41
+ throw new Error('File content must be a string or buffer');
42
+ }
43
+ this._content = content;
44
+ const now = Date.now();
45
+ this.setCTime(new Date(now));
46
+ this.setMTime(new Date(now));
47
+ };
48
+
49
+ /**
50
+ * Get file stats.
51
+ * @param {boolean} bigint Use BigInt.
52
+ * @return {object} Stats properties.
53
+ */
54
+ File.prototype.getStats = function (bigint) {
55
+ const size = this._content.length;
56
+ const stats = Item.prototype.getStats.call(this, bigint);
57
+ const convert = bigint ? (v) => BigInt(v) : (v) => v;
58
+
59
+ stats[1] = convert(this.getMode() | constants.S_IFREG); // mode
60
+ stats[8] = convert(size); // size
61
+ stats[9] = convert(Math.ceil(size / 512)); // blocks
62
+
63
+ return stats;
64
+ };
65
+
66
+ /**
67
+ * Export the constructor.
68
+ * @type {function()}
69
+ */
70
+ module.exports = File;
71
+ exports = module.exports;
72
+
73
+ /**
74
+ * Standard input.
75
+ * @class
76
+ */
77
+ function StandardInput() {
78
+ File.call(this);
79
+ this.setMode(438); // 0666
80
+ }
81
+ util.inherits(StandardInput, File);
82
+
83
+ exports.StandardInput = StandardInput;
84
+
85
+ /**
86
+ * Standard output.
87
+ * @class
88
+ */
89
+ function StandardOutput() {
90
+ File.call(this);
91
+ this.setMode(438); // 0666
92
+ }
93
+ util.inherits(StandardOutput, File);
94
+
95
+ /**
96
+ * Write the contents to stdout.
97
+ * @param {string|Buffer} content File contents.
98
+ */
99
+ StandardOutput.prototype.setContent = function (content) {
100
+ if (process.stdout.isTTY) {
101
+ process.stdout.write(content);
102
+ }
103
+ };
104
+
105
+ exports.StandardOutput = StandardOutput;
106
+
107
+ /**
108
+ * Standard error.
109
+ * @class
110
+ */
111
+ function StandardError() {
112
+ File.call(this);
113
+ this.setMode(438); // 0666
114
+ }
115
+ util.inherits(StandardError, File);
116
+
117
+ /**
118
+ * Write the contents to stderr.
119
+ * @param {string|Buffer} content File contents.
120
+ */
121
+ StandardError.prototype.setContent = function (content) {
122
+ if (process.stderr.isTTY) {
123
+ process.stderr.write(content);
124
+ }
125
+ };
126
+
127
+ exports.StandardError = StandardError;