-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathloadRelativeFile.js
108 lines (96 loc) · 3.02 KB
/
loadRelativeFile.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// @flow
const csb = require('codesandboxer');
const fs = require('fs');
const path = require('path');
const resolve = require('resolve');
const safeGuardPath = (resolvedPath: string) =>
path.join(...resolvedPath.split('/'));
const relToRelPkgRoot = (resolvedPath, rootDir) =>
path.relative(rootDir, resolvedPath);
async function loadJS(resolvedPath, pkgJSON, rootDir) {
let content = fs.readFileSync(safeGuardPath(resolvedPath), 'utf-8');
let file = await csb.parseFile(content, pkgJSON);
return Object.assign({}, file, {
filePath: relToRelPkgRoot(resolvedPath, rootDir),
});
}
async function loadSass(resolvedPath, pkgJSON, rootDir) {
let content = fs.readFileSync(safeGuardPath(resolvedPath), 'utf-8');
let file = await csb.parseSassFile(content);
return Object.assign({}, file, {
filePath: relToRelPkgRoot(resolvedPath, rootDir),
});
}
async function loadScss(resolvedPath, pkgJSON, rootDir) {
let content = fs.readFileSync(safeGuardPath(resolvedPath), 'utf-8');
let file = await csb.parseScssFile(content);
return Object.assign({}, file, {
filePath: relToRelPkgRoot(resolvedPath, rootDir),
});
}
async function loadRaw(resolvedPath, rootDir) {
let file = fs.readFileSync(safeGuardPath(resolvedPath), 'utf-8');
return {
file,
deps: {},
internalImports: [],
filePath: relToRelPkgRoot(resolvedPath, rootDir),
};
}
/* Remove the disable once image loading has been built */
/* eslint-disable-next-line no-unused-vars */
async function loadImages(resolvedPath, rootDir) {
let file = fs.readFileSync(safeGuardPath(resolvedPath));
return {
file: new Buffer(file).toString('base64'),
deps: {},
internalImports: [],
filePath: relToRelPkgRoot(resolvedPath, rootDir),
};
}
/*::
import type { Package } from 'codesandboxer'
type LoadRelativeObj = {
filePath: string,
pkgJSON: Package,
rootDir: string,
extensions: Array<string>,
}
*/
async function loadRelativeFile(
{ filePath, pkgJSON, rootDir, extensions } /*: LoadRelativeObj */,
) {
let absPath = path.resolve(rootDir, filePath);
let resolvedPath = resolve.sync(absPath, { extensions });
let extension = path.extname(resolvedPath);
if (!extension) {
throw { key: 'fileNoExtension', path: resolvedPath };
}
if (extensions.includes(extension)) {
return loadJS(resolvedPath, pkgJSON, rootDir);
}
switch (extension) {
case '.png':
case '.jpeg':
case '.jpg':
case '.gif':
case '.bmp':
case '.tiff':
return loadImages(resolvedPath, rootDir);
case '.json':
case '.css':
return loadRaw(resolvedPath, rootDir);
// Our scss and sass loaders currently don't resolve imports - we need to come back and update these.
case '.scss':
return loadScss(resolvedPath, pkgJSON, rootDir);
case '.sass':
return loadSass(resolvedPath, pkgJSON, rootDir);
case '.js':
return loadJS(resolvedPath, pkgJSON, rootDir);
default:
throw new Error(
`unparseable filetype: ${extension} for file ${resolvedPath}`,
);
}
}
module.exports = loadRelativeFile;