Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
715 changes: 97 additions & 618 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"@icp-sdk/core": "^5.0.0",
"@junobuild/admin": "^4.2.1",
"@junobuild/cdn": "^2.3.1",
"@junobuild/cli-tools": "^0.12.4",
"@junobuild/config": "^2.15.0",
"@junobuild/cli-tools": "file:../juno-js/packages/cli-tools",
"@junobuild/config": "file:../juno-js/packages/config",
"@junobuild/config-loader": "^0.4.9",
"@junobuild/core": "^5.2.2",
"@junobuild/functions-tools": "^0.5.3",
Expand Down
45 changes: 45 additions & 0 deletions src/commands/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {red} from 'kleur';
import {logHelpStorageClear} from '../help/storage.clear.help';
import {logHelpStorageDeploy} from '../help/storage.deploy.help';
import {logHelpStorage} from '../help/storage.help';
import {logHelpStoragePrune} from '../help/storage.prune.help';
import {clearStorage} from '../services/storage/clear.services';
import {deployStorage} from '../services/storage/deploy.services';
import {pruneStorage} from '../services/storage/prune.services';

export const storage = async (args?: string[]) => {
const [subCommand] = args ?? [];

switch (subCommand) {
case 'deploy':
await deployStorage(args);
break;
case 'clear':
await clearStorage(args);
break;
case 'prune':
await pruneStorage(args);
break;
default:
console.log(red('Unknown subcommand.'));
logHelpStorage(args);
}
};

export const helpStorage = (args?: string[]) => {
const [subCommand] = args ?? [];

switch (subCommand) {
case 'deploy':
logHelpStorageDeploy(args);
break;
case 'clear':
logHelpStorageClear(args);
break;
case 'prune':
logHelpStoragePrune(args);
break;
default:
logHelpStorage(args);
}
};
7 changes: 7 additions & 0 deletions src/constants/help.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export const HOSTING_CLEAR_DESCRIPTION =
export const HOSTING_PRUNE_DESCRIPTION =
'Remove stale frontend files from your satellite that are no longer in your build output.';

export const STORAGE_DESCRIPTION =
'Deploy and manage assets in your satellite storage collections.';
export const STORAGE_DEPLOY_DESCRIPTION = 'Deploy files to your satellite storage collections.';
export const STORAGE_CLEAR_DESCRIPTION = 'Remove assets from your satellite storage collections.';
export const STORAGE_PRUNE_DESCRIPTION =
'Remove stale assets from your satellite storage collections that are no longer in your local source.';

export const EMULATOR_START_DESCRIPTION = 'Start the emulator for local development.';
export const EMULATOR_WAIT_DESCRIPTION = 'Wait until the emulator is ready.';
export const EMULATOR_CLEAR_DESCRIPTION = 'Clear the local emulator state (volume and container).';
Expand Down
2 changes: 2 additions & 0 deletions src/help/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
START_DESCRIPTION,
STATUS_DESCRIPTION,
STOP_DESCRIPTION,
STORAGE_DESCRIPTION,
UPGRADE_DESCRIPTION,
VERSION_DESCRIPTION,
WHOAMI_DESCRIPTION
Expand Down Expand Up @@ -44,6 +45,7 @@ Commands:
${cyan('start')} ${START_DESCRIPTION}
${cyan('stop')} ${STOP_DESCRIPTION}
${cyan('status')} ${STATUS_DESCRIPTION}
${cyan('storage')} ${STORAGE_DESCRIPTION}
${cyan('upgrade')} ${UPGRADE_DESCRIPTION}
${cyan('version')} ${VERSION_DESCRIPTION}
${cyan('whoami')} ${WHOAMI_DESCRIPTION}
Expand Down
30 changes: 30 additions & 0 deletions src/help/storage.clear.help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {STORAGE_CLEAR_DESCRIPTION} from '../constants/help.constants';
import {helpOutput} from './common.help';
import {TITLE} from './help';

const usage = `Usage: ${green('juno')} ${cyan('storage')} ${magenta('clear')} ${yellow('[options]')}

Options:
${yellow('-c, --collection')} Specify the collection to clear a specific file from.
${yellow('-f, --fullPath')} Specify the full path of a specific file to clear.
${yellow('-m, --mode')} Choose which environment to use (production, staging, development).
${yellow('-h, --help')} Output usage information.`;

const doc = `${STORAGE_CLEAR_DESCRIPTION}

\`\`\`
${usage}
\`\`\`
`;

const help = `${TITLE}

${STORAGE_CLEAR_DESCRIPTION}

${usage}
`;

export const logHelpStorageClear = (args?: string[]) => {
console.log(helpOutput(args) === 'doc' ? doc : help);
};
30 changes: 30 additions & 0 deletions src/help/storage.deploy.help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {STORAGE_DEPLOY_DESCRIPTION} from '../constants/help.constants';
import {helpOutput} from './common.help';
import {TITLE} from './help';

const usage = `Usage: ${green('juno')} ${cyan('storage')} ${magenta('deploy')} ${yellow('[options]')}

Options:
${yellow('--batch')} Number of files to upload per batch.
${yellow('--prune')} Remove stale remote assets not present in local source after deploy.
${yellow('-m, --mode')} Choose which environment to use (production, staging, development).
${yellow('-h, --help')} Output usage information.`;

const doc = `${STORAGE_DEPLOY_DESCRIPTION}

\`\`\`
${usage}
\`\`\`
`;

const help = `${TITLE}

${STORAGE_DEPLOY_DESCRIPTION}

${usage}
`;

