-
Notifications
You must be signed in to change notification settings - Fork 485
Refactor clasp push #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor clasp push #514
Changes from 1 commit
257fdde
da038b4
3b35e72
6b5228e
4ffb8c0
b0ee106
7edf8ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| 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 }) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, great question. The constants 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; | ||
| }; | ||
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.tstopush.tsthat is now unused incommands.tsisPROJECT_MANIFEST_FILENAMEfromutils. I can remove it.