Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 0 additions & 64 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,70 +63,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
105 changes: 105 additions & 0 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given all the imports added to this new file, are there now any unused imports in commands.ts? I can't tell just by looking at this PR, but feels like there is a good chance that at least a few of these imports aren't needed by the remaining code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are many unused imports in commands.ts (unrelated).

The only one that was moved over from commands.ts to push.ts that is now unused in commands.ts is PROJECT_MANIFEST_FILENAME from utils. I can remove it.

loadAPICredentials,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Should this be collapsed into one line, since there is only one import happening?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It could be. I figured to keep it multi-line to stay with the style of the other imports. I can change to one-line.

} 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