-
Notifications
You must be signed in to change notification settings - Fork 483
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
Merged
Merged
Refactor clasp push #514
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
257fdde
Refactor clone code
campionfellin da038b4
Make urls.ts file and refactor
campionfellin 3b35e72
Refactor clasp push
campionfellin 6b5228e
Fix nits
campionfellin 4ffb8c0
Fix merge issue
campionfellin b0ee106
Merge branch 'master' of https://github.com/campionfellin/clasp
campionfellin 7edf8ab
Merge branch 'master' into push
campionfellin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }) => { | ||
| 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; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
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.
Yes, great question.
The constants
manifestHasChangesandconfirmManifestUpdatewere 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.