Skip to content

Commit

Permalink
wip: refactoring to more types and asyn/await
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesHoppe committed Aug 2, 2019
1 parent c87ed88 commit df7d476
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 85 deletions.
180 changes: 101 additions & 79 deletions engine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
var path = require('path'),
fs = require('fs'),
fse = require('fs-extra'),
ghpages = require('gh-pages'),
denodeify = require('denodeify');
import * as denodeify from 'denodeify';
import * as path from 'path';
import * as fs from 'fs';
import * as fse from 'fs-extra';

function run(options) {
import { GHPages } from './interfaces';
import { Schema as RealDeployOptions } from './deploy/schema';

const ghpages = require('gh-pages');
var access = denodeify(fs.access);

function run(options: RealDeployOptions) {

options = options || {};

Expand Down Expand Up @@ -49,84 +54,101 @@ function run(options) {
ghpages.clean();
}

var access = publish = denodeify(fs.access);

var publish = denodeify(ghpages.publish);


return Promise.resolve()
.then(function checkIfDistFolderExists() {
return access(dir, fs.F_OK)
})
.catch(function handleMissingDistFolder(error) {
console.error('*** Dist folder does not exist. Check the dir --dir parameter or build the project first!\n');
return Promise.reject(error);
})
.then(function createNotFoundPage() {

if (options.dryRun) {
console.info('*** Dry-run / SKIPPED: copying of index.html to 404.html');
return;
}

// Note:
// There is no guarantee that there will be an index.html file,
// as the developer may specify a custom index file.
const indexHtml = path.join(dir, 'index.html');
const notFoundPage = path.join(dir, '404.html');

return fse.copy(indexHtml, notFoundPage).
catch(function (err) {
console.info('index.html could not be copied to 404.html. Continuing without an error.');
console.info('(Hint: are you sure that you have setup the --dir parameter correctly?)');
console.dir(err);
return;
})
})
.then(function createCnameFile() {

if (!options.cname) {
return;
}

const cnameFile = path.join(dir, 'CNAME');
if (options.dryRun) {
console.info('*** Dry-run / SKIPPED: creating of CNAME file with content: ' + options.cname);
return;
}

return fse.writeFile(cnameFile, options.cname)
.then(function () {
console.log('*** CNAME file created');
})
.catch(function (err) {
console.info('*** CNAME file could not be created. Stopping execution.');
throw err;
})
.then(() => checkIfDistFolderExists(dir))
.catch((error) => handleMissingDistFolder(error))
.then(() => createNotFoundPage(dir, options))
.then(() => createCnameFile(dir, options))
.then(() => publishViaGhPages(ghpages, dir, options))
.then(() => showSuccess())
.catch((error) => showError(error));
};


function checkIfDistFolderExists(dir: string) {
const flag = fs['F_OK'];
return access(dir, flag);
}

function handleMissingDistFolder(error) {
console.error('*** Dist folder does not exist. Check the dir --dir parameter or build the project first!\n');
return Promise.reject(error);
}

function createNotFoundPage(dir: string, options: RealDeployOptions) {

if (options.dryRun) {
console.info('*** Dry-run / SKIPPED: copying of index.html to 404.html');
return;
}

// Note:
// There is no guarantee that there will be an index.html file,
// as the developer may specify a custom index file.
const indexHtml = path.join(dir, 'index.html');
const notFoundPage = path.join(dir, '404.html');

return fse.copy(indexHtml, notFoundPage).
catch(function (err) {
console.info('index.html could not be copied to 404.html. Continuing without an error.');
console.info('(Hint: are you sure that you have setup the --dir parameter correctly?)');
console.dir(err);
return;
})
.then(function publishViaGhPages() {
if (options.dryRun) {
console.info('*** Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:', {
dir: dir,
repo: options.repo || 'undefined: current working directory (which must be a git repo in this case) will be used to commit & push',
message: options.message,
branch: options.branch,
user: options.user || 'undefined: local or global git username & email properties will be taken',
noSilent: options.noSilent || 'undefined: logging is in silent mode by default',
noDotfiles: options.noDotfiles || 'undefined: dotfiles are included by default',
dryRun: options.dryRun,
cname: options.cname || 'undefined: no CNAME file will be created',
});
return;
}

return publish(dir, options)
}

function createCnameFile(dir: string, options: RealDeployOptions) {

if (!options.cname) {
return;
}

const cnameFile = path.join(dir, 'CNAME');
if (options.dryRun) {
console.info('*** Dry-run / SKIPPED: creating of CNAME file with content: ' + options.cname);
return;
}

return fse.writeFile(cnameFile, options.cname)
.then(function () {
console.log('*** CNAME file created');
})
.then(function showSuccess() {
console.log('*** Successfully published!\n');
.catch(function (err) {
console.info('*** CNAME file could not be created. Stopping execution.');
throw err;
})
.catch(function showError(error) {
console.error('*** An error occurred!\n');
console.dir(error);
return Promise.reject(error);
}


async function publishViaGhPages(ghPages: GHPages, dir: string, options: RealDeployOptions) {
if (options.dryRun) {
console.info('*** Dry-run / SKIPPED: publishing to "' + dir + '" with the following options:', {
dir: dir,
repo: options.repo || 'undefined: current working directory (which must be a git repo in this case) will be used to commit & push',
message: options.message,
branch: options.branch,
user: options.user || 'undefined: local or global git username & email properties will be taken',
noSilent: options.noSilent || 'undefined: logging is in silent mode by default',
noDotfiles: options.noDotfiles || 'undefined: dotfiles are included by default',
dryRun: options.dryRun,
cname: options.cname || 'undefined: no CNAME file will be created',
});
};
return;
}

return await ghPages.publish(dir, options)
}

function showSuccess() {
console.log('*** Successfully published!\n');
}

function showError(error) {
console.error('*** An error occurred!\n');
console.dir(error);
return Promise.reject(error);
}
4 changes: 2 additions & 2 deletions index.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ exports.run = function (options) {
const notFoundPage = path.join(dir, '404.html');

return fse.copy(indexHtml, notFoundPage).
catch(function (err) {
catch(function (err) {
console.info('index.html could not be copied to 404.html. Continuing without an error.');
console.info('(Hint: are you sure that you have setup the --dir parameter correctly?)');
console.dir(err);
Expand All @@ -101,7 +101,7 @@ exports.run = function (options) {
.then(function () {
console.log('*** CNAME file created');
})
.catch(function (err) {
.catch(function (err) {
console.info('*** CNAME file could not be created. Stopping execution.');
throw err;
})
Expand Down
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
"@angular-devkit/architect": "^0.800.6",
"@angular-devkit/core": "^8.0.6",
"@angular-devkit/schematics": "^8.0.6",
"@types/denodeify": "^1.2.31",
"@types/fs-extra": "^8.0.0",
"@types/jest": "^24.0.15",
"@types/node": "^12.0.10",
"@types/node": "^12.6.9",
"jest": "^24.8.0",
"json-schema-to-typescript": "^6.1.3",
"ts-jest": "^24.0.2",
Expand All @@ -62,8 +64,10 @@
"@angular-devkit/schematics": ">=8.0.0"
},
"dependencies": {
"gh-pages": "^2.1.0",
"commander": "^2.20.0"
"commander": "^2.20.0",
"denodeify": "^1.2.1",
"fs-extra": "^8.1.0",
"gh-pages": "^2.1.0"
},
"jest": {
"transform": {
Expand All @@ -79,4 +83,4 @@
"node"
]
}
}
}

0 comments on commit df7d476

Please sign in to comment.