Skip to content

Commit

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

// Functions (not yet moved out of this file)
Expand Down Expand Up @@ -141,162 +141,6 @@ const deployments = async () => {
});
});
};
const deploy = async (version: string, description: string) => {
await checkIfOnline();
description = description || '';
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_START(scriptId)).start();
function createDeployment(versionNumber: string) {
spinner.setSpinnerTitle(LOG.DEPLOYMENT_CREATE);
script.projects.deployments.create({
scriptId,
resource: {
versionNumber,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
},
}, {}, (err: any, response: any) => {
spinner.stop(true);
if (err) {
console.error(ERROR.DEPLOYMENT_COUNT);
} else if (response) {
console.log(`- ${response.data.deploymentId} @${versionNumber}.`);
}
});
}

// If the version is specified, update that deployment
const versionRequestBody = {
description,
};
if (version) {
createDeployment(version);
} else { // if no version, create a new version and deploy that
script.projects.versions.create({
scriptId,
resource: versionRequestBody,
}, {}, (err: any, { data }: any) => {
spinner.stop(true);
if (err) {
logError(null, ERROR.ONE_DEPLOYMENT_CREATE);
} else {
console.log(LOG.VERSION_CREATED(data.versionNumber));
createDeployment(data.versionNumber);
}
});
}
});
};
const undeploy = async (deploymentId: string) => {
await checkIfOnline();
getAPICredentials(() => {
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.UNDEPLOYMENT_START(deploymentId)).start();
script.projects.deployments.delete({
scriptId,
deploymentId,
}, {}, (err: any, res: any) => {
spinner.stop(true);
if (err) {
logError(null, ERROR.READ_ONLY_DELETE);
} else {
console.log(LOG.UNDEPLOYMENT_FINISH(deploymentId));
}
});
});
});
};
const versions = async () => {
await checkIfOnline();
spinner.setSpinnerTitle('Grabbing versions...').start();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
script.projects.versions.list({
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
if (data && data.versions && data.versions.length) {
const numVersions = data.versions.length;
console.log(LOG.VERSION_NUM(numVersions));
data.versions.map((version: string) => {
console.log(LOG.VERSION_DESCRIPTION(version));
});
} else {
console.error(LOG.DEPLOYMENT_DNE);
}
}
});
});
};
const version = async (description: string) => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.VERSION_CREATE).start();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
script.projects.versions.create({
scriptId,
description,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
console.log(LOG.VERSION_CREATED(data.versionNumber));
}
});
});
};
const list = async () => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.FINDING_SCRIPTS).start();
getAPICredentials(async () => {
const res = await drive.files.list({
pageSize: 50,
fields: 'nextPageToken, files(id, name)',
q: 'mimeType="application/vnd.google-apps.script"',
});
spinner.stop(true);
const files = res.data.files;
if (files.length) {
files.map((file: any) => {
console.log(`${file.name.padEnd(20)}${getScriptURL(file.id)}`);
});
} else {
console.log('No script files found.');
}
});
};
const redeploy = async (deploymentId: string, version: string, description: string) => {
await checkIfOnline();
getAPICredentials(() => {
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
script.projects.deployments.update({
scriptId,
deploymentId,
resource: {
deploymentConfig: {
versionNumber: version,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
},
},
}, {}, (error: any, res: any) => {
spinner.stop(true);
if (error) {
logError(null, error); // TODO prettier error
} else {
console.log(LOG.REDEPLOY_END);
}
});
});
});
};

// CLI

Expand Down
159 changes: 158 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DOT, PROJECT_NAME, getScriptURL,
logError, ClaspSettings, DOTFILE, ERROR,
checkIfOnline, spinner, saveProjectId, manifestExists,
getProjectSettings, ProjectSettings } from './utils';
getProjectSettings, ProjectSettings, PROJECT_MANIFEST_BASENAME } from './utils';
const open = require('open');
import {hasProject, fetchProject} from './files';
import { authorize, getAPICredentials, drive, script, logger} from './auth';
Expand Down Expand Up @@ -218,4 +218,161 @@ export const run = (functionName:string) => {
});
});
});
};

export const deploy = async (version: string, description: string) => {
await checkIfOnline();
description = description || '';
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_START(scriptId)).start();
function createDeployment(versionNumber: string) {
spinner.setSpinnerTitle(LOG.DEPLOYMENT_CREATE);
script.projects.deployments.create({
scriptId,
resource: {
versionNumber,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
},
}, {}, (err: any, response: any) => {
spinner.stop(true);
if (err) {
console.error(ERROR.DEPLOYMENT_COUNT);
} else if (response) {
console.log(`- ${response.data.deploymentId} @${versionNumber}.`);
}
});
}

// If the version is specified, update that deployment
const versionRequestBody = {
description,
};
if (version) {
createDeployment(version);
} else { // if no version, create a new version and deploy that
script.projects.versions.create({
scriptId,
resource: versionRequestBody,
}, {}, (err: any, { data }: any) => {
spinner.stop(true);
if (err) {
logError(null, ERROR.ONE_DEPLOYMENT_CREATE);
} else {
console.log(LOG.VERSION_CREATED(data.versionNumber));
createDeployment(data.versionNumber);
}
});
}
});
};
export const undeploy = async (deploymentId: string) => {
await checkIfOnline();
getAPICredentials(() => {
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.UNDEPLOYMENT_START(deploymentId)).start();
script.projects.deployments.delete({
scriptId,
deploymentId,
}, {}, (err: any, res: any) => {
spinner.stop(true);
if (err) {
logError(null, ERROR.READ_ONLY_DELETE);
} else {
console.log(LOG.UNDEPLOYMENT_FINISH(deploymentId));
}
});
});
});
};
export const versions = async () => {
await checkIfOnline();
spinner.setSpinnerTitle('Grabbing versions...').start();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
script.projects.versions.list({
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
if (data && data.versions && data.versions.length) {
const numVersions = data.versions.length;
console.log(LOG.VERSION_NUM(numVersions));
data.versions.map((version: string) => {
console.log(LOG.VERSION_DESCRIPTION(version));
});
} else {
console.error(LOG.DEPLOYMENT_DNE);
}
}
});
});
};
export const version = async (description: string) => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.VERSION_CREATE).start();
getAPICredentials(async () => {
const { scriptId } = await getProjectSettings();
script.projects.versions.create({
scriptId,
description,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
logError(error);
} else {
console.log(LOG.VERSION_CREATED(data.versionNumber));
}
});
});
};
export const list = async () => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.FINDING_SCRIPTS).start();
getAPICredentials(async () => {
const res = await drive.files.list({
pageSize: 50,
fields: 'nextPageToken, files(id, name)',
q: 'mimeType="application/vnd.google-apps.script"',
});
spinner.stop(true);
const files = res.data.files;
if (files.length) {
files.map((file: any) => {
console.log(`${file.name.padEnd(20)}${getScriptURL(file.id)}`);
});
} else {
console.log('No script files found.');
}
});
};
export const redeploy = async (deploymentId: string, version: string, description: string) => {
await checkIfOnline();
getAPICredentials(() => {
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
script.projects.deployments.update({
scriptId,
deploymentId,
resource: {
deploymentConfig: {
versionNumber: version,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
},
},
}, {}, (error: any, res: any) => {
spinner.stop(true);
if (error) {
logError(null, error); // TODO prettier error
} else {
console.log(LOG.REDEPLOY_END);
}
});
});
});
};

0 comments on commit 8252305

Please sign in to comment.