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

Feature Download DCU, Add SiteSettings, Add Stack #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea
node_modules/
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ program
.description('An application to help you with your daily OCC development.')
.option('-s, --start', 'start the environment setup')
.option('-d, --dev', 'start watcher + Browsersync')
.option('-c, --create <type>', 'create widget or element [widget|element]')
.option('-c, --create <type> <path>', 'create widget or element [widget|element|siteSettings|stack]')
.option('-r, --refresh <path>', 'refresh path')
.option('-p, --putAll <path>', 'upload the entire path')
.option('-e, --env <operation>', 'start the environment manager [change|config|current]')
Expand All @@ -30,19 +30,19 @@ if (program.start) {
if (program.dev) {
dev.start();
}

if(program.grab) {
dcu.grab();
}

if (program.refresh) {
dcu.refresh(program.refresh);
}

if (program.putAll) {
dcu.putAll(program.putAll);
}

if (program.transfer) {
dcu.transfer(program.transfer);
}
Expand All @@ -60,18 +60,23 @@ if (program.start) {
break;
}
}

if (program.create) {
switch (program.create) {
case 'widget':
ccw.createWidget();
break;
case 'element':
ccw.createElement();
ccw.createElement(program.args);
break;
case 'siteSettings':
ccw.createSiteSettings();
break;
case 'stack':
ccw.createStack();
}
}

if (program.env) {
switch (program.env) {
case 'config':
Expand Down
18 changes: 15 additions & 3 deletions src/ccw.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
require('dotenv').config();
const fs = require('fs');
const childProcess = require('child_process');
const { CONSTANTS } = require('./constants');

const CCW_BASE_COMMAND = `node ${CONSTANTS.PATHS.CCW} -b ${CONSTANTS.PATHS.SRC} -n ${process.env.OCC_ADMIN_URL} -k ${process.env.OCC_APP_KEY}`;
const CCW_BASE_COMMAND = `node ${CONSTANTS.FILES.CCW} -b ${CONSTANTS.PATHS.SRC} -n ${process.env.OCC_ADMIN_URL} -k ${process.env.OCC_APP_KEY}`;

const Methods = {
createWidget: () => {
childProcess.execSync(`${CCW_BASE_COMMAND} -w`, { stdio: 'inherit' });
},
createElement: () => {
childProcess.execSync(`${CCW_BASE_COMMAND} -e`, { stdio: 'inherit' });
createElement: (args) => {
const path = args[0] ? args[0] : null;
if(path && !fs.existsSync(path)) {
console.log(CONSTANTS.COLORS.ERROR, 'No such directory');
return;
}
childProcess.execSync(`${CCW_BASE_COMMAND} -e ${path}`, { stdio: 'inherit' });
},
createSiteSettings: () => {
childProcess.execSync(`${CCW_BASE_COMMAND} -t`, { stdio: 'inherit' });
},
createStack: () => {
childProcess.execSync(`${CCW_BASE_COMMAND} -s`, { stdio: 'inherit' });
},
};

Expand Down
19 changes: 15 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
exports.CONSTANTS = {
ENVS: ['TEST', 'STAGE', 'PROD', 'NOENV'],
COLORS: {
TITLE: '\x1b[33m\x1b[1m%s\x1b[0m',
SUCCESS: '\x1b[32m\x1b[1m%s\x1b[0m',
ERROR: '\x1b[31m\x1b[1m%s\x1b[0m',
},
FILES: {
DCU_ZIP: './DesignCodeUtility.zip',
DCU: './DesignCodeUtility/dcuIndex.js',
CCW: './DesignCodeUtility/ccwIndex.js',
PLSU: './DesignCodeUtility/plsuIndex.js',
},
PATHS: {
SRC: './src',
SSE: './sse',
DCU: './DesignCodeUtility/dcuIndex.js',
CCW: './DesignCodeUtility/ccwIndex.js',
DCU: './DesignCodeUtility',
},
ENDPOINT: {
LOGIN: '/login',
SSE_LIST: '/serverExtensions',
SSE_UPLOAD: '/serverExtensions',
}
}
DCU: '/occs-admin/js/DesignCodeUtility.zip',
},
}
96 changes: 89 additions & 7 deletions src/dcu.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@
require('dotenv').config();
const axios = require('axios');
const fs = require('fs');
const extract = require('extract-zip');
const shell = require('shelljs');
const { CONSTANTS } = require('./constants');
const { occEnv } = require('./occEnv');

const DCU_BASE_COMMAND = `node ${CONSTANTS.PATHS.DCU} -b ${CONSTANTS.PATHS.SRC} -n ${process.env.OCC_ADMIN_URL} -k ${process.env.OCC_APP_KEY}`;
const DCU_BASE_COMMAND = `node ${CONSTANTS.FILES.DCU} -b ${CONSTANTS.PATHS.SRC} -n ${process.env.OCC_ADMIN_URL} -k ${process.env.OCC_APP_KEY}`;

const Methods = {

get: async (env) => {
env = env || process.env.ACTIVE_ENV;

const response = await axios.get(`${process.env[`OCC_${env}_ADMIN_URL`]}${CONSTANTS.ENDPOINT.DCU}`, {
responseType: 'stream',
}).catch(err => {
if (err.response.status === 404) {
console.log(CONSTANTS.COLORS.ERROR, `Design Code Utility not found in ${env}.`);
} else {
console.log(CONSTANTS.COLORS.ERROR, `Design Code Utility error in ${env}.`);
}
});

return new Promise((resolve, reject) => {
if (response && response.data) {
const writer = fs.createWriteStream(CONSTANTS.FILES.DCU_ZIP);
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
console.log(CONSTANTS.COLORS.ERROR, 'Download not completed. Please try again.');
writer.close();
reject(err);
});
writer.on('close', async () => {
if (!error) {
resolve(true);
}
});
}
});
},

