forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-mappings.js
63 lines (56 loc) · 1.64 KB
/
generate-mappings.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
/* @flow */
const path = require('path');
const fs = require('fs');
const types = require('babel-types');
const babylon = require('babylon');
const output = path.join(__dirname, '../dist/mappings.json');
const source = fs.readFileSync(require.resolve('..'), 'utf8');
const ast = babylon.parse(source, {
sourceType: 'module',
plugins: [
'jsx',
'flow',
'objectRestSpread',
'classProperties',
'asyncGenerators',
],
});
const relative = (value /* : string */) =>
path.relative(
path.resolve(__dirname, '..'),
path.resolve(path.dirname(require.resolve('..')), value)
);
const mappings = ast.program.body.reduce((acc, declaration, index, self) => {
if (types.isExportNamedDeclaration(declaration)) {
if (declaration.source) {
declaration.specifiers.forEach(specifier => {
acc[specifier.exported.name] = {
path: relative(declaration.source.value),
name: specifier.local.name,
};
});
} else {
declaration.specifiers.forEach(specifier => {
const name = specifier.exported.name;
self.forEach(it => {
if (
types.isImportDeclaration(it) &&
it.specifiers.some(
s =>
types.isImportNamespaceSpecifier(s) &&
s.local.name === specifier.local.name
)
) {
acc[name] = {
path: relative(it.source.value),
name: '*',
};
}
});
});
}
}
return acc;
}, {});
fs.existsSync(path.dirname(output)) || fs.mkdirSync(path.dirname(output));
fs.writeFileSync(output, JSON.stringify(mappings, null, 2));