git ssb

0+

cel-desktop / ssb-pkg



Tree: b5f837feead2289e0e40ebb9331c756fc171e063

Files: b5f837feead2289e0e40ebb9331c756fc171e063 / lib / follow.js

1991 bytesRaw
1import { core, sync } from 'resolve';
2import assert from 'assert';
3import fs from 'fs';
4import path from 'path';
5
6Object.keys(core).forEach((key) => {
7 // 'resolve' hardcodes the list to host's one, but i need
8 // to be able to allow 'worker_threads' (target 12) on host 8
9 assert(typeof core[key] === 'boolean');
10 core[key] = true;
11});
12
13export const natives = core;
14
15const PROOF = 'a-proof-that-main-is-captured.js';
16
17function parentDirectoriesContain (parent, directory) {
18 while (true) {
19 if (parent === directory) return true;
20 const newParent = path.dirname(parent);
21 if (newParent === parent) return false;
22 parent = newParent;
23 }
24}
25
26export function follow (x, opts) {
27 // TODO async version
28 return new Promise((resolve) => {
29 resolve(sync(x, {
30 basedir: opts.basedir,
31 extensions: opts.extensions,
32 isFile: (file) => {
33 if (opts.ignoreFile && path.join(path.dirname(opts.ignoreFile), PROOF) === file) return true;
34 let stat;
35
36 try {
37 stat = fs.statSync(file);
38 } catch (e) {
39 if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
40 throw e;
41 }
42
43 return stat.isFile() || stat.isFIFO();
44 },
45 isDirectory: (directory) => {
46 if (opts.ignoreFile && parentDirectoriesContain(opts.ignoreFile, directory)) return false;
47 let stat;
48
49 try {
50 stat = fs.statSync(directory);
51 } catch (e) {
52 if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
53 throw e;
54 }
55
56 return stat.isDirectory();
57 },
58 readFileSync: (file) => {
59 if (opts.ignoreFile && opts.ignoreFile === file) return Buffer.from(`{"main":"${PROOF}"}`);
60 if (opts.readFile) opts.readFile(file);
61 return fs.readFileSync(file);
62 },
63 packageFilter: (config, base) => {
64 if (opts.packageFilter) opts.packageFilter(config, base);
65 return config;
66 }
67 }));
68 });
69}
70

Built with git-ssb-web