UNPKG

@nx/gatsby

Version:

Gatsby Plugin for Nx

99 lines 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const run_tasks_in_serial_1 = require("@nx/workspace/src/utilities/run-tasks-in-serial"); const child_process_1 = require("child_process"); const path_1 = require("path"); const versions_1 = require("../../utils/versions"); /** * For gatsby app, replace gatsby-image with gatsby-plugin-image. * It updates the package names in package.json. * It uses gatsby-codemods to update imports. * It also add gatsby-plugin-image to project app's package.json and gatsby-config.js. */ function update(tree) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const packageJson = (0, devkit_1.readJson)(tree, 'package.json'); // does not proceed if gatsby-image is not a part of devDependencies if (!packageJson.dependencies['gatsby-image']) { return; } const replacePackagesTasks = replaceInPackageJson(tree); const updateImportsTasks = updateImports(tree); updateAppPackageJson(tree); yield (0, devkit_1.formatFiles)(tree); return (0, run_tasks_in_serial_1.runTasksInSerial)(...replacePackagesTasks, ...updateImportsTasks); }); } exports.default = update; function replaceInPackageJson(tree) { const uninstallTask = (0, devkit_1.removeDependenciesFromPackageJson)(tree, ['gatsby-image'], []); const installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, { 'gatsby-plugin-image': versions_1.gatsbyPluginImageVersion }, {}); return [uninstallTask, installTask]; } function updateImports(tree) { const projects = (0, devkit_1.getProjects)(tree); const tasks = []; const replaceProjectRef = new RegExp('gatsby-image', 'g'); for (const [name, definition] of Array.from(projects.entries())) { (0, devkit_1.visitNotIgnoredFiles)(tree, definition.root, (file) => { const contents = tree.read(file, 'utf-8'); if (!replaceProjectRef.test(contents)) { return; } tasks.push(runGatsbyCodemods(file, tree)); }); } return tasks; } /** * Run gatsby-codemods to transform file with import gatsby-image to use gatsby-plugin-image * https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-codemods/src/transforms/gatsby-plugin-image.js */ function runGatsbyCodemods(file, tree) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const childProcess = (0, child_process_1.exec)(require.resolve('gatsby-codemods/bin/gatsby-codemods.js') + ' gatsby-plugin-image ' + file, { cwd: (0, path_1.join)(tree.root), }); childProcess.on('error', (err) => { reject(err); childProcess.kill(); }); childProcess.on('exit', (code) => { if (code !== 0) { reject(new Error('Could not migrate to gatsby-plugin-image. See errors above.')); } else { resolve(); } childProcess.kill(); }); }); }); } function updateAppPackageJson(tree) { const projects = (0, devkit_1.getProjects)(tree); projects.forEach((project) => { var _a, _b; if (((_b = (_a = project.targets) === null || _a === void 0 ? void 0 : _a.build) === null || _b === void 0 ? void 0 : _b.executor) !== '@nx/gatsby:build') return; const appPackageJsonPath = `${project.root}/package.json`; if (tree.exists(appPackageJsonPath)) { (0, devkit_1.addDependenciesToPackageJson)(tree, { 'gatsby-plugin-image': '*' }, {}, appPackageJsonPath); } const gatsbyConfigPath = `${project.root}/gatsby-config.js`; if (tree.exists(gatsbyConfigPath)) { const contents = tree.read(gatsbyConfigPath, 'utf-8'); const gatsbyPluginImageRegex = new RegExp('gatsby-plugin-image', 'g'); if (gatsbyPluginImageRegex.test(contents)) { return; } tree.write(gatsbyConfigPath, contents.replace(`\`gatsby-transformer-sharp\``, `\`gatsby-plugin-image\`, \`gatsby-transformer-sharp\``)); } }); } //# sourceMappingURL=replace-gatsby-image-with-gatsby-plugin-image-12-9-1.js.map