Skip to content

Commit

Permalink
Move status, open, deployments to commands.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
grant committed May 30, 2018
1 parent 8252305 commit 98c40e4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
65 changes: 2 additions & 63 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME,
getProjectSettings, getAPIFileType, checkIfOnline,
saveProjectId, manifestExists } from './src/utils';
import { drive, script, logger, getAPICredentials, login } from './src/auth';
import { LOG, help, defaultCmd, logs, run,
logout, create, clone, deploy, undeploy, redeploy, version, versions, list} from './src/commands';
import { LOG, help, defaultCmd, logs, run, logout, create, clone, deploy,
undeploy, redeploy, version, versions, list, deployments, openCmd, status} from './src/commands';
import {getProjectFiles, fetchProject, getFileType, hasProject} from './src/files';

// Functions (not yet moved out of this file)
Expand Down Expand Up @@ -81,67 +81,6 @@ const push = async () => {
});
};

const status = async (cmd: { json: boolean }) => {
await checkIfOnline();
getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => {
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles) => {
if(err) return console.log(err);
else if (projectFiles) {
const [filesToPush, untrackedFiles] = projectFiles;
if (cmd.json) {
console.log(JSON.stringify({ filesToPush, untrackedFiles }));
} else {
console.log(LOG.STATUS_PUSH);
filesToPush.forEach((file) => console.log(`└─ ${file}`));
console.log(LOG.STATUS_IGNORE);
untrackedFiles.forEach((file) => console.log(`└─ ${file}`));
}
}
});
});
};
const openCmd = async (scriptId: any) => {
if (!scriptId) {
const settings = await getProjectSettings();
scriptId = settings.scriptId;
}
if (scriptId.length < 30) {
logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
} else {
console.log(LOG.OPEN_PROJECT(scriptId));
open(getScriptURL(scriptId));
}
};
const deployments = async () => {
await checkIfOnline();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_LIST(scriptId)).start();
script.projects.deployments.list({
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
const deployments = data.deployments;
const numDeployments = deployments.length;
const deploymentWord = pluralize('Deployment', numDeployments);
console.log(`${numDeployments} ${deploymentWord}.`);
deployments.map(({ deploymentId, deploymentConfig }: any) => {
const versionString = !!deploymentConfig.versionNumber ?
`@${deploymentConfig.versionNumber}` : '@HEAD';
const description = deploymentConfig.description ?
'- ' + deploymentConfig.description : '';
console.log(`- ${deploymentId} ${versionString} ${description}`);
});
}
});
});
};

// CLI

/**
Expand Down
62 changes: 61 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DOT, PROJECT_NAME, getScriptURL,
checkIfOnline, spinner, saveProjectId, manifestExists,
getProjectSettings, ProjectSettings, PROJECT_MANIFEST_BASENAME } from './utils';
const open = require('open');
import {hasProject, fetchProject} from './files';
import {hasProject, fetchProject, getProjectFiles} from './files';
import { authorize, getAPICredentials, drive, script, logger} from './auth';
import * as pluralize from 'pluralize';
const commander = require('commander');
Expand Down Expand Up @@ -375,4 +375,64 @@ export const redeploy = async (deploymentId: string, version: string, descriptio
});
});
});
};
export const status = async (cmd: { json: boolean }) => {
await checkIfOnline();
getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => {
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles) => {
if(err) return console.log(err);
else if (projectFiles) {
const [filesToPush, untrackedFiles] = projectFiles;
if (cmd.json) {
console.log(JSON.stringify({ filesToPush, untrackedFiles }));
} else {
console.log(LOG.STATUS_PUSH);
filesToPush.forEach((file) => console.log(`└─ ${file}`));
console.log(LOG.STATUS_IGNORE);
untrackedFiles.forEach((file) => console.log(`└─ ${file}`));
}
}
});
});
};
export const openCmd = async (scriptId: any) => {
if (!scriptId) {
const settings = await getProjectSettings();
scriptId = settings.scriptId;
}
if (scriptId.length < 30) {
logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
} else {
console.log(LOG.OPEN_PROJECT(scriptId));
open(getScriptURL(scriptId));
}
};
export const deployments = async () => {
await checkIfOnline();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_LIST(scriptId)).start();
script.projects.deployments.list({
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
const deployments = data.deployments;
const numDeployments = deployments.length;
const deploymentWord = pluralize('Deployment', numDeployments);
console.log(`${numDeployments} ${deploymentWord}.`);
deployments.map(({ deploymentId, deploymentConfig }: any) => {
const versionString = !!deploymentConfig.versionNumber ?
`@${deploymentConfig.versionNumber}` : '@HEAD';
const description = deploymentConfig.description ?
'- ' + deploymentConfig.description : '';
console.log(`- ${deploymentId} ${versionString} ${description}`);
});
}
});
});
};

0 comments on commit 98c40e4

Please sign in to comment.