Skip to content

Commit

Permalink
Refactor config paths, fix CLI options (#865)
Browse files Browse the repository at this point in the history
fix: Honor CLI overrides for config file paths.

Reworks config paths -- removes PathProxy, fixes a few issues with CLI overrides not being honored.
  • Loading branch information
sqrrrl committed Aug 9, 2021
1 parent 40e0b3d commit deacf03
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 312 deletions.
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 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 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
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 deacf03

Please sign in to comment.