Skip to content

Commit f12a9e6

Browse files
authored
feat(assets): can specify --protected flag for asset uploads (#288)
Ticks off one feature for #281
1 parent bb3ce51 commit f12a9e6

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

packages/plugin-assets/src/commands/assets/upload.js

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class UploadCommand extends TwilioClientCommand {
88
await super.run();
99

1010
try {
11+
const visibility = this.flags.protected ? 'protected' : 'public';
1112
const pluginConfig = getPluginConfig(this);
1213
const assets = await upload({
1314
apiKey: this.currentProfile.apiKey,
@@ -16,6 +17,7 @@ class UploadCommand extends TwilioClientCommand {
1617
pluginConfig: pluginConfig,
1718
file: this.args.file,
1819
logger: this.logger,
20+
visibility,
1921
});
2022
this.output(assets, this.flags.properties);
2123
} catch (error) {
@@ -35,6 +37,10 @@ UploadCommand.args = [
3537
];
3638

3739
UploadCommand.flags = {
40+
protected: flags.boolean({
41+
default: false,
42+
description: "Sets the uploaded asset's visibility to 'protected'",
43+
}),
3844
properties: flags.string({
3945
default: 'sid, path, url, visibility',
4046
description:

packages/plugin-assets/src/list.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ async function list({ pluginConfig, apiKey, apiSecret, accountSid, logger }) {
3434
try {
3535
logger.debug(`Fetching build with sid ${environment.build_sid}`);
3636
const build = await getBuild(environment.build_sid, serviceSid, client);
37-
const assets = build.asset_versions.map(assetVersion => {
38-
if (assetVersion.visibility === 'public') {
37+
const assets = build.asset_versions.map((assetVersion) => {
38+
if (
39+
assetVersion.visibility === 'public' ||
40+
assetVersion.visibility === 'protected'
41+
) {
3942
assetVersion.url = `https://${environment.domain_name}${assetVersion.path}`;
43+
} else {
44+
assetVersion.url = '';
4045
}
4146
return assetVersion;
4247
});

packages/plugin-assets/src/upload.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,20 @@ ${debugFlagMessage}`,
8686
}
8787
}
8888

89-
async function prepareAsset(file) {
89+
async function prepareAsset(file, options = {}) {
9090
let filePath;
9191
spinner.text = 'Preparing asset';
9292
try {
9393
filePath = resolve(file);
9494
debug(`Reading file from ${filePath}`);
9595
const assetContent = await readFile(filePath);
9696
const path = `${encodeURIComponent(basename(filePath))}`;
97+
const access = options.access || 'public';
9798
const newAsset = {
99+
content: assetContent,
98100
name: path,
99-
access: 'public',
100101
path,
101-
content: assetContent,
102+
access,
102103
};
103104
return newAsset;
104105
} catch (error) {
@@ -149,7 +150,7 @@ ${debugFlagMessage}`,
149150
]);
150151
let newName = newNameAnswers.newName.trim();
151152
while (
152-
existingAssets.find(asset => asset.friendly_name === newName) ||
153+
existingAssets.find((asset) => asset.friendly_name === newName) ||
153154
newName === ''
154155
) {
155156
const message =
@@ -244,7 +245,7 @@ ${debugFlagMessage}`,
244245
async function waitForBuild(buildSid, serviceSid, client) {
245246
try {
246247
const updateHandler = new EventEmitter();
247-
updateHandler.on('status-update', update => debug(update.message));
248+
updateHandler.on('status-update', (update) => debug(update.message));
248249
await waitForSuccessfulBuild(buildSid, serviceSid, client, updateHandler);
249250
} catch (error) {
250251
handleError(
@@ -298,6 +299,7 @@ async function upload({
298299
accountSid,
299300
file,
300301
logger,
302+
visibility,
301303
}) {
302304
const spinner = ora({
303305
isSilent: logger.config.level > 0,
@@ -337,10 +339,10 @@ async function upload({
337339
accountSid
338340
);
339341
await checkServiceForFunctions(client, serviceSid);
340-
let newAsset = await prepareAsset(file);
342+
let newAsset = await prepareAsset(file, { access: visibility });
341343
const existingAssets = await getExistingAssets(client, serviceSid);
342344
const existingAsset = existingAssets.find(
343-
asset => asset.friendly_name === newAsset.name
345+
(asset) => asset.friendly_name === newAsset.name
344346
);
345347
if (existingAsset) {
346348
newAsset = await overwriteRenameOrAbort(
@@ -367,8 +369,8 @@ async function upload({
367369
client
368370
);
369371
existingAssetVersions
370-
.filter(av => av.asset_sid !== assetVersion.asset_sid)
371-
.forEach(assetVersion => assetVersions.push(assetVersion.sid));
372+
.filter((av) => av.asset_sid !== assetVersion.asset_sid)
373+
.forEach((assetVersion) => assetVersions.push(assetVersion.sid));
372374
}
373375
const build = await triggerNewBuild(
374376
Array.from(assetVersions),

0 commit comments

Comments
 (0)