UNPKG

3.27 kBJavaScriptView Raw
1const filters = require("./docs/filters.js");
2const files = require("./docs/files.js");
3const sass = require("node-sass");
4
5const fs = require("fs");
6
7function extract(src) {
8 const data = {};
9 const readme = fs.readFileSync(src + "README.md", "utf-8");
10 data.title = (readme.match(/^\#\s(.+)/gm) || []).map(one =>
11 one.replace(/^\#\s/, "")
12 )[0];
13 if (!data.title)
14 throw new Error("Your file " + file + "/README.md has no h1 in markdown");
15 data.sections = (readme.match(/^\#\#[\s](.+)/gm) || []).map(one =>
16 one.replace(/^\#\#\s/, "")
17 );
18 return data;
19}
20
21function getInfo(src) {
22 delete require.cache[require.resolve(src)];
23 const info = {};
24 if (/documentation/.test(src)) {
25 const base = { title: "Introduction", url: "/documentation/" };
26 info.introduction = Object.assign({}, extract(src), base);
27 }
28 return require(src).reduce((obj, one) => {
29 return Object.assign({}, obj, { [one]: extract(src + one + "/") });
30 }, info);
31}
32
33// Generate the documentation final:origin pairs
34const transform = dir =>
35 files(__dirname + "/" + dir)
36 .filter(str => /\.html\.pug$/.test(str))
37 .reduce((docs, one) => {
38 docs[one.replace(/\.pug$/, "")] = one;
39 return docs;
40 }, {});
41
42// This builds the library itself
43module.exports = function(grunt) {
44 // Configuration
45 grunt.initConfig({
46 bytesize: {
47 all: {
48 src: ["docs/assets/style.min.css", "docs/assets/javascript.js"]
49 }
50 },
51
52 jshint: {
53 options: { esversion: 6 },
54 src: ["Gruntfile.js", "server.js", "src"]
55 },
56
57 // Launch a small static server
58 connect: {
59 server: {
60 options: {
61 port: 3000,
62 hostname: "*",
63 base: "docs",
64 livereload: true,
65 useAvailablePort: false
66 }
67 }
68 },
69
70 sass: {
71 dist: {
72 options: { implementation: sass, outputStyle: "compressed" },
73 files: { "docs/assets/style.min.css": "docs/assets/style.scss" }
74 }
75 },
76
77 pug: {
78 compile: {
79 options: {
80 client: false,
81 data: file => {
82 return {
83 require,
84 file,
85 tutorials: getInfo("./docs/tutorials/"),
86 documentation: getInfo("./docs/documentation/"),
87 slug: str => str.toLowerCase().replace(/[^\w]+/g, "-")
88 };
89 },
90 filters: filters
91 },
92 files: transform("docs")
93 }
94 },
95
96 watch: {
97 scripts: {
98 files: [
99 "Gruntfile.js",
100
101 // Docs
102 "docs/**/*.*",
103 "README.md",
104
105 // For testing:
106 "server.js",
107 "src/**/*.js",
108
109 // To bump versions
110 "package.js"
111 ],
112 tasks: ["default"],
113 options: {
114 spawn: false,
115 livereload: true
116 }
117 }
118 }
119 });
120
121 grunt.loadNpmTasks("grunt-contrib-connect");
122 grunt.loadNpmTasks("grunt-contrib-jshint");
123 grunt.loadNpmTasks("grunt-contrib-pug");
124 grunt.loadNpmTasks("grunt-contrib-watch");
125 grunt.loadNpmTasks("grunt-bytesize");
126 grunt.loadNpmTasks("grunt-sass");
127
128 grunt.registerTask("build", ["sass", "pug"]);
129 grunt.registerTask("test", ["bytesize"]);
130 grunt.registerTask("default", ["build", "test", "connect"]);
131};