Skip to content
Merged
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
86 changes: 3 additions & 83 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import type {UploadFileStorage} from '@junobuild/cli-tools';
import {
deploy as cliDeploy,
postDeploy as cliPostDeploy,
preDeploy as cliPreDeploy,
hasArgs
} from '@junobuild/cli-tools';
import {uploadBlob, type Asset} from '@junobuild/core';
import {red} from 'kleur';
import {lstatSync} from 'node:fs';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {hasArgs} from '@junobuild/cli-tools';
import {junoConfigExist} from '../configs/juno.config';
import {clear} from '../services/clear.services';
import {assertSatelliteMemorySize, listAssets} from '../services/deploy.services';
import {executeDeploy} from '../services/deploy/deploy.execute.services';
import {links} from '../services/links.services';
import {configEnv} from '../utils/config.utils';
import {satelliteParameters} from '../utils/satellite.utils';
import {init} from './init';

export const deploy = async (args?: string[]) => {
Expand All @@ -29,72 +18,3 @@ export const deploy = async (args?: string[]) => {

await links(args);
};

const executeDeploy = async (args?: string[]) => {
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

const listExistingAssets = async ({startAfter}: {startAfter?: string}): Promise<Asset[]> =>
await listAssets({
startAfter,
env: {
env,
satellite: satelliteConfig
}
});

const assertMemory = async () => {
await assertSatelliteMemorySize(args);
};

const satellite = await satelliteParameters({satellite: satelliteConfig, env});

const uploadFile = async ({
filename,
fullPath,
data,
collection,
headers,
encoding
}: UploadFileStorage) => {
await uploadBlob({
satellite,
filename,
fullPath,
// @ts-expect-error type incompatibility NodeJS vs bundle
data,
collection,
headers,
encoding
});
};

await cliPreDeploy({config: satelliteConfig});

const {result} = await cliDeploy({
config: satelliteConfig,
listAssets: listExistingAssets,
assertSourceDirExists,
assertMemory,
uploadFile
});

if (result === 'skipped') {
process.exit(0);
}

await cliPostDeploy({config: satelliteConfig});
};

const assertSourceDirExists = (source: string) => {
try {
lstatSync(source);
} catch (_err: unknown) {
console.log(
`${red(
'Cannot proceed deployment.'
)}\nAre you sure the folder containing your built app (the "source" tag in the configuration file for Juno) files is correctly configured, or have you built your app?`
);
process.exit(1);
}
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import {satelliteMemorySize, satelliteVersion} from '@junobuild/admin';
import type {Asset} from '@junobuild/core';
import {listAssets as listAssetsLib} from '@junobuild/core';
import {yellow} from 'kleur';
import {compare} from 'semver';
import {readJunoConfig} from '../configs/juno.config';
import {DAPP_COLLECTION} from '../constants/constants';
import {
DEPLOY_LIST_ASSETS_PAGINATION,
MEMORY_HEAP_WARNING,
MEMORY_SIZE_ENDPOINT_VERSION
} from '../constants/deploy.constants';
import type {SatelliteConfigEnv} from '../types/config';
import {configEnv} from '../utils/config.utils';
import {NEW_CMD_LINE, confirmAndExit} from '../utils/prompt.utils';
import {satelliteParameters} from '../utils/satellite.utils';
import {readJunoConfig} from '../../configs/juno.config';
import {MEMORY_HEAP_WARNING, MEMORY_SIZE_ENDPOINT_VERSION} from '../../constants/deploy.constants';
import {configEnv} from '../../utils/config.utils';
import {NEW_CMD_LINE, confirmAndExit} from '../../utils/prompt.utils';
import {satelliteParameters} from '../../utils/satellite.utils';

export const assertSatelliteMemorySize = async (args?: string[]) => {
const env = configEnv(args);
Expand Down Expand Up @@ -64,41 +56,3 @@ export const assertSatelliteMemorySize = async (args?: string[]) => {
)} MB. Are you sure you want to proceed with the deployment?`
);
};

export const listAssets = async ({
startAfter,
env
}: {
startAfter?: string;
env: SatelliteConfigEnv;
}): Promise<Asset[]> => {
const {assets, items_page, matches_pages} = await listAssetsLib({
collection: DAPP_COLLECTION,
satellite: await satelliteParameters(env),
filter: {
order: {
desc: true,
field: 'keys'
},
paginate: {
startAfter,
limit: DEPLOY_LIST_ASSETS_PAGINATION
}
}
});

const last = <T>(elements: T[]): T | undefined => {
const {length, [length - 1]: last} = elements;
return last;
};

if ((items_page ?? 0n) < (matches_pages ?? 0n)) {
const nextAssets = await listAssets({
startAfter: last(assets)?.fullPath,
env
});
return [...assets, ...nextAssets];
}

return assets;
};
44 changes: 44 additions & 0 deletions src/services/deploy/deploy.assets.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type {Asset} from '@junobuild/core';
import {listAssets as listAssetsLib} from '@junobuild/core';
import {DAPP_COLLECTION} from '../../constants/constants';
import {DEPLOY_LIST_ASSETS_PAGINATION} from '../../constants/deploy.constants';
import type {SatelliteConfigEnv} from '../../types/config';
import {satelliteParameters} from '../../utils/satellite.utils';

export const listAssets = async ({
startAfter,
env
}: {
startAfter?: string;
env: SatelliteConfigEnv;
}): Promise<Asset[]> => {
const {assets, items_page, matches_pages} = await listAssetsLib({
collection: DAPP_COLLECTION,
satellite: await satelliteParameters(env),
filter: {
order: {
desc: true,
field: 'keys'
},
paginate: {
startAfter,
limit: DEPLOY_LIST_ASSETS_PAGINATION
}
}
});

const last = <T>(elements: T[]): T | undefined => {
const {length, [length - 1]: last} = elements;
return last;
};

if ((items_page ?? 0n) < (matches_pages ?? 0n)) {
const nextAssets = await listAssets({
startAfter: last(assets)?.fullPath,
env
});
return [...assets, ...nextAssets];
}

return assets;
};
83 changes: 83 additions & 0 deletions src/services/deploy/deploy.execute.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type {UploadFileStorage} from '@junobuild/cli-tools';
import {
deploy as cliDeploy,
postDeploy as cliPostDeploy,
preDeploy as cliPreDeploy
} from '@junobuild/cli-tools';
import {type Asset, uploadBlob} from '@junobuild/core';
import {red} from 'kleur';
import {lstatSync} from 'node:fs';
import {readJunoConfig} from '../../configs/juno.config';
import {configEnv} from '../../utils/config.utils';
import {satelliteParameters} from '../../utils/satellite.utils';
import {assertSatelliteMemorySize} from './deploy.assert.services';
import {listAssets} from './deploy.assets.services';

export const executeDeploy = async (args?: string[]) => {
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

const listExistingAssets = async ({startAfter}: {startAfter?: string}): Promise<Asset[]> =>
await listAssets({
startAfter,
env: {
env,
satellite: satelliteConfig
}
});

const assertMemory = async () => {
await assertSatelliteMemorySize(args);
};

const satellite = await satelliteParameters({satellite: satelliteConfig, env});

const uploadFile = async ({
filename,
fullPath,
data,
collection,
headers,
encoding
}: UploadFileStorage) => {
await uploadBlob({
satellite,
filename,
fullPath,
// @ts-expect-error type incompatibility NodeJS vs bundle
data,
collection,
headers,
encoding
});
};

await cliPreDeploy({config: satelliteConfig});

const {result} = await cliDeploy({
config: satelliteConfig,
listAssets: listExistingAssets,
assertSourceDirExists,
assertMemory,
uploadFile
});

if (result === 'skipped') {
process.exit(0);
}

await cliPostDeploy({config: satelliteConfig});
};

const assertSourceDirExists = (source: string) => {
try {
lstatSync(source);
} catch (_err: unknown) {
console.log(
`${red(
'Cannot proceed deployment.'
)}\nAre you sure the folder containing your built app (the "source" tag in the configuration file for Juno) files is correctly configured, or have you built your app?`
);
process.exit(1);
}
};