|
1 |
| -var fss = require('../../../fs_extras.js'), |
2 |
| - Q = require('q'), |
3 |
| - path = require('path'), |
4 |
| - md5 = require('md5'), |
5 |
| - promiseLock = require("../../../promise_lock"); |
6 |
| - |
7 |
| -var queue = promiseLock(), |
8 |
| - buildHash = require("./build_hash"); |
| 1 | +var fss = require("../../../fs_extras"); |
| 2 | +var Q = require("q"); |
| 3 | +var path = require("path"); |
| 4 | +var buildHash = require("./build_hash"); |
| 5 | +var promiseLock = require("../../../promise_lock"); |
| 6 | +var fs = require("fs-extra"); |
| 7 | +var makePackageJson = require("./make_package_json"); |
| 8 | +var npmInstall = require("./npm_install"); |
| 9 | + |
| 10 | +var copy = Q.denodeify(fs.copy); |
| 11 | +var writeFile = Q.denodeify(fs.writeFile); |
| 12 | +var queue = promiseLock(); |
9 | 13 |
|
10 | 14 | /**
|
11 | 15 | * @function documentjs.generators.html.build.staticDist
|
12 | 16 | * @parent documentjs.generators.html.build.methods
|
13 |
| - * |
| 17 | + * |
14 | 18 | * Builds a static distributable which will eventually be copied
|
15 | 19 | * to the `static` folder of the generated output.
|
16 |
| - * |
| 20 | + * |
17 | 21 | * @signature `.build.staticDist(options)`
|
18 |
| - * |
| 22 | + * |
19 | 23 | * Builds the static distributable with the following steps:
|
20 |
| - * |
21 |
| - * 1. Copies everything from _documentjs/site/default/static_ to |
22 |
| - * _documentjs/site/static/build_. |
23 |
| - * 2. Copies the path in `options.dest` to _documentjs/site/static/build_. |
24 |
| - * 3. `require`s the module at _documentjs/site/static/build/build.js_. |
25 |
| - * 4. Calls that "build" module function with the options and returns the result. |
26 |
| - * |
27 |
| - * The "build" module is expected to build a minified distributable |
| 24 | + * |
| 25 | + * 1. Copies everything from _documentjs/site/default/static_ to _documentjs/site/static/build_. |
| 26 | + * 2. Overwrites site/static/build with content in `options.static`. |
| 27 | + * 3. Writes a `package.json` file to _documentjs/site/static/build_. |
| 28 | + * 4. Runs `npm install` in _documentjs/site/static/build_ to get the dependencies for the build. |
| 29 | + * 5. Calls that "build" function at _documentjs/site/static/build/build.js_ with |
| 30 | + * the options and returns the result. |
| 31 | + * |
| 32 | + * The "build" module is expected to build a minified distributable |
28 | 33 | * and copy the necessary contents to _documentjs/site/static/dist_ and
|
29 | 34 | * return a promise that resolves when complete.
|
30 |
| - * |
| 35 | + * |
31 | 36 | * @param {{}} options
|
32 |
| - * |
33 |
| - * @option {Boolean} [forceBuild=false] If set to `true`, rebuilds the |
| 37 | + * |
| 38 | + * @option {Boolean} [forceBuild=false] If set to `true`, rebuilds the |
34 | 39 | * static bundle even if it has already been built.
|
35 |
| - * |
| 40 | + * |
36 | 41 | * @option {String} dest The final destination ouput of the static
|
37 | 42 | * distributable.
|
38 |
| - * |
| 43 | + * |
39 | 44 | * @option {String} static The location of static content used to overwrite or
|
40 | 45 | * add to the default static content.
|
41 |
| - * |
42 |
| - * @option {Boolean} [minifyBuild=true] If set to `false` the build will not |
| 46 | + * |
| 47 | + * @option {Boolean} [minifyBuild=true] If set to `false` the build will not |
43 | 48 | * be minified. This behavior should be implemented by the "build" module.
|
44 |
| - * |
| 49 | + * |
45 | 50 | * @return {Promise} A promise that resolves if the static dist was successfully created.
|
46 |
| - * |
| 51 | + * |
47 | 52 | */
|
48 |
| -module.exports = function(options){ |
| 53 | +module.exports = function(options) { |
49 | 54 | // only run one build at a time.
|
50 |
| - return queue(function(){ |
51 |
| - var builtAlready; |
52 |
| - |
| 55 | + return queue(function staticDistQueue() { |
53 | 56 | var hash = buildHash(options);
|
54 |
| - |
55 |
| - var distFolder = path.join("site","static","dist", hash), |
56 |
| - buildFolder = path.join("site","static","build", hash); |
| 57 | + var distFolder = path.join("site", "static", "dist", hash); |
| 58 | + var buildFolder = path.join("site", "static", "build", hash); |
57 | 59 |
|
58 | 60 | var mkdirPromise = Q.all([
|
59 | 61 | fss.mkdirs(distFolder),
|
60 | 62 | fss.mkdirs(buildFolder)
|
61 | 63 | ]);
|
62 |
| - |
63 |
| - return mkdirPromise.then(function(){ |
64 |
| - return fss.exists(path.join(distFolder,"bundles","static.css")) |
65 |
| - .then(function(exists){ |
| 64 | + |
| 65 | + var buildPromise = mkdirPromise |
| 66 | + .then(function() { |
| 67 | + return fss.exists( |
| 68 | + path.join(distFolder, "bundles", "static.css") |
| 69 | + ); |
| 70 | + }) |
| 71 | + .then(function(exists) { |
66 | 72 | // If we have already built, don't build again
|
67 |
| - if(exists && !options.forceBuild) { |
68 |
| - builtAlready = true; |
69 |
| - if(options.debug) { |
70 |
| - console.log("BUILD: Using cache",distFolder); |
| 73 | + if (exists && !options.forceBuild) { |
| 74 | + if (options.debug) { |
| 75 | + console.log("BUILD: Using cache", distFolder); |
71 | 76 | }
|
72 |
| - |
73 |
| - return; |
| 77 | + } else { |
| 78 | + return buildSiteStaticAssets(); |
74 | 79 | }
|
75 |
| - |
76 |
| - return fss.copy(path.join("site","default","static"), buildFolder) |
77 |
| - .then(function(){ |
78 |
| - if(options["static"]){ |
| 80 | + }); |
| 81 | + |
| 82 | + function buildSiteStaticAssets() { |
| 83 | + var docjsRoot = path.join(__dirname, "..", "..", "..", ".."); |
| 84 | + |
| 85 | + return Promise.resolve() |
| 86 | + .then(function copyFromSiteDefaultToBuildFolder() { |
| 87 | + return fss.copy( |
| 88 | + path.join("site", "default", "static"), |
| 89 | + buildFolder |
| 90 | + ); |
| 91 | + }) |
| 92 | + .then(function overrideWithOptionsStatic() { |
| 93 | + if (options["static"]) { |
79 | 94 | return fss.copyFrom(options["static"], buildFolder);
|
80 |
| - } |
| 95 | + } |
| 96 | + }) |
| 97 | + .then(function writeBuildPackageJson() { |
| 98 | + var pkg = makePackageJson(options); |
| 99 | + return writeFile( |
| 100 | + path.join(docjsRoot, buildFolder, "package.json"), |
| 101 | + JSON.stringify(pkg, null, 2) |
| 102 | + ); |
| 103 | + }) |
| 104 | + .then(function installDependencies() { |
| 105 | + if (options.debug) { |
| 106 | + console.log("BUILD: Installing node_modules"); |
| 107 | + } |
| 108 | + return npmInstall({ |
| 109 | + stdio: options.debug ? "inherit" : "pipe", |
| 110 | + cwd: path.join(docjsRoot, buildFolder) |
| 111 | + }); |
| 112 | + }) |
| 113 | + .then(function runBuildScript() { |
| 114 | + if (options.debug) { |
| 115 | + console.log("BUILD: Running build script"); |
| 116 | + } |
| 117 | + |
| 118 | + var build = require(path.join( |
| 119 | + docjsRoot, |
| 120 | + buildFolder, |
| 121 | + "build.js" |
| 122 | + )); |
| 123 | + |
| 124 | + return build(options, { |
| 125 | + dist: distFolder, |
| 126 | + build: buildFolder |
| 127 | + }); |
81 | 128 | });
|
82 |
| - }); |
83 |
| - }).then(function(){ |
84 |
| - if(builtAlready){ |
85 |
| - return; |
86 |
| - } |
87 |
| - if(options.debug) { |
88 |
| - console.log("BUILD: Getting build module"); |
89 |
| - } |
90 |
| - |
91 |
| - var build = require("../../../../site/static/build/"+hash+"/build.js"); |
92 |
| - return build(options,{ |
93 |
| - dist: distFolder, |
94 |
| - build: buildFolder |
95 |
| - }); |
96 |
| - }); |
| 129 | + } |
| 130 | + |
| 131 | + return buildPromise; |
97 | 132 | });
|
98 |
| - |
99 |
| - |
100 | 133 | };
|
0 commit comments