Skip to content

Commit

Permalink
feat(nx-mesh): add build watcher (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
domjtalbot committed Jan 31, 2023
1 parent 3c45307 commit d42ba4b
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 132 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-grapes-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nx-mesh': minor
---

Add build watcher
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ This is the equivalent of using `graphql-mesh dev`, but with extra steps for pac
| `transformers` | `string[]` | - | - | List of TypeScript Transformer Plugins. |
| `tsConfig` | `string` || - | The path to the Typescript configuration file. |
| `updateBuildableProjectDepsInPackageJson` | `boolean` | - | `true` | Whether to update the buildable project dependencies in `package.json`. |
| `watch` | `boolean` | - | `false` | Rebuild upon file changes. |

</details>

Expand Down Expand Up @@ -391,6 +392,7 @@ This is the equivalent of using `graphql-mesh build`, but with extra steps for p
| `transformers` | `string[]` | - | - | List of TypeScript Transformer Plugins. |
| `tsConfig` | `string` || - | The path to the Typescript configuration file. |
| `updateBuildableProjectDepsInPackageJson` | `boolean` | - | `true` | Whether to update the buildable project dependencies in `package.json`. |
| `watch` | `boolean` | - | `false` | Rebuild upon file changes. |

</details>

Expand Down
7 changes: 7 additions & 0 deletions libs/example/sdk/graphql/star-wars-api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
}
}
},
"dev": {
"executor": "nx-mesh:serve",
"options": {
"dev": true,
"dir": "libs/example/sdk/graphql/star-wars-api"
}
},
"serve": {
"executor": "nx-mesh:serve",
"options": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default async function* buildExecutor(
context
);

logger.info('Done.');

yield {
success: true,
};
Expand Down
124 changes: 67 additions & 57 deletions libs/nx-mesh/src/executors/build-swc/build-swc.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ExecutorContext } from '@nrwl/devkit';
import { logger } from '@nrwl/devkit';
import { resolve } from 'path';

