Skip to content

Commit

Permalink
Small bits (#630)
Browse files Browse the repository at this point in the history
* 2.1.0

* Low impact changes (#619)

* dependencies clean-up

* types for child_process' spawnSync

* types for child_process' spawnSync + options fix

* linting

* Relative rootdir (#620)

* relative rootDir support

* relative rootDir support (untrackedFiles behavior changed)

* relative rootDir doc changes

* Cleanup before next pr (#621)

* prettier + sort imports

* splitLines types

* unused package 'connect'

* ucfirst, isOnline types

* ellipsize types

* redundant package 'fs-copy-file-sync'

* removing extra line

* // TODO

* packages dependencies update

* comment fix

* fixes

* nicer ellipsize typing

* better?

* Refactor `inquirer` package (#622)

* regroup `inquirer` into a single file

* fix typo

* linting

* switch to `find-up`

* switch to `find-up` & `strip-bom`

* dependencies update

* findUp implementation fix

* enum accessor fix

* fs-extra & typescript dependency fix

* linting

* dependencies clean-up (again)

* non any cast

* TODOs & types

* dependencies

* relative paths fix

* relative paths fix, types & minor code changes

* commented-out cleanup
  • Loading branch information
PopGoesTheWza authored and grant committed May 23, 2019
1 parent ef7b058 commit 7c38877
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 75 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@
"string.prototype.padend": "^3.0.0",
"strip-bom": "^4.0.0",
"ts2gas": "^3.4.4",
"typescript": "^3.4",
"typescript": "^3.4.5",
"watch": "^1.0.2"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/cli-spinner": "^0.2.0",
"@types/events": "^3.0.0",
"@types/fs-extra": "^7.0.0",
"@types/inquirer": "^6.0.2",
"@types/inquirer": "^6.0.3",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "^5.2.6",
"@types/node": "^12.0.2",
"@types/node": "^10.14.7",
"@types/pluralize": "^0.0.29",
"@types/recursive-readdir": "^2.2.0",
"@types/tmp": "^0.1.0",
"@types/watch": "^1.0.1",
"axios": "^0.18.0",
"chai": "^4.1.2",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
Expand Down
8 changes: 5 additions & 3 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as url from 'url';
import { Credentials, GenerateAuthUrlOpts, OAuth2Client, OAuth2ClientOptions } from 'google-auth-library';
import { google, script_v1 } from 'googleapis';
import * as open from 'open';
import { ClaspToken, DOTFILE } from './dotfile';
import { ClaspToken, DOTFILE, Dotf } from './dotfile';
import { oauthScopesPrompt } from './inquirer';
import { readManifest } from './manifest';
import { ClaspCredentials, ERROR, LOG, checkIfOnline, getOAuthSettings, logError } from './utils';
Expand Down Expand Up @@ -154,7 +154,9 @@ export async function authorize(options: {

// Save the token and own creds together.
let claspToken: ClaspToken;
let dotfile: ReturnType<Dotf>;
if (options.creds) {
dotfile = DOTFILE.RC_LOCAL();
// Save local ClaspCredentials.
claspToken = {
token,
Expand All @@ -165,16 +167,16 @@ export async function authorize(options: {
},
isLocalCreds: true,
};
await DOTFILE.RC_LOCAL().write(claspToken);
} else {
dotfile = DOTFILE.RC;
// Save global ClaspCredentials.
claspToken = {
token,
oauth2ClientSettings: globalOauth2ClientSettings,
isLocalCreds: false,
};
await DOTFILE.RC.write(claspToken);
}
await dotfile.write(claspToken);
console.log(LOG.SAVED_CREDS(!!options.creds));
} catch (err) {
logError(null, ERROR.ACCESS_TOKEN + err);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/defaultCmd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERROR, logError } from './../utils';
import { ERROR, logError } from '../utils';

/**
* Displays a default message when an unknown command is typed.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadAPICredentials, script } from './../auth';
import { loadAPICredentials, script } from '../auth';
import {
ERROR,
LOG,
Expand All @@ -7,7 +7,7 @@ import {
getProjectSettings,
logError,
spinner,
} from './../utils';
} from '../utils';

/**
* Deploys an Apps Script project.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/deployments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { script_v1 } from 'googleapis';
import * as pluralize from 'pluralize';
import { loadAPICredentials, script } from './../auth';
import { LOG, checkIfOnline, getProjectSettings, logError, spinner } from './../utils';
import { loadAPICredentials, script } from '../auth';
import { LOG, checkIfOnline, getProjectSettings, logError, spinner } from '../utils';

/**
* Lists a script's deployments.
Expand Down
5 changes: 2 additions & 3 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Clasp command method bodies.
*/
import { readFileSync } from 'fs-extra';
import { readJsonSync } from 'fs-extra';
import { enableAppsScriptAPI } from '../apiutils';
import { authorize } from '../auth';
import { readManifest } from '../manifest';
Expand Down Expand Up @@ -47,8 +47,7 @@ File > Project Properties > Scopes
`);

// Read credentials file.
const credsFile = readFileSync(options.creds, 'utf8');
const credentials = JSON.parse(credsFile);
const credentials = readJsonSync(options.creds, { encoding: 'utf-8' });
await authorize({
useLocalhost,
creds: credentials,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/logout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as del from 'del';
import { DOT } from './../dotfile';
import { hasOauthClientSettings } from './../utils';
import { DOT } from '../dotfile';
import { hasOauthClientSettings } from '../utils';

/**
* Logs out the user by deleting credentials.
Expand Down
16 changes: 4 additions & 12 deletions src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ import chalk from 'chalk';
import { GaxiosResponse } from 'gaxios';
import { logging_v2 } from 'googleapis';
import * as open from 'open';
import { loadAPICredentials, logger } from './../auth';
import { DOTFILE, ProjectSettings } from './../dotfile';
import { URL } from './../urls';
import {
ERROR,
LOG,
checkIfOnline,
getProjectSettings,
isValidProjectId,
logError,
spinner,
} from './../utils';
import { loadAPICredentials, logger } from '../auth';
import { DOTFILE, ProjectSettings } from '../dotfile';
import { projectIdPrompt } from '../inquirer';
import { URL } from '../urls';
import { ERROR, LOG, checkIfOnline, getProjectSettings, isValidProjectId, logError, spinner } from '../utils';

const padEnd = require('string.prototype.padend');

Expand Down
14 changes: 3 additions & 11 deletions src/commands/openCmd.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as open from 'open';
import { loadAPICredentials, script } from './../auth';
import { URL } from './../urls';
import { ERROR, LOG, checkIfOnline, getProjectSettings, getWebApplicationURL, logError } from './../utils';
import { loadAPICredentials, script } from '../auth';
import { deploymentIdPrompt } from '../inquirer';
import { URL } from '../urls';
import { ERROR, LOG, checkIfOnline, getProjectSettings, getWebApplicationURL, logError } from '../utils';

interface EllipizeOptions {
ellipse?: string;
Expand Down Expand Up @@ -58,14 +58,6 @@ export default async (
logError(null, ERROR.SCRIPT_ID_INCORRECT(scriptId));
}
// Order deployments by update time.
// const orderedDeployments = deployments
// .slice()
// .sort((d1, d2) => {
// if (d1.updateTime && d2.updateTime) {
// return d1.updateTime.localeCompare(d2.updateTime);
// }
// return 0; // should never happen
// });
const choices = deployments
.slice()
.sort((d1, d2) => {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetchProject, writeProjectFiles } from './../files';
import { LOG, checkIfOnline, getProjectSettings, spinner } from './../utils';
import { fetchProject, writeProjectFiles } from '../files';
import { LOG, checkIfOnline, getProjectSettings, spinner } from '../utils';

/**
* Force downloads all Apps Script project files into the local filesystem.
Expand Down
12 changes: 6 additions & 6 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import * as path from 'path';
import { readFileSync } from 'fs-extra';
import * as multimatch from 'multimatch';
import { watchTree } from 'watch';
import { loadAPICredentials } from './../auth';
import { DOT, DOTFILE } from './../dotfile';
import { fetchProject, pushFiles } from './../files';
import { isValidManifest } from './../manifest';
import { loadAPICredentials } from '../auth';
import { DOT, DOTFILE } from '../dotfile';
import { fetchProject, pushFiles } from '../files';
import { overwritePrompt } from '../inquirer';
import { isValidManifest } from '../manifest';
import {
LOG,
PROJECT_MANIFEST_BASENAME,
PROJECT_MANIFEST_FILENAME,
checkIfOnline,
getProjectSettings,
spinner,
} from './../utils';
import { overwritePrompt } from '../inquirer';
} from '../utils';

/**
* Uploads all files into the script.google.com filesystem.
Expand Down
6 changes: 3 additions & 3 deletions src/commands/status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getProjectFiles } from './../files';
import { isValidManifest } from './../manifest';
import { LOG, checkIfOnline, getProjectSettings } from './../utils';
import { getProjectFiles } from '../files';
import { isValidManifest } from '../manifest';
import { LOG, checkIfOnline, getProjectSettings } from '../utils';

/**
* Displays the status of which Apps Script files are ignored from .claspignore
Expand Down
4 changes: 2 additions & 2 deletions src/commands/undeploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loadAPICredentials, script } from './../auth';
import { ERROR, LOG, checkIfOnline, getProjectSettings, logError, spinner } from './../utils';
import { loadAPICredentials, script } from '../auth';
import { ERROR, LOG, checkIfOnline, getProjectSettings, logError, spinner } from '../utils';

/**
* Removes a deployment from the Apps Script project.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadAPICredentials, script } from './../auth';
import { LOG, checkIfOnline, getProjectSettings, logError, spinner } from './../utils';
import { loadAPICredentials, script } from '../auth';
import { descriptionPrompt } from '../inquirer';
import { LOG, checkIfOnline, getProjectSettings, logError, spinner } from '../utils';

/**
* Creates a new version of an Apps Script project.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { script_v1 } from 'googleapis';
import { loadAPICredentials, script } from './../auth';
import { LOG, checkIfOnline, getProjectSettings, logError, spinner } from './../utils';
import { loadAPICredentials, script } from '../auth';
import { LOG, checkIfOnline, getProjectSettings, logError, spinner } from '../utils';

/**
* Lists versions of an Apps Script project.
Expand Down
6 changes: 3 additions & 3 deletions src/dotfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { Credentials } from 'google-auth-library';
import { OAuth2ClientOptions } from 'google-auth-library/build/src/auth/oauth2client';
import stripBom = require('strip-bom');

declare type dotf = (dirname: string, name: string) => {
export declare type Dotf = (dirname: string, name: string) => {
exists: () => Promise<boolean>;
read: () => Promise<any>; // tslint:disable-line: no-any
write: <T>(obj: T) => Promise<T>;
delete: () => Promise<void>;
};

const dotf: dotf = require('dotf');
const dotf: Dotf = require('dotf');
const splitLines: (str: string, options?: { preserveNewLines?: boolean })
=> string[] = require('split-lines');

Expand Down Expand Up @@ -102,7 +102,7 @@ export const DOTFILE = {
/**
* Gets the closest DOT.PROJECT.NAME in the parent directory of the directory
* that the command was run in.
* @return {dotf} A dotf with that dotfile. Null if there is no file
* @return {Dotf} A dotf with that dotfile. Null if there is no file
*/
PROJECT: () => {
const projectPath = findUp.sync(DOT.PROJECT.PATH);
Expand Down
8 changes: 4 additions & 4 deletions src/inquirer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { script_v1 } from 'googleapis';
import { prompt, registerPrompt } from 'inquirer';
import { Question, prompt, registerPrompt } from 'inquirer';
import { SCRIPT_TYPES } from './apis';
import { LOG } from './utils';

Expand All @@ -18,8 +18,8 @@ export const functionNamePrompt = (source: functionNameSource) => {
message: 'Select a functionName',
type: 'autocomplete',
source,
};
return prompt<{ functionName: string }>([question]);
} as unknown as Question;
return prompt<{ functionName: string }>(question);
};

interface DeploymentIdPrompt {
Expand Down Expand Up @@ -124,4 +124,4 @@ export const scriptTypePrompt = () => prompt<{ type: string }>([{
message: LOG.CREATE_SCRIPT_QUESTION,
name: 'type',
type: 'list',
}]);
}]);
4 changes: 2 additions & 2 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function readManifest(): Promise<Manifest> {
if (typeof rootDir === 'undefined') rootDir = DOT.PROJECT.DIR;
const manifest = path.join(rootDir, PROJECT_MANIFEST_FILENAME);
try {
return JSON.parse(fs.readFileSync(manifest, 'utf8'));
return fs.readJsonSync(manifest, { encoding: 'utf-8' });
} catch (err) {
logError(null, ERROR.NO_MANIFEST(manifest));
throw Error('Could not read the manifest file.'); // TODO standardize errors.
Expand All @@ -39,7 +39,7 @@ export async function writeManifest(manifest: Manifest) {
if (typeof rootDir === 'undefined') rootDir = DOT.PROJECT.DIR;
const manifestFilePath = path.join(rootDir, PROJECT_MANIFEST_FILENAME);
try {
fs.writeFileSync(manifestFilePath, JSON.stringify(manifest, null, 2));
fs.writeJsonSync(manifestFilePath, manifest, { encoding: 'utf-8', spaces: 2 });
} catch (err) {
logError(null, ERROR.FS_FILE_WRITE);
}
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ export async function saveProject(
newProjectSettings: ProjectSettings,
append = true): Promise<ProjectSettings> {
if (append) {
const projectSettings: ProjectSettings = await DOTFILE.PROJECT().read();
// TODO: const projectSettings: ProjectSettings = await getProjectSettings();
const projectSettings: ProjectSettings = await getProjectSettings();
newProjectSettings = { ...projectSettings, ...newProjectSettings };
}
return DOTFILE.PROJECT().write(newProjectSettings);
Expand Down

0 comments on commit 7c38877

Please sign in to comment.