Skip to content

Commit 13a20bf

Browse files
committed
Make GitHub Pages build script
1 parent d8b93ed commit 13a20bf

6 files changed

+86
-0
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
dist
3+
gh-pages

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ exifreader-*.tgz
1111
test/build/tmp
1212
test/fixtures/images/custom_*
1313
test/fixtures/outputs/custom_*
14+
gh-pages

bin/build-pages.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
*
5+
* This script is used to build GitHub Pages when there is a new release. This
6+
* script will create a temporary directory "gh-pages.tmp" and place all files
7+
* needed for GitHub Pages in there. Then they need to be committed to the
8+
* gh-pages branch.
9+
*/
10+
11+
const fs = require('fs');
12+
const path = require('path');
13+
14+
const ROOT = path.join(__dirname, '..');
15+
const GH_PAGES_TEMP_DIR = path.join(ROOT, 'gh-pages');
16+
17+
createTempDirectory();
18+
buildSite();
19+
20+
function createTempDirectory() {
21+
try {
22+
fs.mkdirSync(GH_PAGES_TEMP_DIR);
23+
} catch (error) {
24+
if (error.code !== 'EEXIST') {
25+
console.error(error); // eslint-disable-line no-console
26+
process.exit(1);
27+
}
28+
}
29+
}
30+
31+
function buildSite() {
32+
const directories = ['amd', 'esm', 'global', 'nodejs', 'src'];
33+
let files = collectFiles(path.join(ROOT, 'examples'), '', ['html', 'css', 'js']);
34+
files = collectFiles(ROOT, 'src', ['js'], files);
35+
files[path.join(ROOT, 'dist/exif-reader.js')] = 'exif-reader.js';
36+
files[path.join(ROOT, 'dist/exif-reader.js.map')] = 'exif-reader.js.map';
37+
38+
createDirectories(directories);
39+
copyFiles(files);
40+
}
41+
42+
function collectFiles(rootDirectory, subDirectory, extensions, files = {}) {
43+
fs.readdirSync(path.join(rootDirectory, subDirectory), {withFileTypes: true}).forEach((file) => {
44+
if (file.isDirectory()) {
45+
files = collectFiles(rootDirectory, path.join(subDirectory, file.name), extensions, files);
46+
} else if (hasFileExtension(file.name, extensions)) {
47+
files[path.join(rootDirectory, subDirectory, file.name)] = path.join(subDirectory, file.name);
48+
}
49+
});
50+
51+
return files;
52+
}
53+
54+
function hasFileExtension(filename, extensions) {
55+
return extensions.some((extension) => filename.endsWith(extension));
56+
}
57+
58+
function createDirectories(directories) {
59+
directories.forEach((directory) => {
60+
try {
61+
fs.mkdirSync(path.join(GH_PAGES_TEMP_DIR, directory));
62+
} catch (error) {
63+
if (error.code !== 'EEXIST') {
64+
console.error(error); // eslint-disable-line no-console
65+
process.exit(1);
66+
}
67+
}
68+
});
69+
}
70+
71+
function copyFiles(files) {
72+
for (const from in files) {
73+
fs.copyFileSync(from, path.join(GH_PAGES_TEMP_DIR, files[from]));
74+
}
75+
}

bin/build.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
15
const path = require('path');
26
const {execSync} = require('child_process');
37
const dependentHasExifReaderConfig = require('./findDependentConfig');

bin/findDependentConfig.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
15
const path = require('path');
26

37
module.exports = getConfigFromParent.bind(null, path.join(__dirname, '..'));

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"scripts": {
5050
"build": "node bin/build.js",
51+
"build:pages": "node bin/build-pages.js",
5152
"coverage": "nyc npm test",
5253
"cypress:open": "cypress open",
5354
"lint": "eslint .",

0 commit comments

Comments
 (0)