-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·43 lines (32 loc) · 1.12 KB
/
index.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
const path = require('path');
const { relative } = path;
const {
createFile,
formulateContent,
getWriteParameters,
readDirectory,
writeToFile,
} = require('./src/index.js');
const addSlash = (string) => {
return string.endsWith('/') ? string : `${string}/`;
}
// Loop through directories
const writeIndex = async (...args) => {
const command = args.pop();
const { directories, entryFile } = getWriteParameters(command, args);
for (let i = 0; i < directories.length; i++) {
// In each directory:
const dir = directories[i];
const path = addSlash(dir.pathToIndex);
const fromDir = addSlash(dir.importFrom) || path;
const relativePath = addSlash(`./${relative(path, fromDir)}`);
await createFile(`${path}${entryFile}`);
// Read sibling files from directory
const files = await readDirectory(fromDir);
// Formulate content
const fileContent = formulateContent(files, entryFile, relativePath);
// Write content to file
await writeToFile(path, fileContent, entryFile);
};
};
module.exports = writeIndex;