From 6b9f6c6e3b2f82ea2a466614626b7bfaa6ad9286 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Mon, 8 Jan 2018 01:47:56 +0900
Subject: [PATCH] Show the licenses in the doc

---
 package.json                 |  2 ++
 src/web/docs/api/gulpfile.ts |  8 ++++----
 src/web/docs/gulpfile.ts     |  4 ++--
 src/web/docs/license.en.pug  | 14 ++++++++++++++
 src/web/docs/license.ja.pug  | 14 ++++++++++++++
 src/web/docs/style.styl      |  2 ++
 src/web/docs/vars.ts         | 17 ++++++++++++++++-
 7 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/package.json b/package.json
index 9245a6e2a..69c92efdf 100644
--- a/package.json
+++ b/package.json
@@ -52,6 +52,7 @@
 		"@types/is-root": "1.0.0",
 		"@types/is-url": "1.2.28",
 		"@types/js-yaml": "3.10.1",
+		"@types/license-checker": "^15.0.0",
 		"@types/mkdirp": "0.5.2",
 		"@types/mocha": "2.2.45",
 		"@types/mongodb": "2.2.18",
@@ -122,6 +123,7 @@
 		"is-root": "1.0.0",
 		"is-url": "1.2.2",
 		"js-yaml": "3.10.0",
+		"license-checker": "^15.0.0",
 		"mecab-async": "0.1.2",
 		"mkdirp": "0.5.1",
 		"mocha": "4.1.0",
diff --git a/src/web/docs/api/gulpfile.ts b/src/web/docs/api/gulpfile.ts
index 4c30871a0..cd1bf1530 100644
--- a/src/web/docs/api/gulpfile.ts
+++ b/src/web/docs/api/gulpfile.ts
@@ -17,8 +17,6 @@ import config from './../../../conf';
 
 import generateVars from '../vars';
 
-const commonVars = generateVars();
-
 const langs = Object.keys(locales);
 
 const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
@@ -94,7 +92,8 @@ gulp.task('doc:api', [
 	'doc:api:entities'
 ]);
 
-gulp.task('doc:api:endpoints', () => {
+gulp.task('doc:api:endpoints', async () => {
+	const commonVars = await generateVars();
 	glob('./src/web/docs/api/endpoints/**/*.yaml', (globErr, files) => {
 		if (globErr) {
 			console.error(globErr);
@@ -144,7 +143,8 @@ gulp.task('doc:api:endpoints', () => {
 	});
 });
 
-gulp.task('doc:api:entities', () => {
+gulp.task('doc:api:entities', async () => {
+	const commonVars = await generateVars();
 	glob('./src/web/docs/api/entities/**/*.yaml', (globErr, files) => {
 		if (globErr) {
 			console.error(globErr);
diff --git a/src/web/docs/gulpfile.ts b/src/web/docs/gulpfile.ts
index 71033e1bc..d5ddda108 100644
--- a/src/web/docs/gulpfile.ts
+++ b/src/web/docs/gulpfile.ts
@@ -23,9 +23,9 @@ gulp.task('doc', [
 	'doc:styles'
 ]);
 
-const commonVars = generateVars();
+gulp.task('doc:docs', async () => {
+	const commonVars = await generateVars();
 
-gulp.task('doc:docs', () => {
 	glob('./src/web/docs/**/*.*.pug', (globErr, files) => {
 		if (globErr) {
 			console.error(globErr);
diff --git a/src/web/docs/license.en.pug b/src/web/docs/license.en.pug
index 240756e7e..45d8b7647 100644
--- a/src/web/docs/license.en.pug
+++ b/src/web/docs/license.en.pug
@@ -1,3 +1,17 @@
 h1 License
 
 div!= common.license
+
+details
+	summary Libraries
+
+	section
+		h2 Libraries
+
+		each dependency, name in common.dependencies
+			details
+				summary= name
+
+				section
+					h3= name
+					pre= dependency.licenseText
diff --git a/src/web/docs/license.ja.pug b/src/web/docs/license.ja.pug
index 1f44f3f5e..7bd9a6294 100644
--- a/src/web/docs/license.ja.pug
+++ b/src/web/docs/license.ja.pug
@@ -1,3 +1,17 @@
 h1 ライセンス
 
 div!= common.license
+
+details
+	summary ライブラリ
+
+	section
+		h2 ライブラリ
+
+		each dependency, name in common.dependencies
+			details
+				summary= name
+
+				section
+					h3= name
+					pre= dependency.licenseText
diff --git a/src/web/docs/style.styl b/src/web/docs/style.styl
index a726d49b1..bc165f872 100644
--- a/src/web/docs/style.styl
+++ b/src/web/docs/style.styl
@@ -114,5 +114,7 @@ code
 	border-radius 4px
 
 pre
+	overflow auto
+
 	> code
 		display block
diff --git a/src/web/docs/vars.ts b/src/web/docs/vars.ts
index 95ae9ee62..6f713f21d 100644
--- a/src/web/docs/vars.ts
+++ b/src/web/docs/vars.ts
@@ -1,13 +1,16 @@
 import * as fs from 'fs';
+import * as util from 'util';
 import * as glob from 'glob';
 import * as yaml from 'js-yaml';
+import * as licenseChecker from 'license-checker';
+import * as tmp from 'tmp';
 
 import { fa } from '../../common/build/fa';
 import config from '../../conf';
 import { licenseHtml } from '../../common/build/license';
 const constants = require('../../const.json');
 
-export default function(): { [key: string]: any } {
+export default async function(): Promise<{ [key: string]: any }> {
 	const vars = {} as { [key: string]: any };
 
 	const endpoints = glob.sync('./src/web/docs/api/endpoints/**/*.yaml');
@@ -45,5 +48,17 @@ export default function(): { [key: string]: any } {
 
 	vars['license'] = licenseHtml;
 
+	const tmpObj = tmp.fileSync();
+	fs.writeFileSync(tmpObj.name, JSON.stringify({
+		licenseText: ''
+	}), 'utf-8');
+	const dependencies = await util.promisify(licenseChecker.init).bind(licenseChecker)({
+		start: __dirname + '/../../../',
+		customPath: tmpObj.name
+	});
+	tmpObj.removeCallback();
+
+	vars['dependencies'] = dependencies;
+
 	return vars;
 }