-
Notifications
You must be signed in to change notification settings - Fork 10
/
localSiteDir.js
58 lines (46 loc) · 1.65 KB
/
localSiteDir.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("fs-extra");
const dirAsJson = require("corvid-dir-as-json");
const initTempDir = require("./initTempDir");
const siteSrcPath = rootPath => path.join(rootPath, "src");
const readLocalSite = rootPath =>
dirAsJson.readDirToJson(siteSrcPath(rootPath));
const writeFile = async (rootPath, filePath, content) => {
const fullPath = path.join(siteSrcPath(rootPath), filePath);
await fs.ensureDir(path.dirname(fullPath));
await fs.writeFile(fullPath, content);
};
const readFile = async (rootPath, filePath) => {
const fullPath = path.join(siteSrcPath(rootPath), filePath);
return fs.readFile(fullPath, "utf8");
};
const deleteFile = async (rootPath, filePath) => {
const fullPath = path.join(siteSrcPath(rootPath), filePath);
await fs.remove(fullPath);
};
const doesExist = async (rootPath, localPath) =>
await fs.exists(path.join(siteSrcPath(rootPath), localPath));
const initLocalSite = async (localSiteFiles, createdRoodDir) => {
const rootDir = createdRoodDir || (await initTempDir());
const corvidRcPath = path.join(rootDir, ".corvid", "corvidrc.json");
await fs.ensureFile(corvidRcPath);
await fs.writeFile(corvidRcPath, JSON.stringify({ metasiteId: 12345 }));
await dirAsJson.writeJsonToDir(siteSrcPath(rootDir), localSiteFiles);
return rootDir;
};
const cleanLocalSite = async rootPath => fs.remove(rootPath);
const initBackup = (rootPath, localSiteFiles) =>
dirAsJson.writeJsonToDir(
path.join(rootPath, ".corvid", "backup"),
localSiteFiles
);
module.exports = {
doesExist,
cleanLocalSite,
initLocalSite,
readLocalSite,
writeFile,
readFile,
deleteFile,
initBackup
};