Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for satelliteIds to config #95

Merged
merged 5 commits into from
Feb 17, 2024
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@dfinity/identity": "^0.20.2",
"@dfinity/principal": "^0.20.2",
"@junobuild/admin": "^0.0.45",
"@junobuild/config": "^0.0.1",
"@junobuild/config": "^0.0.1-next-2024-02-17",
"@junobuild/core-peer": "^0.0.10",
"@junobuild/utils": "^0.0.18",
"conf": "^12.0.0",
Expand Down
8 changes: 5 additions & 3 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {setConfig} from '@junobuild/admin';
import ora from 'ora';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {configEnv} from '../utils/config.utils';
import {satelliteParameters} from '../utils/satellite.utils';
import {init} from './init';
Expand All @@ -10,7 +10,9 @@ export const config = async (args?: string[]) => {
await init();
}

const {satelliteId, storage} = await readSatelliteConfig(configEnv(args));
const env = configEnv(args);
const {satellite} = await readJunoConfig(env);
const {storage} = satellite;

const spinner = ora(`Configuring...`).start();

Expand All @@ -24,7 +26,7 @@ export const config = async (args?: string[]) => {
iframe: storage?.iframe
}
},
satellite: satelliteParameters(satelliteId)
satellite: satelliteParameters({satellite, env})
});
} finally {
spinner.stop();
Expand Down
38 changes: 23 additions & 15 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {minimatch} from 'minimatch';
import {lstatSync} from 'node:fs';
import {readFile} from 'node:fs/promises';
import {basename, extname, join, relative} from 'node:path';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {COLLECTION_DAPP, DAPP_COLLECTION, UPLOAD_BATCH_SIZE} from '../constants/constants';
import {
DEPLOY_DEFAULT_ENCODING,
Expand All @@ -29,6 +29,7 @@ import {
import {clear} from '../services/clear.services';
import {assertSatelliteMemorySize} from '../services/deploy.services';
import {links} from '../services/links.services';
import type {SatelliteConfigEnv} from '../types/config';
import {hasArgs} from '../utils/args.utils';
import {gzipFiles} from '../utils/compress.utils';
import {configEnv} from '../utils/config.utils';
Expand Down Expand Up @@ -59,17 +60,26 @@ export const deploy = async (args?: string[]) => {
};

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

const {
satelliteId,
source = DEPLOY_DEFAULT_SOURCE,
ignore = DEPLOY_DEFAULT_IGNORE,
encoding = DEPLOY_DEFAULT_ENCODING,
gzip = DEPLOY_DEFAULT_GZIP
} = await readSatelliteConfig(configEnv(args));
} = satelliteConfig;

const sourceAbsolutePath = join(process.cwd(), source);

const sourceFiles = await listFiles({sourceAbsolutePath, satelliteId, ignore, encoding, gzip});
const sourceFiles = await listFiles({
sourceAbsolutePath,
ignore,
encoding,
gzip,
satellite: satelliteConfig,
env
});

if (sourceFiles.length === 0) {
console.log('No new files to upload.');
Expand All @@ -78,7 +88,7 @@ const executeDeploy = async (args?: string[]) => {

await assertSatelliteMemorySize(args);

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

const fileDetailsPath = (file: FileDetails): string => file.alternateFile ?? file.file;

Expand Down Expand Up @@ -145,15 +155,14 @@ const assertSourceDirExists = (source: string) => {
const filterFilesToUpload = async ({
files,
sourceAbsolutePath,
satelliteId
...env
}: {
files: FileDetails[];
sourceAbsolutePath: string;
satelliteId: string;
}): Promise<FileDetails[]> => {
} & SatelliteConfigEnv): Promise<FileDetails[]> => {
const existingAssets = await listAssets({
collection: DAPP_COLLECTION,
satellite: satelliteParameters(satelliteId)
satellite: satelliteParameters(env)
});

const promises = files.map(
Expand Down Expand Up @@ -201,15 +210,14 @@ const fileNeedUpload = async ({

const listFiles = async ({
sourceAbsolutePath,
satelliteId,
ignore,
encoding,
gzip
gzip,
...env
}: {
sourceAbsolutePath: string;
} & Required<Pick<SatelliteConfig, 'satelliteId' | 'ignore' | 'encoding' | 'gzip'>>): Promise<
FileDetails[]
> => {
} & SatelliteConfigEnv &
Required<Pick<SatelliteConfig, 'ignore' | 'encoding' | 'gzip'>>): Promise<FileDetails[]> => {
assertSourceDirExists(sourceAbsolutePath);

const sourceFiles = listSourceFiles({sourceAbsolutePath, ignore});
Expand Down Expand Up @@ -282,6 +290,6 @@ const listFiles = async ({
return await filterFilesToUpload({
files: encodingFiles,
sourceAbsolutePath,
satelliteId
...env
});
};
10 changes: 7 additions & 3 deletions src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {listCustomDomains, type CustomDomain} from '@junobuild/admin';
import prompts from 'prompts';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {hasArgs, nextArg} from '../utils/args.utils';
import {configEnv} from '../utils/config.utils';
import {consoleUrl, defaultSatelliteDomain} from '../utils/domain.utils';
Expand All @@ -17,13 +17,17 @@ export const open = async (args?: string[]) => {

const browser = nextArg({args, option: '-b'}) ?? nextArg({args, option: '--browser'});

const {satelliteId} = await readSatelliteConfig(configEnv(args));
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

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

if (hasArgs({args, options: ['-c', '--console']})) {
await openUrl({url: consoleUrl(satelliteId), browser});
return;
}

const satellite = satelliteParameters(satelliteId);
const domains = await listCustomDomains({satellite});

if (domains.length === 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {cyan, green, red, yellow} from 'kleur';
import {clean, compare} from 'semver';
import {version as cliCurrentVersion} from '../../package.json';
import {getCliMissionControl, getCliOrbiters} from '../configs/cli.config';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {
MISSION_CONTROL_WASM_NAME,
ORBITER_WASM_NAME,
Expand Down Expand Up @@ -90,9 +90,11 @@ const satelliteVersion = async (args?: string[]) => {
return;
}

const {satelliteId} = await readSatelliteConfig(configEnv(args));
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

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

const currentVersion = await satelliteVersionLib({
satellite
Expand Down
20 changes: 5 additions & 15 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
JunoConfig,
JunoConfigEnv,
JunoConfigFnOrObject,
SatelliteConfig
} from '@junobuild/config';
import type {JunoConfig, JunoConfigEnv, JunoConfigFnOrObject} from '@junobuild/config';
import {nonNullish} from '@junobuild/utils';
import {existsSync} from 'node:fs';
import {access, readFile, writeFile} from 'node:fs/promises';
Expand All @@ -14,15 +9,15 @@ import {
} from '../constants/config.constants';
import {JUNO_CONFIG_FILENAME, JUNO_JSON} from '../constants/constants';
import {DEPLOY_DEFAULT_SOURCE} from '../constants/deploy.constants';
import type {ConfigType} from '../types/config';
import type {ConfigType, JunoConfigWithSatelliteId} from '../types/config';
import {readTemplateFile} from '../utils/fs.utils';
import {nodeRequire} from '../utils/node.utils';

export const saveJunoConfig = async ({
config,
configType
}: {
config: JunoConfig;
config: JunoConfigWithSatelliteId;
configType: ConfigType;
}): Promise<void> => {
await writeJunoConfig({
Expand All @@ -31,11 +26,6 @@ export const saveJunoConfig = async ({
});
};

export const readSatelliteConfig = async (env: JunoConfigEnv): Promise<SatelliteConfig> => {
const {satellite} = await readJunoConfig(env);
return satellite;
};

export const junoConfigExist = async (): Promise<boolean> => {
try {
const {configPath} = junoConfigFile();
Expand Down Expand Up @@ -107,7 +97,7 @@ const writeJunoConfig = async ({
config,
configType
}: {
config: JunoConfig;
config: JunoConfigWithSatelliteId;
configType: ConfigType;
}): Promise<void> => {
switch (configType) {
Expand Down Expand Up @@ -139,7 +129,7 @@ const writeJunoConfig = async ({
}
};

const readJunoConfig = async (env: JunoConfigEnv): Promise<JunoConfig> => {
export const readJunoConfig = async (env: JunoConfigEnv): Promise<JunoConfig> => {
const {configPath, configType} = junoConfigFile();

const config = (userConfig: JunoConfigFnOrObject): JunoConfig =>
Expand Down
12 changes: 7 additions & 5 deletions src/services/clear.services.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {deleteAssets} from '@junobuild/admin';
import {deleteAsset} from '@junobuild/core-peer';
import ora from 'ora';
import {readSatelliteConfig} from '../configs/juno.config';
import {readJunoConfig} from '../configs/juno.config';
import {DAPP_COLLECTION} from '../constants/constants';
import {configEnv} from '../utils/config.utils';
import {satelliteParameters} from '../utils/satellite.utils';

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

const spinner = ora('Clearing dapp assets...').start();

try {
await deleteAssets({
collection: DAPP_COLLECTION,
satellite: satelliteParameters(satelliteId)
satellite: satelliteParameters({satellite: satelliteConfig, env})
});
} finally {
spinner.stop();
Expand All @@ -27,14 +28,15 @@ const cleanFullPath = (fullPath: string): string => {
};

export const clearAsset = async ({fullPath, args}: {fullPath: string; args?: string[]}) => {
const {satelliteId} = await readSatelliteConfig(configEnv(args));
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

const spinner = ora(`Clearing ${fullPath}...`).start();

try {
await deleteAsset({
collection: DAPP_COLLECTION,
satellite: satelliteParameters(satelliteId),
satellite: satelliteParameters({satellite: satelliteConfig, env}),
fullPath: cleanFullPath(fullPath)
});
} finally {
Expand Down
9 changes: 6 additions & 3 deletions src/services/deploy.services.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import {satelliteMemorySize, satelliteVersion} from '@junobuild/admin';
import {yellow} from 'kleur';
import {compare} from 'semver';
import {readSatelliteConfig} from '../configs/juno.config';
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 {satelliteId, assertions} = await readSatelliteConfig(configEnv(args));
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

const {assertions} = satelliteConfig;

if (assertions?.heapMemory === false) {
return;
}

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

const currentVersion = await satelliteVersion({
satellite
Expand Down
9 changes: 6 additions & 3 deletions src/services/links.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {listCustomDomains} from '@junobuild/admin';
import {red} from 'kleur';
import {junoConfigExist, readSatelliteConfig} from '../configs/juno.config';
import {junoConfigExist, readJunoConfig} from '../configs/juno.config';
import {configEnv} from '../utils/config.utils';
import {consoleUrl, defaultSatelliteDomain} from '../utils/domain.utils';
import {terminalLink} from '../utils/links.utils';
Expand All @@ -18,13 +18,16 @@ export const links = async (args?: string[]) => {
return;
}

const {satelliteId} = await readSatelliteConfig(configEnv(args));
const env = configEnv(args);
const {satellite: satelliteConfig} = await readJunoConfig(env);

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

const defaultUrl = defaultSatelliteDomain(satelliteId);
const adminUrl = consoleUrl(satelliteId);

try {
const satellite = satelliteParameters(satelliteId);
const domains = await listCustomDomains({satellite});

console.log(`\n🛠️ ${terminalLink(adminUrl)}`);
Expand Down
Loading
Loading