From 19bc29059bb069610ed1e72d676067e313030cd9 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Fri, 25 May 2018 10:41:00 -0700 Subject: [PATCH] Remove tsc errors and improve code quality (no implicity types, no double quotes). Previous errors: ``` sudo npm run build; > @google/clasp@1.3.0 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/clasp@1.3.0 added 1 package from 1 contributor in 2.725s ``` No errors now: ``` sudo npm run build Password: Sorry, try again. Password: > @google/clasp@1.3.1 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/clasp@1.3.1 updated 1 package in 4.189s ``` --- index.ts | 41 +++++++++++++++++++++-------------------- package.json | 2 +- src/auth.ts | 10 ++++++---- tests/test.ts | 2 +- tslint.json | 1 + 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/index.ts b/index.ts index 2cf724bc..7355107f 100755 --- a/index.ts +++ b/index.ts @@ -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'; @@ -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; @@ -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); }); } @@ -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})`); @@ -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 { @@ -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) => { @@ -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); @@ -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 @@ -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; @@ -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; @@ -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}`); } @@ -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); }); }); @@ -759,7 +760,7 @@ commander commander .command('run ') .description('Run a function in your Apps Scripts project') - .action((functionName) => { + .action((functionName:string) => { getAPICredentials(async () => { await checkIfOnline(); getProjectSettings().then(({ scriptId }: ProjectSettings) => { diff --git a/package.json b/package.json index b468005d..a37df4dd 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/auth.ts b/src/auth.ts index b407a330..4c22b760 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -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'; @@ -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); @@ -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 ) const client = new OAuth2Client({ ...oauth2ClientSettings, - redirectUri: `http://localhost:${server.address().port}`}); + redirectUri: `http://localhost:${port}`}); const authCode = await new Promise((res, rej) => { server.on('request', (req: http.ServerRequest, resp: http.ServerResponse) => { const urlParts = url.parse(req.url || '', true); @@ -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(); }); diff --git a/tests/test.ts b/tests/test.ts index 1bbaadc1..2ecabb4e 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -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: ' ' }, diff --git a/tslint.json b/tslint.json index d568b545..ae4ba99b 100644 --- a/tslint.json +++ b/tslint.json @@ -84,6 +84,7 @@ "allow-named-functions" ], "prefer-const": true, + "quotemark": [true, "single"], "radix": true, "semicolon": [ true,