download: async () => {
if (fs.existsSync(CONSTANTS.FILES.DCU_ZIP)) {
console.log(`Delete: DCU`);
await Methods.delete();
}

console.log(`Downloading: DCU`);
await Methods.get();

console.log(`Extracting: DCU`);
await Methods.unzip();

console.log(`Install: DCU`);
await Methods.install();
},

unzip: async () => {
if (fs.existsSync(CONSTANTS.FILES.DCU_ZIP)) {
await extract(CONSTANTS.FILES.DCU_ZIP, { dir: `${process.cwd()}/${CONSTANTS.PATHS.DCU}` });
} else {
console.log(CONSTANTS.COLORS.ERROR, 'DCU required to extract.')
}
},

install: async () => {
return new Promise((resolve) => {
shell.cd(CONSTANTS.PATHS.DCU, {
async: false,
});
shell.exec(`npm i`, {
async: false,
}, () => {
fs.chmodSync(process.cwd(), 0o755);
console.log(CONSTANTS.COLORS.SUCCESS, 'Installed: DCU')
shell.cd('..', { async: false });
resolve(true);
});
});
},

delete: async () => {
fs.unlinkSync(CONSTANTS.FILES.DCU_ZIP);
fs.rmdirSync(CONSTANTS.PATHS.DCU, { recursive: true });
},

