@theintern/dev
Version:
Development support scripts for Intern projects.
96 lines • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var typedoc_1 = require("typedoc");
var path_1 = require("path");
var fs_1 = require("fs");
var resolve_1 = require("resolve");
var chalk_1 = tslib_1.__importDefault(require("chalk"));
var common_1 = require("./common");
common_1.log('Generating API data');
var entryPoints = process.argv.slice(2);
if (entryPoints.length === 0) {
entryPoints.push('./src/index.ts');
}
var app = new typedoc_1.Application();
app.options.addReader(new typedoc_1.TSConfigReader());
app.bootstrap({
logger: 'none',
excludePrivate: true,
});
var project = app.convert(entryPoints);
if (!project) {
common_1.log(chalk_1.default.red('The project could not be analyzed.'));
var typedoc = require.resolve('typedoc');
var tsc = path_1.relative(process.cwd(), resolve_1.sync('typescript/bin/tsc', { basedir: typedoc }));
common_1.log(chalk_1.default.red("Try building with " + tsc + " to see what's wrong."));
process.exit(1);
}
var cwd = process.cwd();
common_1.log('Scrubbing file paths');
scrubPaths(project);
common_1.log('Normalizing line endings');
normalizeLineEndings(project);
var json = JSON.stringify(app.serializer.projectToObject(project), null, '\t');
if (!fs_1.existsSync('docs')) {
common_1.log('Making docs directory');
fs_1.mkdirSync('docs');
}
var outFile = path_1.join('docs', 'api.json');
fs_1.writeFileSync(outFile, json);
common_1.log("Wrote API data to " + outFile);
function normalizeLineEndings(reflection) {
walk(reflection, '__lenormalized__', function (_, value) { return typeof value === 'string' && /\r\n/.test(value); }, function (value) { return value.replace(/\r\n/g, '\n'); });
}
function scrubPaths(reflection) {
walk(reflection, '__scrubbed__', function (key, value) {
return typeof value === 'string' &&
(key === 'originalName' || key === 'fileName' || key === 'name');
}, scrubPath);
}
function scrubPath(value) {
if (/".*"/.test(value)) {
var testValue = value.replace(/^"/, '').replace(/"$/, '');
if (path_1.isAbsolute(testValue)) {
var newPath = "\"" + path_1.relative(cwd, testValue) + "\"";
return newPath.replace(/\\/g, '/');
}
}
else if (path_1.isAbsolute(value)) {
var newPath = path_1.relative(cwd, value);
return newPath.replace(/\\/g, '/');
}
return value;
}
function walk(reflection, sentinel, test, modify) {
if (reflection[sentinel]) {
return;
}
reflection[sentinel] = true;
if (Array.isArray(reflection)) {
for (var _i = 0, reflection_1 = reflection; _i < reflection_1.length; _i++) {
var item = reflection_1[_i];
if (typeof item === 'object') {
walk(item, sentinel, test, modify);
}
}
}
else if (typeof reflection === 'object') {
var keys = Object.keys(reflection);
for (var _a = 0, keys_1 = keys; _a < keys_1.length; _a++) {
var key = keys_1[_a];
var value = reflection[key];
if (value == null) {
continue;
}
if (test(key, value)) {
reflection[key] = modify(value);
}
else if (typeof value === 'object') {
walk(value, sentinel, test, modify);
}
}
}
}
//# sourceMappingURL=intern-dev-api.js.map