forked from electron/asar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSymlinkApp.js
58 lines (51 loc) · 1.88 KB
/
createSymlinkApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const path = require('path');
const fs = require('../../lib/wrapped-fs').default;
const rimraf = require('rimraf');
/**
* Directory structure:
* tmp
* ├── private
* │ └── var
* │ ├── app
* │ │ └── file.txt -> ../file.txt
* │ └── file.txt
* └── var -> private/var
*/
module.exports = (testName) => {
const tmpPath = path.join(__dirname, '../..', 'tmp', testName || 'app');
const privateVarPath = path.join(tmpPath, 'private', 'var');
const varPath = path.join(tmpPath, 'var');
rimraf.sync(tmpPath, fs);
fs.mkdirSync(privateVarPath, { recursive: true });
fs.symlinkSync(path.relative(tmpPath, privateVarPath), varPath);
const originFilePath = path.join(varPath, 'file.txt');
fs.writeFileSync(originFilePath, 'hello world');
const appPath = path.join(varPath, 'app');
fs.mkdirpSync(appPath);
fs.symlinkSync('../file.txt', path.join(appPath, 'file.txt'));
const ordering = walk(tmpPath).map((filepath) => filepath.substring(tmpPath.length)); // convert to paths relative to root
return {
appPath,
tmpPath,
varPath,
// helper function for generating the `ordering.txt` file data
buildOrderingData: (getProps) =>
ordering.reduce((prev, curr) => {
return `${prev}${curr}:${JSON.stringify(getProps(curr))}\n`;
}, ''),
};
};
// returns a list of all directories, files, and symlinks. Automates testing `ordering` logic easy.
const walk = (root) => {
const getPaths = (filepath, filter) =>
fs
.readdirSync(filepath, { withFileTypes: true })
.filter((dirent) => filter(dirent))
.map(({ name }) => path.join(filepath, name));
const dirs = getPaths(root, (dirent) => dirent.isDirectory());
const files = dirs.map((dir) => walk(dir)).flat();
return files.concat(
dirs,
getPaths(root, (dirent) => dirent.isFile() || dirent.isSymbolicLink()),
);
};