This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
ddoc.js
91 lines (91 loc) · 4.13 KB
/
ddoc.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
#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const util_1 = require("util");
const sade_1 = __importDefault(require("sade"));
const typescript_1 = __importDefault(require("typescript"));
const require_from_string_1 = __importDefault(require("require-from-string"));
const glob_1 = __importDefault(require("glob"));
const mkdirp_1 = __importDefault(require("mkdirp"));
const chalk_1 = __importDefault(require("chalk"));
const stat = util_1.promisify(fs_1.default.stat);
const readFile = util_1.promisify(fs_1.default.readFile);
const writeFile = util_1.promisify(fs_1.default.writeFile);
const unlink = util_1.promisify(fs_1.default.unlink);
const glob = util_1.promisify(glob_1.default);
async function deleteOldDdocs(dest) {
const oldDdocs = await glob(path_1.default.join(dest, '**/*.json'));
return Promise.all(oldDdocs.map(file => unlink(file)));
}
const prog = sade_1.default('ddoc');
prog.version('0.1.0');
prog
.command('build <src>', 'Build design document(s) from TypeScript soruce directory or file.', {
default: true,
})
.option('-c, --config', 'Provide path to custom tsconfig.json', './tsconfig.json')
.example('build src/db/designs')
.example('build src/db/designs/patient.ts -c src/db/tsconfig.json')
.action(async (src, opts) => {
var _a;
try {
const cwd = process.cwd();
const tsconfigPath = path_1.default.isAbsolute(opts.config) ? opts.config : path_1.default.join(cwd, opts.config);
console.log(`> using ${tsconfigPath} config`);
const tsconfig = require(tsconfigPath);
src = path_1.default.isAbsolute(src) ? path_1.default.normalize(src) : path_1.default.join(cwd, src);
const srcStats = await stat(src);
let dest = ((_a = tsconfig === null || tsconfig === void 0 ? void 0 : tsconfig.compilerOptions) === null || _a === void 0 ? void 0 : _a.outDir) ? path_1.default.join(path_1.default.dirname(tsconfigPath), tsconfig.compilerOptions.outDir)
: '';
let ddocs;
if (srcStats.isDirectory()) {
dest = dest || src;
ddocs = await glob(path_1.default.join(src, '**/*.ts'));
}
else {
dest = dest || path_1.default.dirname(src);
ddocs = [src];
}
console.log(`> src directory is ${src}`);
await mkdirp_1.default(dest);
await deleteOldDdocs(dest);
console.log(`> destination directory is ${dest}`);
const errors = [];
await Promise.all(ddocs.map(async (srcPath) => {
try {
const sourceFile = (await readFile(srcPath)).toString();
const output = typescript_1.default.transpileModule(sourceFile, tsconfig);
const filename = path_1.default.basename(srcPath, '.ts');
await writeFile(path_1.default.join(src, `${filename}.js`), output.outputText);
const ddoc = require_from_string_1.default(output.outputText);
const stringifiedDesign = JSON.stringify(ddoc, (_, val) => {
if (typeof val === 'function') {
return val.toString();
}
return val;
}, 1);
await writeFile(path_1.default.join(dest, `${filename}.json`), stringifiedDesign);
}
catch (error) {
errors.push({ file: srcPath, error });
}
}));
if (errors.length > 0) {
errors.forEach(err => {
var _a;
console.log(`\n${chalk_1.default.red('ddoc error')} - ${chalk_1.default.cyan(err.file)}${(_a = err.error.stack) === null || _a === void 0 ? void 0 : _a.toString()}\n`);
});
throw new Error(`Compilation failed. Resolve errors in your code and try again.`);
}
}
catch (err) {
console.error(err);
process.exit(1);
}
});
prog.parse(process.argv);