import { createPackageJson } from '../../utils';
import { createPackageJson, watcher } from '../../utils';
import { runCodegenCli } from '../../utils/graphql-codegen-cli';
import { runMeshCli } from '../../utils/mesh-cli';
import { swcExecutor } from './swc-executor/swc.impl';
Expand All @@ -20,69 +20,79 @@ export default async function* buildExecutor(
const dir = resolve(context.root, options.dir);
let success = false;

await runMeshCli(
'build',
{
args: {
dir,
require: options.require,
},
env: {
debug: options.debug,
},
},
context
);
await watcher(
async () => {
await runMeshCli(
'build',
{
args: {
dir,
require: options.require,
},
env: {
debug: options.debug,
},
},
context
);

if (options.codegen?.config) {
logger.info('');
logger.info('Running GraphQL Codegen...');
if (options.codegen?.config) {
logger.info('');
logger.info('Running GraphQL Codegen...');

await runCodegenCli(
{
...options.codegen,
debug: options.debug,
verbose: true,
watch: options.watch,
},
context
);
}
await runCodegenCli(
{
...options.codegen,
debug: options.debug,
verbose: true,
watch: false,
},
context
);
}

logger.info('');
logger.info('Running SWC compiler...');
logger.info('');
logger.info('Running SWC compiler...');

const tsc = swcExecutor(
{
assets: [...options.assets],
main: options.main,
outputPath: options.outputPath,
skipTypeCheck: options.skipTypeCheck,
swcrc: options.swcrc,
transformers: [],
tsConfig: options.tsConfig,
watch: false,
},
context
);
const tsc = swcExecutor(
{
assets: [...options.assets],
main: options.main,
outputPath: options.outputPath,
skipTypeCheck: options.skipTypeCheck,
swcrc: options.swcrc,
transformers: [],
tsConfig: options.tsConfig,
watch: false,
},
context
);

for await (const result of tsc) {
success = result.success;
}
for await (const result of tsc) {
success = result.success;
}

if (success) {
logger.info('');
logger.info('Creating package.json...');
if (success) {
logger.info('');
logger.info('Creating package.json...');

await createPackageJson(
{
dir: options.dir,
outputPath: options.outputPath,
projectRoot: options.outputPath,
},
context
);
}
await createPackageJson(
{
dir,
outputPath: options.outputPath,
projectRoot: options.outputPath,
},
context
);

logger.info('Done.');
}
},
{
dir,
watch: options.watch,
}
);

yield {
success,
Expand Down
2 changes: 1 addition & 1 deletion libs/nx-mesh/src/executors/build-swc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"watch": {
"type": "boolean",
"description": "Enable re-building when files change.",
"description": "Rebuild upon file changes.",
"default": false
},
"transformers": {
Expand Down
120 changes: 65 additions & 55 deletions libs/nx-mesh/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '@nrwl/devkit';
import { tscExecutor } from '@nrwl/js/src/executors/tsc/tsc.impl';
import { resolve } from 'path';

import { createPackageJson } from '../../utils';
import { createPackageJson, watcher } from '../../utils';
import { runCodegenCli } from '../../utils/graphql-codegen-cli';
import { runMeshCli } from '../../utils/mesh-cli';

Expand All @@ -21,67 +21,77 @@ export default async function* buildExecutor(
const dir = resolve(context.root, options.dir);
let success = false;

await runMeshCli(
'build',
{
args: {
dir,
require: options.require,
},
env: {
debug: options.debug,
},
},
context
);
await watcher(
async () => {
await runMeshCli(
'build',
{
args: {
dir,
require: options.require,
},
env: {
debug: options.debug,
},
},
context
);

if (options.codegen?.config) {
logger.info('');
logger.info('Running GraphQL Codegen...');
if (options.codegen?.config) {
logger.info('');
logger.info('Running GraphQL Codegen...');

await runCodegenCli(
{
...options.codegen,
debug: options.debug,
verbose: true,
watch: options.watch,
},
context
);
}
await runCodegenCli(
{
...options.codegen,
debug: options.debug,
verbose: true,
watch: options.watch,
},
context
);
}

logger.info('');
logger.info('Running Typescript compiler...');
logger.info('');
logger.info('Running Typescript compiler...');

const tsc = tscExecutor(
{
assets: [...options.assets],
main: options.main,
outputPath: options.outputPath,
transformers: [],
tsConfig: options.tsConfig,
watch: false,
},
context
);
const tsc = tscExecutor(
{
assets: [...options.assets],
main: options.main,
outputPath: options.outputPath,
transformers: [],
tsConfig: options.tsConfig,
watch: false,
},
context
);

for await (const result of tsc) {
success = result.success;
}
for await (const result of tsc) {
success = result.success;
}

if (success) {
logger.info('');
logger.info('Creating package.json...');
if (success) {
logger.info('');
logger.info('Creating package.json...');

await createPackageJson(
{
dir: options.dir,
outputPath: options.outputPath,
projectRoot: options.outputPath,
},
context
);
}
await createPackageJson(
{
dir,
outputPath: options.outputPath,
projectRoot: options.outputPath,
},
context
);

logger.info('Done.');
}
},
{
dir,
watch: options.watch,
}
);

yield {
success,
Expand Down
2 changes: 1 addition & 1 deletion libs/nx-mesh/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"watch": {
"type": "boolean",
"description": "Enable re-building when files change.",
"description": "Rebuild upon file changes.",
"default": false
},
"transformers": {
Expand Down
1 change: 1 addition & 0 deletions libs/nx-mesh/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './get-package-versions';
export * from './get-source-file';
export * from './get-wildcard-packages';
export * from './mesh-packages';
export * from './watcher';
1 change: 1 addition & 0 deletions libs/nx-mesh/src/utils/watcher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './watcher';
Loading

0 comments on commit d42ba4b

Please sign in to comment.