Skip to content

download-runtime: Add tool to download AMP runtime#663

Merged
sebastianbenz merged 11 commits intoampproject:masterfrom
mdmower:pr-download
Apr 1, 2020
Merged

download-runtime: Add tool to download AMP runtime#663
sebastianbenz merged 11 commits intoampproject:masterfrom
mdmower:pr-download

Conversation

@mdmower
Copy link
Copy Markdown
Contributor

@mdmower mdmower commented Mar 29, 2020

Add tool to download a complete, compiled AMP runtime (and components). CLI and module included.

Supported CLI options:

  • --dest (optional string): Specify the destination directory where the AMP framework should be saved. Defaults to amp-runtime folder in current working directory.
  • --clear (optional boolean): Remove all contents from the destination directory before saving the AMP framework. Defaults to true.
  • --rtv (optional string): Specify the runtime version to download. Defaults to the latest production version available.
  • --host (optional string): Specify the URL where the AMP framework is hosted. Defaults to https://cdn.ampproject.org.

Fixes #642

Copy link
Copy Markdown
Contributor Author

@mdmower mdmower left a comment

Choose a reason for hiding this comment

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

@sebastianbenz I ran out of steam when it came to writing tests. Since I'm unsure how much time I'll have to put towards this tool during the coming week, I thought I'd post what is otherwise a finished tool in case you'd like to review implementation.

EDIT: I found some time to add tests. I'm happy to accept reviews when you (and others!) have time.

"dependencies": {
"@ampproject/toolbox-cache-list": "^2.0.0",
"@ampproject/toolbox-cache-url": "^2.0.0",
"@ampproject/toolbox-download-framework": "file:../../packages/download-framework",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TODO: Revise this to "^2.00" once testing is complete.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You should be able to set this to 2.0.0 as lerna takes care of managing local dependencies.

@mdmower mdmower changed the title [WIP] download-framework: Add tool to download AMP framework download-framework: Add tool to download AMP framework Mar 30, 2020
@mdmower mdmower marked this pull request as ready for review March 30, 2020 06:04
expect(ret.dest).toBe(options.dest);
expect(ret.rtv).toBe(defaultRtv);
done();
});
Copy link
Copy Markdown
Contributor Author

@mdmower mdmower Mar 30, 2020

Choose a reason for hiding this comment

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

Should also readdir and expect all framework files made it to the destination dir.

@@ -0,0 +1,160 @@
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nit: fix year

Copy link
Copy Markdown
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks a lot! Left a few suggestions, mostly around naming.

