|
1 |
| -var path = require('path'), |
2 |
| - fs = require('fs'), |
3 |
| - fse = require('fs-extra'), |
4 |
| - ghpages = require('gh-pages'), |
5 |
| - denodeify = require('denodeify'); |
| 1 | +import * as denodeify from 'denodeify'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as fse from 'fs-extra'; |
6 | 5 |
|
7 |
| -function run(options) { |
| 6 | +import { GHPages } from './interfaces'; |
| 7 | +import { Schema as RealDeployOptions } from './deploy/schema'; |
| 8 | + |
| 9 | +const ghpages = require('gh-pages'); |
| 10 | +var access = denodeify(fs.access); |
| 11 | + |
| 12 | +function run(options: RealDeployOptions) { |
8 | 13 |
|
9 | 14 | options = options || {};
|
10 | 15 |
|
@@ -49,84 +54,101 @@ function run(options) {
|
49 | 54 | ghpages.clean();
|
50 | 55 | }
|
51 | 56 |
|
52 |
| - var access = publish = denodeify(fs.access); |
| 57 | + |
53 | 58 | var publish = denodeify(ghpages.publish);
|
54 | 59 |
|
55 | 60 |
|
56 | 61 | return Promise.resolve()
|
57 |
| - .then(function checkIfDistFolderExists() { |
58 |
| - return access(dir, fs.F_OK) |
59 |
| - }) |
60 |
| - .catch(function handleMissingDistFolder(error) { |
61 |
| - console.error('*** Dist folder does not exist. Check the dir --dir parameter or build the project first!\n'); |
62 |
| - return Promise.reject(error); |
63 |
| - }) |
64 |
| - .then(function createNotFoundPage() { |
65 |
| - |
66 |
| - if (options.dryRun) { |
67 |
| - console.info('*** Dry-run / SKIPPED: copying of index.html to 404.html'); |
68 |
| - return; |
69 |
| - } |
70 |
| - |
71 |
| - // Note: |
72 |
| - // There is no guarantee that there will be an index.html file, |
73 |
| - // as the developer may specify a custom index file. |
74 |
| - const indexHtml = path.join(dir, 'index.html'); |
75 |
| - const notFoundPage = path.join(dir, '404.html'); |
76 |
| - |
77 |
| - return fse.copy(indexHtml, notFoundPage). |
78 |
| - catch(function (err) { |
79 |
| - console.info('index.html could not be copied to 404.html. Continuing without an error.'); |
80 |
| - console.info('(Hint: are you sure that you have setup the --dir parameter correctly?)'); |
81 |
| - console.dir(err); |
82 |
| - return; |
83 |
| - }) |
84 |
| - }) |
85 |
| - .then(function createCnameFile() { |
86 |
| - |
87 |
| - if (!options.cname) { |
88 |
| - return; |
89 |
| - } |
90 |
| - |
91 |
| - const cnameFile = path.join(dir, 'CNAME'); |
92 |
| - if (options.dryRun) { |
93 |
| - console.info('*** Dry-run / SKIPPED: creating of CNAME file with content: ' + options.cname); |
94 |
| - return; |
95 |
| - } |
96 |
| - |
97 |
| - return fse.writeFile(cnameFile, options.cname) |
98 |
| - .then(function () { |
99 |
| - console.log('*** CNAME file created'); |
100 |
| - }) |
101 |
| - .catch(function (err) { |
102 |
| - console.info('*** CNAME file could not be created. Stopping execution.'); |
103 |
| - throw err; |
104 |
| - }) |
| 62 | + .then(() => checkIfDistFolderExists(dir)) |
| 63 | + .catch((error) => handleMissingDistFolder(error)) |
| 64 | + .then(() => createNotFoundPage(dir, options)) |
| 65 | + .then(() => createCnameFile(dir, options)) |
| 66 | + .then(() => publishViaGhPages(ghpages, dir, options)) |
| 67 | + .then(() => showSuccess()) |
| 68 | + .catch((error) => showError(error)); |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +function checkIfDistFolderExists(dir: string) { |
| 73 | + const flag = fs['F_OK']; |
| 74 | + return access(dir, flag); |
| 75 | +} |
| 76 | + |
| 77 | +function handleMissingDistFolder(error) { |
| 78 | + console.error('*** Dist folder does not exist. Check the dir --dir parameter or build the project first!\n'); |
| 79 | + return Promise.reject(error); |
| 80 | +} |
| 81 | + |
| 82 | +function createNotFoundPage(dir: string, options: RealDeployOptions) { |
| 83 | + |
| 84 | + if (options.dryRun) { |
| 85 | + console.info('*** Dry-run / SKIPPED: copying of index.html to 404.html'); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Note: |
| 90 | + // There is no guarantee that there will be an index.html file, |
| 91 | + // as the developer may specify a custom index file. |
| 92 | + const indexHtml = path.join(dir, 'index.html'); |
| 93 | + const notFoundPage = path.join(dir, '404.html'); |
| 94 | + |
| 95 | + return fse.copy(indexHtml, notFoundPage). |
| 96 | + catch(function (err) { |
| 97 | + console.info('index.html could not be copied to 404.html. Continuing without an error.'); |
| 98 | + console.info('(Hint: are you sure that you have setup the --dir parameter correctly?)'); |
| 99 | + console.dir(err); |
| 100 | + return; |
105 | 101 | })
|
106 |
| - .then(function publishViaGhPages() { |
107 |
| - if (options.dryRun) { |
108 |
| - console.info('*** Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:', { |
109 |
| - dir: dir, |
110 |
| - repo: options.repo || 'undefined: current working directory (which must be a git repo in this case) will be used to commit & push', |
111 |
| - message: options.message, |
112 |
| - branch: options.branch, |
113 |
| - user: options.user || 'undefined: local or global git username & email properties will be taken', |
114 |
| - noSilent: options.noSilent || 'undefined: logging is in silent mode by default', |
115 |
| - noDotfiles: options.noDotfiles || 'undefined: dotfiles are included by default', |
116 |
| - dryRun: options.dryRun, |
117 |
| - cname: options.cname || 'undefined: no CNAME file will be created', |
118 |
| - }); |
119 |
| - return; |
120 |
| - } |
121 |
| - |
122 |
| - return publish(dir, options) |
| 102 | +} |
| 103 | + |
| 104 | +function createCnameFile(dir: string, options: RealDeployOptions) { |
| 105 | + |
| 106 | + if (!options.cname) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const cnameFile = path.join(dir, 'CNAME'); |
| 111 | + if (options.dryRun) { |
| 112 | + console.info('*** Dry-run / SKIPPED: creating of CNAME file with content: ' + options.cname); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + return fse.writeFile(cnameFile, options.cname) |
| 117 | + .then(function () { |
| 118 | + console.log('*** CNAME file created'); |
123 | 119 | })
|
124 |
| - .then(function showSuccess() { |
125 |
| - console.log('*** Successfully published!\n'); |
| 120 | + .catch(function (err) { |
| 121 | + console.info('*** CNAME file could not be created. Stopping execution.'); |
| 122 | + throw err; |
126 | 123 | })
|
127 |
| - .catch(function showError(error) { |
128 |
| - console.error('*** An error occurred!\n'); |
129 |
| - console.dir(error); |
130 |
| - return Promise.reject(error); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +async function publishViaGhPages(ghPages: GHPages, dir: string, options: RealDeployOptions) { |
| 128 | + if (options.dryRun) { |
| 129 | + console.info('*** Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:', { |
| 130 | + dir: dir, |
| 131 | + repo: options.repo || 'undefined: current working directory (which must be a git repo in this case) will be used to commit & push', |
| 132 | + message: options.message, |
| 133 | + branch: options.branch, |
| 134 | + user: options.user || 'undefined: local or global git username & email properties will be taken', |
| 135 | + noSilent: options.noSilent || 'undefined: logging is in silent mode by default', |
| 136 | + noDotfiles: options.noDotfiles || 'undefined: dotfiles are included by default', |
| 137 | + dryRun: options.dryRun, |
| 138 | + cname: options.cname || 'undefined: no CNAME file will be created', |
131 | 139 | });
|
132 |
| -}; |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + return await ghPages.publish(dir, options) |
| 144 | +} |
| 145 | + |
| 146 | +function showSuccess() { |
| 147 | + console.log('*** Successfully published!\n'); |
| 148 | +} |
| 149 | + |
| 150 | +function showError(error) { |
| 151 | + console.error('*** An error occurred!\n'); |
| 152 | + console.dir(error); |
| 153 | + return Promise.reject(error); |
| 154 | +} |
0 commit comments