export const logHelpStorageDeploy = (args?: string[]) => {
console.log(helpOutput(args) === 'doc' ? doc : help);
};
34 changes: 34 additions & 0 deletions src/help/storage.help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {
STORAGE_CLEAR_DESCRIPTION,
STORAGE_DEPLOY_DESCRIPTION,
STORAGE_DESCRIPTION,
STORAGE_PRUNE_DESCRIPTION
} from '../constants/help.constants';
import {helpOutput} from './common.help';
import {TITLE} from './help';

const usage = `Usage: ${green('juno')} ${cyan('storage')} ${magenta('<subcommand>')} ${yellow('[options]')}

Subcommands:
${magenta('clear')} ${STORAGE_CLEAR_DESCRIPTION}
${magenta('deploy')} ${STORAGE_DEPLOY_DESCRIPTION}
${magenta('prune')} ${STORAGE_PRUNE_DESCRIPTION}`;

const doc = `${STORAGE_DESCRIPTION}

\`\`\`
${usage}
\`\`\`
`;

const help = `${TITLE}

${STORAGE_DESCRIPTION}

${usage}
`;

export const logHelpStorage = (args?: string[]) => {
console.log(helpOutput(args) === 'doc' ? doc : help);
};
28 changes: 28 additions & 0 deletions src/help/storage.prune.help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {cyan, green, magenta, yellow} from 'kleur';
import {STORAGE_PRUNE_DESCRIPTION} from '../constants/help.constants';
import {helpOutput} from './common.help';
import {TITLE} from './help';

const usage = `Usage: ${green('juno')} ${cyan('storage')} ${magenta('prune')} ${yellow('[options]')}

Options:
${yellow('-m, --mode')} Choose which environment to use (production, staging, development).
${yellow('-h, --help')} Output usage information.`;

const doc = `${STORAGE_PRUNE_DESCRIPTION}

\`\`\`
${usage}
\`\`\`
`;

const help = `${TITLE}

${STORAGE_PRUNE_DESCRIPTION}

${usage}
`;

export const logHelpStoragePrune = (args?: string[]) => {
console.log(helpOutput(args) === 'doc' ? doc : help);
};
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {helpHosting, hosting} from './commands/hosting';
import {open} from './commands/open';
import {helpRun, run as runCmd} from './commands/run';
import {helpSnapshot, snapshot} from './commands/snapshot';
import {helpStorage, storage} from './commands/storage';
import {startStop} from './commands/start-stop';
import {status} from './commands/status';
import {upgrade} from './commands/upgrade';
Expand Down Expand Up @@ -98,6 +99,9 @@ export const run = async () => {
case 'snapshot':
helpSnapshot(args);
break;
case 'storage':
helpStorage(args);
break;
case 'init':
helpInit(args);
break;
Expand Down Expand Up @@ -188,6 +192,9 @@ export const run = async () => {
case 'snapshot':
await snapshot(args);
break;
case 'storage':
await storage(args);
break;
case 'changes':
await changes(args);
break;
Expand Down
80 changes: 80 additions & 0 deletions src/services/storage/clear.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {isNullish} from '@dfinity/utils';
import {deleteAssets} from '@junobuild/admin';
import {hasArgs, nextArg} from '@junobuild/cli-tools';
import {deleteAsset} from '@junobuild/core';
import {cyan, green, yellow} from 'kleur';
import ora from 'ora';
import {noJunoConfig} from '../../configs/juno.config';
import {assertConfigAndLoadSatelliteContext} from '../../utils/juno.config.utils';
import {consoleNoConfigFound} from '../../utils/msg.utils';

export const clearStorage = async (args?: string[]) => {
if (await noJunoConfig()) {
consoleNoConfigFound();
return;
}

const {satellite, satelliteConfig} = await assertConfigAndLoadSatelliteContext();
const deployMappings = satelliteConfig.deploy;

if (isNullish(deployMappings) || deployMappings.length === 0) {
console.log(yellow('No storage deploy mappings found in configuration.'));
return;
}

if (hasArgs({args, options: ['-f', '--fullpath', '--fullPath']})) {
const file =
nextArg({args, option: '-f'}) ??
nextArg({args, option: '--fullpath'}) ??
nextArg({args, option: '--fullPath'});
const collection = nextArg({args, option: '-c'}) ?? nextArg({args, option: '--collection'});

if (isNullish(file) || isNullish(collection)) {
console.log(
`Provide both ${yellow('--collection')} and ${yellow('--fullPath')} to clear a specific file.`
);
return;
}

const spinner = ora(`Clearing ${file} from collection "${collection}"...`).start();

try {
await deleteAsset({
collection,
satellite,
fullPath: cleanFullPath(file)
});

console.log(`${green('✔')} ${file} cleared from "${collection}".`);
} finally {
spinner.stop();
}

return;
}

for (const {collection} of deployMappings) {
const spinner = ora(`Clearing collection "${collection}"...`).start();

try {
await deleteAssets({
collection,
satellite
});

spinner.stop();
console.log(`${green('✔')} Collection ${cyan(`"${collection}"`)} cleared.`);
} catch (err: unknown) {
spinner.stop();
throw err;
}
}

console.log('');
console.log(`${green('✔')} All storage collections cleared.`);
};

const cleanFullPath = (fullPath: string): string => {
const path = fullPath.replace(/\\/g, '/');
return `${path.startsWith('/') ? '' : '/'}${path}`;
};
Loading
Loading