Skip to content

Commit

Permalink
Refactoring part two (google#783)
Browse files Browse the repository at this point in the history
chore: update dependencies
refactor: code cleanup and normalize style
refactor: break up `getProjectFiles` into smaller bits
style: rename `getFileType` to `getLocalFileType`
  • Loading branch information
PopGoesTheWza authored Jun 19, 2020
1 parent 7acc646 commit 36efb87
Show file tree
Hide file tree
Showing 27 changed files with 1,057 additions and 972 deletions.
324 changes: 170 additions & 154 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"license": "Apache-2.0",
"dependencies": {
"@sindresorhus/is": "^2.1.1",
"chalk": "^4.0.0",
"chalk": "^4.1.0",
"cli-spinner": "^0.2.10",
"cli-truncate": "^2.1.0",
"commander": "^5.1.0",
Expand All @@ -76,9 +76,9 @@
"fs-extra": "^9.0.1",
"fuzzy": "^0.1.3",
"gaxios": "^3.0.3",
"google-auth-library": "^6.0.1",
"google-auth-library": "^6.0.2",
"googleapis": "^52.1.0",
"inquirer": "^7.1.0",
"inquirer": "^7.2.0",
"inquirer-autocomplete-prompt-ipt": "^2.0.0",
"is-online": "^8.4.0",
"loud-rejection": "^2.2.0",
Expand All @@ -99,19 +99,19 @@
"@types/cli-spinner": "^0.2.0",
"@types/fs-extra": "^9.0.1",
"@types/inquirer": "^6.5.0",
"@types/mkdirp": "^1.0.0",
"@types/mkdirp": "^1.0.1",
"@types/mocha": "^7.0.2",
"@types/node": "^12.12.44",
"@types/node": "^12.12.47",
"@types/recursive-readdir": "^2.2.0",
"@types/tmp": "^0.2.0",
"@types/watch": "^1.0.1",
"chai": "^4.2.0",
"coveralls": "^3.1.0",
"gts": "^2.0.2",
"mocha": "^7.2.0",
"mocha": "^8.0.1",
"nyc": "^15.1.0",
"prettier": "^2.0.4",
"tmp": "^0.2.1",
"type-fest": "^0.15.0"
"type-fest": "^0.15.1"
}
}
2 changes: 1 addition & 1 deletion src/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export enum SCRIPT_TYPES {

// Also see:
// https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/appsscript.json
interface AdvancedService {
export interface AdvancedService {
readonly userSymbol: string;
readonly serviceId: string;
readonly version: string;
Expand Down
45 changes: 24 additions & 21 deletions src/apiutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import {ClaspError} from './clasp-error';
import {functionNamePrompt, functionNameSource} from './inquirer';
import {enableOrDisableAdvanceServiceInManifest} from './manifest';
import {ERROR} from './messages';
import {getProjectId, spinner} from './utils';
import {getProjectId, spinner, stopSpinner} from './utils';

/**
* Prompts for the function name.
*/
export async function getFunctionNames(script: ReadonlyDeep<scriptV1.Script>, scriptId: string): Promise<string> {
export const getFunctionNames = async (script: ReadonlyDeep<scriptV1.Script>, scriptId: string): Promise<string> => {
spinner.setSpinnerTitle('Getting functions').start();
const content = await script.projects.getContent({
scriptId,
});
if (spinner.isSpinning()) spinner.stop(true);
stopSpinner();
if (content.status !== 200) throw new ClaspError(content.statusText);
const files = content.data.files ?? [];
type TypeFunction = scriptV1.Schema$GoogleAppsScriptTypeFunction;
Expand All @@ -28,28 +28,27 @@ export async function getFunctionNames(script: ReadonlyDeep<scriptV1.Script>, sc
}, [])
.map((func: Readonly<TypeFunction>) => func.name) as string[];

const source: functionNameSource = async (_unused: object, input = '') =>
// Returns a Promise
// https://www.npmjs.com/package/inquirer-autocomplete-prompt-ipt#options
new Promise(resolve => {
// Example: https://github.com/ruyadorno/inquirer-autocomplete-prompt/blob/master/example.js#L76
const original = fuzzy.filter(input, functionNames).map(element => element.original);

resolve(original);
});
// Returns a Promise
// https://www.npmjs.com/package/inquirer-autocomplete-prompt-ipt#options
// Example: https://github.com/ruyadorno/inquirer-autocomplete-prompt/blob/master/example.js#L76
const source: functionNameSource = async (_answers: object, input = '') =>
fuzzy.filter(input, functionNames).map(element => element.original);

const answers = await functionNamePrompt(source);
return answers.functionName;
}
};

/**
* Gets the project ID from the manifest. If there is no project ID, it returns an error.
*/
async function getProjectIdWithErrors(): Promise<string> {
const getProjectIdWithErrors = async (): Promise<string> => {
const projectId = await getProjectId(); // Will prompt user to set up if required
if (!projectId) throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
if (!projectId) {
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
}

return projectId;
}
};

// /**
// * Returns true if the service is enabled for the Google Cloud Project.
Expand All @@ -66,7 +65,7 @@ async function getProjectIdWithErrors(): Promise<string> {
* @param {string} serviceName The name of the service. i.e. sheets
* @param {boolean} enable Enables the API if true, otherwise disables.
*/
export async function enableOrDisableAPI(serviceName: string, enable: boolean): Promise<void> {
export const enableOrDisableAPI = async (serviceName: string, enable: boolean): Promise<void> => {
if (!serviceName) throw new ClaspError('An API name is required. Try sheets');
const projectId = await getProjectIdWithErrors();
const name = `projects/${projectId}/services/${serviceName}.googleapis.com`;
Expand All @@ -80,20 +79,24 @@ export async function enableOrDisableAPI(serviceName: string, enable: boolean):
await enableOrDisableAdvanceServiceInManifest(serviceName, enable);
console.log(`${enable ? 'Enable' : 'Disable'}d ${serviceName} API.`);
} catch (error) {
if (error instanceof ClaspError) throw error;
if (error instanceof ClaspError) {
throw error;
}

// If given non-existent API (like fakeAPI, it throws 403 permission denied)
// We will log this for the user instead:
console.log(error);

throw new ClaspError(ERROR.NO_API(enable, serviceName));
}
}
};

/**
* Enable 'script.googleapis.com' of Google API.
*/
export async function enableAppsScriptAPI(): Promise<void> {
export const enableAppsScriptAPI = async (): Promise<void> => {
await loadAPICredentials();
const projectId = await getProjectIdWithErrors();
const name = `projects/${projectId}/services/script.googleapis.com`;
await serviceUsage.services.enable({name});
}
};
Loading

0 comments on commit 36efb87

Please sign in to comment.