Skip to content

Commit

Permalink
chore(build): create folder structure for docs based on structure.ts (#…
Browse files Browse the repository at this point in the history
…543)

so that we don't have a lot of 404 responses from ghpages (SEO unfriendly)
  • Loading branch information
nnixaa authored Jul 4, 2018
1 parent 82a5c93 commit 1b6c10e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ testem.log
/docs/docs.json
/docs/output.json
/docs/assets/examples
/docs/dist

# e2e
/e2e/*.js
Expand Down
2 changes: 1 addition & 1 deletion docs/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export const structure = [
source: [
'NbStepperComponent',
'NbStepComponent',
]
],
},
{
type: 'tabs',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"docs:prepare": "npm-run-all docs:parse",
"docs:start": "npm start -- docs --port 4100",
"docs:serve": "npm-run-all docs:prepare docs:start",
"docs:build": "npm run docs:prepare && npm run build:prod -- docs --base-href '/nebular/'",
"docs:build": "npm run docs:prepare && npm run build:prod -- docs --base-href '/nebular/' && npm run docs:dirs",
"docs:dirs": "gulp create-docs-dirs",
"docs:gh-pages": "npm run docs:build && npm run ngh -- --dir ./docs/dist",
"build": "ng build",
"build:prod": "npm run build -- --prod --aot",
Expand Down
1 change: 1 addition & 0 deletions scripts/gulp/tasks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const LIB_DIR = './src/.lib';
export const PLAYGROUND_ROOT = './src/playground/';
export const DOCS_OUTPUT = './docs/output.json';
export const EXTENSIONS = ['ts', 'html', 'scss'];
export const DOCS_DIST = './docs/dist';
75 changes: 75 additions & 0 deletions scripts/gulp/tasks/docs/docs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { src, task } from 'gulp';
import { exportThemes } from './export-themes';
import './example';
import { structure as DOCS } from '../../../../docs/structure';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { isAbsolute, join, resolve, sep } from 'path';
import { DOCS_DIST } from '../config';

const typedoc = require('gulp-typedoc');
const sass = require('gulp-sass');
Expand All @@ -9,6 +13,10 @@ const exec = require('child_process').execSync;
task('docs', ['generate-doc-json', 'copy-examples', 'find-full-examples']);
task('generate-doc-json', generateDocJson);
task('parse-themes', ['generate-doc-json'], parseThemes);
task('create-docs-dirs', () => {
const docsStructure = flatten('docs', routesTree(DOCS));
createDirsStructure(docsStructure);
});

function generateDocJson() {
return src(['src/framework/**/*.ts', '!src/framework/theme/**/node_modules{,/**}'])
Expand All @@ -35,3 +43,70 @@ function parseThemes() {
functions: exportThemes('docs/', ''),
}));
}

function routesTree(structure) {
return structure
.filter((page: any) => ['section', 'page', 'tabs'].includes(page.type))
.map((page: any) => {
if (page.type === 'tabs') {
page.children = ['overview', 'api', 'theme', 'examples']
.map(name => ({ name, type: 'page'}));
}
return page;
})
.map((page: any) => {
return {
path: prepareSlag(page.name),
children: page.children ? routesTree(page.children) : [],
}
});
}

function prepareSlag(name) {
return name.replace(/[^a-zA-Z0-9\s]+/g, '')
.replace(/\s/g, '-')
.toLowerCase();
}

function flatten(root, arr) {
let res: any[] = [];
arr.forEach((item: any) => {
const path = `${root}/${item.path}`;
res.push(path);
if (item.children) {
res = res.concat(flatten(path, item.children));
}
});

return res;
}

function createDirsStructure(dirs) {
const index = readFileSync(join(DOCS_DIST, 'index.html'), 'utf8');
dirs.forEach((dir: any) => {
const fullPath = join(DOCS_DIST, dir);
if (!existsSync(fullPath)) {
mkDirByPathSync(fullPath);
}

writeFileSync(join(fullPath, 'index.html'), index);
});
}

function mkDirByPathSync(targetDir, {isRelativeToScript = false} = {}) {
const initDir = isAbsolute(targetDir) ? sep : '';
const baseDir = isRelativeToScript ? __dirname : '.';

targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = resolve(baseDir, parentDir, childDir);
try {
mkdirSync(curDir);
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}

return curDir;
}, initDir);
}

0 comments on commit 1b6c10e

Please sign in to comment.