|
| 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 | +} |
0 commit comments