@nx/gatsby
Version:
Gatsby Plugin for Nx
122 lines (121 loc) • 5.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const child_process_1 = require("child_process");
const path_1 = require("path");
const build_impl_1 = require("../build/build.impl");
function serverExecutor(options, context) {
return tslib_1.__asyncGenerator(this, arguments, function* serverExecutor_1() {
devkit_1.logger.warn((0, devkit_1.stripIndents) `
⚠️ The Gatsby plugin will be deprecated in Nx 15 and removed in Nx 16. We are committed to providing high-quality tooling to community, and we no longer have the capacity to keep this plugin updated.
If you are interested in taking stewardship please contact jack@nrwl.io or drop a message in our [community Slack](https://go.nrwl.io/join-slack?utm_source=nx.dev).
`);
const buildTarget = (0, devkit_1.parseTargetString)(options.buildTarget);
const baseUrl = `${options.https ? 'https' : 'http'}://${options.host}:${options.port}`;
const projectRoot = context.workspace.projects[context.projectName].root;
const buildOptions = (0, devkit_1.readTargetOptions)(buildTarget, context);
try {
if (context.configurationName === 'production') {
yield tslib_1.__await((0, build_impl_1.runGatsbyBuild)(context.root, projectRoot, context.projectName, buildOptions));
yield tslib_1.__await(runGatsbyServe(context.root, projectRoot, options));
yield yield tslib_1.__await({ baseUrl, success: true });
}
else {
const success = yield tslib_1.__await(runGatsbyDevelop(context.root, projectRoot, createGatsbyOptions(options)));
yield yield tslib_1.__await({
baseUrl,
success,
});
}
// This Promise intentionally never resolves, leaving the process running
// eslint-disable-next-line @typescript-eslint/no-empty-function
yield tslib_1.__await(new Promise(() => { }));
}
finally {
if (childProcess) {
childProcess.kill();
}
}
});
}
exports.default = serverExecutor;
function createGatsbyOptions(options) {
return Object.keys(options).reduce((acc, k) => {
if (k === 'port' || k === 'host' || k === 'https' || k === 'open')
acc.push(`--${k}=${options[k]}`);
return acc;
}, []);
}
let childProcess;
function runGatsbyDevelop(workspaceRoot, projectRoot, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
childProcess = (0, child_process_1.fork)(require.resolve('gatsby-cli'), ['develop', ...options], {
cwd: (0, path_1.join)(workspaceRoot, projectRoot),
env: Object.assign({}, process.env),
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
});
childProcess.on('message', ({ action }) => {
var _a, _b;
if ((action === null || action === void 0 ? void 0 : action.type) === 'ACTIVITY_END' &&
((_a = action === null || action === void 0 ? void 0 : action.payload) === null || _a === void 0 ? void 0 : _a.status) === 'SUCCESS' &&
((_b = action === null || action === void 0 ? void 0 : action.payload) === null || _b === void 0 ? void 0 : _b.id) === 'webpack-develop') {
resolve(true);
}
});
childProcess.on('error', (err) => {
reject(err);
});
childProcess.on('exit', (code) => {
if (code !== 0) {
reject(new Error('Could not start Gatsby Development Server. See errors above.'));
}
});
});
});
}
function runGatsbyServe(workspaceRoot, projectRoot, options) {
return new Promise((resolve, reject) => {
childProcess = (0, child_process_1.fork)(require.resolve('gatsby-cli'), ['serve', ...createGatsbyServeOptions(options)], { cwd: (0, path_1.join)(workspaceRoot, projectRoot) });
childProcess.on('message', ({ action }) => {
var _a, _b, _c, _d;
if ((action === null || action === void 0 ? void 0 : action.type) === 'LOG' &&
((_b = (_a = action === null || action === void 0 ? void 0 : action.payload) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.includes(options.host)) &&
((_d = (_c = action === null || action === void 0 ? void 0 : action.payload) === null || _c === void 0 ? void 0 : _c.text) === null || _d === void 0 ? void 0 : _d.includes(options.port))) {
resolve(true);
}
});
childProcess.on('error', (err) => {
reject(err);
});
childProcess.on('exit', (code) => {
if (code === 0) {
resolve(code);
}
else {
reject(new Error('Could not start Gatsby Production Server. See errors above.'));
}
});
});
}
function createGatsbyServeOptions(options) {
return Object.keys(options).reduce((acc, k) => {
const val = options[k];
if (typeof val === 'undefined')
return acc;
switch (k) {
case 'host':
return val ? acc.concat([`--host`, val]) : acc;
case 'open':
return val ? acc.concat(`--open`) : acc;
case 'prefixPaths':
return val ? acc.concat(`--prefix-paths`) : acc;
case 'port':
return val ? acc.concat([`--port`, val]) : acc;
default:
return acc;
}
}, []);
}
//# sourceMappingURL=server.impl.js.map