switch (command) {
case 'curls':
return require('./cmds/curls')(args, this.logger_);
case 'download-framework':
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion: rename to simply download

"dependencies": {
"@ampproject/toolbox-cache-list": "^2.0.0",
"@ampproject/toolbox-cache-url": "^2.0.0",
"@ampproject/toolbox-download-framework": "file:../../packages/download-framework",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You should be able to set this to 2.0.0 as lerna takes care of managing local dependencies.

README.md Outdated
- **[amp-cache-list](./packages/cache-list):** a javascript library for listing the known AMP Caches.
- **[amp-cli](./packages/cli):** a command line version of AMP Toolbox
- **[amp-cors](./packages/cors):** a connect/express middleware to automatically add [AMP Cors headers](https://www.ampproject.org/docs/fundamentals/amp-cors-requests).
- **[amp-download-framework](./packages/download-framework):** a javascript library for downloading the AMP framework.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Discussion: I'd prefer to rename this to runtime-download. Personally, I find framework a bit confusing in this context.

const downloadFrameworkProvider = require('@ampproject/toolbox-download-framework');

async function downloadFramework(args, _) {
const {ampUrlPrefix, clear, dest, rtv} = args;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We need to check if dest has been provided. Would it make sense to use rtv/$RTV_VERSION/ as the default value for dest? This way it'd be easy to replicate the cache fs structure.

const filesTxtUrl = frameworkBaseUrl + FRAMEWORK_FILES_TXT;

ret.url = frameworkBaseUrl;
log.info('AMP framework base URL: ' + frameworkBaseUrl);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove framework from log string

throw new Error(`Expected ${FRAMEWORK_FILES_TXT} in file listing, but it was not found.`);
}
} catch (ex) {
ret.error = 'Unable to read AMP framework files listing\n' + ex.message;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove framework from log string

// Create all subdirectories in destination directory
this.createSubdirectories_(files, dest);

log.info('Downloading AMP framework...');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

s/framework/runtime/

await Promise.all(fetchAndSavePromises)
.then(() => {
ret.status = true;
log.info('AMP framework download complete: ' + dest);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove framework from log string

log.info('AMP framework download complete: ' + dest);
})
.catch((error) => {
ret.error = 'Failed to download AMP framework\n' + error.message;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

s/framework/runtime

mdmower added 2 commits March 30, 2020 19:24
* framework --> runtime
* Copyright year
* Random tmp dir for testing
* Reformat for prettier
* cli/package.json dependency
* CLI command 'download'
* Download to rtv-specific paths
@mdmower
Copy link
Copy Markdown
Contributor Author

mdmower commented Mar 31, 2020

Tool tested on Windows successfully:

PS C:\Users\mdmower\source\amp-toolbox> node .\packages\cli\bin\amp download  --dest="C:\Users\mdmower\source\testx"
AMP Download Runtime Creating destination directory: C:\Users\mdmower\source\testx
AMP Download Runtime RTV: 012003261442330
AMP Download Runtime URL: https://cdn.ampproject.org/rtv/012003261442330/
AMP Download Runtime File count: 1151
AMP Download Runtime Clearing destination directory
AMP Download Runtime Downloading...
AMP Download Runtime Download complete: C:\Users\mdmower\source\testx\rtv\012003261442330

and unit tests (for this tool) pass as well:

 PASS  packages/download-runtime/spec/lib/DownloadRuntimeSpec.js
  ● Console

    console.log packages/core/lib/Log.js:74
      AMP Download Runtime RTV: 012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime URL: https://cdn.ampproject.org/rtv/012003261442330/
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime File count: 4
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Clearing destination directory
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Downloading...
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Download complete: C:\Users\mdmower\AppData\Local\Temp\amp-download-2NZxyl\rtv\012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime RTV: 012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime URL: https://example.com/amp/rtv/012003261442330/
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime File count: 4
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Clearing destination directory
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Downloading...
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Download complete: C:\Users\mdmower\AppData\Local\Temp\amp-download-FslBAT\rtv\012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime RTV: 123
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime URL: https://cdn.ampproject.org/rtv/123/
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime File count: 4
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Clearing destination directory
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Downloading...
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Download complete: C:\Users\mdmower\AppData\Local\Temp\amp-download-Atnhrv\rtv\123
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime RTV: 012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime URL: https://cdn.ampproject.org/rtv/012003261442330/
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime File count: 4
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Downloading...
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Download complete: C:\Users\mdmower\AppData\Local\Temp\amp-download-FLzGRX\rtv\012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime RTV: 012003261442330
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime URL: https://cdn.ampproject.org/rtv/012003261442330/
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime File count: 4
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Clearing destination directory
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Downloading...
    console.log packages/core/lib/Log.js:74
      AMP Download Runtime Download complete: C:\Users\mdmower\AppData\Local\Temp\amp-download-feePYq\rtv\012003261442330

@sebastianbenz
Copy link
Copy Markdown
Collaborator

Great - thanks! Can you take a look at the travis build? Looks like Node 10 and 11 fails.

@mdmower
Copy link
Copy Markdown
Contributor Author

mdmower commented Mar 31, 2020

Oh, I see. Recursive rmdir not available until node 12. Will fix.

@mdmower mdmower requested a review from sebastianbenz March 31, 2020 16:41
@mdmower mdmower changed the title download-framework: Add tool to download AMP framework download-runtime: Add tool to download AMP runtime Mar 31, 2020
"cross-fetch": "3.0.4",
"fetch-mock": "9.2.1"
"fetch-mock": "9.2.1",
"fs-extra": "^9.0.0"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please pin the version

"@ampproject/toolbox-runtime-version": "^2.0.0",
"cross-fetch": "3.0.4",
"fetch-mock": "9.2.1"
"fetch-mock": "9.2.1",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please remove. This should already be included as a dependency (in the root package).

'--dest (required).......... Destination directory',
'--clear (optional)......... Clear destination directory before download (default: true)',
'--rtv (optional)........... Runtime version to download (default: latest available)',
'--ampUrlPrefix (optional).. AMP host (default: https://cdn.ampproject.org)',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should we rename this to host?

@@ -0,0 +1,36 @@
{
"name": "@ampproject/toolbox-download-runtime",
"version": "2.0.0",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this needs to be 2.0.1 for lerna to pick this up as a local dependency.

Copy link
Copy Markdown
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

Left a few more comments. Only nits.

One thing: what I meant with making dest default to rtv/$RTV was that we use this as the default value, but allow full customization (without rtv prefix). Thinking about this I like your approach more, but I'm not sure whether this would work for everyone. Wdyt?

mdmower added 2 commits March 31, 2020 18:04
- Support `amp download` without any options to CLI tool. Defaults to
  `amp-runtime` folder within current working directory as destination.
- Remove redundant package specification in CLI package.json
- Set download-runtime version to 2.0.1
- Update CLI readme
- Fix 'supports disabling destination dir clearing' test
- Rename --ampUrlPrefix flag to --host for CLI tool
@mdmower
Copy link
Copy Markdown
Contributor Author

mdmower commented Apr 1, 2020

Left a few more comments. Only nits.

One thing: what I meant with making dest default to rtv/$RTV was that we use this as the default value, but allow full customization (without rtv prefix). Thinking about this I like your approach more, but I'm not sure whether this would work for everyone. Wdyt?

I like the idea of downloading to rtv/<rtv>/ paths. The CLI outputs the final directory to the terminal and DownloadRuntime.getRuntime() returns the final directory as one of its properties (ret.dest), so I think developers have enough information at their fingertips to know where to look for the downloaded runtime.

I took your suggestion to make --dest an optional parameter for the CLI tool. If not specified, it defaults to amp-runtime under the current working directory. So, it's now possible to download a runtime with just amp download... pretty slick.

Other minor updates are included in latest commit as well. Please let me know if I've left anything unaddressed and thank you much for all the reviews!

Copy link
Copy Markdown
Collaborator

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

Love it! Thanks a lot!

@sebastianbenz sebastianbenz merged commit 8f98ccb into ampproject:master Apr 1, 2020
@mdmower
Copy link
Copy Markdown
Contributor Author

mdmower commented Apr 1, 2020

@sebastianbenz a couple docs need some minor tweaks since you removed amp-runtime from the default download path, and path.join(process.cwd(), '') can be reduced to process.cwd().

@mdmower mdmower deleted the pr-download branch October 9, 2022 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cli] Provide a command for downloading a specific AMP release

3 participants