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

Convert clasp create to inquirer functionality #130

Merged
merged 2 commits into from
Apr 23, 2018
Merged
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
60 changes: 38 additions & 22 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import * as url from 'url';
const readline = require('readline');
const logging = require('@google-cloud/logging');
const chalk = require('chalk');
const { prompt } = require('inquirer');

// Names / Paths
const PROJECT_NAME = 'clasp';
Expand All @@ -66,7 +67,7 @@ const DOT = {
LOCAL_DIR: './',
NAME: `${PROJECT_NAME}rc.json`,
PATH: path.join('~', `.${PROJECT_NAME}rc.json`),
ABSOLUTE_PATH: path.join(os.homedir(), `.${PROJECT_NAME}rc.json`)
ABSOLUTE_PATH: path.join(os.homedir(), `.${PROJECT_NAME}rc.json`),
},
};

Expand Down Expand Up @@ -95,7 +96,7 @@ interface FilesCallback {
(
error: Error | boolean,
result: string[][] | null,
files: Array<AppsScriptFile | undefined> | null
files: Array<AppsScriptFile | undefined> | null,
) : void;
}

Expand Down Expand Up @@ -126,7 +127,7 @@ const DOTFILE = {
},
// See `login`: Stores { accessToken, refreshToken }
RC: dotf(DOT.RC.DIR, DOT.RC.NAME),
RC_LOCAL: dotf(DOT.RC.LOCAL_DIR, DOT.RC.NAME)
RC_LOCAL: dotf(DOT.RC.LOCAL_DIR, DOT.RC.NAME),
};

// API settings
Expand Down Expand Up @@ -209,6 +210,14 @@ Did you provide the correct scriptId?`,
UNAUTHENTICATED: 'Error: Unauthenticated request: Please try again.',
};

// Questions (prompts) for clasp create
const createQuestions = [{
type : 'input',
name : 'title',
message : 'give a script title: ',
default: LOG.UNTITLED_SCRIPT_TITLE,
}];

// Utils
const spinner = new Spinner();

Expand Down Expand Up @@ -317,7 +326,7 @@ function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
'https://www.googleapis.com/auth/script.deployments',
'https://www.googleapis.com/auth/script.projects',
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/script.webapp.deploy'
'https://www.googleapis.com/auth/script.webapp.deploy',
],
// code_challenge_method: 'S256',
// code_challenge: codes.codeChallenge,
Expand Down Expand Up @@ -384,7 +393,7 @@ function authorizeWithoutLocalhost(opts: any): Promise<string> {
return new Promise((res, rej) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
output: process.stdout,
});
rl.question(LOG.AUTH_CODE, (code: string) => {
if (code && code.length) {
Expand Down Expand Up @@ -471,10 +480,10 @@ function getProjectFiles(rootDir: string, callback: FilesCallback): void {
//
// TODO: implement renaming files from .gs to .js
// let canRenameToJS = false;
// filePaths.map((name, i) => {
// if (path.extname(name) === '.gs') {
// canRenameToJS = true;
// }
// filePaths.map((name, i) => {
// if (path.extname(name) === '.gs') {
// canRenameToJS = true;
// }
// });
// Check if there are files that will conflict if renamed .gs to .js
filePaths.map((name: string) => {
Expand Down Expand Up @@ -507,15 +516,15 @@ function getProjectFiles(rootDir: string, callback: FilesCallback): void {
if (rootDir) {
formattedName = nameWithoutExt.slice(
rootDir.length + 1,
nameWithoutExt.length
nameWithoutExt.length,
);
}
if (getAPIFileType(name) && !anymatch(ignorePatterns, name)) {
nonIgnoredFilePaths.push(name);
const file: AppsScriptFile = {
name: formattedName, // the file base name
type: getAPIFileType(name), // the file extension
source: contents[i] //the file contents
source: contents[i], //the file contents
};
return file;
} else {
Expand Down Expand Up @@ -581,7 +590,14 @@ commander
commander
.command('create [scriptTitle] [scriptParentId]')
.description('Create a script')
.action(async (title: string = LOG.UNTITLED_SCRIPT_TITLE, parentId: string) => {
.action(async (title: string, parentId: string) => {
if (!title) {
await prompt(createQuestions).then((answers) => {
title = answers.title;
}).catch((err) => {
console.log(err);
});
}
await checkIfOnline();
if (fs.existsSync(DOT.PROJECT.PATH)) {
logError(null, ERROR.FOLDER_EXISTS);
Expand Down Expand Up @@ -707,7 +723,7 @@ commander
const [nonIgnoredFilePaths] = projectFiles;
script.projects.updateContent({
scriptId,
resource: { files }
resource: { files },
}, {}, (error: any, res: Function) => {
spinner.stop(true);
if (error) {
Expand Down Expand Up @@ -797,7 +813,7 @@ commander
if (!scriptId) return;
spinner.setSpinnerTitle(LOG.DEPLOYMENT_LIST(scriptId)).start();
script.projects.deployments.list({
scriptId
scriptId,
}, {}, (error: any, { data }: any) => {
spinner.stop(true);
if (error) {
Expand Down Expand Up @@ -842,7 +858,7 @@ commander
versionNumber,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description,
}
},
}, {}, (err: any, { data }: any) => {
spinner.stop(true);
if (err) {
Expand All @@ -855,14 +871,14 @@ commander

// If the version is specified, update that deployment
const versionRequestBody = {
description
description,
};
if (version) {
createDeployment(version);
} else { // if no version, create a new version and deploy that
script.projects.versions.create({
scriptId,
resource: versionRequestBody
resource: versionRequestBody,
}, {}, (err: any, { data }: any) => {
spinner.stop(true);
if (err) {
Expand Down Expand Up @@ -922,9 +938,9 @@ commander
deploymentConfig: {
versionNumber: version,
manifestFileName: PROJECT_MANIFEST_BASENAME,
description
}
}
description,
},
},
}, {}, (error: any, res: any) => { // TODO remove any
spinner.stop(true);
if (error) {
Expand Down Expand Up @@ -1073,7 +1089,7 @@ commander
ERROR: chalk.red(severity),
INFO: chalk.blue(severity),
DEBUG: chalk.yellow(severity),
NOTICE: chalk.magenta(severity)
NOTICE: chalk.magenta(severity),
})[severity] || severity;
coloredSeverity = String(coloredSeverity).padEnd(20);
console.log(`${coloredSeverity} ${timestamp} ${functionName} ${payloadData}`);
Expand Down Expand Up @@ -1122,7 +1138,7 @@ commander
const params = {
scriptId,
function: functionName,
devMode: true
devMode: true,
};
console.log('about to run');
script.scripts.run(params).then(response => {
Expand Down
Loading