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
11 changes: 7 additions & 4 deletions src/services/build/build.javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@ import {
INDEX_MJS,
INDEX_TS
} from '../../constants/dev.constants';
import type {BuildArgs, BuildLang} from '../../types/build';
import {detectPackageManager} from '../../utils/pm.utils';
import {confirmAndExit} from '../../utils/prompt.utils';

export const buildTypeScript = async ({path}: {path?: string | undefined} = {}) => {
export const buildTypeScript = async ({path}: Pick<BuildArgs, 'path'> = {}) => {
await build({lang: 'ts', path});
};

export const buildJavaScript = async ({path}: {path?: string | undefined} = {}) => {
export const buildJavaScript = async ({path}: Pick<BuildArgs, 'path'> = {}) => {
await build({lang: 'mjs', path});
};

const build = async (params: {lang: 'ts' | 'mjs'; path?: string | undefined}) => {
type BuildArgsTsJs = {lang: Omit<BuildLang, 'rs'>} & Pick<BuildArgs, 'path'>;

const build = async (params: BuildArgsTsJs) => {
await installEsbuild();

await createTargetDir();

await buildWithEsbuild(params);
};

const buildWithEsbuild = async ({lang, path}: {lang: 'ts' | 'mjs'; path?: string | undefined}) => {
const buildWithEsbuild = async ({lang, path}: BuildArgsTsJs) => {
const infile =
path ?? join(DEVELOPER_PROJECT_SATELLITE_PATH, lang === 'mjs' ? INDEX_MJS : INDEX_TS);

Expand Down
3 changes: 2 additions & 1 deletion src/services/build/build.rust.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {
DEVELOPER_PROJECT_SATELLITE_PATH,
IC_WASM_MIN_VERSION
} from '../../constants/dev.constants';
import type {BuildArgs} from '../../types/build';
import {readSatelliteDid} from '../../utils/did.utils';
import {checkCargoBinInstalled, checkIcWasmVersion, checkRustVersion} from '../../utils/env.utils';
import {confirmAndExit} from '../../utils/prompt.utils';

const CARGO_RELEASE_DIR = join(process.cwd(), 'target', 'wasm32-unknown-unknown', 'release');
const SATELLITE_OUTPUT = join(DEPLOY_LOCAL_REPLICA_PATH, 'satellite.wasm');

export const buildRust = async ({path}: {path?: string | undefined} = {}) => {
export const buildRust = async ({path}: Pick<BuildArgs, 'path'> = {}) => {
const {valid: validRust} = await checkRustVersion();

if (validRust === 'error' || !validRust) {
Expand Down
43 changes: 35 additions & 8 deletions src/services/build/build.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ import {
DEVELOPER_PROJECT_SATELLITE_INDEX_MJS,
DEVELOPER_PROJECT_SATELLITE_INDEX_TS
} from '../../constants/dev.constants';
import {type BuildArgs} from '../../types/build';
import {buildJavaScript, buildTypeScript} from './build.javascript';
import {buildRust} from './build.rust.services';

export const build = async (args?: string[]) => {
const path = nextArg({args, option: '-p'}) ?? nextArg({args, option: '--path'});

const lang = nextArg({args, option: '-l'}) ?? nextArg({args, option: '--lang'});
const {lang, path} = buildArgs(args);

// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (lang?.toLowerCase()) {
switch (lang) {
case 'rs':
case 'rust':
await buildRust({path});
return;
case 'ts':
case 'typescript':
await buildTypeScript({path});
return;
case 'js':
case 'javascript':
case 'mjs':
await buildJavaScript({path});
return;
}
Expand Down Expand Up @@ -77,3 +73,34 @@ export const build = async (args?: string[]) => {
)
);
};

const buildArgs = (args?: string[]): BuildArgs => {
const path = nextArg({args, option: '-p'}) ?? nextArg({args, option: '--path'});

const {lang} = buildLang(args);

return {
path,
lang
};
};

const buildLang = (args?: string[]): Pick<BuildArgs, 'lang'> => {
const lang = nextArg({args, option: '-l'}) ?? nextArg({args, option: '--lang'});

switch (lang?.toLowerCase()) {
case 'rs':
case 'rust':
return {lang: 'rs'};
case 'ts':
case 'mts':
case 'typescript':
return {lang: 'ts'};
case 'js':
case 'mjs':
case 'javascript':
return {lang: 'mjs'};
default:
return {};
}
};
6 changes: 6 additions & 0 deletions src/types/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type BuildLang = 'ts' | 'mjs' | 'rs';

export interface BuildArgs {
lang?: BuildLang;
path?: string | undefined;
}