forked from sequelize/sequelize-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-browser.js
75 lines (63 loc) · 2.89 KB
/
generate-browser.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
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp-promise');
const recursive = require("recursive-readdir");
const lodash = require('lodash');
const base = 'lib';
const targetBase = 'browser';
async function main() {
const annotationsDir = path.posix.join(base, 'annotations');
// gathering annotation inputs
const inputs = (await recursive(annotationsDir))
.filter(f => f.endsWith('.js'))
// since recursive-readdir dosen't provide an a posix api, replace all \\ with //
.map(p => p.replace(/\\/g, '/'));
// write out the noop function that all the annotations will use
const noopDir = path.posix.join(targetBase, 'noop.js');
await mkdirp(path.posix.dirname(noopDir));
fs.writeFileSync(noopDir, `export function noop(){}`);
const exportedFiles = [];
// write the noop functions for all the annotations
for(const f of inputs) {
const fileParts = f.split('/');
// assume the function exported is the same as the filename
const functionName = fileParts[fileParts.length -1].replace('.js', '');
const outPath = path.posix.join(targetBase, f);
exportedFiles.push({functionName, path: outPath});
const noopRelatitiveDir = path.posix.relative(path.posix.dirname(outPath), path.posix.dirname(noopDir));
const file = `import {noop} from '${path.posix.join(noopRelatitiveDir, 'noop')}';
export function ${functionName}() { return noop; }
`;
await mkdirp(path.posix.dirname(outPath));
fs.writeFileSync(outPath, file)
}
// we need to include the model file
const modelFilePath = path.posix.join(targetBase, 'models', 'Model.js')
await mkdirp(path.posix.dirname(modelFilePath));
fs.writeFileSync(modelFilePath, `export class Model {}\n`)
exportedFiles.push({functionName: 'Model', path: modelFilePath})
// DataType.js
const dataTypeFilePath = path.posix.join(targetBase, 'enums', 'DataType.js')
await mkdirp(path.posix.dirname(dataTypeFilePath));
fs.writeFileSync(dataTypeFilePath, `export const DataType = {}\n`)
exportedFiles.push({functionName: 'DataType', path: dataTypeFilePath})
// export all the files we made in our index
let indexFile = '';
for(const fn of exportedFiles) {
const p = path.posix.relative(targetBase, fn.path).replace('.js', '');
indexFile += `export {${fn.functionName}} from './${p}'\n`;
}
fs.writeFileSync(path.posix.join(targetBase, 'index.js'), indexFile);
// update package.json with new set of browser files
const browser = {
"index.js": './' + path.posix.join(targetBase, "index.js"),
};
for(const fn of lodash.sortBy(exportedFiles, f => f.path)) {
const relativePath = path.posix.relative(targetBase, fn.path);
browser[relativePath] = './' + fn.path;
}
const packageJson = JSON.parse(fs.readFileSync('package.json').toString());
packageJson.browser = browser;
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2) + '\n')
}
main();