Skip to content

Commit

Permalink
Remove tsc errors and improve code quality (no implicity types, no do…
Browse files Browse the repository at this point in the history
…uble quotes).

Previous errors:
```
sudo npm run build;

> @google/[email protected] build /Users/timmerman/Documents/github/google/clasp
> tsc --project tsconfig.json; npm i -g;

index.ts(200,17): error TS7006: Parameter 'answers' implicitly has an 'any' type.
index.ts(202,17): error TS7006: Parameter 'err' implicitly has an 'any' type.
index.ts(297,17): error TS7034: Variable 'fileIds' implicitly has type 'any[]' in some locations where its type cannot be determined.
index.ts(300,15): error TS7005: Variable 'fileIds' implicitly has an 'any[]' type.
index.ts(306,25): error TS7005: Variable 'fileIds' implicitly has an 'any[]' type.
index.ts(307,23): error TS7006: Parameter 'answers' implicitly has an 'any' type.
index.ts(312,23): error TS7006: Parameter 'err' implicitly has an 'any' type.
index.ts(714,25): error TS7017: Element implicitly has an 'any' type because type '{ textPayload: any; jsonPayload: any; protoPayload: any; }' has no index signature.
index.ts(720,31): error TS7017: Element implicitly has an 'any' type because type '{ ERROR: any; INFO: any; DEBUG: any; NOTICE: any; }' has no index signature.
index.ts(745,55): error TS7006: Parameter 'err' implicitly has an 'any' type.
index.ts(762,12): error TS7006: Parameter 'functionName' implicitly has an 'any' type.
src/auth.ts(63,55): error TS2339: Property 'port' does not exist on type 'string | AddressInfo'.
  Property 'port' does not exist on type 'string'.
/usr/local/bin/clasp -> /usr/local/lib/node_modules/@google/clasp/index.js
+ @google/[email protected]
added 1 package from 1 contributor in 2.725s
```

No errors now:
```
sudo npm run build
Password:
Sorry, try again.
Password:

> @google/[email protected] build /Users/timmerman/Documents/github/google/clasp
> tsc --project tsconfig.json; npm i -g;

/usr/local/bin/clasp -> /usr/local/lib/node_modules/@google/clasp/index.js
+ @google/[email protected]
updated 1 package in 4.189s
```
  • Loading branch information
grant committed May 25, 2018
1 parent b40f4d2 commit 19bc290
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
41 changes: 21 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* clasp – The Apps Script CLI
*/
import * as anymatch from "anymatch";
import * as anymatch from 'anymatch';
import 'connect';
import * as del from 'del';
import * as fs from 'fs';
Expand All @@ -41,6 +41,7 @@ import { DOT, PROJECT_NAME, PROJECT_MANIFEST_BASENAME,
saveProjectId, manifestExists } from './src/utils.js';
import { oauth2Client, getAPICredentials } from './src/auth';
import { LOG, login } from './src/commands.js';

