Skip to content

Commit

Permalink
Move pull and push to commands. Lint project.
Browse files Browse the repository at this point in the history
  • Loading branch information
grant committed May 30, 2018
1 parent 98c40e4 commit 66086ab
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 103 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ sudo npm run build;
npm run test
```

#### Lint

- Use `npm run lint` to find common style errors.
- Download [sort-imports](https://marketplace.visualstudio.com/items?itemName=amatiasq.sort-imports) for VSC to automatically sort imports.

#### Publishing `clasp` to npm (admin)

1. Build `index.js` locally. `.gitignore`/`.npmignore` will hide js/ts files appropriately.
Expand Down
84 changes: 25 additions & 59 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,33 @@
/**
* clasp – The Apps Script CLI
*/

import 'connect';
const open = require('open');
const path = require('path');
const commander = require('commander');
import * as pluralize from 'pluralize';
import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME,
ProjectSettings, DOTFILE, spinner, logError, ERROR, getScriptURL,
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, deployments, openCmd, status} from './src/commands';
import {getProjectFiles, fetchProject, getFileType, hasProject} from './src/files';

// Functions (not yet moved out of this file)
const pull = async () => {
await checkIfOnline();
const { scriptId, rootDir } = await getProjectSettings();
if (scriptId) {
spinner.setSpinnerTitle(LOG.PULLING);
fetchProject(scriptId, rootDir);
}
};
const push = async () => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.PUSHING).start();
getAPICredentials(async () => {
const { scriptId, rootDir } = await getProjectSettings();
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles, files) => {
if(err) {
console.log(err);
spinner.stop(true);
} else if (projectFiles) {
const [nonIgnoredFilePaths] = projectFiles;
script.projects.updateContent({
scriptId,
resource: { files },
}, {}, (error: any) => {
spinner.stop(true);
if (error) {
console.error(LOG.PUSH_FAILURE);
error.errors.map((err: any) => {
console.error(err.message);
});
console.error(LOG.FILES_TO_PUSH);
nonIgnoredFilePaths.map((filePath: string) => {
console.error(`└─ ${filePath}`);
});
process.exit(1);
} else {
nonIgnoredFilePaths.map((filePath: string) => {
console.log(`└─ ${filePath}`);
});
console.log(LOG.PUSH_SUCCESS(nonIgnoredFilePaths.length));
}
});
}
});
});
};
import { login } from './src/auth';
import {
clone,
create,
defaultCmd,
deploy,
deployments,
help,
list,
logout,
logs,
openCmd,
pull,
push,
redeploy,
run,
status,
undeploy,
version,
versions,
} from './src/commands';
import { PROJECT_NAME } from './src/utils';

const commander = require('commander');

// CLI

Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
"extension",
"add-on"
],
"importSort": {
".ts": {
"parser": "typescript",
"style": "module-compact"
},
"on-save": false
},
"author": "Grant Timmerman",
"license": "Apache-2.0",
"dependencies": {
Expand Down
46 changes: 23 additions & 23 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { OAuth2Client } from 'google-auth-library';
import { checkIfOnline, ClaspSettings, DOTFILE, ERROR } from './utils';
import * as http from 'http';
import * as url from 'url';
import { AddressInfo } from 'net';
import open = require('open');
import readline = require('readline');
import { LOG } from './commands';
import * as url from 'url';
import { OAuth2Client } from 'google-auth-library';
import { google } from 'googleapis';
import { Logging } from 'googleapis/build/src/apis/logging/v2';
import { Drive } from 'googleapis/build/src/apis/drive/v3';
import { Logging } from 'googleapis/build/src/apis/logging/v2';
import { LOG } from './commands';
import { ClaspSettings, DOTFILE, ERROR, checkIfOnline } from './utils';
import open = require('open');
import readline = require('readline');

// API settings
// @see https://developers.google.com/oauthplayground/
Expand Down Expand Up @@ -45,6 +45,22 @@ export const drive = google.drive({
auth: oauth2Client,
}) as Drive;

/**
* Requests authorization to manage Apps Script projects.
* @param {boolean} useLocalhost True if a local HTTP server should be run
* to handle the auth response. False if manual entry used.
*/
async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
try {
const token = await (useLocalhost ? authorizeWithLocalhost() : authorizeWithoutLocalhost());
await (writeToOwnKey ? DOTFILE.RC_LOCAL.write(token) : DOTFILE.RC.write(token));
console.log(LOG.AUTH_SUCCESSFUL);
process.exit(0); // gracefully exit after successful login
} catch(err) {
console.error(ERROR.ACCESS_TOKEN + err);
}
}

/**
* Loads the Apps Script API credentials for the CLI.
* Required before every API call.
Expand Down Expand Up @@ -126,22 +142,6 @@ async function authorizeWithoutLocalhost() {
return (await client.getToken(authCode)).tokens;
}

/**
* Requests authorization to manage Apps Script projects.
* @param {boolean} useLocalhost True if a local HTTP server should be run
* to handle the auth response. False if manual entry used.
*/
export async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
try {
const token = await (useLocalhost ? authorizeWithLocalhost() : authorizeWithoutLocalhost());
await (writeToOwnKey ? DOTFILE.RC_LOCAL.write(token) : DOTFILE.RC.write(token));
console.log(LOG.AUTH_SUCCESSFUL);
process.exit(0); // gracefully exit after successful login
} catch(err) {
console.error(ERROR.ACCESS_TOKEN + err);
}
}

/**
* Logs the user in. Saves the client credentials to an rc file.
* @param options the localhost and ownkey options from commander
Expand Down
73 changes: 65 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { DOT, PROJECT_NAME, getScriptURL,
logError, ClaspSettings, DOTFILE, ERROR,
checkIfOnline, spinner, saveProjectId, manifestExists,
getProjectSettings, ProjectSettings, PROJECT_MANIFEST_BASENAME } from './utils';
const open = require('open');
import {hasProject, fetchProject, getProjectFiles} from './files';
import { authorize, getAPICredentials, drive, script, logger} from './auth';
import * as del from 'del';
import * as pluralize from 'pluralize';
import { drive, getAPICredentials, logger, script } from './auth';
import {fetchProject, getProjectFiles, hasProject} from './files';
import {
DOT,
ERROR,
PROJECT_MANIFEST_BASENAME,
PROJECT_NAME,
ProjectSettings,
checkIfOnline,
getProjectSettings,
getScriptURL,
logError,
manifestExists,
saveProjectId,
spinner,
} from './utils';
const open = require('open');
const commander = require('commander');
import * as del from 'del';
const chalk = require('chalk');
const { prompt } = require('inquirer');

Expand Down Expand Up @@ -48,6 +58,53 @@ export const LOG = {
VERSION_NUM: (numVersions: number) => `~ ${numVersions} ${pluralize('Version', numVersions)} ~`,
};

export const pull = async () => {
await checkIfOnline();
const { scriptId, rootDir } = await getProjectSettings();
if (scriptId) {
spinner.setSpinnerTitle(LOG.PULLING);
fetchProject(scriptId, rootDir);
}
};
export const push = async () => {
await checkIfOnline();
spinner.setSpinnerTitle(LOG.PUSHING).start();
getAPICredentials(async () => {
const { scriptId, rootDir } = await getProjectSettings();
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles, files) => {
if(err) {
console.log(err);
spinner.stop(true);
} else if (projectFiles) {
const [nonIgnoredFilePaths] = projectFiles;
script.projects.updateContent({
scriptId,
resource: { files },
}, {}, (error: any) => {
spinner.stop(true);
if (error) {
console.error(LOG.PUSH_FAILURE);
error.errors.map((err: any) => {
console.error(err.message);
});
console.error(LOG.FILES_TO_PUSH);
nonIgnoredFilePaths.map((filePath: string) => {
console.error(`└─ ${filePath}`);
});
process.exit(1);
} else {
nonIgnoredFilePaths.map((filePath: string) => {
console.log(`└─ ${filePath}`);
});
console.log(LOG.PUSH_SUCCESS(nonIgnoredFilePaths.length));
}
});
}
});
});
};

export const help = () => {
commander.outputHelp();
};
Expand Down
6 changes: 3 additions & 3 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from 'fs';
import * as anymatch from 'anymatch';
import * as mkdirp from 'mkdirp';
import * as recursive from 'recursive-readdir';
import {getAPICredentials, script} from './auth';
import {LOG} from './commands';
import {checkIfOnline, DOTFILE, getAPIFileType, spinner, logError, DOT, ERROR} from './utils';
import * as fs from 'fs';
import {DOT, DOTFILE, ERROR, checkIfOnline, getAPIFileType, logError, spinner} from './utils';
const path = require('path');
import * as recursive from 'recursive-readdir';
const readMultipleFiles = require('read-multiple-files');

// An Apps Script API File
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from 'fs';
import * as os from 'os';
import { Spinner } from 'cli-spinner';
const path = require('path');
const findParentDir = require('find-parent-dir');
const splitLines = require('split-lines');
import * as fs from 'fs';
const dotf = require('dotf');
const read = require('read-file');
import { Spinner } from 'cli-spinner';
const isOnline = require('is-online');

// Names / Paths
Expand Down
14 changes: 6 additions & 8 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import * as fs from 'fs-extra';
import * as os from 'os';

const { spawnSync } = require('child_process');
import { getScriptURL, getAPIFileType, saveProjectId } from './../src/utils.js';
import { getFileType } from './../src/files';
import * as path from 'path';
import { expect } from 'chai';
import * as fs from 'fs-extra';
import { describe, it } from 'mocha';
import * as tmp from 'tmp';

import { getFileType } from './../src/files';
import { getAPIFileType, getScriptURL, saveProjectId } from './../src/utils.js';
const { spawnSync } = require('child_process');
const TEST_CODE_JS = 'function test() { Logger.log(\'test\'); }';
const TEST_JSON = '{"timeZone": "America/New_York"}';

Expand Down

0 comments on commit 66086ab

Please sign in to comment.