Skip to content

Commit

Permalink
Merge pull request #33 from OperationSpark/codenvy
Browse files Browse the repository at this point in the history
Codenvy
  • Loading branch information
Andy Nguyen authored Aug 23, 2019
2 parents 1bff9e7 + ed70498 commit 0d62867
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 32 deletions.
5 changes: 3 additions & 2 deletions controller/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports.home = function getUserHome() {
module.exports.hostname = function () {
return os.hostname();
};

// codenvy project filePath /projects
module.exports.codenvyUser = process.env.CHE_PROJECTS_ROOT;
// cloud9 user name
module.exports.cloud9User = !!process.env.C9_USER;

33 changes: 19 additions & 14 deletions controller/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ const janitor = require('./janitor');
const github = require('./github');
const configWebsite = require('../config.json').website;
const configPortfolio = require('../config.json').portfolio;
const { home, cloud9User } = require('./env');
const { home, cloud9User, codenvyUser } = require('./env');

const jQueryCdnScript = ' <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js\"></script>\n';
const portfolioScript = " <script id=\"portfolioScript\">$(document).ready(function() {$.getJSON('projects/projects.json').then(function(data) { data.projects.forEach(function(project){ $('#portfolio').append('<li><a href=\"projects/' + project.name + '/\">' + project.title + ' : ' + project.description + '</a></li>'); }); }); });</script>\n </body>";
require('colors');

let rootDirectory = './';
let githubDir;

if (cloud9User) {
const githubDir = fs.readdirSync(`${home()}/environment`)
githubDir = fs.readdirSync(`${home()}/environment`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${home()}/environment/${githubDir}`;
} else if (codenvyUser) {
rootDirectory = codenvyUser;
githubDir = fs.readdirSync(`${codenvyUser}/`).filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${rootDirectory}/${githubDir}`;
}

module.exports.jQueryCdnScript = jQueryCdnScript;
Expand All @@ -32,8 +37,7 @@ function login() {
}
module.exports.login = login;

function portfolio(filepath) {
filepath = (filepath ? filepath : configPortfolio.filepath);
function portfolio(filepath = configPortfolio.filepath) {
if (!fs.existsSync(filepath)) return console.log(configPortfolio.help.incomplete.red);
const html = fs.readFileSync(filepath, 'utf8');
const $ = cheerio.load(html);
Expand Down Expand Up @@ -61,9 +65,10 @@ module.exports.portfolio = portfolio;
*/
module.exports.website = function (next) {
console.log('Initializing website project, please wait...'.green);
installWebsiteFiles(function(err) {
installWebsiteFiles(function (err) {
if (err) return console.log(err);
if (cloud9User) {
// check if rootDirectory is relative path
if (rootDirectory !== './') {
portfolio(`${rootDirectory}/${configPortfolio.filepath}`);
} else {
portfolio();
Expand All @@ -73,15 +78,15 @@ module.exports.website = function (next) {
};

function installWebsiteFiles(next) {
var numFiles = configWebsite.url.length;
var downloaded = 0;
configWebsite.url.forEach(function(fileUrl){
var filename = url.parse(fileUrl).pathname.split('/').pop();
var message = 'Downloading ' + filename + ', please wait...';
const numFiles = configWebsite.url.length;
let downloaded = 0;
configWebsite.url.forEach(function (fileUrl) {
const filename = url.parse(fileUrl).pathname.split('/').pop();
let message = 'Downloading ' + filename + ', please wait...';
console.log(message.green);
console.log(fileUrl);
var wget = 'wget -nc -P ' + rootDirectory + ' ' + fileUrl;
var child = exec(wget, function(err, stdout, stderr) {
const wget = 'wget -nc -P ' + rootDirectory + ' ' + fileUrl;
const child = exec(wget, function (err, stdout, stderr) {
if (err) return next(err);
message = filename + ' downloaded to ' + rootDirectory;
console.log(message.green);
Expand All @@ -91,4 +96,4 @@ function installWebsiteFiles(next) {
}
});
});
}
}
15 changes: 11 additions & 4 deletions controller/janitor.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
require('colors');
const fs = require('fs');
const _ = require('lodash');
const { home, cloud9User } = require('./env');
const { home, codenvyUser, cloud9User } = require('./env');
const projects = require('./projects');

let root = `${home()}/environment`;
if (cloud9User) {
const githubDir = fs.readdirSync(`${home()}/environment`)
let githubDir;
if (codenvyUser) {
root = `${codenvyUser}`;
githubDir = fs.readdirSync(`${root}/`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
root = `${home()}/environment/${githubDir}`;
root = `${root}/${githubDir}`;
} else if (cloud9User) {
githubDir = fs.readdirSync(`${root}/`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
root = `${root}/${githubDir}`;
}

const projectsDirectory = `${root}/projects`;

module.exports.error = function (message) {
Expand Down
15 changes: 12 additions & 3 deletions controller/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ const inquirer = require('inquirer');
const changeCase = require('change-case');
const exec = require('child_process').exec;

const { home, cloud9User } = require('./env');
const { home, cloud9User, codenvyUser } = require('./env');
const greenlight = require('./greenlight');
const projects = require('./projects');

let rootDirectory = `${home()}/environment`;
let githubDir;
if (cloud9User) {
const githubDir = fs.readdirSync(`${home()}/environment`)
// if on cloud9, use project root at environment to get github repo
githubDir = fs.readdirSync(`${rootDirectory}`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${home()}/environment/${githubDir}`;
rootDirectory = `${rootDirectory}/${githubDir}`;
} else if (codenvyUser) {
// else if on codenvy, look for github repo on root from app env
rootDirectory = codenvyUser;
githubDir = fs.readdirSync(`${rootDirectory}`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${rootDirectory}/${githubDir}`;
}

const projectsDirectory = `${rootDirectory}/projects`;

/**
Expand Down
12 changes: 10 additions & 2 deletions controller/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ const changeCase = require('change-case');
const exec = require('child_process').exec;
const { waterfall, series } = require('async');

const { home, cloud9User } = require('./env');
const { home, cloud9User, codenvyUser } = require('./env');
const github = require('./github');
const { downloadProject } = require('./helpers');

let rootDirectory = `${home()}/environment`;
let githubDir;

if (cloud9User) {
const githubDir = fs.readdirSync(`${home()}/environment`)
githubDir = fs.readdirSync(`${rootDirectory}`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${home()}/environment/${githubDir}`;
} else if (codenvyUser) {
rootDirectory = codenvyUser;
githubDir = fs.readdirSync(`${rootDirectory}/`)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${rootDirectory}/${githubDir}`;
}

const projectEntriesPath = `${rootDirectory}/projects/projects.json`;
const projectsDirectory = `${rootDirectory}/projects`;
const cancelOption = '[cancel]';
Expand Down
11 changes: 9 additions & 2 deletions controller/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const changeCase = require('change-case');
const exec = require('child_process').exec;

const { home, cloud9User } = require('./env');
const { codenvyUser, home, cloud9User } = require('./env');
const janitor = require('./janitor');
const github = require('./github');
const greenlight = require('./greenlight');
Expand All @@ -19,10 +19,17 @@ const {
} = require('./helpers');

let rootDirectory = `${home()}/environment`;
let githubDir;

if (cloud9User) {
const githubDir = fs.readdirSync(`${home()}/environment`)
githubDir = fs.readdirSync(rootDirectory)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${home()}/environment/${githubDir}`;
} else if (codenvyUser) {
rootDirectory = codenvyUser;
githubDir = fs.readdirSync(rootDirectory)
.filter(dir => /[\w]+\.github\.io/.test(dir))[0];
rootDirectory = `${rootDirectory}/${githubDir}`;
}
const projectsDirectory = `${rootDirectory}/projects`;

Expand Down
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const program = require('commander');
const fs = require('fs');
const pjson = require('./package.json');

const env = require('./controller/env');
const init = require('./controller/init');
const github = require('./controller/github');
const install = require('./controller/install');
Expand Down Expand Up @@ -128,13 +129,16 @@ function pairdown() {
}

function initPortfolio() {
if (process.env.C9_USER) {
const githubDir = fs.readdirSync(`${process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']}/environment`).filter(dir => /[\w]+\.github\.io/.test(dir))[0];
init.portfolio(`${process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']}/environment/${githubDir}/portfolio.html`);
return;
if (env.cloud9User) {
const githubDir = fs.readdirSync(`${env.home()}/environment`).filter(dir => /[\w]+\.github\.io/.test(dir))[0];
init.portfolio(`${env.home()}/environment/${githubDir}/portfolio.html`);
return;
} else if (env.codenvyUser) {
const githubDir = fs.readdirSync(`${env.codenvyUser}`).filter(dir => /[\w]+\.github\.io/.test(dir))[0];
init.portfolio(`${env.codenvyUser}/${githubDir}/portfolio.html`);
return;
}
init.portfolio();
return;
}

function fix() {
Expand Down

0 comments on commit 0d62867

Please sign in to comment.