Skip to content

Commit 921a4bc

Browse files
committed
Merge branch 'master' of https://github.com/google/clasp
2 parents d4328a3 + deacf03 commit 921a4bc

File tree

19 files changed

+248
-340
lines changed

19 files changed

+248
-340
lines changed

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"commander": "^7.2.0",
6767
"debounce": "^1.2.1",
6868
"dotf": "^2.0.0",
69+
"find-up": "^5.0.0",
6970
"fs-extra": "^10.0.0",
7071
"fuzzy": "^0.1.3",
7172
"google-auth-library": "^7.1.2",
@@ -83,6 +84,7 @@
8384
"p-map": "^5.0.0",
8485
"read-pkg-up": "^8.0.0",
8586
"recursive-readdir": "^2.2.2",
87+
"server-destroy": "^1.0.1",
8688
"split-lines": "^3.0.0",
8789
"strip-bom": "^5.0.0",
8890
"ts2gas": "^4.0.0",
@@ -97,6 +99,7 @@
9799
"@types/mocha": "^8.2.2",
98100
"@types/node": "^12.20.15",
99101
"@types/recursive-readdir": "^2.2.0",
102+
"@types/server-destroy": "^1.0.1",
100103
"@types/tmp": "^0.2.0",
101104
"@types/wtfnode": "^0.7.0",
102105
"chai": "^4.3.4",

src/apiutils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const getProjectIdOrDie = async (): Promise<string> => {
5050
return projectId;
5151
}
5252

53-
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
53+
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT());
5454
};
5555

5656
// /**

src/auth.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {ReadonlyDeep} from 'type-fest';
1717

1818
import type {ClaspToken} from './dotfile';
1919
import type {ClaspCredentials} from './utils';
20+
import enableDestroy from 'server-destroy';
2021

2122
/**
2223
* Authentication with Google's APIs.
@@ -217,6 +218,7 @@ const authorizeWithLocalhost = async (
217218
// the server port needed to set up the Oauth2Client.
218219
const server = await new Promise<Server>(resolve => {
219220
const s = createServer();
221+
enableDestroy(s);
220222
s.listen(0, () => resolve(s));
221223
});
222224
const {port} = server.address() as AddressInfo;
@@ -240,7 +242,7 @@ const authorizeWithLocalhost = async (
240242
console.log(LOG.AUTHORIZE(authUrl));
241243
(async () => await open(authUrl))();
242244
});
243-
server.close();
245+
server.destroy();
244246

245247
return (await client.getToken(authCode)).tokens;
246248
};
@@ -287,11 +289,7 @@ const setOauthClientCredentials = async (rc: ClaspToken) => {
287289
*/
288290
const refreshCredentials = async (oAuthClient: ReadonlyDeep<OAuth2Client>) => {
289291
await oAuthClient.getAccessToken(); // Refreshes expiry date if required
290-
const {expiry_date = 0} = oAuthClient.credentials;
291-
292-
if (expiry_date !== expiry_date) {
293-
rc.token = oAuthClient.credentials;
294-
}
292+
rc.token = oAuthClient.credentials;
295293
};
296294

297295
// Set credentials and refresh them.

src/commands/clone.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {ERROR, LOG} from '../messages.js';
88
import {extractScriptId} from '../urls.js';
99
import {checkIfOnlineOrDie, saveProject, spinner} from '../utils.js';
1010
import status from './status.js';
11+
import {Conf} from '../conf.js';
12+
13+
const config = Conf.get();
1114

1215
interface CommandOption {
1316
readonly rootDir: string;
@@ -26,18 +29,20 @@ export default async (
2629
versionNumber: number | undefined,
2730
options: CommandOption
2831
): Promise<void> => {
32+
if (options.rootDir) {
33+
config.projectRootDirectory = options.rootDir;
34+
}
2935
await checkIfOnlineOrDie();
3036
if (hasProject()) {
31-
throw new ClaspError(ERROR.FOLDER_EXISTS);
37+
throw new ClaspError(ERROR.FOLDER_EXISTS());
3238
}
3339

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

3642
spinner.start(LOG.CLONING);
3743

38-
const {rootDir} = options;
39-
await saveProject({scriptId: id, rootDir}, false);
40-
await writeProjectFiles(await fetchProject(id, versionNumber), rootDir);
44+
await saveProject({scriptId: id, rootDir: config.projectRootDirectory}, false);
45+
await writeProjectFiles(await fetchProject(id, versionNumber), config.projectRootDirectory);
4146
await status();
4247
};
4348

src/commands/create.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
spinner,
1414
stopSpinner,
1515
} from '../utils.js';
16+
import {Conf} from '../conf.js';
17+
18+
const config = Conf.get();
1619

