Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
fix: fix ddoc build command on empty src folder
Browse files Browse the repository at this point in the history
  • Loading branch information
fox1t committed Mar 4, 2020
1 parent 6c640d8 commit ef09ae3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 14 additions & 2 deletions bin/ddoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ prog
console.log(`> ${chalk_1.default.bgBlueBright(chalk_1.default.black(' ddoc build config '))} ${chalk_1.default.cyan(tsconfigPath)}`);
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 srcStats;
try {
srcStats = await stat(src);
}
catch (err) {
if (err.code === 'ENOENT') {
console.log(chalk_1.default.bgGreen(chalk_1.default.black(`\n ddoc build - No input files found. Done. `)));
process.exit(0);
}
throw err;
}
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;
Expand All @@ -57,12 +67,13 @@ prog
console.log(`> ${chalk_1.default.bgBlueBright(chalk_1.default.black(' ddoc build dest '))} ${chalk_1.default.cyan(dest)}`);
const errors = [];
await Promise.all(ddocs.map(async (srcPath) => {
var _a;
try {
const sourceFile = (await readFile(srcPath)).toString();
const output = typescript_1.default.transpileModule(sourceFile, tsconfig);
const filename = path_1.default.basename(srcPath, '.ts');
const ddoc = require_from_string_1.default(output.outputText);
const stringifiedDesign = JSON.stringify(ddoc, (_, val) => {
const stringifiedDesign = JSON.stringify((_a = ddoc.default) !== null && _a !== void 0 ? _a : ddoc, (_, val) => {
if (typeof val === 'function') {
return val.toString();
}
Expand All @@ -81,6 +92,7 @@ prog
});
throw new Error(`ddoc compilation failed. Resolve errors ${errors.length} ${errors.length > 1 ? 'files' : 'file'} and try again.`);
}
console.log(chalk_1.default.bgGreen(chalk_1.default.black(`\n ddoc build - done on ${ddocs.length} files. `)));
}
catch (err) {
console.error(chalk_1.default.bgRed(chalk_1.default.white(` ${err.message} `)));
Expand Down
16 changes: 13 additions & 3 deletions src/bin/ddoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ prog
const tsconfig = require(tsconfigPath) // eslint-disable-line

src = path.isAbsolute(src) ? path.normalize(src) : path.join(cwd, src) // eslint-disable-line

const srcStats = await stat(src)
let srcStats: any
try {
srcStats = await stat(src)
} catch (err) {
if (err.code === 'ENOENT') {
console.log(chalk.bgGreen(chalk.black(`\n ddoc build - No input files found. Done. `)))
process.exit(0)
}
throw err
}
// use outDir if specified inside tsconfig, otherwise build json alongside ts files
let dest: string = tsconfig?.compilerOptions?.outDir
? path.join(path.dirname(tsconfigPath), tsconfig.compilerOptions.outDir)
Expand All @@ -72,8 +80,9 @@ prog
const output = ts.transpileModule(sourceFile, tsconfig)
const filename = path.basename(srcPath, '.ts')
const ddoc = requireFromString(output.outputText)

const stringifiedDesign = JSON.stringify(
ddoc,
ddoc.default ?? ddoc,
(_, val) => {
if (typeof val === 'function') {
return val.toString()
Expand Down Expand Up @@ -102,6 +111,7 @@ prog
} and try again.`,
)
}
console.log(chalk.bgGreen(chalk.black(`\n ddoc build - done on ${ddocs.length} files. `)))
} catch (err) {
console.error(chalk.bgRed(chalk.white(` ${err.message} `)))
process.exit(1)
Expand Down

0 comments on commit ef09ae3

Please sign in to comment.