pwd-fs 3.1.4 → 3.2.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 @@
1
+ export declare function bitmask(mode: number): number;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bitmask = void 0;
4
+ function bitmask(mode) {
5
+ const type = typeof mode;
6
+ if (type !== 'number') {
7
+ throw new Error(`Argument of type '${type}' is not assignable to parameter of type 'number'.`);
8
+ }
9
+ const permissions = [0o400, 0o200, 0o100, 0o040, 0o020, 0o010, 0o004, 0o002, 0o001];
10
+ let umask = 0o000;
11
+ for (const flag of permissions) {
12
+ if (mode & flag) {
13
+ umask += flag;
14
+ }
15
+ }
16
+ return umask;
17
+ }
18
+ exports.bitmask = bitmask;
@@ -1,16 +1,17 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import fs from 'node:fs';
4
+ import { bitmask } from './bitmask';
4
5
  export type Mode = keyof IConstants;
5
6
  export type Flag = Mode | 'a';
6
7
  export type Stats = fs.Stats;
8
+ export * from './bitmask';
7
9
  export interface IConstants {
8
10
  e: number;
9
11
  r: number;
10
12
  w: number;
11
13
  x: number;
12
14
  }
13
- export declare function bitmask(mode: number): number;
14
15
  export declare class PoweredFileSystem {
15
16
  readonly pwd: string;
16
17
  readonly constants: IConstants;
@@ -37,11 +38,15 @@ export declare class PoweredFileSystem {
37
38
  chmod(src: string, mode: number, options?: {
38
39
  sync?: false;
39
40
  }): Promise<void>;
40
- chown(src: string, uid: number, gid: number, options: {
41
+ chown(src: string, options: {
41
42
  sync: true;
43
+ uid?: number;
44
+ gid?: number;
42
45
  }): void;
43
- chown(src: string, uid: number, gid: number, options?: {
46
+ chown(src: string, options?: {
44
47
  sync?: false;
48
+ uid?: number;
49
+ gid?: number;
45
50
  }): Promise<void>;
46
51
  symlink(src: string, use: string, options: {
47
52
  sync: true;
@@ -113,24 +118,36 @@ export declare class PoweredFileSystem {
113
118
  umask?: number;
114
119
  flag?: Flag;
115
120
  }): Promise<void>;
121
+ /**
122
+ * @deprecated The method should not be used
123
+ */
116
124
  append(src: string, data: Buffer, options: {
117
125
  sync: true;
118
126
  encoding: null;
119
127
  umask?: number;
120
128
  flag?: Flag;
121
129
  }): void;
130
+ /**
131
+ * @deprecated The method should not be used
132
+ */
122
133
  append(src: string, data: string, options: {
123
134
  sync: true;
124
135
  encoding?: BufferEncoding;
125
136
  umask?: number;
126
137
  flag?: Flag;
127
138
  }): void;
139
+ /**
140
+ * @deprecated The method should not be used
141
+ */
128
142
  append(src: string, data: Buffer, options: {
129
143
  sync?: false;
130
144
  encoding: null;
131
145
  umask?: number;
132
146
  flag?: Flag;
133
147
  }): Promise<void>;
148
+ /**
149
+ * @deprecated The method should not be used
150
+ */
134
151
  append(src: string, data: string, options?: {
135
152
  sync?: false;
136
153
  encoding?: BufferEncoding;
@@ -1,38 +1,29 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
18
  };
5
19
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PoweredFileSystem = exports.bitmask = void 0;
20
+ exports.PoweredFileSystem = void 0;
7
21
  const node_fs_1 = __importDefault(require("node:fs"));
8
22
  const node_path_1 = __importDefault(require("node:path"));
9
- const recurse_io_1 = __importDefault(require("./recurse-io"));
10
- const recurse_io_sync_1 = __importDefault(require("./recurse-io-sync"));
11
- const permissions = [
12
- 0o400, // OWNER_READ
13
- 0o200, // OWNER_WRITE
14
- 0o100, // OWNER_EXECUTE
15
- 0o040, // GROUP_READ
16
- 0o020, // GROUP_WRITE
17
- 0o010, // GROUP_EXECUTE
18
- 0o004, // OTHERS_READ
19
- 0o002, // OTHERS_WRITE
20
- 0o001 // OTHERS_EXECUTE
21
- ];
22
- function bitmask(mode) {
23
- const type = typeof mode;
24
- if (type !== 'number') {
25
- throw new Error(`Argument of type '${type}' is not assignable to parameter of type 'number'.`);
26
- }
27
- let umask = 0o000;
28
- for (const flag of permissions) {
29
- if (mode & flag) {
30
- umask += flag;
31
- }
32
- }
33
- return umask;
34
- }
35
- exports.bitmask = bitmask;
23
+ const recurse_io_1 = require("./recurse-io");
24
+ const recurse_io_sync_1 = require("./recurse-io-sync");
25
+ const bitmask_1 = require("./bitmask");
26
+ __exportStar(require("./bitmask"), exports);
36
27
  class PoweredFileSystem {
37
28
  pwd;
38
29
  constants = {
@@ -41,7 +32,7 @@ class PoweredFileSystem {
41
32
  w: node_fs_1.default.constants.W_OK,
42
33
  x: node_fs_1.default.constants.X_OK
43
34
  };
44
- static bitmask = bitmask;
35
+ static bitmask = bitmask_1.bitmask;
45
36
  constructor(pwd) {
46
37
  this.pwd = pwd ? node_path_1.default.resolve(pwd) : process.cwd();
47
38
  }
@@ -80,10 +71,10 @@ class PoweredFileSystem {
80
71
  chmod(src, mode, { sync = false } = {}) {
81
72
  src = this.resolve(src);
82
73
  if (sync) {
83
- return recurse_io_sync_1.default.chmod(src, mode);
74
+ return (0, recurse_io_sync_1.chmodSync)(src, mode);
84
75
  }
85
76
  return new Promise((resolve, reject) => {
86
- recurse_io_1.default.chmod(src, mode, (err) => {
77
+ (0, recurse_io_1.chmod)(src, mode, (err) => {
87
78
  if (err) {
88
79
  return reject(err);
89
80
  }
@@ -91,13 +82,13 @@ class PoweredFileSystem {
91
82
  });
92
83
  });
93
84
  }
94
- chown(src, uid, gid, { sync = false } = {}) {
85
+ chown(src, { sync = false, uid = 0, gid = 0 } = {}) {
95
86
  src = this.resolve(src);
96
87
  if (sync) {
97
- return recurse_io_sync_1.default.chown(src, uid, gid);
88
+ return (0, recurse_io_sync_1.chownSync)(src, uid, gid);
98
89
  }
99
90
  return new Promise((resolve, reject) => {
100
- recurse_io_1.default.chown(src, uid, gid, (err) => {
91
+ (0, recurse_io_1.chown)(src, uid, gid, (err) => {
101
92
  if (err) {
102
93
  return reject(err);
103
94
  }
@@ -124,10 +115,10 @@ class PoweredFileSystem {
124
115
  src = this.resolve(src);
125
116
  dir = this.resolve(dir);
126
117
  if (sync) {
127
- return recurse_io_sync_1.default.copy(src, dir, umask);
118
+ return (0, recurse_io_sync_1.copySync)(src, dir, umask);
128
119
  }
129
120
  return new Promise((resolve, reject) => {
130
- recurse_io_1.default.copy(src, dir, umask, (err) => {
121
+ (0, recurse_io_1.copy)(src, dir, umask, (err) => {
131
122
  if (err) {
132
123
  return reject(err);
133
124
  }
@@ -153,15 +144,19 @@ class PoweredFileSystem {
153
144
  remove(src, { sync = false } = {}) {
154
145
  src = this.resolve(src);
155
146
  if (sync) {
156
- return recurse_io_sync_1.default.remove(src);
147
+ (0, recurse_io_sync_1.removeSync)(src);
157
148
  }
158
149
  return new Promise((resolve, reject) => {
159
- recurse_io_1.default.remove(src, (err) => {
150
+ const callback = (err) => {
160
151
  if (err) {
161
152
  return reject(err);
162
153
  }
163
154
  resolve();
164
- });
155
+ };
156
+ if ('rm' in node_fs_1.default) {
157
+ return node_fs_1.default.rm(src, { recursive: true }, callback);
158
+ }
159
+ (0, recurse_io_1.remove)(src, callback);
165
160
  });
166
161
  }
167
162
  read(src, { sync = false, encoding = 'utf8', flag = 'r' } = {}) {
@@ -207,6 +202,9 @@ class PoweredFileSystem {
207
202
  });
208
203
  });
209
204
  }
205
+ /**
206
+ * @deprecated The method should not be used
207
+ */
210
208
  append(src, data, { sync = false, encoding = 'utf8', umask = 0o000, flag = 'a' } = {}) {
211
209
  src = this.resolve(src);
212
210
  const mode = 0o666 - umask;
@@ -249,10 +247,10 @@ class PoweredFileSystem {
249
247
  mkdir(dir, { umask = 0o000, sync = false } = {}) {
250
248
  dir = this.resolve(dir);
251
249
  if (sync) {
252
- return recurse_io_sync_1.default.mkdir(dir, umask);
250
+ return (0, recurse_io_sync_1.mkdirSync)(dir, umask);
253
251
  }
254
252
  return new Promise((resolve, reject) => {
255
- recurse_io_1.default.mkdir(dir, umask, (err) => {
253
+ (0, recurse_io_1.mkdir)(dir, umask, (err) => {
256
254
  if (err) {
257
255
  return reject(err);
258
256
  }
@@ -1,13 +1,5 @@
1
- declare function chmod(src: string, mode: number): void;
2
- declare function chown(src: string, uid: number, gid: number): void;
3
- declare function copy(src: string, dir: string, umask: number): void;
4
- declare function remove(src: string): void;
5
- declare function mkdir(dir: string, umask: number): void;
6
- declare const _default: {
7
- chmod: typeof chmod;
8
- chown: typeof chown;
9
- copy: typeof copy;
10
- remove: typeof remove;
11
- mkdir: typeof mkdir;
12
- };
13
- export default _default;
1
+ export declare function chmodSync(src: string, mode: number): void;
2
+ export declare function chownSync(src: string, uid: number, gid: number): void;
3
+ export declare function copySync(src: string, dir: string, umask: number): void;
4
+ export declare function removeSync(src: string): void;
5
+ export declare function mkdirSync(dir: string, umask: number): void;
@@ -3,30 +3,39 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mkdirSync = exports.removeSync = exports.copySync = exports.chownSync = exports.chmodSync = void 0;
6
7
  const node_fs_1 = __importDefault(require("node:fs"));
7
8
  const node_path_1 = __importDefault(require("node:path"));
8
9
  const { sep } = node_path_1.default;
9
- function chmod(src, mode) {
10
- const stat = node_fs_1.default.statSync(src);
11
- if (stat.isDirectory()) {
10
+ function chmodSync(src, mode) {
11
+ const stats = node_fs_1.default.statSync(src);
12
+ if (stats.isDirectory()) {
12
13
  const list = node_fs_1.default.readdirSync(src);
13
14
  for (const loc of list) {
14
- chmod(`${src}${sep}${loc}`, mode);
15
+ chmodSync(`${src}${sep}${loc}`, mode);
15
16
  }
16
17
  }
17
18
  node_fs_1.default.chmodSync(src, mode);
18
19
  }
19
- function chown(src, uid, gid) {
20
- const stat = node_fs_1.default.statSync(src);
21
- if (stat.isDirectory()) {
20
+ exports.chmodSync = chmodSync;
21
+ function chownSync(src, uid, gid) {
22
+ const stats = node_fs_1.default.statSync(src);
23
+ if (uid === 0) {
24
+ uid = stats.uid;
25
+ }
26
+ if (gid === 0) {
27
+ gid = stats.gid;
28
+ }
29
+ if (stats.isDirectory()) {
22
30
  const list = node_fs_1.default.readdirSync(src);
23
31
  for (const loc of list) {
24
- chown(`${src}${sep}${loc}`, uid, gid);
32
+ chownSync(`${src}${sep}${loc}`, uid, gid);
25
33
  }
26
34
  }
27
35
  node_fs_1.default.chownSync(src, uid, gid);
28
36
  }
29
- function copy(src, dir, umask) {
37
+ exports.chownSync = chownSync;
38
+ function copySync(src, dir, umask) {
30
39
  const stat = node_fs_1.default.statSync(src);
31
40
  if (stat.isDirectory()) {
32
41
  const list = node_fs_1.default.readdirSync(src);
@@ -36,7 +45,7 @@ function copy(src, dir, umask) {
36
45
  dir = `${dir}${sep}${loc}`;
37
46
  node_fs_1.default.mkdirSync(dir, mode);
38
47
  for (const loc of list) {
39
- copy(`${src}${sep}${loc}`, dir, umask);
48
+ copySync(`${src}${sep}${loc}`, dir, umask);
40
49
  }
41
50
  }
42
51
  else {
@@ -45,12 +54,13 @@ function copy(src, dir, umask) {
45
54
  node_fs_1.default.copyFileSync(src, use);
46
55
  }
47
56
  }
48
- function remove(src) {
49
- const stat = node_fs_1.default.statSync(src);
50
- if (stat.isDirectory()) {
57
+ exports.copySync = copySync;
58
+ function removeSync(src) {
59
+ const stats = node_fs_1.default.statSync(src);
60
+ if (stats.isDirectory()) {
51
61
  const list = node_fs_1.default.readdirSync(src);
52
62
  for (const loc of list) {
53
- remove(`${src}${sep}${loc}`);
63
+ removeSync(`${src}${sep}${loc}`);
54
64
  }
55
65
  node_fs_1.default.rmdirSync(src);
56
66
  }
@@ -58,13 +68,14 @@ function remove(src) {
58
68
  node_fs_1.default.unlinkSync(src);
59
69
  }
60
70
  }
61
- function mkdir(dir, umask) {
71
+ exports.removeSync = removeSync;
72
+ function mkdirSync(dir, umask) {
62
73
  const mode = 0o777 - umask;
63
74
  const cwd = process.cwd();
64
75
  let use = '';
65
76
  if (dir.indexOf(cwd) === 0) {
66
77
  use = cwd;
67
- dir = dir.substr(cwd.length);
78
+ dir = dir.substring(cwd.length);
68
79
  }
69
80
  const ways = dir.split(sep).slice(1);
70
81
  for (const loc of ways) {
@@ -79,10 +90,4 @@ function mkdir(dir, umask) {
79
90
  }
80
91
  }
81
92
  }
82
- exports.default = {
83
- chmod,
84
- chown,
85
- copy,
86
- remove,
87
- mkdir
88
- };
93
+ exports.mkdirSync = mkdirSync;
@@ -1,17 +1,7 @@
1
1
  /// <reference types="node" />
2
- import fs from 'node:fs';
3
- export type Files = Array<string>;
4
- export type NoParamCallback = fs.NoParamCallback;
5
- declare function chmod(src: string, mode: number, callback: NoParamCallback): void;
6
- declare function chown(src: string, uid: number, gid: number, callback: NoParamCallback): void;
7
- declare function copy(src: string, dir: string, umask: number, callback: NoParamCallback): void;
8
- declare function remove(src: string, callback: NoParamCallback): void;
9
- declare function mkdir(dir: string, umask: number, callback: NoParamCallback): void;
10
- declare const _default: {
11
- chmod: typeof chmod;
12
- chown: typeof chown;
13
- copy: typeof copy;
14
- remove: typeof remove;
15
- mkdir: typeof mkdir;
16
- };
17
- export default _default;
2
+ import { NoParamCallback } from 'node:fs';
3
+ export declare function chmod(src: string, mode: number, callback: NoParamCallback): void;
4
+ export declare function chown(src: string, uid: number, gid: number, callback: NoParamCallback): void;
5
+ export declare function copy(src: string, dir: string, umask: number, callback: NoParamCallback): void;
6
+ export declare function remove(src: string, callback: NoParamCallback): void;
7
+ export declare function mkdir(dir: string, umask: number, callback: NoParamCallback): void;
@@ -3,16 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.mkdir = exports.remove = exports.copy = exports.chown = exports.chmod = void 0;
6
7
  const node_fs_1 = __importDefault(require("node:fs"));
7
8
  const node_path_1 = __importDefault(require("node:path"));
8
9
  const { sep } = node_path_1.default;
9
10
  function chmod(src, mode, callback) {
10
11
  let reduce = 0;
11
- node_fs_1.default.stat(src, (err, stat) => {
12
+ node_fs_1.default.stat(src, (err, stats) => {
12
13
  if (err) {
13
14
  return callback(err);
14
15
  }
15
- if (stat.isDirectory()) {
16
+ if (stats.isDirectory()) {
16
17
  node_fs_1.default.readdir(src, (err, list) => {
17
18
  if (err) {
18
19
  return callback(err);
@@ -38,12 +39,19 @@ function chmod(src, mode, callback) {
38
39
  }
39
40
  });
40
41
  }
42
+ exports.chmod = chmod;
41
43
  function chown(src, uid, gid, callback) {
42
44
  let reduce = 0;
43
45
  node_fs_1.default.stat(src, (err, stats) => {
44
46
  if (err) {
45
47
  return callback(err);
46
48
  }
49
+ if (uid === 0) {
50
+ uid = stats.uid;
51
+ }
52
+ if (gid === 0) {
53
+ gid = stats.gid;
54
+ }
47
55
  if (stats.isDirectory()) {
48
56
  node_fs_1.default.readdir(src, (err, list) => {
49
57
  if (err) {
@@ -70,6 +78,7 @@ function chown(src, uid, gid, callback) {
70
78
  }
71
79
  });
72
80
  }
81
+ exports.chown = chown;
73
82
  function copy(src, dir, umask, callback) {
74
83
  let reduce = 0;
75
84
  node_fs_1.default.stat(src, (err, stat) => {
@@ -122,6 +131,7 @@ function copy(src, dir, umask, callback) {
122
131
  }
123
132
  });
124
133
  }
134
+ exports.copy = copy;
125
135
  function remove(src, callback) {
126
136
  node_fs_1.default.stat(src, (err, stat) => {
127
137
  if (err) {
@@ -153,6 +163,7 @@ function remove(src, callback) {
153
163
  }
154
164
  });
155
165
  }
166
+ exports.remove = remove;
156
167
  function mkdir(dir, umask, callback) {
157
168
  const cwd = process.cwd();
158
169
  if (dir === cwd) {
@@ -175,7 +186,7 @@ function mkdir(dir, umask, callback) {
175
186
  let use = '';
176
187
  if (dir.indexOf(cwd) === 0) {
177
188
  use = cwd;
178
- dir = dir.substr(cwd.length);
189
+ dir = dir.substring(cwd.length);
179
190
  }
180
191
  const files = dir.split(sep);
181
192
  const mode = 0o777 - umask;
@@ -183,10 +194,4 @@ function mkdir(dir, umask, callback) {
183
194
  iter.next();
184
195
  iter.next(iter);
185
196
  }
186
- exports.default = {
187
- chmod,
188
- chown,
189
- copy,
190
- remove,
191
- mkdir
192
- };
197
+ exports.mkdir = mkdir;
@@ -9,7 +9,7 @@ const chance_1 = __importDefault(require("chance"));
9
9
  const expect_1 = require("expect");
10
10
  const __fmock_1 = require("./__fmock");
11
11
  const src_1 = require("../src");
12
- describe('chown(src, uid, gid [, options])', () => {
12
+ describe('chown(src, [, options])', () => {
13
13
  const chance = new chance_1.default();
14
14
  beforeEach(() => {
15
15
  (0, __fmock_1.fmock)({
@@ -25,34 +25,38 @@ describe('chown(src, uid, gid [, options])', () => {
25
25
  });
26
26
  it('Positive: Changes the permissions of a file', async () => {
27
27
  const { uid, gid } = node_fs_1.default.statSync('./tmpdir/tings.txt');
28
- await src_1.pfs.chown('./tmpdir/tings.txt', uid, gid);
28
+ await src_1.pfs.chown('./tmpdir/tings.txt', { uid, gid });
29
29
  (0, node_assert_1.default)(uid && gid);
30
30
  });
31
31
  it('Positive: Changes the permissions of a directory', async () => {
32
32
  const { uid, gid } = node_fs_1.default.statSync('./tmpdir/digest');
33
- await src_1.pfs.chown('./tmpdir/digest', uid, gid);
33
+ await src_1.pfs.chown('./tmpdir/digest', { uid, gid });
34
34
  (0, node_assert_1.default)(uid && gid);
35
35
  });
36
36
  it('Negative: To a non-existent resource to return an Error', async () => {
37
37
  const guid = chance.guid();
38
38
  const { uid, gid } = node_fs_1.default.statSync('./tmpdir/tings.txt');
39
39
  await (0, expect_1.expect)(async () => {
40
- await src_1.pfs.chown(`./tmpdir/${guid}`, uid, gid);
40
+ await src_1.pfs.chown(`./tmpdir/${guid}`, { uid, gid });
41
41
  })
42
42
  .rejects
43
43
  .toThrow();
44
44
  });
45
45
  it('[sync] Positive: Changes the permissions of a file', () => {
46
46
  const { uid, gid } = node_fs_1.default.statSync('./tmpdir/tings.txt');
47
- src_1.pfs.chown('./tmpdir/tings.txt', uid, gid, {
48
- sync: true
47
+ src_1.pfs.chown('./tmpdir/tings.txt', {
48
+ sync: true,
49
+ uid,
50
+ gid
49
51
  });
50
52
  (0, node_assert_1.default)(uid && gid);
51
53
  });
52
54
  it('[sync] Positive: Changes the permissions of a directory', () => {
53
55
  const { uid, gid } = node_fs_1.default.statSync('./tmpdir/digest');
54
- src_1.pfs.chown('./tmpdir/digest', uid, gid, {
55
- sync: true
56
+ src_1.pfs.chown('./tmpdir/digest', {
57
+ sync: true,
58
+ uid,
59
+ gid
56
60
  });
57
61
  (0, node_assert_1.default)(uid && gid);
58
62
  });
@@ -60,8 +64,10 @@ describe('chown(src, uid, gid [, options])', () => {
60
64
  const guid = chance.guid();
61
65
  const { uid, gid } = node_fs_1.default.statSync('./tmpdir/tings.txt');
62
66
  node_assert_1.default.throws(() => {
63
- src_1.pfs.chown(`./tmpdir/${guid}`, uid, gid, {
64
- sync: true
67
+ src_1.pfs.chown(`./tmpdir/${guid}`, {
68
+ sync: true,
69
+ uid,
70
+ gid
65
71
  });
66
72
  });
67
73
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwd-fs",
3
- "version": "3.1.4",
3
+ "version": "3.2.0",
4
4
  "description": "Extend the file system the capabilities of declaring the present working directory and recursive execution",
5
5
  "keywords": [
6
6
  "umask",
@@ -24,7 +24,7 @@
24
24
  "license": "MIT",
25
25
  "repository": {
26
26
  "type": "git",
27
- "url": "https://github.com/woodger/pwd-fs.git"
27
+ "url": "git@github.com:woodger/pwd-fs.git"
28
28
  },
29
29
  "engines": {
30
30
  "node": ">=13.2.0"
package/readme.md CHANGED
@@ -47,7 +47,7 @@ The scope `URI` of the class methods are divided into groups.
47
47
  | URI | Methods |
48
48
  |-----------------------------|------------------------------------------------------------------|
49
49
  | Common (file and directory) | `chmod` `chown` `copy` `remove` `rename` `symlink` `stat` `test` |
50
- | File only | `read` `write` |
50
+ | File only | `read` `write` |
51
51
  | Directory only | `mkdir` `readdir` |
52
52
 
53
53
 
@@ -151,13 +151,13 @@ console.log(bitmask(mode) === 0o750); // true
151
151
 
152
152
  See manuals [chmod(2)](http://man7.org/linux/man-pages/man2/chmod.2.html)
153
153
 
154
- #### pfs.chown(src, uid, gid[, options])
154
+ #### pfs.chown(src, [, options])
155
155
 
156
156
  - `src` <[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)> Absolute or relative path to the resource in the file system. Relative paths will be resolved relative to the present working directory as specified by `pfs.pwd`.
157
- - `uid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>
158
- - `gid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>
159
157
  - `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>
160
158
  - `sync` <[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)> Synchronous execution. **Default:** `false`.
159
+ - `uid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new owner's user id.
160
+ - `gid` <[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)> The file's new group's group id.
161
161
  - returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)> Following successful change, the `Promise` is resolved with an value with a `undefined`.
162
162
 
163
163
  Asynchronously changes owner and group of a file.
package/src/bitmask.ts ADDED
@@ -0,0 +1,20 @@
1
+ export function bitmask(mode: number) {
2
+ const type = typeof mode;
3
+
4
+ if (type !== 'number') {
5
+ throw new Error(
6
+ `Argument of type '${type}' is not assignable to parameter of type 'number'.`
7
+ );
8
+ }
9
+
10
+ const permissions: number[] = [ 0o400, 0o200, 0o100, 0o040, 0o020, 0o010, 0o004, 0o002, 0o001 ];
11
+ let umask = 0o000;
12
+
13
+ for (const flag of permissions) {
14
+ if (mode & flag) {
15
+ umask += flag;
16
+ }
17
+ }
18
+
19
+ return umask;
20
+ }
@@ -1,12 +1,15 @@
1
- import fs from 'node:fs';
1
+ import fs, { NoParamCallback } from 'node:fs';
2
2
  import path from 'node:path';
3
- import recurse, { Files, NoParamCallback } from './recurse-io';
4
- import recurseSync from './recurse-io-sync';
3
+ import { chmod, chown, copy, remove, mkdir } from './recurse-io';
4
+ import { chmodSync, chownSync, copySync, removeSync, mkdirSync } from './recurse-io-sync';
5
+ import { bitmask } from './bitmask';
5
6
 
6
7
  export type Mode = keyof IConstants;
7
8
  export type Flag = Mode | 'a';
8
9
  export type Stats = fs.Stats;
9
10
 
11
+ export * from './bitmask';
12
+
10
13
  export interface IConstants {
11
14
  e: number,
12
15
  r: number,
@@ -14,38 +17,6 @@ export interface IConstants {
14
17
  x: number
15
18
  }
16
19
 
17
- const permissions: number[] = [
18
- 0o400, // OWNER_READ
19
- 0o200, // OWNER_WRITE
20
- 0o100, // OWNER_EXECUTE
21
- 0o040, // GROUP_READ
22
- 0o020, // GROUP_WRITE
23
- 0o010, // GROUP_EXECUTE
24
- 0o004, // OTHERS_READ
25
- 0o002, // OTHERS_WRITE
26
- 0o001 // OTHERS_EXECUTE
27
- ];
28
-
29
- export function bitmask(mode: number) {
30
- const type = typeof mode;
31
-
32
- if (type !== 'number') {
33
- throw new Error(
34
- `Argument of type '${type}' is not assignable to parameter of type 'number'.`
35
- );
36
- }
37
-
38
- let umask = 0o000;
39
-
40
- for (const flag of permissions) {
41
- if (mode & flag) {
42
- umask += flag;
43
- }
44
- }
45
-
46
- return umask;
47
- }
48
-
49
20
  export class PoweredFileSystem {
50
21
  readonly pwd: string;
51
22
 
@@ -133,11 +104,11 @@ export class PoweredFileSystem {
133
104
  src = this.resolve(src);
134
105
 
135
106
  if (sync) {
136
- return recurseSync.chmod(src, mode);
107
+ return chmodSync(src, mode);
137
108
  }
138
109
 
139
110
  return new Promise<void>((resolve, reject) => {
140
- recurse.chmod(src, mode, (err) => {
111
+ chmod(src, mode, (err) => {
141
112
  if (err) {
142
113
  return reject(err);
143
114
  }
@@ -147,23 +118,27 @@ export class PoweredFileSystem {
147
118
  });
148
119
  }
149
120
 
150
- chown(src: string, uid: number, gid: number, options: {
151
- sync: true
121
+ chown(src: string, options: {
122
+ sync: true,
123
+ uid?: number,
124
+ gid?: number
152
125
  }): void;
153
126
 
154
- chown(src: string, uid: number, gid: number, options?: {
155
- sync?: false
127
+ chown(src: string, options?: {
128
+ sync?: false,
129
+ uid?: number,
130
+ gid?: number
156
131
  }): Promise<void>;
157
132
 
158
- chown(src: string, uid: number, gid: number, { sync = false }: { sync?: boolean } = {}) {
133
+ chown(src: string, { sync = false, uid = 0, gid = 0 }: { sync?: boolean, uid?: number, gid?: number } = {}) {
159
134
  src = this.resolve(src);
160
135
 
161
136
  if (sync) {
162
- return recurseSync.chown(src, uid, gid);
137
+ return chownSync(src, uid, gid);
163
138
  }
164
139
 
165
140
  return new Promise<void>((resolve, reject) => {
166
- recurse.chown(src, uid, gid, (err) => {
141
+ chown(src, uid, gid, (err) => {
167
142
  if (err) {
168
143
  return reject(err);
169
144
  }
@@ -218,11 +193,11 @@ export class PoweredFileSystem {
218
193
  dir = this.resolve(dir);
219
194
 
220
195
  if (sync) {
221
- return recurseSync.copy(src, dir, umask);
196
+ return copySync(src, dir, umask);
222
197
  }
223
198
 
224
199
  return new Promise<void>((resolve, reject) => {
225
- recurse.copy(src, dir, umask, (err) => {
200
+ copy(src, dir, umask, (err) => {
226
201
  if (err) {
227
202
  return reject(err);
228
203
  }
@@ -271,17 +246,23 @@ export class PoweredFileSystem {
271
246
  src = this.resolve(src);
272
247
 
273
248
  if (sync) {
274
- return recurseSync.remove(src);
249
+ removeSync(src);
275
250
  }
276
251
 
277
252
  return new Promise<void>((resolve, reject) => {
278
- recurse.remove(src, (err) => {
253
+ const callback: NoParamCallback = (err) => {
279
254
  if (err) {
280
255
  return reject(err);
281
256
  }
282
257
 
283
258
  resolve();
284
- });
259
+ };
260
+
261
+ if ('rm' in fs) {
262
+ return fs.rm(src, { recursive: true }, callback);
263
+ }
264
+
265
+ remove(src, callback);
285
266
  });
286
267
  }
287
268
 
@@ -400,6 +381,9 @@ export class PoweredFileSystem {
400
381
  });
401
382
  }
402
383
 
384
+ /**
385
+ * @deprecated The method should not be used
386
+ */
403
387
  append(src: string, data: Buffer, options: {
404
388
  sync: true,
405
389
  encoding: null,
@@ -407,6 +391,9 @@ export class PoweredFileSystem {
407
391
  flag?: Flag
408
392
  }): void;
409
393
 
394
+ /**
395
+ * @deprecated The method should not be used
396
+ */
410
397
  append(src: string, data: string, options: {
411
398
  sync: true,
412
399
  encoding?: BufferEncoding,
@@ -414,6 +401,9 @@ export class PoweredFileSystem {
414
401
  flag?: Flag
415
402
  }): void;
416
403
 
404
+ /**
405
+ * @deprecated The method should not be used
406
+ */
417
407
  append(src: string, data: Buffer, options: {
418
408
  sync?: false,
419
409
  encoding: null,
@@ -421,6 +411,9 @@ export class PoweredFileSystem {
421
411
  flag?: Flag
422
412
  }): Promise<void>;
423
413
 
414
+ /**
415
+ * @deprecated The method should not be used
416
+ */
424
417
  append(src: string, data: string, options?: {
425
418
  sync?: false,
426
419
  encoding?: BufferEncoding,
@@ -428,6 +421,9 @@ export class PoweredFileSystem {
428
421
  flag?: Flag
429
422
  }): Promise<void>;
430
423
 
424
+ /**
425
+ * @deprecated The method should not be used
426
+ */
431
427
  append(src: string, data: Buffer | string, { sync = false, encoding = 'utf8', umask = 0o000, flag = 'a' }: {
432
428
  sync?: boolean,
433
429
  encoding?: BufferEncoding | null,
@@ -511,11 +507,11 @@ export class PoweredFileSystem {
511
507
  dir = this.resolve(dir);
512
508
 
513
509
  if (sync) {
514
- return recurseSync.mkdir(dir, umask);
510
+ return mkdirSync(dir, umask);
515
511
  }
516
512
 
517
513
  return new Promise<void>((resolve, reject) => {
518
- recurse.mkdir(dir, umask, (err) => {
514
+ mkdir(dir, umask, (err) => {
519
515
  if (err) {
520
516
  return reject(err);
521
517
  }
@@ -524,4 +520,4 @@ export class PoweredFileSystem {
524
520
  });
525
521
  });
526
522
  }
527
- }
523
+ }
@@ -3,35 +3,43 @@ import path from 'node:path';
3
3
 
4
4
  const { sep } = path;
5
5
 
6
- function chmod(src: string, mode: number) {
7
- const stat = fs.statSync(src);
6
+ export function chmodSync(src: string, mode: number) {
7
+ const stats = fs.statSync(src);
8
8
 
9
- if (stat.isDirectory()) {
9
+ if (stats.isDirectory()) {
10
10
  const list = fs.readdirSync(src);
11
11
 
12
12
  for (const loc of list) {
13
- chmod(`${src}${sep}${loc}`, mode);
13
+ chmodSync(`${src}${sep}${loc}`, mode);
14
14
  }
15
15
  }
16
16
 
17
17
  fs.chmodSync(src, mode);
18
18
  }
19
19
 
20
- function chown(src: string, uid: number, gid: number) {
21
- const stat = fs.statSync(src);
20
+ export function chownSync(src: string, uid: number, gid: number) {
21
+ const stats = fs.statSync(src);
22
22
 
23
- if (stat.isDirectory()) {
23
+ if (uid === 0) {
24
+ uid = stats.uid;
25
+ }
26
+
27
+ if (gid === 0) {
28
+ gid = stats.gid;
29
+ }
30
+
31
+ if (stats.isDirectory()) {
24
32
  const list = fs.readdirSync(src);
25
33
 
26
34
  for (const loc of list) {
27
- chown(`${src}${sep}${loc}`, uid, gid);
35
+ chownSync(`${src}${sep}${loc}`, uid, gid);
28
36
  }
29
37
  }
30
38
 
31
39
  fs.chownSync(src, uid, gid);
32
40
  }
33
41
 
34
- function copy(src: string, dir: string, umask: number) {
42
+ export function copySync(src: string, dir: string, umask: number) {
35
43
  const stat = fs.statSync(src);
36
44
 
37
45
  if (stat.isDirectory()) {
@@ -45,7 +53,7 @@ function copy(src: string, dir: string, umask: number) {
45
53
  fs.mkdirSync(dir, mode);
46
54
 
47
55
  for (const loc of list) {
48
- copy(`${src}${sep}${loc}`, dir, umask);
56
+ copySync(`${src}${sep}${loc}`, dir, umask);
49
57
  }
50
58
  }
51
59
  else {
@@ -56,14 +64,14 @@ function copy(src: string, dir: string, umask: number) {
56
64
  }
57
65
  }
58
66
 
59
- function remove(src: string) {
60
- const stat = fs.statSync(src);
67
+ export function removeSync(src: string) {
68
+ const stats = fs.statSync(src);
61
69
 
62
- if (stat.isDirectory()) {
70
+ if (stats.isDirectory()) {
63
71
  const list = fs.readdirSync(src);
64
72
 
65
73
  for (const loc of list) {
66
- remove(`${src}${sep}${loc}`);
74
+ removeSync(`${src}${sep}${loc}`);
67
75
  }
68
76
 
69
77
  fs.rmdirSync(src);
@@ -73,14 +81,14 @@ function remove(src: string) {
73
81
  }
74
82
  }
75
83
 
76
- function mkdir(dir: string, umask: number) {
84
+ export function mkdirSync(dir: string, umask: number) {
77
85
  const mode = 0o777 - umask;
78
86
  const cwd = process.cwd();
79
87
  let use = '';
80
88
 
81
89
  if (dir.indexOf(cwd) === 0) {
82
90
  use = cwd;
83
- dir = dir.substr(cwd.length);
91
+ dir = dir.substring(cwd.length);
84
92
  }
85
93
 
86
94
  const ways = dir.split(sep).slice(1);
@@ -98,11 +106,3 @@ function mkdir(dir: string, umask: number) {
98
106
  }
99
107
  }
100
108
  }
101
-
102
- export default {
103
- chmod,
104
- chown,
105
- copy,
106
- remove,
107
- mkdir
108
- }
package/src/recurse-io.ts CHANGED
@@ -1,20 +1,19 @@
1
- import fs from 'node:fs';
1
+ import fs, { NoParamCallback } from 'node:fs';
2
2
  import path from 'node:path';
3
3
 
4
- export type Files = Array<string>;
5
- export type NoParamCallback = fs.NoParamCallback;
4
+ type Files = Array<string>;
6
5
 
7
6
  const { sep } = path;
8
7
 
9
- function chmod(src: string, mode: number, callback: NoParamCallback) {
8
+ export function chmod(src: string, mode: number, callback: NoParamCallback) {
10
9
  let reduce = 0;
11
10
 
12
- fs.stat(src, (err, stat) => {
11
+ fs.stat(src, (err, stats) => {
13
12
  if (err) {
14
13
  return callback(err);
15
14
  }
16
15
 
17
- if (stat.isDirectory()) {
16
+ if (stats.isDirectory()) {
18
17
  fs.readdir(src, (err, list) => {
19
18
  if (err) {
20
19
  return callback(err);
@@ -45,7 +44,7 @@ function chmod(src: string, mode: number, callback: NoParamCallback) {
45
44
  });
46
45
  }
47
46
 
48
- function chown(src: string, uid: number, gid: number, callback: NoParamCallback) {
47
+ export function chown(src: string, uid: number, gid: number, callback: NoParamCallback) {
49
48
  let reduce = 0;
50
49
 
51
50
  fs.stat(src, (err, stats) => {
@@ -53,6 +52,14 @@ function chown(src: string, uid: number, gid: number, callback: NoParamCallback)
53
52
  return callback(err);
54
53
  }
55
54
 
55
+ if (uid === 0) {
56
+ uid = stats.uid;
57
+ }
58
+
59
+ if (gid === 0) {
60
+ gid = stats.gid;
61
+ }
62
+
56
63
  if (stats.isDirectory()) {
57
64
  fs.readdir(src, (err, list) => {
58
65
  if (err) {
@@ -84,7 +91,7 @@ function chown(src: string, uid: number, gid: number, callback: NoParamCallback)
84
91
  });
85
92
  }
86
93
 
87
- function copy(src: string, dir: string, umask: number, callback: NoParamCallback) {
94
+ export function copy(src: string, dir: string, umask: number, callback: NoParamCallback) {
88
95
  let reduce = 0;
89
96
 
90
97
  fs.stat(src, (err, stat) => {
@@ -150,7 +157,7 @@ function copy(src: string, dir: string, umask: number, callback: NoParamCallback
150
157
  });
151
158
  }
152
159
 
153
- function remove(src: string, callback: NoParamCallback) {
160
+ export function remove(src: string, callback: NoParamCallback) {
154
161
  fs.stat(src, (err, stat) => {
155
162
  if (err) {
156
163
  return callback(err);
@@ -187,7 +194,7 @@ function remove(src: string, callback: NoParamCallback) {
187
194
  });
188
195
  }
189
196
 
190
- function mkdir(dir: string, umask: number, callback: NoParamCallback) {
197
+ export function mkdir(dir: string, umask: number, callback: NoParamCallback) {
191
198
  const cwd = process.cwd();
192
199
 
193
200
  if (dir === cwd) {
@@ -218,7 +225,7 @@ function mkdir(dir: string, umask: number, callback: NoParamCallback) {
218
225
 
219
226
  if (dir.indexOf(cwd) === 0) {
220
227
  use = cwd;
221
- dir = dir.substr(cwd.length);
228
+ dir = dir.substring(cwd.length);
222
229
  }
223
230
 
224
231
  const files = dir.split(sep);
@@ -229,11 +236,3 @@ function mkdir(dir: string, umask: number, callback: NoParamCallback) {
229
236
  iter.next();
230
237
  iter.next(iter);
231
238
  }
232
-
233
- export default {
234
- chmod,
235
- chown,
236
- copy,
237
- remove,
238
- mkdir
239
- }
@@ -5,7 +5,7 @@ import { expect } from 'expect';
5
5
  import { fmock, restore } from './__fmock';
6
6
  import { pfs } from '../src';
7
7
 
8
- describe('chown(src, uid, gid [, options])', () => {
8
+ describe('chown(src, [, options])', () => {
9
9
  const chance = new Chance();
10
10
 
11
11
  beforeEach(() => {
@@ -25,7 +25,7 @@ describe('chown(src, uid, gid [, options])', () => {
25
25
 
26
26
  it('Positive: Changes the permissions of a file', async () => {
27
27
  const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
28
- await pfs.chown('./tmpdir/tings.txt', uid, gid);
28
+ await pfs.chown('./tmpdir/tings.txt', { uid, gid });
29
29
 
30
30
  assert(uid && gid);
31
31
  });
@@ -33,7 +33,7 @@ describe('chown(src, uid, gid [, options])', () => {
33
33
 
34
34
  it('Positive: Changes the permissions of a directory', async () => {
35
35
  const { uid, gid } = fs.statSync('./tmpdir/digest');
36
- await pfs.chown('./tmpdir/digest', uid, gid);
36
+ await pfs.chown('./tmpdir/digest', { uid, gid });
37
37
 
38
38
  assert(uid && gid);
39
39
  });
@@ -44,7 +44,7 @@ describe('chown(src, uid, gid [, options])', () => {
44
44
  const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
45
45
 
46
46
  await expect(async () => {
47
- await pfs.chown(`./tmpdir/${guid}`, uid, gid);
47
+ await pfs.chown(`./tmpdir/${guid}`, { uid, gid });
48
48
  })
49
49
  .rejects
50
50
  .toThrow();
@@ -54,8 +54,10 @@ describe('chown(src, uid, gid [, options])', () => {
54
54
  it('[sync] Positive: Changes the permissions of a file', () => {
55
55
  const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
56
56
 
57
- pfs.chown('./tmpdir/tings.txt', uid, gid, {
58
- sync: true
57
+ pfs.chown('./tmpdir/tings.txt', {
58
+ sync: true,
59
+ uid,
60
+ gid
59
61
  });
60
62
 
61
63
  assert(uid && gid);
@@ -65,8 +67,10 @@ describe('chown(src, uid, gid [, options])', () => {
65
67
  it('[sync] Positive: Changes the permissions of a directory', () => {
66
68
  const { uid, gid } = fs.statSync('./tmpdir/digest');
67
69
 
68
- pfs.chown('./tmpdir/digest', uid, gid, {
69
- sync: true
70
+ pfs.chown('./tmpdir/digest', {
71
+ sync: true,
72
+ uid,
73
+ gid
70
74
  });
71
75
 
72
76
  assert(uid && gid);
@@ -78,8 +82,10 @@ describe('chown(src, uid, gid [, options])', () => {
78
82
  const { uid, gid } = fs.statSync('./tmpdir/tings.txt');
79
83
 
80
84
  assert.throws(() => {
81
- pfs.chown(`./tmpdir/${guid}`, uid, gid, {
82
- sync: true
85
+ pfs.chown(`./tmpdir/${guid}`, {
86
+ sync: true,
87
+ uid,
88
+ gid
83
89
  });
84
90
  });
85
91
  });