Skip to content

Commit df7d476

Browse files
committed
wip: refactoring to more types and asyn/await
1 parent c87ed88 commit df7d476

File tree

4 files changed

+156
-85
lines changed

4 files changed

+156
-85
lines changed

engine.ts

Lines changed: 101 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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';
65

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) {
813

914
options = options || {};
1015

@@ -49,84 +54,101 @@ function run(options) {
4954
ghpages.clean();
5055
}
5156

52-
var access = publish = denodeify(fs.access);
57+
5358
var publish = denodeify(ghpages.publish);
5459

5560

5661
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;
105101
})
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');
123119
})
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;
126123
})
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',
131139
});
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+
}

index.old.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ exports.run = function (options) {
7878
const notFoundPage = path.join(dir, '404.html');
7979

8080
return fse.copy(indexHtml, notFoundPage).
81-
catch(function (err) {
81+
catch(function (err) {
8282
console.info('index.html could not be copied to 404.html. Continuing without an error.');
8383
console.info('(Hint: are you sure that you have setup the --dir parameter correctly?)');
8484
console.dir(err);
@@ -101,7 +101,7 @@ exports.run = function (options) {
101101
.then(function () {
102102
console.log('*** CNAME file created');
103103
})
104-
.catch(function (err) {
104+
.catch(function (err) {
105105
console.info('*** CNAME file could not be created. Stopping execution.');
106106
throw err;
107107
})

package-lock.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
"@angular-devkit/architect": "^0.800.6",
5050
"@angular-devkit/core": "^8.0.6",
5151
"@angular-devkit/schematics": "^8.0.6",
52+
"@types/denodeify": "^1.2.31",
53+
"@types/fs-extra": "^8.0.0",
5254
"@types/jest": "^24.0.15",
53-
"@types/node": "^12.0.10",
55+
"@types/node": "^12.6.9",
5456
"jest": "^24.8.0",
5557
"json-schema-to-typescript": "^6.1.3",
5658
"ts-jest": "^24.0.2",
@@ -62,8 +64,10 @@
6264
"@angular-devkit/schematics": ">=8.0.0"
6365
},
6466
"dependencies": {
65-
"gh-pages": "^2.1.0",
66-
"commander": "^2.20.0"
67+
"commander": "^2.20.0",
68+
"denodeify": "^1.2.1",
69+
"fs-extra": "^8.1.0",
70+
"gh-pages": "^2.1.0"
6771
},
6872
"jest": {
6973
"transform": {
@@ -79,4 +83,4 @@
7983
"node"
8084
]
8185
}
82-
}
86+
}

0 commit comments

Comments
 (0)