Skip to content

Commit

Permalink
Empty output folder before generating doc
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Jun 27, 2020
1 parent 683a1b0 commit fae4742
Showing 1 changed file with 69 additions and 64 deletions.
133 changes: 69 additions & 64 deletions src/lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,85 @@ const SIDEBAR = 'sidebar-schema.js';
const HOMEPAGE_ID = 'schema';

module.exports = class Renderer {
constructor(printer, outputDir, baseURL) {
this.outputDir = outputDir;
this.baseURL = baseURL;
this.p = printer;
}
constructor(printer, outputDir, baseURL) {
this.outputDir = outputDir;
this.baseURL = baseURL;
this.p = printer;
this.emptyOutputDir();
}

emptyOutputDir() {
fs.emptyDirSync(this.outputDir);
}

async renderRootTypes(name, type) {
let pages = [];
if (type) {
const slug = toSlug(name);
const dirPath = path.join(this.outputDir, slug);
if (isArray(type)) {
type = type.reduce(function (r, o) {
if (o && o.name) r[o.name] = o;
return r;
}, {});
}
async renderRootTypes(name, type) {
let pages = [];
if (type) {
const slug = toSlug(name);
const dirPath = path.join(this.outputDir, slug);
if (isArray(type)) {
type = type.reduce(function (r, o) {
if (o && o.name) r[o.name] = o;
return r;
}, {});
}

fs.ensureDir(dirPath);
fs.ensureDir(dirPath);

await Promise.all(Object.keys(type).map((name) => this.renderTypeEntities(dirPath, name, type[name]))).then(
(p) => {
pages = [...p];
},
);
}
return pages;
await Promise.all(Object.keys(type).map((name) => this.renderTypeEntities(dirPath, name, type[name]))).then(
(p) => {
pages = [...p];
},
);
}
return pages;
}

async renderTypeEntities(dirPath, name, type) {
if (type) {
const fileName = toSlug(name);
const filePath = path.join(dirPath, `${fileName}.md`);
const content = this.p.printType(fileName, type);
fs.outputFile(filePath, content, async (err) => {
if (err) throw err;
});
const page = path.relative(this.outputDir, filePath).match(/(?<category>[A-z-]+)\/(?<pageId>[A-z-]+).md$/);
const slug = path.join(page.groups.category, page.groups.pageId);
return { category: startCase(page.groups.category), slug: slug };
}
async renderTypeEntities(dirPath, name, type) {
if (type) {
const fileName = toSlug(name);
const filePath = path.join(dirPath, `${fileName}.md`);
const content = this.p.printType(fileName, type);
fs.outputFile(filePath, content, async (err) => {
if (err) throw err;
});
const page = path.relative(this.outputDir, filePath).match(/(?<category>[A-z-]+)\/(?<pageId>[A-z-]+).md$/);
const slug = path.join(page.groups.category, page.groups.pageId);
return { category: startCase(page.groups.category), slug: slug };
}
}

renderSidebar(pages) {
const filePath = path.join(this.outputDir, SIDEBAR);
const content = prettifyJavascript(`module.exports = {
renderSidebar(pages) {
const filePath = path.join(this.outputDir, SIDEBAR);
const content = prettifyJavascript(`module.exports = {
schemaSidebar:
${JSON.stringify(this.generateSidebar(pages))}
};`);
fs.outputFileSync(filePath, content);
return path.relative('./', filePath);
}
fs.outputFileSync(filePath, content);
return path.relative('./', filePath);
}

generateSidebar(pages) {
let graphqlSidebar = [{ type: 'doc', id: path.join(this.baseURL, HOMEPAGE_ID) }];
pages.map((page) => {
const category = graphqlSidebar.find((entry) => 'label' in entry && page.category == entry.label);
const items = category && 'items' in category ? category.items : [];
const slug = path.join(this.baseURL, page.slug);
graphqlSidebar = [
...pull(graphqlSidebar, category),
{ type: 'category', label: page.category, items: [...items, slug].sort() },
];
});
return graphqlSidebar;
}
generateSidebar(pages) {
let graphqlSidebar = [{ type: 'doc', id: path.join(this.baseURL, HOMEPAGE_ID) }];
pages.map((page) => {
const category = graphqlSidebar.find((entry) => 'label' in entry && page.category == entry.label);
const items = category && 'items' in category ? category.items : [];
const slug = path.join(this.baseURL, page.slug);
graphqlSidebar = [
...pull(graphqlSidebar, category),
{ type: 'category', label: page.category, items: [...items, slug].sort() },
];
});
return graphqlSidebar;
}

renderHomepage(homepageLocation) {
const homePage = path.basename(homepageLocation);
const destLocation = path.join(this.outputDir, homePage);
fs.copySync(homepageLocation, destLocation);
const data = fs
.readFileSync(destLocation, 'utf8')
.replace(/##generated-date-time##/gm, moment().format('MMMM DD, YYYY [at] h:mm:ss A'));
fs.outputFileSync(destLocation, data, 'utf8');
}
renderHomepage(homepageLocation) {
const homePage = path.basename(homepageLocation);
const destLocation = path.join(this.outputDir, homePage);
fs.copySync(homepageLocation, destLocation);
const data = fs
.readFileSync(destLocation, 'utf8')
.replace(/##generated-date-time##/gm, moment().format('MMMM DD, YYYY [at] h:mm:ss A'));
fs.outputFileSync(destLocation, data, 'utf8');
}
};

0 comments on commit fae4742

Please sign in to comment.