Skip to content

Commit

Permalink
feat: add entry and outdir to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnig committed Dec 26, 2021
1 parent 9d10610 commit 1a6885d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { runWorkerSdkGeneration } from '../piscina/index.js';

async function handler() {
const config = await getConfig();
await pMap(config.projects, async (project) => {
await pMap(config.tasks, async (project) => {
if (!project.sdk) {
return;
}
Expand Down
29 changes: 15 additions & 14 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ interface Options {
debug: boolean;
}

function startProcess(
command: FluxProjectConfig['exec'],
projectIndex: number,
) {
const subprocess = execa(command.command, command.args, {
function startProcess(command: string[], projectIndex: number) {
const [file, ...args] = command;
const subprocess = execa(file, args, {
env: { FLUX_PROJECT_INDEX: `${projectIndex}` },
});

Expand All @@ -42,7 +40,7 @@ function startProcess(
class ExecHandler {
private procs: ExecaChildProcess[] = [];

constructor(private config: FluxProjectConfig['exec'][]) {}
constructor(private config: FluxProjectConfig['run'][]) {}

cancelAll() {
this.procs.forEach((x) => {
Expand All @@ -61,20 +59,21 @@ class WatchHandler {
constructor(private options: Options) {}

async setup() {
const { projects } = await getConfig();
const commands = projects.map((x) => x.exec);
const config = await getConfig();
const { tasks } = config;
const commands = tasks.map((x) => x.run);

commands.forEach((x) => {
if (this.options.debug) {
x.args.push('--inspect');
x.push('--inspect');
}
});

this.excecHandler = new ExecHandler(commands);
this.excecHandler.startAll();

chokidar
.watch('./src/', {
.watch(config.entry, {
disableGlobbing: true,
persistent: true,
ignoreInitial: true,
Expand All @@ -101,16 +100,18 @@ class WatchHandler {
}

async function startSdkWatch() {
const { projects } = await getConfig();
projects.forEach((project) => {
const { tasks } = await getConfig();
tasks.forEach((project) => {
if (!project.sdk) {
return;
}

const handler = async () => await runWorkerSdkGeneration(project.sdk);
const { sdk } = project;

const handler = async () => await runWorkerSdkGeneration(sdk);

chokidar
.watch(project.sdk.input, {
.watch(sdk.input, {
disableGlobbing: true,
persistent: true,
ignoreInitial: true,
Expand Down
21 changes: 16 additions & 5 deletions packages/cli/src/helper/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { join } from 'node:path';
import { getRootDir } from './index.js';
import { FluxCliConfig } from '../types.js';

function getSdkDefaultConfig(projectIndex: number) {
function getSdkDefaultConfig(projectIndex: number, outdir: string) {
const sdk = {
name: 'GeneratedApi.ts',
input: join(process.cwd(), `/dist/openapi-${projectIndex}.json`),
input: join(outdir, `/openapi-${projectIndex}.json`),
templates: join(getRootDir(), '/sdk-templates/'),
httpClientType: 'axios',
moduleNameIndex: 0,
Expand All @@ -15,14 +15,25 @@ function getSdkDefaultConfig(projectIndex: number) {
return sdk;
}

export async function getConfig(): Promise<FluxCliConfig> {
async function readConfig() {
const config: FluxCliConfig = (
await import(join(process.cwd(), '/flux.config.js'))
).default;

config.projects.forEach((project, index) => {
return _.cloneDeep(config);
}

export async function getConfig(): Promise<FluxCliConfig> {
const config = await readConfig();

config.outdir = join(process.cwd(), config.outdir);

config.tasks.forEach((project, index) => {
if (project.sdk) {
project.sdk = _.merge(getSdkDefaultConfig(index), project.sdk);
project.sdk = _.merge(
getSdkDefaultConfig(index, config.outdir),
project.sdk,
);

if (project.sdk.output && project.sdk.output.startsWith('.')) {
project.sdk.output = join(process.cwd(), project.sdk.output);
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/helper/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { build, Plugin } from 'esbuild';
import fg from 'fast-glob';
import { join } from 'node:path';
import { getConfig } from './config.js';

const makeAllPackagesExternalPlugin = (): Plugin => ({
name: 'make-all-packages-external',
Expand All @@ -13,7 +15,9 @@ const makeAllPackagesExternalPlugin = (): Plugin => ({
});

export async function esbuildHelper() {
const files = await fg('./src/**/*.ts', {
const config = await getConfig();

const files = await fg(join(config.entry, '/**/*.ts'), {
absolute: true,
markDirectories: true,
});
Expand All @@ -23,8 +27,8 @@ export async function esbuildHelper() {
target: 'node16',
format: 'cjs',
entryPoints: files,
outdir: 'dist/',
outbase: 'src/',
outdir: config.outdir,
outbase: config.entry,
bundle: false,
sourcemap: true,
plugins: [makeAllPackagesExternalPlugin()],
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/helper/generateMeta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fg from 'fast-glob';
import { join } from 'node:path';
import pMap from 'p-map';
import { getConfig } from './config.js';
import { getControllerFunctions } from './getControllerFunctions.js';

export async function generateMeta() {
const controllers = await fg('./src/**/*.controller.ts', {
const config = await getConfig();
const controllers = await fg(join(config.entry, '/**/*.controller.ts'), {
absolute: true,
markDirectories: true,
});
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/helper/generateSdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readFile } from 'node:fs/promises';
import { GenerateApiParams } from 'swagger-typescript-api';
import { log } from '../log.js';

Expand Down
11 changes: 6 additions & 5 deletions packages/cli/src/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { getConfig } from './config.js';
import { generateMeta } from './generateMeta.js';
import { generateSchema } from './generateSchema.js';
import { writeFile } from './writeFile.js';

const dir = join(process.cwd(), '/dist/');

export async function writeControllerJson() {
const config = await getConfig();
const meta = await generateMeta();

writeFile(join(dir, 'flux-controller.json'), meta);
writeFile(join(config.outdir, 'flux-controller.json'), meta);
}

export async function writeSchemaJson() {
const schema = await generateSchema('./src/', { removeDateTime: false });
const config = await getConfig();
const schema = await generateSchema(config.entry, { removeDateTime: false });

writeFile(join(dir, 'flux-schema.json'), schema);
writeFile(join(config.outdir, 'flux-schema.json'), schema);
}

export function getRootDir() {
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { GenerateApiParams } from 'swagger-typescript-api';

export interface FluxProjectConfig {
sdk: GenerateApiParams;
exec: { command: string; args: string[] };
sdk?: GenerateApiParams;
run: string[];
}

export interface FluxCliConfig {
projects: FluxProjectConfig[];
entry: string;
outdir: string;
tasks: FluxProjectConfig[];
}
6 changes: 4 additions & 2 deletions playground/template-prisma/flux.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/** @type {import('@fluxapi/cli').FluxCliConfig} */
const config = {
projects: [
entry: './src/',
outdir: './dist/',
tasks: [
{
exec: { command: 'node', args: ['server.js'] },
run: ['node', 'server.js'],
sdk: {
name: 'GeneratedApi.ts',
output: './tests-e2e/api/',
Expand Down

0 comments on commit 1a6885d

Please sign in to comment.