grab: (adminUrl, appKey) => {
var finalShellScript = `${DCU_BASE_COMMAND} -g -c`;
if (adminUrl && appKey) {
finalShellScript = `node ${CONSTANTS.PATHS.DCU} -b ${CONSTANTS.PATHS.SRC} -n ${adminUrl} -k ${appKey} -c -g`
finalShellScript = `node ${CONSTANTS.FILES.DCU} -b ${CONSTANTS.PATHS.SRC} -n ${adminUrl} -k ${appKey} -c -g`
}
shell.exec(finalShellScript, {
async: false
Expand All @@ -27,7 +109,7 @@ const Methods = {
async: false,
});
},

putAll: path => {
shell.exec(`${DCU_BASE_COMMAND} -m "${path.replace(/\/$/g, '')}"`, {
async: false,
Expand All @@ -36,17 +118,17 @@ const Methods = {

transfer: async path => {
const { selectedEnv } = await occEnv.selector("Select an environment to transfer:");

if (occEnv.validate(selectedEnv)) {
const { url, appKey } = occEnv.get(selectedEnv);
finalShellScript = `node ${CONSTANTS.PATHS.DCU} -b ${CONSTANTS.PATHS.SRC} -n ${url} -k ${appKey} -x "${path}" -o`;
finalShellScript = `node ${CONSTANTS.FILES.DCU} -b ${CONSTANTS.PATHS.SRC} -n ${url} -k ${appKey} -x "${path}" -o`;
shell.exec(finalShellScript, {
async: false,
});
} else {
console.log(`${selectedEnv} is not configured.`);
console.log(CONSTANTS.COLORS.ERROR, `${selectedEnv} is not configured.`);
}
}
};

exports.dcu = Methods;
exports.dcu = Methods;
28 changes: 21 additions & 7 deletions src/occEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const Methods = {
const keys = Object.keys(obj);
let file = '';
keys.forEach(item => {
file += `
${item}=${obj[item]}
`;
file += `${item}=${obj[item]}\n`;
});
return file;
},
Expand All @@ -20,11 +18,25 @@ const Methods = {
},

hasSrc: () => {
return fs.existsSync('./src');
return fs.existsSync(CONSTANTS.PATHS.SRC);
},

hasDCU: () => {
return fs.existsSync(CONSTANTS.PATHS.DCU);
},

createSrc: () => {
return fs.mkdirSync('./src');
return fs.mkdirSync(CONSTANTS.PATHS.SRC);
},

confirm: message => {
return inquirer
.prompt([{
type: 'list',
name: 'selectedEnv',
message: message || 'Refresh SRC path:',
choices: ['YES', 'NO'],
}]);
},

selector: message => {
Expand Down Expand Up @@ -77,7 +89,7 @@ const Methods = {
var { selectedEnv } = await Methods.selector();
environment = selectedEnv;
}

if (Methods.validate(environment)) {
Methods.writeEnvFile({
ACTIVE_ENV: environment,
Expand Down Expand Up @@ -147,8 +159,10 @@ const Methods = {
});
}

process.env = {...process.env, ...envFile};

fs.writeFileSync('.env', Methods.parseToEnvFile(envFile));
}
}

exports.occEnv = Methods;
exports.occEnv = Methods;
2 changes: 1 addition & 1 deletion src/services/cc-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ exports.ccAdminApi = {
STAGE,
PROD,
NOENV,
};
};
15 changes: 10 additions & 5 deletions src/setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
const { occEnv } = require('./occEnv');
const { dcu } = require('./dcu');
const { CONSTANTS } = require('./constants');

const Methods = {
start: async () => {
Expand All @@ -18,18 +18,23 @@ const Methods = {

occEnv.writeEnvFile(envFile);

if (!occEnv.hasDCU()) {
console.log(CONSTANTS.COLORS.TITLE, 'Download DCU...');
await dcu.download();
}

if (!occEnv.hasSrc()) {
console.log('Creating src folder...');
console.log(CONSTANTS.COLORS.TITLE, 'Creating src folder...');
occEnv.createSrc();
console.log('Grabbing your files, please wait.');
console.log(CONSTANTS.COLORS.TITLE, 'Grabbing your files, please wait.');
dcu.grab(adminUrl, appKey);
} else {
console.log('Your project is ready!');
console.log(CONSTANTS.COLORS.SUCCESS, 'Your project is ready!');
}
} else {
console.log('.env found, delete it and try again.');
}
}
}

exports.setup = Methods;
exports.setup = Methods;
Loading