// An Apps Script API File
interface AppsScriptFile {
name: string;
Expand Down Expand Up @@ -195,11 +196,11 @@ commander
await prompt([{
type : 'input',
name : 'title',
message : 'give a script title:',
message : 'Give a script title:',
default: LOG.UNTITLED_SCRIPT_TITLE,
}]).then((answers) => {
}]).then((answers: any) => {
title = answers.title;
}).catch((err) => {
}).catch((err: any) => {
console.log(err);
});
}
Expand Down Expand Up @@ -291,10 +292,10 @@ commander
const { data } = await drive.files.list({
pageSize: 10,
fields: 'files(id, name)',
q: "mimeType='application/vnd.google-apps.script'",
q: 'mimeType="application/vnd.google-apps.script"',
});
const files = data.files;
const fileIds = [];
const fileIds:string[] = [];
if (files.length) {
files.map((file: any) => {
fileIds.push(`${file.name}`.padEnd(20) + ` - (${file.id})`);
Expand All @@ -304,12 +305,12 @@ commander
name : 'scriptId',
message : 'Clone which script? ',
choices : fileIds,
}]).then((answers) => {
}]).then((answers: any) => {
checkIfOnline();
spinner.setSpinnerTitle(LOG.CLONING);
saveProjectId(answers.scriptId);
fetchProject(answers.scriptId, '', versionNumber);
}).catch((err) => {
}).catch((err: any) => {
console.log(err);
});
} else {
Expand Down Expand Up @@ -398,7 +399,7 @@ commander
commander
.command('status')
.description('Lists files that will be pushed by clasp')
.option('--json', "Show status in JSON form")
.option('--json', 'Show status in JSON form')
.action(async (cmd: { json: boolean }) => {
await checkIfOnline();
getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => {
Expand Down Expand Up @@ -546,7 +547,7 @@ commander
script.projects.deployments.delete({
scriptId,
deploymentId,
}, {}, (err: any, res: any) => { // TODO remove any
}, {}, (err: any, res: any) => {
spinner.stop(true);
if (err) {
logError(null, ERROR.READ_ONLY_DELETE);
Expand Down Expand Up @@ -578,7 +579,7 @@ commander
description,
},
},
}, {}, (error: any, res: any) => { // TODO remove any
}, {}, (error: any, res: any) => {
spinner.stop(true);
if (error) {
logError(null, error); // TODO prettier error
Expand Down Expand Up @@ -667,7 +668,7 @@ commander
const res = await drive.files.list({
pageSize: 50,
fields: 'nextPageToken, files(id, name)',
q: "mimeType='application/vnd.google-apps.script'",
q: 'mimeType="application/vnd.google-apps.script"',
});
spinner.stop(true);
const files = res.data.files;
Expand All @@ -689,14 +690,14 @@ commander
commander
.command('logs')
.description('Shows the StackDriver logs')
.option('--json', "Show logs in JSON form")
.option('--json', 'Show logs in JSON form')
.option('--open', 'Open the StackDriver logs in browser')
.action(async (cmd: {
json: boolean,
open: boolean,
}) => {
await checkIfOnline();
function printLogs([entries]:any[]) {
function printLogs([entries]: any[]) {
for (let i = 0; i < 5; ++i) {
const metadata = entries[i].metadata;
const { severity, timestamp, payload } = metadata;
Expand All @@ -706,23 +707,23 @@ commander
if (cmd.json) {
payloadData = JSON.stringify(entries[i], null, 2);
} else {
const data = {
const data: any = {
textPayload: metadata.textPayload,
jsonPayload: metadata.jsonPayload ? metadata.jsonPayload.fields.message.stringValue : '',
protoPayload: metadata.protoPayload,
};
payloadData = data[payload] || ERROR.PAYLOAD_UNKNOWN;

if (payloadData && typeof(payloadData) === 'string') {
payloadData = payloadData.padEnd(20);
}
}
let coloredSeverity = ({
const coloredStringMap: any = {
ERROR: chalk.red(severity),
INFO: chalk.blue(severity),
DEBUG: chalk.yellow(severity),
NOTICE: chalk.magenta(severity),
})[severity] || severity;
};
let coloredSeverity:string = coloredStringMap[severity] || severity;
coloredSeverity = String(coloredSeverity).padEnd(20);
console.log(`${coloredSeverity} ${timestamp} ${functionName} ${payloadData}`);
}
Expand All @@ -742,7 +743,7 @@ commander
const logger = new logging({
projectId,
});
return logger.getEntries().then(printLogs).catch((err) => {
return logger.getEntries().then(printLogs).catch((err: any) => {
console.error(ERROR.LOGS_UNAVAILABLE);
});
});
Expand All @@ -759,7 +760,7 @@ commander
commander
.command('run <functionName>')
.description('Run a function in your Apps Scripts project')
.action((functionName) => {
.action((functionName:string) => {
getAPICredentials(async () => {
await checkIfOnline();
getProjectSettings().then(({ scriptId }: ProjectSettings) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@google/clasp",
"version": "1.3.0",
"version": "1.3.1",
"description": "Develop Apps Script Projects locally",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OAuth2Client } from 'google-auth-library';
import { 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';
Expand Down Expand Up @@ -35,11 +36,11 @@ export function getAPICredentials(cb: (rc: ClaspSettings | void) => void) {
DOTFILE.RC_LOCAL.read().then((rc: ClaspSettings) => {
oauth2Client.setCredentials(rc);
cb(rc);
}).catch((err: object) => {
}).catch((err: any) => {
DOTFILE.RC.read().then((rc: ClaspSettings) => {
oauth2Client.setCredentials(rc);
cb(rc);
}).catch((err: object) => {
}).catch((err: any) => {
console.error('Could not read API credentials. Error:');
console.error(err);
process.exit(-1);
Expand All @@ -58,9 +59,10 @@ async function authorizeWithLocalhost() {
const s = http.createServer();
s.listen(0, () => resolve(s));
});
const port = (server.address() as AddressInfo).port; // (Cast from <string | AddressInfo>)
const client = new OAuth2Client({
...oauth2ClientSettings,
redirectUri: `http://localhost:${server.address().port}`});
redirectUri: `http://localhost:${port}`});
const authCode = await new Promise<string>((res, rej) => {
server.on('request', (req: http.ServerRequest, resp: http.ServerResponse) => {
const urlParts = url.parse(req.url || '', true);
Expand Down Expand Up @@ -97,7 +99,7 @@ async function authorizeWithoutLocalhost() {
if (code && code.length) {
res(code);
} else {
rej("No authorization code entered.");
rej('No authorization code entered.');
}
rl.close();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe.skip('Test clasp status function', () => {
});
return tmpdir;
}
it("should respect globs and negation rules", () => {
it('should respect globs and negation rules', () => {
const tmpdir = setupTmpDirectory([
{ file: '.claspignore', data: '**/**\n!build/main.js\n!appsscript.json' },
{ file: 'build/main.js', data: ' ' },
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"allow-named-functions"
],
"prefer-const": true,
"quotemark": [true, "single"],
"radix": true,
"semicolon": [
true,
Expand Down

0 comments on commit 19bc290

Please sign in to comment.