Skip to content

Commit

Permalink
Merge pull request #251 from quantcdn/feat/command-publish
Browse files Browse the repository at this point in the history
Feat: Add publish command.
  • Loading branch information
steveworley authored Mar 21, 2024
2 parents 18ec5ca + b7d7f8f commit 73f4cf9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Commands:
quant page <file> <location> Make a local page asset available via Quant
quant proxy <path> <origin> [status] Create a proxy to allow traffic directly to origin
[basicAuthUser] [basicAuthPass]
quant publish <path> Publish an asset
quant purge <path> Purge the cache for a given url
quant redirect <from> <to> [status] [author] Create a redirect
quant search <index|unindex|clear> Perform search index operations
Expand Down
51 changes: 51 additions & 0 deletions src/commands/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Unpublish a QuantCDN url.
*
* @usage
* quant publish <path>
*/
const chalk = require('chalk');
const client = require('../quant-client');
const config = require('../config');

const command = {};

command.command = 'publish <path>';
command.describe = 'Publish an asset';
command.builder = (yargs) => {
yargs.options('revision', {
describe: 'The revision id to publish',
alias: 'r',
type: 'string',
default: 'latest',
});
};

command.handler = function(argv) {
console.log(chalk.bold.green('*** Quant publish ***'));

// config.fromArgs(argv);
if (!config.fromArgs(argv)) {
return console.error(chalk.yellow('Quant is not configured, run init.'));
}

const _client = client(config);

if (argv.revision == 'latest') {
_client.revisions(argv.path)
.then((res) => {
const revisionIds = Object.keys(res.revisions);
const latestRevision = Math.max(...revisionIds);
_client.publish(argv.path, latestRevision)
.then((res) => console.log(chalk.green('Success:') + ` Published successfully`)) // eslint-disable-line
.catch((err) => console.log(chalk.red.bold('Error:') + ` ${err}`));
})
.catch((err) => console.log(chalk.red.bold('Error:') + ` ${err}`));
} else {
_client.publish(argv.path, argv.revision)
.then((res) => console.log(chalk.green('Success:') + ` Published successfully`)) // eslint-disable-line
.catch((err) => console.log(chalk.red.bold('Error:') + ` ${err}`));
}
};

module.exports = command;
2 changes: 1 addition & 1 deletion src/commands/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ command.handler = async function(argv) {
}

try {
revision = await quant.revisions(filepath);
revision = await quant.revision(filepath);
} catch (err) {}

if (!revision) {
Expand Down
32 changes: 31 additions & 1 deletion src/quant-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ const client = function(config) {
*
* @throws Error.
*/
revisions: async function(url, revision = false) {
revision: async function(url, revision = false) {
const path = revision ? revision : 'published';

url = url.indexOf('/') == 0 ? url : `/${url}`;
Expand All @@ -532,6 +532,36 @@ const client = function(config) {
return handleResponse(res);
},

/**
* Get the revision history from Quant.
*
* @param {string} url
* The URL path to get revisions for.
* @param {string|bool} revision
* Retrieve a specific revision.
*
* @return {object}
* The response.
*
* @throws Error.
*/
revisions: async function(url) {
url = url.indexOf('/') == 0 ? url : `/${url}`;
url = url.toLowerCase();
url = url.replace(/\/?index\.html/, '');

const options = {
url: `${config.get('endpoint')}/revisions`,
headers: {
...headers,
'Quant-Url': url,
},
json: true,
};
const res = await get(options);
return handleResponse(res);
},

/**
* Purge URL patterns from Quants Varnish.
*
Expand Down

0 comments on commit 73f4cf9

Please sign in to comment.