Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup util #18

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions bin/deploy-to-github-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ async function main() {
.option('-b, --branch [branch]', 'Branch name')
.option('-u, --build-url [build-url]', 'Link displayed when deployment fails')
.option('-m, --defaultBranch [defaultBranch]', 'Specify the default branch for your repo')
.option(
'-e, --expireBranchFolders [expireBranchFolders]',
'Removes folders older than the specified days',
)
.option('--dotfiles', 'Include dotfiles')
.option('--verbose', 'Log verbose information from gh-pages')
.parse(process.argv);
Expand All @@ -26,6 +30,7 @@ async function main() {
branch: program.branch,
buildUrl: program.buildUrl,
defaultBranch: program.defaultBranch,
expireBranchFolders: program.expireBranchFolders,
dotfiles: !!program.dotfiles,
verbose: !!program.verbose,
});
Expand Down
5 changes: 3 additions & 2 deletions lib/deployment/deployment.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { prepareDeployDirectory } = require('./preparation');
const { prepareDeployDirectory, getExpiredBranchFolders } = require('./preparation');
const { deployDirectoryToGithubPages } = require('./pages');

const DEPLOY_DIRECTORY = 'deploy-directory';

function deployToGithubPages(options) {
prepareDeployDirectory(DEPLOY_DIRECTORY, options);
const expiredBranchFolders = getExpiredBranchFolders(options);

return deployDirectoryToGithubPages(DEPLOY_DIRECTORY, options);
return deployDirectoryToGithubPages(DEPLOY_DIRECTORY, expiredBranchFolders, options);
}

module.exports = { deployToGithubPages };
3 changes: 3 additions & 0 deletions lib/deployment/pages/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ function deploy(directory, { token, owner, repo, verbose, dotfiles }) {
const params = {
add: true,
repo: `https://${token}@github.com/${owner}/${repo}.git`,
async beforeAdd(git) {
return git.rm('/branch/docs-fix /branch/add-target-to-alert');
},
silent: !verbose,
dotfiles,
};
Expand Down
31 changes: 30 additions & 1 deletion lib/deployment/preparation/preparation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const shell = require('shelljs');
const path = require('path');
const { execSync } = require('child_process');

const BRANCH_DIRECTORY_NAME = 'branch';

shell.set('-e');

module.exports = { prepareDeployDirectory };
module.exports = { prepareDeployDirectory, getExpiredBranchFolders };

function prepareDeployDirectory(
deployDirectory,
Expand Down Expand Up @@ -39,3 +40,31 @@ function copyContentWithReplacement(source, target) {
shell.rm('-rf', target);
shell.cp('-r', source, target);
}

function getExpiredBranchFolders({ expireBranchFolders }) {
if (!expireBranchFolders) {
return null;
}

const root = `/Users/andrea.piras/Projects/Neptune/neptune-web/${BRANCH_DIRECTORY_NAME}/`;
let glob = execSync(`git ls-tree -d gh-pages --name-only -- ${root}`).toString();

glob = glob.split('\n').reduce((accumulator, branchPath) => {
if (!branchPath) {
return accumulator;
}
const gitModified = execSync(`git log gh-pages -1 --format="%ad" -- ${branchPath}`, {
cwd: process.cwd(),
}).toString();

const differenceInTime = new Date().getTime() - new Date(gitModified).getTime();
const differenceInDays = Math.floor(differenceInTime / (1000 * 3600 * 24));

if (differenceInDays < expireBranchFolders) {
accumulator.push(branchPath.split('/').pop());
}
return accumulator;
}, []);

return glob;
}
1 change: 1 addition & 0 deletions lib/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function extendWithDefaultOptions(options) {
defaultBranch: 'master',
dotfiles: false,
verbose: false,
expireBranchFolders: null,
...options,
};
}
Expand Down