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
40 changes: 26 additions & 14 deletions src/services/build/build.javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,38 @@ import {readPackageJson} from '../../utils/pkg.utils';
import {detectPackageManager} from '../../utils/pm.utils';
import {confirmAndExit} from '../../utils/prompt.utils';

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

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

type BuildArgsTsJs = {lang: Omit<BuildLang, 'rs'>} & Pick<BuildArgs, 'path'>;
type BuildArgsTsJs = {lang: Omit<BuildLang, 'rs'>} & Pick<BuildArgs, 'path' | 'exitOnError'>;

const build = async (params: BuildArgsTsJs) => {
const build = async ({exitOnError, ...params}: BuildArgsTsJs) => {
await installEsbuild();

await createTargetDir();

const metadata = await prepareMetadata();
try {
const metadata = await prepareMetadata();

const buildResult = await buildWithEsbuild({params, metadata});
const buildResult = await buildWithEsbuild({params, metadata});

printResults({metadata, buildResult});
printResults({metadata, buildResult});
} catch (_error: unknown) {
if (exitOnError !== false) {
process.exit(1);
}
}
};

interface BuildResult {
Expand All @@ -50,7 +62,7 @@ const buildWithEsbuild = async ({
params: {lang, path},
metadata
}: {
params: BuildArgsTsJs;
params: Omit<BuildArgsTsJs, 'exitOnError'>;
metadata: BuildMetadata;
}): Promise<BuildResult> => {
const infile =
Expand All @@ -76,14 +88,14 @@ const buildWithEsbuild = async ({
}

if (errors.length > 0) {
process.exit(1);
throw new Error();
}

const entry = Object.entries(metafile.outputs);

if (entry.length === 0) {
console.log(red('Unexpected: No metafile resulting from the build was found.'));
process.exit(1);
throw new Error();
}

return {
Expand Down Expand Up @@ -148,9 +160,9 @@ const prepareMetadata = async (): Promise<BuildMetadata> => {
...(notEmptyString(version) && {version}),
...(notEmptyString(functionsVersion) && {juno})
};
} catch (_err: unknown) {
} catch (err: unknown) {
console.log(red('⚠️ Could not read build metadata from package.json.'));
process.exit(1);
throw err;
}
};

Expand Down
16 changes: 8 additions & 8 deletions src/services/build/build.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ export const build = async (args?: string[]) => {
await executeBuild(params);
};

const executeBuild = async ({lang, path}: Omit<BuildArgs, 'watch'>) => {
const executeBuild = async ({lang, path, exitOnError}: Omit<BuildArgs, 'watch'>) => {
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (lang) {
case 'rs':
await buildRust({path});
return;
case 'ts':
await buildTypeScript({path});
await buildTypeScript({path, exitOnError});
return;
case 'mjs':
await buildJavaScript({path});
await buildJavaScript({path, exitOnError});
return;
}

Expand All @@ -52,15 +52,15 @@ const executeBuild = async ({lang, path}: Omit<BuildArgs, 'watch'>) => {
nonNullish(path) && extname(path) === extname(DEVELOPER_PROJECT_SATELLITE_INDEX_TS);

if (isPathTypeScript) {
await buildTypeScript({path});
await buildTypeScript({path, exitOnError});
return;
}

const isPathJavaScript =
nonNullish(path) && extname(path) === extname(DEVELOPER_PROJECT_SATELLITE_INDEX_MJS);

if (isPathJavaScript) {
await buildJavaScript({path});
await buildJavaScript({path, exitOnError});
return;
}

Expand All @@ -70,12 +70,12 @@ const executeBuild = async ({lang, path}: Omit<BuildArgs, 'watch'>) => {
}

if (existsSync(DEVELOPER_PROJECT_SATELLITE_INDEX_TS)) {
await buildTypeScript();
await buildTypeScript({exitOnError});
return;
}

if (existsSync(DEVELOPER_PROJECT_SATELLITE_INDEX_MJS)) {
await buildJavaScript();
await buildJavaScript({exitOnError});
return;
}

Expand All @@ -89,7 +89,7 @@ const executeBuild = async ({lang, path}: Omit<BuildArgs, 'watch'>) => {
export const watchBuild = ({watch, path, ...params}: BuildArgs) => {
const doBuild = async () => {
console.log(`\n⏱ Rebuilding serverless functions...`);
await executeBuild({path, ...params});
await executeBuild({path, exitOnError: false, ...params});
};

const DEFAULT_TIMEOUT = 10_000;
Expand Down
1 change: 0 additions & 1 deletion src/services/upgrade/upgrade.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,5 @@ export const consoleUpgradeResult = ({
return;
}

// eslint-disable-next-line @typescript-eslint/only-throw-error
throw err;
};
1 change: 1 addition & 0 deletions src/types/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface BuildArgs {
lang?: BuildLang;
path?: string | undefined;
watch?: boolean | string;
exitOnError?: boolean;
}