Skip to content

Commit

Permalink
feat(expo): add to project package.json for install command
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jun 10, 2024
1 parent b54f5c3 commit a03a6ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/expo/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ function buildExpoTargets(
outputs: [getOutputs(projectRoot, 'dist')],
},
[options.installTargetName]: {
command: `expo install`,
options: { cwd: workspaceRoot }, // install at workspace root
command: '@nx/expo:install',
options: { cwd: projectRoot },
},
[options.prebuildTargetName]: {
executor: `@nx/expo:prebuild`,
Expand Down
29 changes: 28 additions & 1 deletion packages/expo/src/executors/install/install.impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ExecutorContext, names } from '@nx/devkit';
import { readJsonFile, writeJsonFile } from 'nx/src/utils/fileutils';
import { ChildProcess, fork } from 'child_process';

import { ExpoInstallOptions } from './schema';
import { join } from 'path';
import { existsSync, read } from 'fs';
import { PackageJson } from 'nx/src/utils/package-json';

export interface ExpoInstallOutput {
success: boolean;
Expand All @@ -18,7 +22,7 @@ export default async function* installExecutor(

try {
await installAsync(context.root, options);

updateProjectPackageJson(projectRoot, options);
yield {
success: true,
};
Expand Down Expand Up @@ -77,3 +81,26 @@ function createInstallOptions(options: ExpoInstallOptions) {
return acc;
}, []);
}

/**
* This function updates the package.json file in the project root with the packages passed in the options
* @param projectRoot
* @param options
*/
function updateProjectPackageJson(
projectRoot: string,
options: ExpoInstallOptions
) {
const packageJsonPath = join(projectRoot, 'package.json');
const packages: string[] =
typeof options.packages === 'string'
? options.packages.split(',')
: options.packages;
if (options.packages?.length && existsSync(packageJsonPath)) {
const packageJson = readJsonFile<PackageJson>(packageJsonPath);
for (const p of packages) {
packageJson.dependencies[p] = '*';
}
writeJsonFile(packageJsonPath, packageJson);
}
}

0 comments on commit a03a6ac

Please sign in to comment.