Skip to content

Commit

Permalink
feat(assets): can specify --protected flag for asset uploads (#288)
Browse files Browse the repository at this point in the history
Ticks off one feature for #281
  • Loading branch information
philnash committed Jun 25, 2021
1 parent bb3ce51 commit f12a9e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
6 changes: 6 additions & 0 deletions packages/plugin-assets/src/commands/assets/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class UploadCommand extends TwilioClientCommand {
await super.run();

try {
const visibility = this.flags.protected ? 'protected' : 'public';
const pluginConfig = getPluginConfig(this);
const assets = await upload({
apiKey: this.currentProfile.apiKey,
Expand All @@ -16,6 +17,7 @@ class UploadCommand extends TwilioClientCommand {
pluginConfig: pluginConfig,
file: this.args.file,
logger: this.logger,
visibility,
});
this.output(assets, this.flags.properties);
} catch (error) {
Expand All @@ -35,6 +37,10 @@ UploadCommand.args = [
];

UploadCommand.flags = {
protected: flags.boolean({
default: false,
description: "Sets the uploaded asset's visibility to 'protected'",
}),
properties: flags.string({
default: 'sid, path, url, visibility',
description:
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-assets/src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ async function list({ pluginConfig, apiKey, apiSecret, accountSid, logger }) {
try {
logger.debug(`Fetching build with sid ${environment.build_sid}`);
const build = await getBuild(environment.build_sid, serviceSid, client);
const assets = build.asset_versions.map(assetVersion => {
if (assetVersion.visibility === 'public') {
const assets = build.asset_versions.map((assetVersion) => {
if (
assetVersion.visibility === 'public' ||
assetVersion.visibility === 'protected'
) {
assetVersion.url = `https://${environment.domain_name}${assetVersion.path}`;
} else {
assetVersion.url = '';
}
return assetVersion;
});
Expand Down
20 changes: 11 additions & 9 deletions packages/plugin-assets/src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,20 @@ ${debugFlagMessage}`,
}
}

async function prepareAsset(file) {
async function prepareAsset(file, options = {}) {
let filePath;
spinner.text = 'Preparing asset';
try {
filePath = resolve(file);
debug(`Reading file from ${filePath}`);
const assetContent = await readFile(filePath);
const path = `${encodeURIComponent(basename(filePath))}`;
const access = options.access || 'public';
const newAsset = {
content: assetContent,
name: path,
access: 'public',
path,
content: assetContent,
access,
};
return newAsset;
} catch (error) {
Expand Down Expand Up @@ -149,7 +150,7 @@ ${debugFlagMessage}`,
]);
let newName = newNameAnswers.newName.trim();
while (
existingAssets.find(asset => asset.friendly_name === newName) ||
existingAssets.find((asset) => asset.friendly_name === newName) ||
newName === ''
) {
const message =
Expand Down Expand Up @@ -244,7 +245,7 @@ ${debugFlagMessage}`,
async function waitForBuild(buildSid, serviceSid, client) {
try {
const updateHandler = new EventEmitter();
updateHandler.on('status-update', update => debug(update.message));
updateHandler.on('status-update', (update) => debug(update.message));
await waitForSuccessfulBuild(buildSid, serviceSid, client, updateHandler);
} catch (error) {
handleError(
Expand Down Expand Up @@ -298,6 +299,7 @@ async function upload({
accountSid,
file,
logger,
visibility,
}) {
const spinner = ora({
isSilent: logger.config.level > 0,
Expand Down Expand Up @@ -337,10 +339,10 @@ async function upload({
accountSid
);
await checkServiceForFunctions(client, serviceSid);
let newAsset = await prepareAsset(file);
let newAsset = await prepareAsset(file, { access: visibility });
const existingAssets = await getExistingAssets(client, serviceSid);
const existingAsset = existingAssets.find(
asset => asset.friendly_name === newAsset.name
(asset) => asset.friendly_name === newAsset.name
);
if (existingAsset) {
newAsset = await overwriteRenameOrAbort(
Expand All @@ -367,8 +369,8 @@ async function upload({
client
);
existingAssetVersions
.filter(av => av.asset_sid !== assetVersion.asset_sid)
.forEach(assetVersion => assetVersions.push(assetVersion.sid));
.filter((av) => av.asset_sid !== assetVersion.asset_sid)
.forEach((assetVersion) => assetVersions.push(assetVersion.sid));
}
const build = await triggerNewBuild(
Array.from(assetVersions),
Expand Down

0 comments on commit f12a9e6

Please sign in to comment.