Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/google/clasp
Browse files Browse the repository at this point in the history
  • Loading branch information
PopGoesTheWza committed Aug 9, 2021
2 parents d4328a3 + deacf03 commit 921a4bc
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 340 deletions.
31 changes: 31 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"commander": "^7.2.0",
"debounce": "^1.2.1",
"dotf": "^2.0.0",
"find-up": "^5.0.0",
"fs-extra": "^10.0.0",
"fuzzy": "^0.1.3",
"google-auth-library": "^7.1.2",
Expand All @@ -83,6 +84,7 @@
"p-map": "^5.0.0",
"read-pkg-up": "^8.0.0",
"recursive-readdir": "^2.2.2",
"server-destroy": "^1.0.1",
"split-lines": "^3.0.0",
"strip-bom": "^5.0.0",
"ts2gas": "^4.0.0",
Expand All @@ -97,6 +99,7 @@
"@types/mocha": "^8.2.2",
"@types/node": "^12.20.15",
"@types/recursive-readdir": "^2.2.0",
"@types/server-destroy": "^1.0.1",
"@types/tmp": "^0.2.0",
"@types/wtfnode": "^0.7.0",
"chai": "^4.3.4",
Expand Down
2 changes: 1 addition & 1 deletion src/apiutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getProjectIdOrDie = async (): Promise<string> => {
return projectId;
}

throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT());
};

// /**
Expand Down
10 changes: 4 additions & 6 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {ReadonlyDeep} from 'type-fest';

import type {ClaspToken} from './dotfile';
import type {ClaspCredentials} from './utils';
import enableDestroy from 'server-destroy';

/**
* Authentication with Google's APIs.
Expand Down Expand Up @@ -217,6 +218,7 @@ const authorizeWithLocalhost = async (
// the server port needed to set up the Oauth2Client.
const server = await new Promise<Server>(resolve => {
const s = createServer();
enableDestroy(s);
s.listen(0, () => resolve(s));
});
const {port} = server.address() as AddressInfo;
Expand All @@ -240,7 +242,7 @@ const authorizeWithLocalhost = async (
console.log(LOG.AUTHORIZE(authUrl));
(async () => await open(authUrl))();
});
server.close();
server.destroy();

return (await client.getToken(authCode)).tokens;
};
Expand Down Expand Up @@ -287,11 +289,7 @@ const setOauthClientCredentials = async (rc: ClaspToken) => {
*/
const refreshCredentials = async (oAuthClient: ReadonlyDeep<OAuth2Client>) => {
await oAuthClient.getAccessToken(); // Refreshes expiry date if required
const {expiry_date = 0} = oAuthClient.credentials;

if (expiry_date !== expiry_date) {
rc.token = oAuthClient.credentials;
}
rc.token = oAuthClient.credentials;
};

// Set credentials and refresh them.
Expand Down
13 changes: 9 additions & 4 deletions src/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {ERROR, LOG} from '../messages.js';
import {extractScriptId} from '../urls.js';
import {checkIfOnlineOrDie, saveProject, spinner} from '../utils.js';
import status from './status.js';
import {Conf} from '../conf.js';

const config = Conf.get();

interface CommandOption {
readonly rootDir: string;
Expand All @@ -26,18 +29,20 @@ export default async (
versionNumber: number | undefined,
options: CommandOption
): Promise<void> => {
if (options.rootDir) {
config.projectRootDirectory = options.rootDir;
}
await checkIfOnlineOrDie();
if (hasProject()) {
throw new ClaspError(ERROR.FOLDER_EXISTS);
throw new ClaspError(ERROR.FOLDER_EXISTS());
}

const id = scriptId ? extractScriptId(scriptId) : await getScriptId();

spinner.start(LOG.CLONING);

const {rootDir} = options;
await saveProject({scriptId: id, rootDir}, false);
await writeProjectFiles(await fetchProject(id, versionNumber), rootDir);
await saveProject({scriptId: id, rootDir: config.projectRootDirectory}, false);
await writeProjectFiles(await fetchProject(id, versionNumber), config.projectRootDirectory);
await status();
};

Expand Down
19 changes: 14 additions & 5 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
spinner,
stopSpinner,
} from '../utils.js';
import {Conf} from '../conf.js';

const config = Conf.get();

