diff --git a/src/commands.ts b/src/commands.ts index 0db40d21..06d6e4af 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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, @@ -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, @@ -41,7 +35,6 @@ import { ERROR, LOG, PROJECT_MANIFEST_BASENAME, - PROJECT_MANIFEST_FILENAME, checkIfOnline, getDefaultProjectName, getProjectId, @@ -52,7 +45,6 @@ import { saveProject, spinner, } from './utils'; -import multimatch = require('multimatch'); const ellipsize = require('ellipsize'); const open = require('opn'); @@ -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 => { - 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 => { - 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. */ diff --git a/src/commands/push.ts b/src/commands/push.ts new file mode 100644 index 00000000..64edf84e --- /dev/null +++ b/src/commands/push.ts @@ -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 }) => { + 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} + */ +const confirmManifestUpdate = async (): Promise => { + 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} + */ +const manifestHasChanges = async (): Promise => { + 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; +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index cdb39d4b..209c89ae 100755 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,6 @@ import { logout, logs, openCmd, - push, run, setting, status, @@ -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');