Skip to content

Commit

Permalink
fix: cannot use splitBySubmodule and multiple languages together
Browse files Browse the repository at this point in the history
Fixes: #1250
  • Loading branch information
mrgrain committed Dec 20, 2023
1 parent c49acc7 commit 31f5489
Show file tree
Hide file tree
Showing 5 changed files with 6,398 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@ type GenerateOptions = {
submodule?: string;
allSubmodules?: boolean;
splitBySubmodules?: boolean;
format?: 'md' | 'json';
output?: string;
format: 'md' | 'json';
output: string;
}

async function generateForLanguage(docs: Documentation, options: GenerateOptions) {
const { format, output = 'API' } = options;
const fileSuffix = format === 'md' ? 'md' : 'json';
let submoduleSuffix = fileSuffix;

const outputFileName = output.endsWith(`.${fileSuffix}`)
? output.slice(0, -(fileSuffix.length + 1))
: output;
const { format, output } = options;
// e.g. API.typescript as name
if (outputFileName.includes('.')) {
const languageSeparator = outputFileName.split('.')[1];
submoduleSuffix = `${languageSeparator}.${fileSuffix}`;
}
const splitByLanguage = output.endsWith(`.${options.language.name}`);
const submoduleSuffix = splitByLanguage ? `${options.language.name}.${format}` : format;

// Ensure the output path exists
const outputPath = path.dirname(outputFileName);
const outputPath = path.dirname(output);
await fs.mkdir(outputPath, { recursive: true });

if (options.splitBySubmodules) {
Expand All @@ -49,10 +41,10 @@ async function generateForLanguage(docs: Documentation, options: GenerateOptions
await fs.writeFile(path.join(outputPath, `${submoduleRelName(submodule)}.${submoduleSuffix}`), content.render());
}

await fs.writeFile(`${outputFileName}.${fileSuffix}`, await (await docs.toIndexMarkdown(submoduleSuffix, options)).render());
await fs.writeFile(`${output}.${format}`, await (await docs.toIndexMarkdown(submoduleSuffix, options)).render());
} else {
const content = await (format === 'md' ? docs.toMarkdown(options) : docs.toJson(options));
await fs.writeFile(`${outputFileName}.${fileSuffix}`, content.render());
await fs.writeFile(`${output}.${format}`, content.render());
}
}

Expand All @@ -78,22 +70,35 @@ export async function main() {
? Documentation.forPackage(args.package)
: Documentation.forProject(process.cwd()));

const options = (lang: string, output?: string): GenerateOptions => ({
readme,
language: Language.fromString(lang),
submodule,
allSubmodules,
splitBySubmodules,
format: args.format as 'md' | 'json',
output,
});
const options = (lang: string, output: string = 'API', includeLanguageInOutputName = false): GenerateOptions => {
const format = args.format === 'md' ? 'md' : 'json';

// Clean the user provided output of a possible file ending
let outputFileName = output.endsWith(`.${format}`)
? output.slice(0, -(format.length + 1))
: output;

// for multi language docs, include the language in the filename
if (includeLanguageInOutputName) {
outputFileName = `${outputFileName}.${lang}`;
}

return ({
readme,
language: Language.fromString(lang),
submodule,
allSubmodules,
splitBySubmodules,
format,
output: outputFileName,
});
};

if (args.language.length <= 1) {
await generateForLanguage(docs, options(args.language[0], args.output));
} else {
for (const lang of args.language) {
const output = `${args.output ?? 'API'}.${lang}`;
await generateForLanguage(docs, options(lang, output));
await generateForLanguage(docs, options(lang, args.output, true));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/__fixtures__/libraries/construct-library/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export class GreeterBucket extends s3.Bucket {
public greet() {
console.log(this.bucketName);
}

public greetWithSalutation(salution: string) {
console.log(salution + ' ' + this.bucketName);
}
}

export * as submod1 from './submod1';
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export class GoodbyeBucket extends s3.Bucket {
public goodbye() {
console.log(this.bucketName);
}

public goodbyeWithPhrase(phrase: string) {
console.log(phrase + ' ' + this.bucketName);
}
}
Loading

0 comments on commit 31f5489

Please sign in to comment.