-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #838 from zapier/zapier-pull-cmd
feat(cli): Introduce zapier pull command
- Loading branch information
Showing
8 changed files
with
235 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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 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 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,49 @@ | ||
const colors = require('colors/safe'); | ||
const debug = require('debug')('zapier:pull'); | ||
const inquirer = require('inquirer'); | ||
const path = require('path'); | ||
const Generator = require('yeoman-generator'); | ||
|
||
const maybeOverwriteFiles = async (gen) => { | ||
const dstDir = gen.options.dstDir; | ||
const srcDir = gen.options.srcDir; | ||
for (const file of gen.options.sourceFiles) { | ||
gen.fs.copy(path.join(srcDir, file), path.join(dstDir, file), gen.options); | ||
} | ||
}; | ||
|
||
module.exports = class PullGenerator extends Generator { | ||
initializing() { | ||
debug('SRC', this.options.sourceFiles); | ||
} | ||
|
||
prompting() { | ||
const prompts = [ | ||
{ | ||
type: 'confirm', | ||
name: 'confirm', | ||
message: `Warning: You are about to overwrite existing files. | ||
Before proceeding, please make sure you have saved your work. Consider creating a backup or saving your current state in a git branch. During the process, you may abort anytime by pressing 'x'. | ||
Do you want to continue?`, | ||
default: false, | ||
}, | ||
]; | ||
|
||
return inquirer.prompt(prompts).then((answers) => { | ||
if (!answers.confirm) { | ||
this.log(colors.green('zapier pull cancelled')); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
|
||
writing() { | ||
maybeOverwriteFiles(this); | ||
} | ||
|
||
end() { | ||
this.log(colors.green('zapier pull completed successfully')); | ||
} | ||
}; |
This file contains 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,52 @@ | ||
const AdmZip = require('adm-zip'); | ||
const { ensureFileSync } = require('fs-extra'); | ||
const path = require('path'); | ||
const yeoman = require('yeoman-environment'); | ||
|
||
const ZapierBaseCommand = require('../ZapierBaseCommand'); | ||
const { downloadSourceZip } = require('../../utils/api'); | ||
const { ensureDir, makeTempDir, removeDirSync } = require('../../utils/files'); | ||
const { listFiles } = require('../../utils/build'); | ||
const { buildFlags } = require('../buildFlags'); | ||
const PullGenerator = require('../../generators/pull'); | ||
|
||
class PullCommand extends ZapierBaseCommand { | ||
async perform() { | ||
// Fetch the source zip from API | ||
const tmpDir = makeTempDir(); | ||
const srcZipDst = path.join(tmpDir, 'download', 'source.zip'); | ||
|
||
try { | ||
ensureFileSync(srcZipDst); | ||
await downloadSourceZip(srcZipDst); | ||
|
||
// Write source zip to tmp dir | ||
const srcDst = path.join(tmpDir, 'source'); | ||
await ensureDir(srcDst); | ||
const zip = new AdmZip(srcZipDst); | ||
zip.extractAllTo(srcDst, true); | ||
|
||
// Prompt user to confirm overwrite | ||
const currentDir = process.cwd(); | ||
const sourceFiles = await listFiles(srcDst); | ||
|
||
const env = yeoman.createEnv(); | ||
const namespace = 'zapier:pull'; | ||
env.registerStub(PullGenerator, namespace); | ||
await env.run(namespace, { | ||
sourceFiles, | ||
srcDir: srcDst, | ||
dstDir: currentDir, | ||
}); | ||
} finally { | ||
removeDirSync(tmpDir); | ||
} | ||
} | ||
} | ||
|
||
PullCommand.flags = buildFlags(); | ||
PullCommand.description = | ||
'Pull the source code of the latest version of your integration from Zapier, overwriting your local integration files.'; | ||
PullCommand.skipValidInstallCheck = true; | ||
|
||
module.exports = PullCommand; |
This file contains 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 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
Oops, something went wrong.