interface CommandOption {
readonly parentId?: string;
Expand All @@ -30,10 +33,14 @@ interface CommandOption {
* If not specified, clasp will default to the current directory.
*/
export default async (options: CommandOption): Promise<void> => {
if (options.rootDir) {
config.projectRootDirectory = options.rootDir;
}

// Handle common errors.
await checkIfOnlineOrDie();
if (hasProject()) {
throw new ClaspError(ERROR.FOLDER_EXISTS);
throw new ClaspError(ERROR.FOLDER_EXISTS());
}

await loadAPICredentials();
Expand Down Expand Up @@ -98,10 +105,12 @@ export default async (options: CommandOption): Promise<void> => {

const scriptId = data.scriptId ?? '';
console.log(LOG.CREATE_PROJECT_FINISH(filetype, scriptId));
const {rootDir} = options;
await saveProject({scriptId, rootDir, parentId: parentId ? [parentId] : undefined}, false);
await saveProject(
{scriptId, rootDir: config.projectRootDirectory, parentId: parentId ? [parentId] : undefined},
false
);

if (!manifestExists(rootDir)) {
await writeProjectFiles(await fetchProject(scriptId), rootDir); // Fetches appsscript.json, o.w. `push` breaks
if (!manifestExists(config.projectRootDirectory)) {
await writeProjectFiles(await fetchProject(scriptId), config.projectRootDirectory); // Fetches appsscript.json, o.w. `push` breaks
}
};
36 changes: 6 additions & 30 deletions src/commands/logout.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
import {Conf} from '../conf.js';
import {DOTFILE} from '../dotfile.js';
import {hasOauthClientSettings} from '../utils.js';
import fs from 'fs-extra';

const {auth} = Conf.get();
const config = Conf.get();

/**
* Logs out the user by deleting credentials.
*/
export default async (): Promise<void> => {
let previousPath: string | undefined;

if (hasOauthClientSettings(true)) {
if (auth.isDefault()) {
// If no local auth defined, try current directory
previousPath = auth.path;
auth.path = '.';
}

await DOTFILE.AUTH().delete();

if (previousPath) {
auth.path = previousPath;
}
if (config.auth && fs.existsSync(config.auth)) {
fs.unlinkSync(config.auth);
}

if (hasOauthClientSettings()) {
if (!auth.isDefault()) {
// If local auth defined, try with default (global)
previousPath = auth.path;
auth.path = '';
}

await DOTFILE.AUTH().delete();

if (previousPath) {
auth.path = previousPath;
}
if (config.authLocal && fs.existsSync(config.authLocal)) {
fs.unlinkSync(config.authLocal);
}
};
8 changes: 4 additions & 4 deletions src/commands/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ const setupLogs = async (projectSettings: ProjectSettings): Promise<string> => {

const dotfile = DOTFILE.PROJECT();
if (!dotfile) {
throw new ClaspError(ERROR.SETTINGS_DNE);
throw new ClaspError(ERROR.SETTINGS_DNE());
}

const settings = await dotfile.read<ProjectSettings>();
if (!settings.scriptId) {
throw new ClaspError(ERROR.SCRIPT_ID_DNE);
throw new ClaspError(ERROR.SCRIPT_ID_DNE());
}

const {projectId} = await projectIdPrompt();
Expand Down Expand Up @@ -186,7 +186,7 @@ const fetchAndPrintLogs = async (
): Promise<void> => {
// Validate projectId
if (!projectId) {
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT());
}

if (!isValidProjectId(projectId)) {
Expand All @@ -195,7 +195,7 @@ const fetchAndPrintLogs = async (

const {isLocalCreds} = await loadAPICredentials();

spinner.start(`${isLocalCreds ? LOG.LOCAL_CREDS : ''}${LOG.GRAB_LOGS}`);
spinner.start(`${isLocalCreds ? LOG.LOCAL_CREDS() : ''}${LOG.GRAB_LOGS}`);

// Create a time filter (timestamp >= "2016-11-29T23:00:00Z")
// https://cloud.google.com/logging/docs/view/advanced-filters#search-by-time
Expand Down
4 changes: 2 additions & 2 deletions src/commands/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async (scriptId: string, options: CommandOption): Promise<void> =
if (options.creds) {
const {projectId} = projectSettings;
if (!projectId) {
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT());
}

console.log(LOG.OPEN_CREDS(projectId));
Expand All @@ -69,7 +69,7 @@ export default async (scriptId: string, options: CommandOption): Promise<void> =
const openAddon = async (projectSettings: ProjectSettings) => {
const {parentId: parentIdList = []} = projectSettings;
if (parentIdList.length === 0) {
throw new ClaspError(ERROR.NO_PARENT_ID);
throw new ClaspError(ERROR.NO_PARENT_ID());
}

if (parentIdList.length > 1) {
Expand Down
7 changes: 4 additions & 3 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import type {ProjectSettings} from '../dotfile';

const {debounce} = debouncePkg;
const {readFileSync} = fs;
const {project} = Conf.get();
const WATCH_DEBOUNCE_MS = 1000;

const config = Conf.get();

interface CommandOption {
readonly watch?: boolean;
readonly force?: boolean;
Expand Down Expand Up @@ -88,8 +89,8 @@ const confirmManifestUpdate = async (): Promise<boolean> => (await overwriteProm
* @returns {Promise<boolean>}
*/
const manifestHasChanges = async (projectSettings: ProjectSettings): Promise<boolean> => {
const {scriptId, rootDir = project.resolvedDir} = projectSettings;
const localManifest = readFileSync(path.join(rootDir, PROJECT_MANIFEST_FILENAME), FS_OPTIONS);
const {scriptId, rootDir = config.projectRootDirectory} = projectSettings;
const localManifest = readFileSync(path.join(rootDir!, PROJECT_MANIFEST_FILENAME), FS_OPTIONS);
const remoteFiles = await fetchProject(scriptId, undefined, true);
const remoteManifest = remoteFiles.find(file => file.name === PROJECT_MANIFEST_BASENAME);
if (remoteManifest) {
Expand Down
Loading

0 comments on commit 921a4bc

Please sign in to comment.