Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { readFileSync } from 'fs';
import * as path from 'path';
/**
* Clasp command method bodies.
*/
import chalk from 'chalk';
import * as commander from 'commander';
import * as del from 'del';
import * as fuzzy from 'fuzzy';
import { script_v1 } from 'googleapis';
import * as pluralize from 'pluralize';
import { watchTree } from 'watch';
import { PUBLIC_ADVANCED_SERVICES, SCRIPT_TYPES } from './apis';
import {
enableOrDisableAPI,
Expand All @@ -30,8 +26,6 @@ import { DOT, DOTFILE, ProjectSettings } from './dotfile';
import { fetchProject, getProjectFiles, hasProject, pushFiles, writeProjectFiles } from './files';
import {
addScopeToManifest,
enableExecutionAPI,
enableOrDisableAdvanceServiceInManifest,
isValidManifest,
manifestExists,
readManifest,
Expand All @@ -41,7 +35,6 @@ import {
ERROR,
LOG,
PROJECT_MANIFEST_BASENAME,
PROJECT_MANIFEST_FILENAME,
checkIfOnline,
getDefaultProjectName,
getProjectId,
Expand All @@ -52,7 +45,6 @@ import {
saveProject,
spinner,
} from './utils';
import multimatch = require('multimatch');

const ellipsize = require('ellipsize');
const open = require('opn');
Expand All @@ -63,70 +55,6 @@ const padEnd = require('string.prototype.padend');
const prompt = inquirer.prompt;
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));

/**
* Uploads all files into the script.google.com filesystem.
* TODO: Only push the specific files that changed (rather than all files).
* @param cmd.watch {boolean} If true, runs `clasp push` when any local file changes. Exit with ^C.
*/
export const push = async (cmd: { watch: boolean, force: boolean }) => {
await checkIfOnline();
await loadAPICredentials();
await isValidManifest();
const { rootDir } = await getProjectSettings();

/**
* Checks if the manifest has changes.
*/
const manifestHasChanges = async (): Promise<boolean> => {
const { scriptId, rootDir } = await getProjectSettings();
const localManifestPath = path.join(rootDir || DOT.PROJECT.DIR, PROJECT_MANIFEST_FILENAME);
const localManifest = readFileSync(localManifestPath, 'utf8');
const remoteFiles = await fetchProject(scriptId, undefined, true);
const remoteManifest = remoteFiles.find((file) => file.name === PROJECT_MANIFEST_BASENAME);
if (!remoteManifest) throw Error('remote manifest no found');
return localManifest !== remoteManifest.source;
};

const confirmManifestUpdate = async (): Promise<boolean> => {
const answers = await prompt([{
name: 'overwrite',
type: 'confirm',
message: 'Manifest file has been updated. Do you want to push and overwrite?',
default: false,
}]) as { overwrite: boolean };
return answers.overwrite;
};

if (cmd.watch) {
console.log(LOG.PUSH_WATCH);
const patterns = await DOTFILE.IGNORE();
// @see https://www.npmjs.com/package/watch
watchTree(rootDir || '.', async (f, curr, prev) => {
// The first watch doesn't give a string for some reason.
if (typeof f === 'string') {
console.log(`\n${LOG.PUSH_WATCH_UPDATED(f)}\n`);
if (multimatch([f], patterns).length) {
// The file matches the ignored files patterns so we do nothing
return;
}
}
if (!cmd.force && await manifestHasChanges() && !await confirmManifestUpdate()) {
console.log('Stopping push...');
return;
}
console.log(LOG.PUSHING);
pushFiles();
});
} else {
if (!cmd.force && await manifestHasChanges() && !await confirmManifestUpdate()) {
console.log('Stopping push...');
return;
}
spinner.setSpinnerTitle(LOG.PUSHING).start();
pushFiles();
}
};

/**
* Outputs the help command.
*/
Expand Down
103 changes: 103 additions & 0 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { loadAPICredentials } from './../auth';

import {
DOT,
DOTFILE,
} from './../dotfile';

import {
fetchProject,
pushFiles,
} from './../files';

import {
isValidManifest,
} from './../manifest';

import {
checkIfOnline,
getProjectSettings,
LOG,
PROJECT_MANIFEST_BASENAME,
PROJECT_MANIFEST_FILENAME,
spinner,
} from './../utils';

import { readFileSync } from 'fs';
import * as path from 'path';
import { watchTree } from 'watch';
import multimatch = require('multimatch');
const inquirer = require('inquirer');
const prompt = inquirer.prompt;
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));

/**
* Uploads all files into the script.google.com filesystem.
* TODO: Only push the specific files that changed (rather than all files).
* @param cmd.watch {boolean} If true, runs `clasp push` when any local file changes. Exit with ^C.
*/
export default async (cmd: { watch: boolean, force: boolean }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there any changes made to the actual code, or was it just copied and pasted as-is? Hard to tell with the diff view.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, great question.

The constants manifestHasChanges and confirmManifestUpdate were extracted to the bottom of the file and comments were added to them.

My thought is that it improves code readability. Additionally, if we wanted in the future, we could export these and test them individually.

await checkIfOnline();
await loadAPICredentials();
await isValidManifest();
const { rootDir } = await getProjectSettings();

if (cmd.watch) {
console.log(LOG.PUSH_WATCH);
const patterns = await DOTFILE.IGNORE();
/**
* @see https://www.npmjs.com/package/watch
*/
watchTree(rootDir || '.', async (f, curr, prev) => {
// The first watch doesn't give a string for some reason.
if (typeof f === 'string') {
console.log(`\n${LOG.PUSH_WATCH_UPDATED(f)}\n`);
if (multimatch([f], patterns).length) {
// The file matches the ignored files patterns so we do nothing
return;
}
}
if (!cmd.force && await manifestHasChanges() && !await confirmManifestUpdate()) {
console.log('Stopping push...');
return;
}
console.log(LOG.PUSHING);
pushFiles();
});
} else {
if (!cmd.force && await manifestHasChanges() && !await confirmManifestUpdate()) {
console.log('Stopping push...');
return;
}
spinner.setSpinnerTitle(LOG.PUSHING).start();
pushFiles();
}
};

/**
* Confirms that the manifest file has been updated.
* @returns {Promise<boolean>}
*/
const confirmManifestUpdate = async (): Promise<boolean> => {
const answers = await prompt([{
name: 'overwrite',
type: 'confirm',
message: 'Manifest file has been updated. Do you want to push and overwrite?',
default: false,
}]) as { overwrite: boolean };
return answers.overwrite;
};

/**
* Checks if the manifest has changes.
* @returns {Promise<boolean>}
*/
const manifestHasChanges = async (): Promise<boolean> => {
const { scriptId, rootDir } = await getProjectSettings();
const localManifestPath = path.join(rootDir || DOT.PROJECT.DIR, PROJECT_MANIFEST_FILENAME);
const localManifest = readFileSync(localManifestPath, 'utf8');
const remoteFiles = await fetchProject(scriptId, undefined, true);
const remoteManifest = remoteFiles.find((file) => file.name === PROJECT_MANIFEST_BASENAME);
if (!remoteManifest) throw Error('remote manifest no found');
return localManifest !== remoteManifest.source;
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
logout,
logs,
openCmd,
push,
run,
setting,
status,
Expand All @@ -47,6 +46,7 @@ import { PROJECT_NAME, handleError } from './utils';

import pull from './commands/pull';
import clone from './commands/clone';
import push from './commands/push';

// const commander = require('commander');

Expand Down