@ampproject/toolbox-cli
Advanced tools
Comparing version 2.0.1 to 2.2.0
@@ -25,7 +25,6 @@ /** | ||
const args = process.argv.slice(2); | ||
cli.run(args) | ||
.catch((err) => { | ||
log.error(err.message); | ||
process.exit(1); | ||
}); | ||
cli.run(args).catch((err) => { | ||
log.error(err.message); | ||
process.exit(1); | ||
}); | ||
}; |
@@ -19,2 +19,3 @@ /** | ||
const buildOptions = require('minimist-options'); | ||
const minimist = require('minimist'); | ||
@@ -24,3 +25,3 @@ const {log} = require('@ampproject/toolbox-core'); | ||
class Cli { | ||
constructor(logger=log) { | ||
constructor(logger = log) { | ||
this.logger_ = logger; | ||
@@ -30,10 +31,50 @@ } | ||
run(argv) { | ||
const args = minimist(argv); | ||
const command = args._[0] || 'help'; | ||
// First pass of minimist is just used to identify command | ||
const command = minimist(argv)._[0] || 'help'; | ||
// Customize minimist options based on command | ||
const minimistOptions = {}; | ||
switch (command) { | ||
case 'download': | ||
Object.assign( | ||
minimistOptions, | ||
buildOptions({ | ||
clear: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
rtv: { | ||
type: 'string', | ||
}, | ||
}) | ||
); | ||
break; | ||
case 'runtime-version': | ||
Object.assign( | ||
minimistOptions, | ||
buildOptions({ | ||
canary: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}) | ||
); | ||
break; | ||
default: | ||
break; | ||
} | ||
// Re-run minimist with param option handling | ||
const args = minimist(argv, minimistOptions); | ||
// Execute command with arguments | ||
switch (command) { | ||
case 'curls': | ||
return require('./cmds/curls')(args, this.logger_); | ||
case 'download': | ||
return require('./cmds/downloadRuntime')(args, this.logger_); | ||
case 'help': | ||
return require('./cmds/help')(args, this.logger_); | ||
case 'lint': | ||
return require('./cmds/lint')(argv, this.logger_); | ||
case 'optimize': | ||
@@ -46,4 +87,2 @@ const OptimizeCmd = require('./cmds/optimize.js'); | ||
return require('./cmds/updateCache')(args, this.logger_); | ||
case 'lint': | ||
return require('./cmds/lint')(argv, this.logger_); | ||
case 'version': | ||
@@ -50,0 +89,0 @@ return require('./cmds/version')(args, this.logger_); |
@@ -21,3 +21,2 @@ /** | ||
const AmpCaches = require('@ampproject/toolbox-cache-list'); | ||
const caches = new AmpCaches(); | ||
@@ -30,7 +29,6 @@ async function curls(args, logger) { | ||
const cacheId = args.cache; | ||
const caches = new AmpCaches(args.fetch || require('node-fetch')); | ||
if (!cacheId) { | ||
const allCaches = await caches.list(); | ||
return Promise.all( | ||
allCaches.map((cache) => printCurl(cache, url, logger)), | ||
); | ||
return Promise.all(allCaches.map((cache) => printCurl(cache, url, logger))); | ||
} else { | ||
@@ -43,3 +41,3 @@ const cache = await caches.get(cacheId); | ||
} | ||
}; | ||
} | ||
@@ -46,0 +44,0 @@ async function printCurl(cache, url, logger) { |
@@ -22,10 +22,8 @@ /** | ||
function help(args, logger) { | ||
const subCmd = args._[0] === 'help' ? | ||
args._[1] : | ||
args._[0]; | ||
const subCmd = args._[0] === 'help' ? args._[1] : args._[0]; | ||
logger.info(menus[subCmd] || menus.main); | ||
return Promise.resolve(); | ||
}; | ||
} | ||
module.exports = help; |
@@ -7,2 +7,3 @@ module.exports = { | ||
'curls ..................... generate AMP cache URL(s)', | ||
'download................... download the AMP runtime', | ||
'help ...................... show this menu', | ||
@@ -23,18 +24,17 @@ 'lint ...................... checks document for errors', | ||
'Options:', | ||
'--privateKey .............. path to the private key file. Defaults to \'./privateKey.pem\'.', | ||
"--privateKey .............. path to the private key file. Defaults to './privateKey.pem'.", | ||
].join('\n'), | ||
'lint': [ | ||
'lint': ['Usage:', '', '', 'amp lint url', '', 'Examples:', ' $ amplint https://amp.dev/'].join( | ||
'\n' | ||
), | ||
'runtime-version': [ | ||
'Usage:', | ||
'', | ||
'', | ||
'amp lint url', | ||
'amp runtime-version', | ||
'', | ||
'Examples:', | ||
' $ amplint https://amp.dev/', | ||
].join('\n'), | ||
'runtime-version': [ | ||
'Usage:', | ||
'', | ||
'', | ||
'amp runtime-version', | ||
'Options:', | ||
'--canary .................. Get canary version. Defaults to false.', | ||
'--host .................... AMP host. Defaults to https://cdn.ampproject.org.', | ||
].join('\n'), | ||
@@ -51,8 +51,20 @@ 'curls': [ | ||
].join('\n'), | ||
'optimize': [ | ||
'optimize': ['Usage:', '', '', 'amp optimize [url|file]'].join('\n'), | ||
'download': [ | ||
'Usage:', | ||
'', | ||
'', | ||
'amp optimize [url|file]', | ||
'amp download <options>', | ||
'', | ||
'', | ||
'Options:', | ||
'--dest (optional).......... Path to download folder', | ||
' Default: current working directory', | ||
'--clear (optional)......... Clear destination directory before downloading', | ||
' Default: true', | ||
'--rtv (optional)........... Runtime version to download', | ||
' Default: latest available', | ||
'--host (optional).......... AMP host', | ||
' Default: https://cdn.ampproject.org', | ||
].join('\n'), | ||
}; |
@@ -23,3 +23,3 @@ /** | ||
class OptimizeCmd { | ||
constructor(optimizer=AmpOptimizer.create(), load=loadUrlOrFile) { | ||
constructor(optimizer = AmpOptimizer.create(), load = loadUrlOrFile) { | ||
this.optimizer_ = optimizer; | ||
@@ -33,6 +33,5 @@ this.load_ = load; | ||
logger.info(optimized); | ||
}; | ||
} | ||
} | ||
module.exports = OptimizeCmd; |
@@ -22,6 +22,10 @@ /** | ||
async function runtimeVersion(args, logger) { | ||
const version = await ampRuntimeVersionProvider.currentVersion(); | ||
const {canary, host} = args; | ||
const version = await ampRuntimeVersionProvider.currentVersion({ | ||
ampUrlPrefix: host, | ||
canary, | ||
}); | ||
logger.info(version); | ||
}; | ||
} | ||
module.exports = runtimeVersion; |
@@ -28,7 +28,6 @@ /** | ||
const updateCacheUrlProvider = UpdateCacheUrlProvider.create(privateKey); | ||
return updateCacheUrlProvider.calculateFromOriginUrl(url) | ||
.then((cacheUpdateUrls) => { | ||
cacheUpdateUrls.forEach((cacheUpdateUrl) => updateCache_(cacheUpdateUrl, logger)); | ||
return; | ||
}); | ||
return updateCacheUrlProvider.calculateFromOriginUrl(url).then((cacheUpdateUrls) => { | ||
cacheUpdateUrls.forEach((cacheUpdateUrl) => updateCache_(cacheUpdateUrl, logger)); | ||
return; | ||
}); | ||
} catch (e) { | ||
@@ -45,26 +44,25 @@ return Promise.reject(new Error(`Error generating cache invalidation URL: ${e}`)); | ||
fetch(cacheUpdateUrlInfo.updateCacheUrl) | ||
.then((response) => { | ||
if (response.status === 200) { | ||
logger.success(`${cacheUpdateUrlInfo.cacheName} Updated`); | ||
return; | ||
.then((response) => { | ||
if (response.status === 200) { | ||
logger.success(`${cacheUpdateUrlInfo.cacheName} Updated`); | ||
return; | ||
} | ||
return response.text().then((body) => { | ||
const match = errorRegex.exec(body); | ||
if (match) { | ||
logger.error( | ||
`Error Invalidating Cache URL. Received response code "${response.status}" ` + | ||
`with message: "${match[1]}"` | ||
); | ||
} else { | ||
logger.error( | ||
`Error Invalidating Cache URL. Received response code "${response.status}"` + | ||
'with an unknown error' | ||
); | ||
} | ||
return response.text() | ||
.then((body) => { | ||
const match = errorRegex.exec(body); | ||
if (match) { | ||
logger.error( | ||
`Error Invalidating Cache URL. Received response code "${response.status}" ` + | ||
`with message: "${match[1]}"`, | ||
); | ||
} else { | ||
logger.error( | ||
`Error Invalidating Cache URL. Received response code "${response.status}"` + | ||
'with an unknown error', | ||
); | ||
} | ||
}); | ||
}) | ||
.catch((e) => { | ||
logger.warn(`Error connecting to the AMP Cache with message: "${e.message}"`); | ||
}); | ||
}) | ||
.catch((e) => { | ||
logger.warn(`Error connecting to the AMP Cache with message: "${e.message}"`); | ||
}); | ||
} | ||
@@ -92,4 +90,4 @@ | ||
return updateCaches_(privateKey, canonicalUrl, logger); | ||
}; | ||
} | ||
module.exports = updateCache; |
@@ -24,4 +24,4 @@ /** | ||
return Promise.resolve(); | ||
}; | ||
} | ||
module.exports = version; |
{ | ||
"name": "@ampproject/toolbox-cli", | ||
"version": "2.0.1", | ||
"version": "2.2.0", | ||
"description": "A Command Line Interface (CLI) for amp-toolbox", | ||
@@ -30,12 +30,14 @@ "main": "index.js", | ||
"dependencies": { | ||
"@ampproject/toolbox-cache-list": "^2.0.0", | ||
"@ampproject/toolbox-cache-url": "^2.0.0", | ||
"@ampproject/toolbox-linter": "^2.0.1", | ||
"@ampproject/toolbox-optimizer": "^2.0.1", | ||
"@ampproject/toolbox-runtime-version": "^2.0.0", | ||
"@ampproject/toolbox-update-cache": "^2.0.0", | ||
"@ampproject/toolbox-cache-list": "^2.2.0", | ||
"@ampproject/toolbox-cache-url": "^2.2.0", | ||
"@ampproject/toolbox-linter": "^2.2.0", | ||
"@ampproject/toolbox-optimizer": "^2.2.0", | ||
"@ampproject/toolbox-runtime-fetch": "^2.2.0", | ||
"@ampproject/toolbox-runtime-version": "^2.2.0", | ||
"@ampproject/toolbox-update-cache": "^2.2.0", | ||
"minimist": "1.2.5", | ||
"minimist-options": "4.0.2", | ||
"node-fetch": "2.6.0" | ||
}, | ||
"gitHead": "2785e09433bb22977b61f153ed7694b68ca5151d" | ||
"gitHead": "e2bea7ac7d4cb6a57d196e124ee8a5f818123a02" | ||
} |
@@ -11,2 +11,3 @@ # AMP-Toolbox CLI | ||
- [help](#help): lists all commands | ||
- [download](#download): download the AMP runtime | ||
- [lint](#lint): checks document for errors | ||
@@ -48,16 +49,12 @@ - [optimize](#optimize): runs AMP Optimizer for a given URL or file | ||
### optimize | ||
### download | ||
Runs AMP Optimizer for the given file or URL: | ||
Download a complete AMP runtime: | ||
```shell | ||
$ amp optimize https://amp.dev | ||
$ amp download | ||
``` | ||
or | ||
Supports options for specifying a custom host, version, and destination directory. See `amp help download` for the complete list. | ||
```shell | ||
$ amp optimize file.html | ||
``` | ||
### lint | ||
@@ -71,2 +68,16 @@ | ||
### optimize | ||
Runs AMP Optimizer for the given file or URL: | ||
```shell | ||
$ amp optimize https://amp.dev | ||
``` | ||
or | ||
```shell | ||
$ amp optimize file.html | ||
``` | ||
### runtime-version | ||
@@ -81,2 +92,4 @@ | ||
Supports options for specifying a custom host or getting the canary version. See `amp help runtime-version` for the complete list. | ||
### update-cache | ||
@@ -83,0 +96,0 @@ |
493
117
22124
10
+ Addedminimist-options@4.0.2
+ Added@ampproject/toolbox-runtime-fetch@2.10.1(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedat-least-node@1.0.0(transitive)
+ Addedcaniuse-lite@1.0.30001702(transitive)
+ Addedfs-extra@9.1.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedis-plain-obj@1.1.0(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedminimist-options@4.0.2(transitive)
+ Addeduniversalify@2.0.1(transitive)
- Removedcaniuse-lite@1.0.30001701(transitive)