Skip to content

Commit

Permalink
Converts clasp clone to inquirer (#132)
Browse files Browse the repository at this point in the history
* Converts clasp clone to inquirer

If user does clasp clone without a scriptId, it will
get the 10 most recent scripts and prompt the user
to choose one.

* Make some quick fixes to PR 132
  • Loading branch information
campionfellin authored and grant committed Apr 25, 2018
1 parent b17eace commit 618b4db
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
59 changes: 45 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const LOG = {
DEPLOYMENT_START: (scriptId: string) => `Deploying project ${scriptId}...`,
FILES_TO_PUSH: 'Files to push were:',
FINDING_SCRIPTS: 'Finding your scripts...',
FINDING_SCRIPTS_DNE: 'No script files found.',
OPEN_PROJECT: (scriptId: string) => `Opening script: ${scriptId}`,
PULLING: 'Pulling files...',
STATUS_PUSH: 'The following files will be pushed by clasp push:',
Expand Down Expand Up @@ -185,14 +186,6 @@ 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 @@ -567,7 +560,12 @@ commander
.description('Create a script')
.action(async (title: string, parentId: string) => {
if (!title) {
await prompt(createQuestions).then((answers) => {
await prompt([{
type : 'input',
name : 'title',
message : 'give a script title: ',
default: LOG.UNTITLED_SCRIPT_TITLE,
}]).then((answers) => {
title = answers.title;
}).catch((err) => {
console.log(err);
Expand Down Expand Up @@ -649,13 +647,46 @@ function fetchProject(scriptId: string, rootDir = '', versionNumber?: number) {
* Fetches a project and saves the script id locally.
*/
commander
.command('clone <scriptId> [versionNumber]')
.command('clone [scriptId] [versionNumber]')
.description('Clone a project')
.action(async (scriptId: string, versionNumber?: number) => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.CLONING);
saveProjectId(scriptId);
fetchProject(scriptId, '', versionNumber);
if (!scriptId) {
getAPICredentials(async () => {
const drive = google.drive({version: 'v3', auth: oauth2Client});
const { data } = await drive.files.list({
pageSize: 10,
fields: 'files(id, name)',
q: "mimeType='application/vnd.google-apps.script'",
});
const files = data.files;
const fileIds = [];
if (files.length) {
files.map((file: any) => {
fileIds.push(file.id);
});
await prompt([{
type : 'list',
name : 'scriptId',
message : 'Clone which script? ',
choices : fileIds,
}]).then((answers) => {
checkIfOnline();
spinner.setSpinnerTitle(LOG.CLONING);
saveProjectId(answers.scriptId);
fetchProject(answers.scriptId, '', versionNumber);
}).catch((err) => {
console.log(err);
});
} else {
console.log(LOG.FINDING_SCRIPTS_DNE);
}
});
} else {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.CLONING);
saveProjectId(scriptId);
fetchProject(scriptId, '', versionNumber);
}
});

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe.skip('Test clasp create <title> function', () => {
});
});

describe.skip('Test clasp clone function', () => {
describe.skip('Test clasp clone <scriptId> function', () => {
it('should clone an existing project correctly', () => {
const settings = JSON.parse(fs.readFileSync('.clasp.json', 'utf8'));
const result = spawnSync(
Expand Down Expand Up @@ -111,6 +111,17 @@ describe.skip('Test clasp open function', () => {
});
});

describe.skip('Test clasp clone function', () => {
it('should prompt for which script to clone correctly', () => {
spawnSync('rm', ['.clasp.json']);
const result = spawnSync(
'clasp', ['clone'], { encoding: 'utf8' },
);
expect(result.stdout).to.contain('Clone which script?');
expect(result.status).to.equal(0);
});
});

// Fails when you logged in using --ownkey flag
describe.skip('Test clasp logout function', () => {
it('should logout correctly', () => {
Expand Down Expand Up @@ -139,6 +150,7 @@ describe.skip('Test clasp logout function', () => {
* [x] clasp create <untitled>
* [x] clasp list
* [x] clasp clone <scriptId>
* [x] clasp clone
* [x] clasp pull
* [x] clasp push
* [ ] echo '// test' >> index.js && clasp push
Expand Down

0 comments on commit 618b4db

Please sign in to comment.