1720
interface CommandOption {
1821
readonly parentId?: string;
@@ -30,10 +33,14 @@ interface CommandOption {
3033
* If not specified, clasp will default to the current directory.
3134
*/
3235
export default async (options: CommandOption): Promise<void> => {
36+
if (options.rootDir) {
37+
config.projectRootDirectory = options.rootDir;
38+
}
39+
3340
// Handle common errors.
3441
await checkIfOnlineOrDie();
3542
if (hasProject()) {
36-
throw new ClaspError(ERROR.FOLDER_EXISTS);
43+
throw new ClaspError(ERROR.FOLDER_EXISTS());
3744
}
3845

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

99106
const scriptId = data.scriptId ?? '';
100107
console.log(LOG.CREATE_PROJECT_FINISH(filetype, scriptId));
101-
const {rootDir} = options;
102-
await saveProject({scriptId, rootDir, parentId: parentId ? [parentId] : undefined}, false);
108+
await saveProject(
109+
{scriptId, rootDir: config.projectRootDirectory, parentId: parentId ? [parentId] : undefined},
110+
false
111+
);
103112

104-
if (!manifestExists(rootDir)) {
105-
await writeProjectFiles(await fetchProject(scriptId), rootDir); // Fetches appsscript.json, o.w. `push` breaks
113+
if (!manifestExists(config.projectRootDirectory)) {
114+
await writeProjectFiles(await fetchProject(scriptId), config.projectRootDirectory); // Fetches appsscript.json, o.w. `push` breaks
106115
}
107116
};

src/commands/logout.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
import {Conf} from '../conf.js';
2-
import {DOTFILE} from '../dotfile.js';
3-
import {hasOauthClientSettings} from '../utils.js';
2+
import fs from 'fs-extra';
43

5-
const {auth} = Conf.get();
4+
const config = Conf.get();
65

76
/**
87
* Logs out the user by deleting credentials.
98
*/
109
export default async (): Promise<void> => {
11-
let previousPath: string | undefined;
12-
13-
if (hasOauthClientSettings(true)) {
14-
if (auth.isDefault()) {
15-
// If no local auth defined, try current directory
16-
previousPath = auth.path;
17-
auth.path = '.';
18-
}
19-
20-
await DOTFILE.AUTH().delete();
21-
22-
if (previousPath) {
23-
auth.path = previousPath;
24-
}
10+
if (config.auth && fs.existsSync(config.auth)) {
11+
fs.unlinkSync(config.auth);
2512
}
26-
27-
if (hasOauthClientSettings()) {
28-
if (!auth.isDefault()) {
29-
// If local auth defined, try with default (global)
30-
previousPath = auth.path;
31-
auth.path = '';
32-
}
33-
34-
await DOTFILE.AUTH().delete();
35-
36-
if (previousPath) {
37-
auth.path = previousPath;
38-
}
13+
if (config.authLocal && fs.existsSync(config.authLocal)) {
14+
fs.unlinkSync(config.authLocal);
3915
}
4016
};

src/commands/logs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ const setupLogs = async (projectSettings: ProjectSettings): Promise<string> => {
153153

154154
const dotfile = DOTFILE.PROJECT();
155155
if (!dotfile) {
156-
throw new ClaspError(ERROR.SETTINGS_DNE);
156+
throw new ClaspError(ERROR.SETTINGS_DNE());
157157
}
158158

159159
const settings = await dotfile.read<ProjectSettings>();
160160
if (!settings.scriptId) {
161-
throw new ClaspError(ERROR.SCRIPT_ID_DNE);
161+
throw new ClaspError(ERROR.SCRIPT_ID_DNE());
162162
}
163163

164164
const {projectId} = await projectIdPrompt();
@@ -186,7 +186,7 @@ const fetchAndPrintLogs = async (
186186
): Promise<void> => {
187187
// Validate projectId
188188
if (!projectId) {
189-
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
189+
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT());
190190
}
191191

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

196196
const {isLocalCreds} = await loadAPICredentials();
197197

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

200200
// Create a time filter (timestamp >= "2016-11-29T23:00:00Z")
201201
// https://cloud.google.com/logging/docs/view/advanced-filters#search-by-time

src/commands/open.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default async (scriptId: string, options: CommandOption): Promise<void> =
4242
if (options.creds) {
4343
const {projectId} = projectSettings;
4444
if (!projectId) {
45-
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT);
45+
throw new ClaspError(ERROR.NO_GCLOUD_PROJECT());
4646
}
4747

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

7575
if (parentIdList.length > 1) {

src/commands/push.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import type {ProjectSettings} from '../dotfile';
1919

2020
const {debounce} = debouncePkg;
2121
const {readFileSync} = fs;
22-
const {project} = Conf.get();
2322
const WATCH_DEBOUNCE_MS = 1000;
2423

24+
const config = Conf.get();
25+
2526
interface CommandOption {
2627
readonly watch?: boolean;
2728
readonly force?: boolean;
@@ -88,8 +89,8 @@ const confirmManifestUpdate = async (): Promise<boolean> => (await overwriteProm
8889
* @returns {Promise<boolean>}
8990
*/
9091
const manifestHasChanges = async (projectSettings: ProjectSettings): Promise<boolean> => {
91-
const {scriptId, rootDir = project.resolvedDir} = projectSettings;
92-
const localManifest = readFileSync(path.join(rootDir, PROJECT_MANIFEST_FILENAME), FS_OPTIONS);
92+
const {scriptId, rootDir = config.projectRootDirectory} = projectSettings;
93+
const localManifest = readFileSync(path.join(rootDir!, PROJECT_MANIFEST_FILENAME), FS_OPTIONS);
9394
const remoteFiles = await fetchProject(scriptId, undefined, true);
9495
const remoteManifest = remoteFiles.find(file => file.name === PROJECT_MANIFEST_BASENAME);
9596
if (remoteManifest) {

0 commit comments

Comments
 (0)