@gjsify/fs 0.0.1 → 0.0.3
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.
- package/README.md +6 -8
- package/lib/cjs/callback.js +112 -0
- package/lib/cjs/dirent.js +98 -0
- package/lib/cjs/encoding.js +34 -0
- package/lib/cjs/file-handle.js +444 -0
- package/lib/cjs/fs-watcher.js +50 -0
- package/lib/cjs/index.js +95 -0
- package/lib/cjs/promises.js +160 -0
- package/lib/cjs/read-stream.js +78 -0
- package/lib/cjs/stats.js +45 -0
- package/lib/cjs/sync.js +126 -0
- package/lib/cjs/types/encoding-option.js +0 -0
- package/lib/cjs/types/file-read-options.js +0 -0
- package/lib/cjs/types/file-read-result.js +0 -0
- package/lib/cjs/types/flag-and-open-mode.js +0 -0
- package/lib/cjs/types/index.js +6 -0
- package/lib/cjs/types/open-flags.js +0 -0
- package/lib/cjs/types/read-options.js +0 -0
- package/lib/cjs/utils.js +18 -0
- package/lib/cjs/write-stream.js +116 -0
- package/lib/esm/callback.js +112 -0
- package/lib/esm/dirent.js +98 -0
- package/lib/esm/encoding.js +34 -0
- package/lib/esm/file-handle.js +444 -0
- package/lib/esm/fs-watcher.js +50 -0
- package/lib/esm/index.js +95 -0
- package/lib/esm/promises.js +160 -0
- package/lib/esm/read-stream.js +78 -0
- package/lib/esm/stats.js +45 -0
- package/lib/esm/sync.js +126 -0
- package/lib/esm/types/encoding-option.js +0 -0
- package/lib/esm/types/file-read-options.js +0 -0
- package/lib/esm/types/file-read-result.js +0 -0
- package/lib/esm/types/flag-and-open-mode.js +0 -0
- package/lib/esm/types/index.js +6 -0
- package/lib/esm/types/open-flags.js +0 -0
- package/lib/esm/types/read-options.js +0 -0
- package/lib/esm/utils.js +18 -0
- package/lib/esm/write-stream.js +116 -0
- package/package.json +52 -17
- package/src/callback.spec.ts +42 -0
- package/src/callback.ts +362 -0
- package/src/dirent.ts +117 -0
- package/src/encoding.ts +40 -0
- package/src/file-handle.spec.ts +34 -0
- package/src/file-handle.ts +606 -0
- package/{fs-watcher.js → src/fs-watcher.ts} +10 -9
- package/src/index.ts +95 -0
- package/src/promises.spec.ts +172 -0
- package/src/promises.ts +349 -0
- package/src/read-stream.ts +98 -0
- package/src/stat.spec.ts +79 -0
- package/src/stats.ts +106 -0
- package/src/symlink.spec.ts +38 -0
- package/src/sync.spec.ts +205 -0
- package/src/sync.ts +239 -0
- package/src/test.mts +11 -0
- package/src/types/encoding-option.ts +3 -0
- package/src/types/file-read-options.ts +15 -0
- package/src/types/file-read-result.ts +4 -0
- package/src/types/flag-and-open-mode.ts +6 -0
- package/src/types/index.ts +6 -0
- package/src/types/open-flags.ts +14 -0
- package/src/types/read-options.ts +9 -0
- package/src/utils.ts +19 -0
- package/src/write-stream.ts +143 -0
- package/test/file.txt +1 -0
- package/test/watch.js +1 -0
- package/test.gjs.js +35359 -0
- package/test.gjs.js.map +7 -0
- package/test.gjs.mjs +40570 -0
- package/test.gjs.mjs.meta.json +1 -0
- package/test.node.js +1479 -0
- package/test.node.js.map +7 -0
- package/test.node.mjs +710 -0
- package/tsconfig.json +18 -0
- package/tsconfig.types.json +8 -0
- package/index.js +0 -114
- package/test/index.js +0 -90
- package/test/resources/file.txt +0 -1
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "ESNext",
|
|
4
|
+
"types": ["node"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"moduleResolution": "NodeNext",
|
|
7
|
+
"experimentalDecorators": true,
|
|
8
|
+
"outDir": "lib",
|
|
9
|
+
"rootDir": "src",
|
|
10
|
+
"composite": true
|
|
11
|
+
},
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"allowJs": true,
|
|
14
|
+
"checkJs": false,
|
|
15
|
+
"reflection": false,
|
|
16
|
+
"include": ["src/**/*.ts"],
|
|
17
|
+
"exclude": ["src/test.mts", "src/**/*.spec.ts"]
|
|
18
|
+
}
|
package/index.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
const { Gio, GLib } = imports.gi;
|
|
2
|
-
|
|
3
|
-
const { Buffer } = require('buffer');
|
|
4
|
-
|
|
5
|
-
function getEncodingFromOptions(options, defaultEncoding = 'utf8') {
|
|
6
|
-
if (options === null) {
|
|
7
|
-
return defaultEncoding;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (typeof options === 'string') {
|
|
11
|
-
return options;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (typeof options === 'object' && typeof options.encoding === 'string') {
|
|
15
|
-
return options.encoding;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return defaultEncoding;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function existsSync(path) {
|
|
22
|
-
// TODO: accept buffer and URL too
|
|
23
|
-
if (typeof path !== 'string' || path === '') {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const file = Gio.File.new_for_path(path);
|
|
28
|
-
return file.query_exists(null);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function readdirSync(path, options = 'utf8') {
|
|
32
|
-
const encoding = getEncodingFromOptions(options);
|
|
33
|
-
const dir = Gio.File.new_for_path(path);
|
|
34
|
-
const list = [];
|
|
35
|
-
|
|
36
|
-
const enumerator = dir.enumerate_children('standard::*', 0, null);
|
|
37
|
-
let info = enumerator.next_file(null);
|
|
38
|
-
|
|
39
|
-
while (info) {
|
|
40
|
-
const child = enumerator.get_child(info);
|
|
41
|
-
const fileName = child.get_basename();
|
|
42
|
-
|
|
43
|
-
if (encoding === 'buffer') {
|
|
44
|
-
const encodedName = Buffer.from(fileName);
|
|
45
|
-
list.push(encodedName);
|
|
46
|
-
} else {
|
|
47
|
-
const encodedName = Buffer.from(fileName).toString(encoding);
|
|
48
|
-
list.push(encodedName);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
info = enumerator.next_file(null);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return list;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function readFileSync(path, options = { encoding: null, flag: 'r' }) {
|
|
58
|
-
const file = Gio.File.new_for_path(path);
|
|
59
|
-
|
|
60
|
-
const [ok, data] = file.load_contents(null);
|
|
61
|
-
|
|
62
|
-
if (!ok) {
|
|
63
|
-
// TODO: throw a better error
|
|
64
|
-
throw new Error('failed to read file');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const encoding = getEncodingFromOptions(options, 'buffer');
|
|
68
|
-
if (encoding === 'buffer') {
|
|
69
|
-
return Buffer.from(data);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return data.toString(encoding);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function mkdirSync(path, mode = 0o777) {
|
|
76
|
-
if (GLib.mkdir_with_parents(path, mode) !== 0) {
|
|
77
|
-
// TODO: throw a better error
|
|
78
|
-
throw new Error(`failed to make ${path} directory`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function rmdirSync(path) {
|
|
83
|
-
const result = GLib.rmdir(path);
|
|
84
|
-
|
|
85
|
-
if (result !== 0) {
|
|
86
|
-
// TODO: throw a better error
|
|
87
|
-
throw new Error(`failed to remove ${path} directory`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function unlinkSync(path) {
|
|
92
|
-
GLib.unlink(path);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function writeFileSync(path, data) {
|
|
96
|
-
GLib.file_set_contents(path, data);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const FSWatcher = require('./fs-watcher');
|
|
100
|
-
function watch(filename, options, listener) {
|
|
101
|
-
return new FSWatcher(filename, options, listener);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
module.exports = {
|
|
105
|
-
FSWatcher,
|
|
106
|
-
existsSync,
|
|
107
|
-
readdirSync,
|
|
108
|
-
readFileSync,
|
|
109
|
-
writeFileSync,
|
|
110
|
-
mkdirSync,
|
|
111
|
-
rmdirSync,
|
|
112
|
-
unlinkSync,
|
|
113
|
-
watch
|
|
114
|
-
};
|
package/test/index.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
const MODULE = require('../index');
|
|
2
|
-
|
|
3
|
-
function checkExistsSync() {
|
|
4
|
-
const existingFiles = ['index.js', 'test', 'test/index.js'];
|
|
5
|
-
const nonExistingFiles = ['asdasd', '/asdasd', ''];
|
|
6
|
-
|
|
7
|
-
for (const file of existingFiles) {
|
|
8
|
-
const result = MODULE.existsSync(file);
|
|
9
|
-
if (result !== true) {
|
|
10
|
-
throw new Error(`${file} exists but existsSync returned ${result}`);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
for (const file of nonExistingFiles) {
|
|
15
|
-
const result = MODULE.existsSync(file);
|
|
16
|
-
if (result !== false) {
|
|
17
|
-
throw new Error(`${file} doesn't exists but existsSync returned ${result}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function checkReaddirSync() {
|
|
23
|
-
const expectedFilesCount = 1;
|
|
24
|
-
const expectedFileName = 'file.txt';
|
|
25
|
-
|
|
26
|
-
const files = MODULE.readdirSync('./test/resources');
|
|
27
|
-
|
|
28
|
-
if (files.length !== expectedFilesCount) {
|
|
29
|
-
throw new Error(`readdirSync returned ${files.length} files instead of ${expectedFilesCount}`);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (files[0] !== expectedFileName) {
|
|
33
|
-
throw new Error(`Got file named ${files[0]} instead of ${expectedFileName}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function checkReadFileSync() {
|
|
38
|
-
const bufferData = MODULE.readFileSync('index.js');
|
|
39
|
-
if (!(bufferData instanceof Buffer)) {
|
|
40
|
-
throw new Error('readFileSync should return a Buffer if no encoding was specified');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const utf8Data = MODULE.readFileSync('./test/resources/file.txt', 'utf-8');
|
|
44
|
-
if (!(typeof utf8Data === 'string')) {
|
|
45
|
-
throw new Error('readFileSync should return a string when encoding is utf-8');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (utf8Data.startsWith('Hello World')) {
|
|
49
|
-
throw new Error('readFileSync returned a string but with the wrong value');
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function checkMkdirSyncRmdirSync() {
|
|
54
|
-
const path = './foobar';
|
|
55
|
-
|
|
56
|
-
MODULE.mkdirSync(path);
|
|
57
|
-
|
|
58
|
-
if (!MODULE.existsSync(path)) {
|
|
59
|
-
throw new Error(`${path} should exists`);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
MODULE.rmdirSync('./foobar');
|
|
63
|
-
|
|
64
|
-
if (MODULE.existsSync(path)) {
|
|
65
|
-
throw new Error(`${path} should not exists`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (!Object.keys(MODULE).length) {
|
|
70
|
-
throw new Error('MODULE was not exported');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
checkExistsSync();
|
|
74
|
-
checkReaddirSync();
|
|
75
|
-
checkReadFileSync();
|
|
76
|
-
checkMkdirSyncRmdirSync();
|
|
77
|
-
|
|
78
|
-
const watch = require('path').join(__dirname, 'watch.js');
|
|
79
|
-
MODULE.writeFileSync(watch, '// test');
|
|
80
|
-
const watcher = MODULE.watch(watch, {persistent: true}, console.log);
|
|
81
|
-
watcher.on('change', console.log).on('rename', console.log);
|
|
82
|
-
|
|
83
|
-
setTimeout(() => { watcher.close(); }, 1000);
|
|
84
|
-
|
|
85
|
-
setTimeout(() => {
|
|
86
|
-
MODULE.writeFileSync(watch, '// test');
|
|
87
|
-
setTimeout(() => {
|
|
88
|
-
MODULE.unlinkSync(watch);
|
|
89
|
-
}, 100);
|
|
90
|
-
}, 100);
|
package/test/resources/file.txt
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Hello World
|